-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
90 lines (73 loc) · 2.01 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
package main
import (
"flag"
"log"
"os"
"strings"
"github.com/golang/glog"
"github.com/jchorl/watchdog"
"github.com/ncw/rclone/fs"
"github.com/jchorl/nasblaze/drives"
"github.com/jchorl/nasblaze/rclone"
)
const (
driveSize = "931.3G"
mountpoint = "/sync"
bucketName = "backup-c3bac1e7-d888-4940-8778-f03adcddfe45"
configPath = "/home/j/nasblaze/rclone.conf"
otherLogFile = "/home/j/logs/nasblaze.OTHER"
)
var (
hardDelete = flag.Bool("hard-delete", false, "whether to hard delete files on b2")
dryRun = flag.Bool("dry-run", true, "whether to run in dry run mode")
filters = arrayFlags{}
)
func main() {
flag.Var(&filters, "exclude", "Exclude flags passed directly to rclone")
flag.Parse()
// set rclone log level
fs.Config.LogLevel = fs.LogLevelInfo
logFile, err := initGolangLog()
if err != nil {
log.Fatal(err)
}
defer logFile.Close()
glog.Info("Starting")
// mount the drive, if necessary
err = drives.MountDriveBySize(driveSize, mountpoint)
if err != nil {
glog.Fatalf("Error mounting drive: %s", err)
}
err = rclone.Sync(configPath, bucketName, mountpoint, filters, *dryRun, *hardDelete)
if err != nil {
glog.Fatalf("Error syncing: %s", err)
}
err = drives.UnmountDriveByMountpoint(mountpoint)
if err != nil {
glog.Fatalf("Error unmounting drive: %s", err)
}
wdClient := watchdog.Client{"https://watchdog.joshchorlton.com"}
wdClient.Ping("nasblaze", watchdog.Watch_WEEKLY)
glog.Info("Complete")
glog.Flush()
}
// rclone uses standard golang log package
// this will ensure that those logs get written to file
func initGolangLog() (*os.File, error) {
f, err := os.OpenFile(otherLogFile, os.O_RDWR|os.O_CREATE|os.O_APPEND, 0666)
if err != nil {
glog.Errorf("Error opening other log file: %v", err)
return nil, err
}
log.SetOutput(f)
log.Println("Other logging initted")
return f, nil
}
type arrayFlags []string
func (i *arrayFlags) String() string {
return strings.Join(*i, ",")
}
func (i *arrayFlags) Set(value string) error {
*i = append(*i, value)
return nil
}