forked from AnimeNL/joomla-backup
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
159 lines (130 loc) · 4.16 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
153
154
155
156
157
158
159
package main
//TODO: Complete log statements
import (
"context"
"os"
"os/exec"
"path/filepath"
"time"
"joomla-backup/internal/config"
"joomla-backup/internal/sftp"
"github.com/docker/docker/api/types"
log "github.com/sirupsen/logrus"
)
var (
workdir = "/tmp/backup/"
layoutISO = "2006-01-02T15:04:05"
dbdump = workdir + "db/"
fsdump = workdir + "fs/"
dc = config.Configuration.DockerClient
timestamp string
timezone *time.Location
ctx context.Context
)
func setup() {
var err error
// set timestamp of backup
timezone, err = time.LoadLocation(config.Configuration.Global.Timezone)
if err != nil {
log.Fatalf("Error loading timezone. Is the format correct?")
}
time := time.Now().In(timezone)
timestamp = time.Format(layoutISO)
ctx = context.Background()
log.Debugf("creating workdir %v", workdir)
os.Mkdir(workdir, 0755)
log.Debugf("creating database dump folder %v", dbdump)
os.Mkdir(dbdump, 0755)
log.Debugf("creating filesystem dump folder %v", fsdump)
os.Mkdir(fsdump, 0755)
}
func consolidateDatabaseDumps() {
dir, err := os.Open(config.Configuration.Paths.DatabaseDumps)
if err != nil {
log.Errorf("error opening dir %v", err)
}
defer dir.Close()
files, err := dir.Readdir(0)
if err != nil {
log.Errorf("error reading dir %v", err)
}
for _, file := range files {
if filepath.Ext(file.Name()) == ".sql" {
err := os.Rename(config.Configuration.Paths.DatabaseDumps+"/"+file.Name(), dbdump+file.Name())
if err != nil {
log.Errorf("unable to move file: %v", err)
}
}
}
}
func databaseDump(ctx context.Context, database string) {
command := []string{"bash", "-c", "/usr/bin/mysqldump -u " + config.Configuration.Database.Credentials.Username + " --password=" + config.Configuration.Database.Credentials.Password + " " + database + " > /dump/" + database + ".sql"}
log.Debugf("constructed docker exec command: %v", command)
execConfig := types.ExecConfig{Tty: false, AttachStdout: true, AttachStderr: false, Cmd: command}
respIdExecCreate, err := dc.ContainerExecCreate(ctx, "mysql", execConfig)
if err != nil {
log.Errorf("error creating db dump command: %v", err)
}
err = dc.ContainerExecStart(ctx, respIdExecCreate.ID, types.ExecStartCheck{})
if err != nil {
log.Errorf("error occured starting db dump: %v", err)
}
execStatus, err := dc.ContainerExecInspect(ctx, respIdExecCreate.ID)
if err != nil {
log.Errorf("error occured inspecting dump progress: %v", err)
}
for execStatus.Running {
log.Info("waiting for db dump to finish...")
time.Sleep(2 * time.Second)
}
}
func compressDir(srcPath string, destFile string) error {
cmd := exec.Command("tar", "-zcvf", destFile+".tar.gz", srcPath)
err := cmd.Run()
if err != nil {
log.Errorf("unable to compress %v: %v", srcPath, err)
return err
}
return nil
}
func cleanupOldBackups() error {
files, err := sftp.ListBackups()
if err != nil {
log.Fatalf("error in listing backups: %v", err)
}
for i := range files {
if files[i].ModTime().Before(time.Now().In(timezone).AddDate(0, -config.Configuration.Global.MaxAge, 0)) {
switch config.Configuration.Global.Dryrun {
case false:
sftp.DeleteBackup(files[i].Name())
default:
log.Infof("simulating delete of file %s", files[i].Name())
}
}
}
return nil
}
func main() {
setup()
defer cleanupOldBackups()
defer cleanup()
// Dump all databases
for _, database := range config.Configuration.Database.Databases {
log.Infof("dumping database %v", database)
databaseDump(ctx, database) //TODO: Use goroutines to dump databases in parallel and make the backup more efficent
}
// Gather dumps in workdir
consolidateDatabaseDumps()
// Compress all data directories
for _, path := range config.Configuration.Paths.FileDumps {
log.Infof("compressing %v", path)
compressDir(path, fsdump+filepath.Base(path)) //TODO: Use goroutines to compress filesystems in parallel and make the backup more efficent
}
// Compress full backup
log.Info("compressing backup")
compressDir(workdir, "/tmp/backup-"+timestamp)
// Upload to SFTP site
log.Info("uploading backup to store")
sftp.UploadBackup("/tmp/backup-"+timestamp+".tar.gz", "backup-"+timestamp+".tar.gz")
log.Info("done. exiting.")
}