-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
152 lines (127 loc) · 3.34 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
package main
import (
"FileAutoBackup/SimpleLogFormatter"
"flag"
"fmt"
"os"
fp "path/filepath"
"strconv"
"time"
"github.com/fsnotify/fsnotify"
log "github.com/sirupsen/logrus"
)
type flags struct {
configPath string
}
var (
launchTime = time.Now()
config = make(Config)
)
func main() {
initLogger()
flags := initFlag()
err := initConfig(flags.configPath)
if err != nil {
log.Fatal(err)
}
startAll()
log.Info("Listening...")
<-make(chan struct{})
}
func initLogger() {
log.SetLevel(log.InfoLevel)
log.SetFormatter(
&SimpleLogFormatter.LogFormat{},
)
}
func initFlag() (f flags) {
f = flags{}
c := flag.String("c", "", "配置文件路径, 默认为程序目录下的 config.yaml")
d := flag.Bool("debug", false, "debug mode~")
t := flag.Bool("trace", false, "trace mode!")
flag.Parse()
if *c != "" {
f.configPath = *c
}
if *d {
log.SetLevel(log.DebugLevel)
}
if *t {
log.SetLevel(log.TraceLevel)
}
if *d && *t {
log.Panic("y u bully me")
}
return f
}
func startAll() {
for key, session := range config {
err := watch(
session.Dir, session.Files, func(event fsnotify.Event) {
sessionHandler(session, event)
},
)
if err != nil {
log.Fatal("failed to watch dir ", session.Dir, " in ", key, ": ", err)
}
}
}
func sessionHandler(s *Session, event fsnotify.Event) {
times := strconv.FormatInt(time.Now().Unix(), 10)
switch len(s.Files) {
case 0: // 备份整个文件夹
if time.Since(s.lastBackupTime) < s.MinimumInterval && s.lastBackupTime != launchTime {
log.Debug(s.Dir, " changed again but less than ", s.MinimumInterval)
return
}
log.Info(s.Dir, " changed again after ", time.Since(s.lastBackupTime))
s.lastBackupTime = time.Now()
time.Sleep(time.Second) //等待更新方写入
if s.Compression {
destPathInTar := fp.ToSlash(fp.Join(times))
destTar := fp.ToSlash(fp.Join(s.CopyTo, times+".tar.gz"))
err := archiveFiles([]string{s.Dir}, destPathInTar, destTar)
if err != nil {
log.Error("failed to archive ", s.Dir, " to ", destTar, ": ", err)
}
} else {
destDir := fp.Join(s.CopyTo, times, fp.Base(s.Dir))
err := copyDir(s.Dir, destDir)
if err != nil {
log.Error("failed to copy ", s.Dir, " to ", destDir, ": ", err)
}
}
default: // 备份个别文件
if time.Since(s.lastBackupTime) < s.MinimumInterval && s.lastBackupTime != launchTime {
log.Debug(event.Name, " changed again but less than ", s.MinimumInterval)
return
}
log.Info(s.Dir, " changed again after ", time.Since(s.lastBackupTime))
s.lastBackupTime = time.Now()
time.Sleep(time.Second) //等待更新方写入
if s.Compression {
destPathInTar := fp.ToSlash(fp.Join(times))
destTar := fp.ToSlash(fp.Join(s.CopyTo, times+".tar.gz"))
err := archiveFiles([]string{event.Name}, destPathInTar, destTar)
if err != nil {
log.Error("failed to archive ", event.Name, " to ", destTar, ": ", err)
}
} else {
destFile := fp.Join(s.CopyTo, times, fp.Base(event.Name))
err := copyFile(event.Name, destFile)
if err != nil {
log.Error("failed to copy ", event.Name, " to ", destFile, ": ", err)
}
}
}
}
func safeCreate(name string) (*os.File, error) {
_, err := os.Stat(name)
if os.IsNotExist(err) {
err := os.MkdirAll(fp.Dir(name), os.ModePerm)
if err != nil {
return nil, fmt.Errorf("failed to mkdir: %w", err)
}
}
return os.Create(name)
}