-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
139 lines (115 loc) · 3.5 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
package main
import (
"context"
"flag"
"fmt"
"log"
"os"
"path"
"runtime/pprof"
"filippo.io/age"
"github.com/dustin/go-humanize"
"github.com/minio/minio-go/v7"
"github.com/minio/minio-go/v7/pkg/credentials"
ignore "github.com/sabhiram/go-gitignore"
"gopkg.in/yaml.v3"
)
const VERSION = "1.4.1"
func main() {
doBackup := flag.Bool("backup", false, "Do a full backup")
doRestore := flag.Bool("restore", false, "Do full restore")
doSize := flag.Bool("size", false, "Get size of backup on disk")
doList := flag.Bool("ls", false, "List archives")
doRemove := flag.Bool("rm", false, "Remove an archive interactively")
showConfig := flag.Bool("show-config", false, "Print the config data")
configFileName := flag.String("config", "", "config file locaiton path")
showVersion := flag.Bool("version", false, "Print the version")
cpuprofile := flag.String("cpuprofile", "", "write cpu profile to file")
flag.Parse()
if *cpuprofile != "" {
f, err := os.Create(*cpuprofile)
if err != nil {
log.Fatal(err)
}
pprof.StartCPUProfile(f)
defer pprof.StopCPUProfile()
}
log.SetFlags(log.Ldate | log.Ltime)
log.Println("Starting")
var config BackupConfig
var name string
for _, name = range []string{
*configFileName,
path.Join(check(os.Getwd()), "backup.yaml"),
"/etc/backup.yaml",
path.Join(dismiss(os.UserHomeDir()), "backup.yaml"),
path.Join(dismiss(os.UserConfigDir()), "backup.yaml"),
} {
raw, err := os.ReadFile(name)
if err == nil {
check0(yaml.Unmarshal(raw, &config.Config))
log.Println("Found config file", name)
break
}
}
if *showConfig {
log.Printf("Config: %+v\n ", config)
}
if *showVersion {
fmt.Println("Version:", VERSION)
os.Exit(2)
}
config.chunkSize = check(humanize.ParseBytes(config.Config.Ram.ChunkSize))
config.maxRam = check(humanize.ParseBytes(config.Config.Ram.Max))
config.Ignorer = ignore.CompileIgnoreLines(config.Config.ExcludePatterns...)
creds := credentials.NewStaticV4(config.Config.S3.Access, config.Config.S3.Secret, "")
config.MinioClient = check(minio.New(config.Config.S3.Endpoint, &minio.Options{
Creds: creds,
Secure: true,
// Region: config.Config.S3.Region,
}))
bucketExists, err := config.MinioClient.BucketExists(context.Background(), config.Config.S3.Bucket)
if err != nil {
panic(err)
}
if !bucketExists {
err := config.MinioClient.MakeBucket(context.Background(), config.Config.S3.Bucket, minio.MakeBucketOptions{})
if err != nil {
panic(err)
}
log.Println("Created S3 bucket", config.Config.S3.Bucket)
}
if config.Config.Age.Private == "" || config.Config.Age.Public == "" {
fmt.Printf("!!! We need to generate a private key and saving it to %s, please remember to backup %s to a flashdrive to make restoring easier\n", name, name)
privateKey, err := age.GenerateX25519Identity()
if err != nil {
fmt.Println("Error generating identity:", err)
return
}
config.Config.Age.Private = privateKey.String()
fmt.Println("Identity Private Key:", privateKey)
publicKey := privateKey.Recipient()
config.Config.Age.Public = publicKey.String()
func() {
f := check(os.Create(name))
defer f.Close()
check0(f.Truncate(0))
raw := check(yaml.Marshal(config.Config))
check(f.Write(raw))
log.Println("Saved keys")
}()
}
if *doList {
List(&config, false)
} else if *doSize {
Size(&config)
} else if *doRemove {
RemoveArchive(&config)
} else if *doBackup {
Backup(&config)
} else if *doRestore {
Restore(&config)
} else {
log.Fatal("No operation selected, quitting")
}
}