/
subvolume.go
140 lines (130 loc) · 3.24 KB
/
subvolume.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
package main
import (
"bufio"
"github.com/golang/snappy"
"log"
"os"
"os/exec"
"path"
"strings"
)
type Subvolume struct {
Directory string
SnapshotsLoc SnapshotsLoc
Remotes []RemoteSnapshotsLoc
}
func (subvolume Subvolume) Print() {
if verbosity > 0 {
log.Printf("Subvolume='%s'", subvolume.Directory)
log.Printf("Snapshot Dir='%s' (%s)\n", subvolume.SnapshotsLoc.Directory, subvolume.SnapshotsLoc.Limits.String())
}
for _, remote := range subvolume.Remotes {
dst := remote.SnapshotsLoc.Directory
if remote.Host != "" {
dst = strings.Join([]string{remote.Host, dst}, ":")
if remote.User != "" {
dst = strings.Join([]string{remote.User, dst}, "@")
}
}
if verbosity > 0 {
log.Printf("Remote Dir='%s' (%s)\n", dst, remote.SnapshotsLoc.Limits.String())
}
}
}
func (subvolume Subvolume) RunSnapshot() (err error) {
//TODO Move Lock to after snapshot. To allow snapshots if a previous long send
//is still running. Need to implement some guarantee that two instances don't
//try to create the same snapshot at the same time and then delete cause one
//will fail
lock, err := NewDirLock(subvolume.SnapshotsLoc.Directory)
if err != nil {
return
}
defer lock.Unlock()
timestamp := getCurrentTimestamp()
snapshot := Snapshot{subvolume.SnapshotsLoc, timestamp}
err = os.MkdirAll(path.Dir(snapshot.Path()), dirMode)
if err != nil {
return
}
btrfsCmd := exec.Command(btrfsBin, "subvolume", "snapshot", "-r", subvolume.Directory, snapshot.Path())
if verbosity > 1 {
printCommand(btrfsCmd)
btrfsCmd.Stdout = os.Stderr
btrfsCmd.Stderr = os.Stderr
}
err = btrfsCmd.Run()
if err != nil {
if verbosity > 0 {
log.Println("Snapshot failed")
}
if _, errTmp := os.Stat(snapshot.Path()); !os.IsNotExist(errTmp) {
errTmp = snapshot.DeleteSnapshot()
if errTmp != nil {
if verbosity > 0 {
log.Println("Failed to deleted to failed snapshot")
}
}
}
return
}
if *pinnedFlag {
subvolume.SnapshotsLoc.PinTimestamp(timestamp)
}
if *archiveFlag {
archiveDir := path.Join(subvolume.SnapshotsLoc.Directory, "archive")
err = os.MkdirAll(archiveDir, dirMode)
if err != nil {
return
}
btrfsCmd = exec.Command(btrfsBin, "send", snapshot.Path())
extension := ""
if !*noCompressionFlag {
extension = ".snpy"
}
archiveFile := path.Join(archiveDir, string(timestamp)+".snap"+extension)
f, err := os.Create(archiveFile)
if err != nil {
return err
}
defer f.Close()
if *noCompressionFlag {
bf := bufio.NewWriter(f)
defer bf.Flush()
btrfsCmd.Stdout = bf
} else {
bf := snappy.NewBufferedWriter(f)
defer bf.Close()
btrfsCmd.Stdout = bf
}
if verbosity > 1 {
printCommand(btrfsCmd)
btrfsCmd.Stderr = os.Stderr
}
err = btrfsCmd.Run()
if err != nil {
// Not sure what to do if error
_ = f.Close()
os.Remove(archiveFile)
return err
}
}
timestamps, err := subvolume.SnapshotsLoc.ReadTimestampsDir()
if err != nil {
return
}
for _, remote := range subvolume.Remotes {
err = remote.sendSnapshotUsingParent(snapshot, timestamps)
if err != nil {
log.Println("Error sending snapshot")
log.Println(err.Error())
err = nil
continue
}
}
_, err = subvolume.SnapshotsLoc.CleanUp(timestamp, timestamps)
if err != nil {
return
}
return
}