/
backup-methods.go
181 lines (149 loc) · 5.19 KB
/
backup-methods.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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
package main
import (
"context"
"io"
"io/ioutil"
"log"
"os"
"os/exec"
"regexp"
"strings"
"sync"
"time"
"github.com/docker/docker/api/types"
"github.com/docker/docker/client"
)
func check(err error) {
if err != nil {
panic(err)
}
}
func packWithRestic(containerName, mountDestination, s3Endpoint, s3Bucket, accKey, secKey string) {
cmd := exec.Command(`/bin/restic`, `init`)
cmd.Dir = containerName
//cmd.Env = append(os.Environ(), `RESTIC_REPOSITORY=/dback-snapshots`+containerName+mountDestination, `RESTIC_PASSWORD=sdf`)
cmd.Env = append(os.Environ(),
`RESTIC_PASSWORD=sdf`,
`RESTIC_REPOSITORY=s3:http://`+s3Endpoint+`/`+s3Bucket+containerName+mountDestination,
`AWS_ACCESS_KEY_ID=`+accKey,
`AWS_SECRET_ACCESS_KEY=`+secKey)
//s3:https://s3.amazonaws.com/BUCKET_NAME
//log.Println(`---`, `RESTIC_REPOSITORY=/dback-snapshots`+containerName+mountDestination)
stdoutStderr, err := cmd.CombinedOutput()
if err != nil {
log.Println(err)
log.Println(string(stdoutStderr))
//panic(`sdf`)
}
//log.Printf("%s\n", stdoutStderr)
files, err := ioutil.ReadDir(containerName)
if err != nil {
//log.Fatal(err)
//panic(`sdf`)
}
log.Println(`===`)
for _, file := range files {
log.Println(`===`, file.Name())
}
log.Println(`----`, containerName+mountDestination)
cmd = exec.Command(`/bin/restic`, `backup`, containerName+mountDestination)
log.Println(`***`, `/bin/restic`, `backup`, containerName+mountDestination)
cmd.Dir = containerName
cmd.Env = append(os.Environ(),
`RESTIC_PASSWORD=sdf`,
`RESTIC_REPOSITORY=s3:http://`+s3Endpoint+`/`+s3Bucket+containerName+mountDestination,
`AWS_ACCESS_KEY_ID=`+accKey,
`AWS_SECRET_ACCESS_KEY=`+secKey)
//log.Println(`---`, `RESTIC_REPOSITORY=/dback-snapshots`+containerName+mountDestination)
stdoutStderr, err = cmd.CombinedOutput()
if err != nil {
log.Println(err)
log.Println(string(stdoutStderr))
}
log.Printf("%s\n", stdoutStderr)
}
func unpackTarToMyself(c types.Container, myContainer types.Container, m types.MountPoint, s3Endpoint, s3Bucket, accKey, secKey string) {
//tmp := fmt.Sprintf("%d", time.Now().UnixNano())
//log.Println(tmp)
//check(os.MkdirAll(c.Names[0], 664))
cli, err := client.NewEnvClient()
check(err)
defer cli.Close()
tar, err := os.Open(`dback-snapshots/` + c.Names[0] + m.Destination + `/tar.tar`)
check(err)
defer tar.Close()
lastSlashIdx := strings.LastIndex(m.Destination, `/`)
destParent := m.Destination[:lastSlashIdx] // "/var/www/lynx" -> "/var/www" "/opt" -> "/"
if destParent == `` {
destParent = `/`
}
// log.Println(`***`, m.Destination)
// log.Println(`###`, destParent)
os.MkdirAll(c.Names[0]+destParent, 0664)
check(cli.CopyToContainer(context.Background(), myContainer.ID, c.Names[0]+destParent, tar, types.CopyToContainerOptions{true, false}))
packWithRestic(c.Names[0], m.Destination, s3Endpoint, s3Bucket, accKey, secKey)
}
func backupMount(cli *client.Client, c types.Container, m types.MountPoint, wg *sync.WaitGroup, s3Endpoint, s3Bucket, accKey, secKey string) {
defer wg.Done()
cli, err := client.NewEnvClient()
check(err)
defer cli.Close()
check(os.MkdirAll(`dback-snapshots/`+c.Names[0]+m.Destination, 0664))
reader, _, err := cli.CopyFromContainer(context.Background(), c.ID, m.Destination)
check(err)
outFile, err := os.Create(`dback-snapshots/` + c.Names[0] + m.Destination + `/tar.tar`)
check(err)
defer outFile.Close()
_, err = io.Copy(outFile, reader)
check(err)
log.Println(`make backup: ` + c.Names[0] + m.Destination)
myselfContainerID, err := os.Hostname()
check(err)
unpackTarToMyself(c, *getContainerByNameOrId(cli, myselfContainerID), m, s3Endpoint, s3Bucket, accKey, secKey)
}
func backupContainer(c types.Container, wg *sync.WaitGroup, excludePattern, s3Endpoint, s3Bucket, accKey, secKey string) {
defer wg.Done()
r, err := regexp.Compile(excludePattern)
check(err)
cli, err := client.NewEnvClient()
check(err)
defer cli.Close()
if c.State == `running` {
if len(c.Mounts) > 0 {
inspect, err := cli.ContainerInspect(context.Background(), c.ID)
check(err)
if inspect.HostConfig.AutoRemove == false {
if !inspect.HostConfig.RestartPolicy.IsNone() {
mounts := []types.MountPoint{}
for _, curMount := range c.Mounts {
if excludePattern == `` {
mounts = append(mounts, curMount)
} else {
if !r.MatchString(c.Names[0] + curMount.Destination) {
mounts = append(mounts, curMount)
} else {
log.Println(`exclude: ` + c.Names[0] + curMount.Destination + ` Reason: --exclude-mount parameter`)
}
}
}
timeout := time.Minute
if len(mounts) == 0 {
return
}
check(cli.ContainerStop(context.Background(), c.ID, &timeout))
var wgMount sync.WaitGroup
wgMount.Add(len(mounts))
for _, curMount := range mounts {
go backupMount(cli, c, curMount, &wgMount, s3Endpoint, s3Bucket, accKey, secKey)
}
wgMount.Wait()
check(cli.ContainerStart(context.Background(), c.ID, types.ContainerStartOptions{}))
} else {
log.Println(`exclude: ` + c.Names[0] + ` Reason: container restart policy==none`)
}
} else {
log.Println(`exclude: ` + c.Names[0] + ` Reason: temporary container (--rm)`)
}
}
}
}