-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathbackup.go
87 lines (73 loc) · 2.54 KB
/
backup.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
package bakery
import (
"fmt"
"time"
)
// Backup a Crostini Container
type Backup struct {
BackupName string
BackupLocation string
ArchiveLocation string
ArchiveContainer string
ContainerName string
SnapShotName string
General
}
// Execute starts the backup
func (b *Backup) Execute() {
fmt.Println("")
b.createSnapShot()
b.stopContainer(b.ContainerName)
b.publishBackup()
b.exportImage()
b.splitBackup()
b.startContainer(b.ContainerName)
b.mountBackupPath(b.BackupLocation, b.ArchiveContainer)
b.moveBackupFiles()
b.deleteBackupImage()
fmt.Println("")
}
func (b *Backup) createSnapShot() {
b.SnapShotName = fmt.Sprintf("%s-%d", b.SnapShotName, time.Now().Nanosecond())
fmt.Printf("Creating snapshot of container:%s name:%s\n", b.ContainerName, b.SnapShotName)
b.executeCommand("lxc", "snapshot", b.ContainerName, b.SnapShotName)
fmt.Println()
}
func (b *Backup) publishBackup() {
fmt.Printf("Publish container: %s to %s\n", b.ContainerName, b.BackupName)
fmt.Println("If the container publish is interupted, your container may be left in a bad state,")
fmt.Printf(
"in this instance you can restore the snapshot using the command: lxc restore %s %s\n",
b.ContainerName,
b.SnapShotName,
)
b.executeCommand("lxc", "publish", b.ContainerName, "--alias", b.BackupName)
fmt.Println()
}
func (b *Backup) exportImage() {
fmt.Printf("Exporting container to: %s\n", b.BackupLocation)
b.executeCommand("lxc", "image", "export", b.BackupName, b.BackupLocation+"/"+b.BackupName)
fmt.Println()
}
func (b *Backup) splitBackup() {
backupFileLocation := b.BackupLocation + "/" + b.BackupName + ".tar.gz"
fmt.Println("Splitting backup into 3GB chunks")
b.executeCommand("split", "-b", "3GB", backupFileLocation, backupFileLocation+".")
b.executeCommand("rm", backupFileLocation)
fmt.Println()
}
func (b *Backup) moveBackupFiles() {
fmt.Printf("Moving backup files to %s in container %s\n", b.ArchiveLocation, b.ArchiveContainer)
// create the user folder if required
b.executeCommand("lxc", "exec", b.ArchiveContainer, "--", "mkdir", "-p", b.ArchiveLocation)
// move the files
b.executeCommand("lxc", "exec", b.ArchiveContainer, "--", "find", "/mnt/lxd_conf", "-name", "*.tar.gz.*", "-exec", "cp", "{}", "-t", b.ArchiveLocation, ";")
// delete the backups
b.executeCommand("find", b.BackupLocation, "-name", "*.tar.gz.*", "-exec", "rm", "{}", ";")
fmt.Println("")
}
func (b *Backup) deleteBackupImage() {
fmt.Printf("Deleting temporary image %s", b.BackupName)
b.executeCommand("lxc", "image", "delete", b.BackupName)
fmt.Println("")
}