forked from calmh/zsync
/
client.go
178 lines (144 loc) · 3.64 KB
/
client.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
package main
import (
"bufio"
"encoding/gob"
"fmt"
"io"
"os/exec"
"strings"
"time"
"github.com/calmh/zfs"
)
func client(ds, host string) {
var command Command
var sourceSs string
var serverDs string
if strings.ContainsRune(host, ':') {
fs := strings.SplitN(host, ":", 2)
host = fs[0]
serverDs = fs[1]
} else {
serverDs = ds
}
if strings.ContainsRune(ds, '@') {
fs := strings.SplitN(ds, "@", 2)
ds = fs[0]
sourceSs = fs[1]
}
sshCmd := exec.Command("ssh", host, opts.ZsyncPath, "--server")
stdin, err := sshCmd.StdinPipe()
panicOn(err)
stdout, err := sshCmd.StdoutPipe()
panicOn(err)
stderr, err := sshCmd.StderrPipe()
panicOn(err)
go printLines("remote: ", stderr)
err = sshCmd.Start()
panicOn(err)
e := gob.NewEncoder(stdin)
d := gob.NewDecoder(stdout)
negotiateVersion(e, d)
command = Command{Command: CmdListSnapshots, Params: []string{serverDs}}
err = e.Encode(&command)
panicOn(err)
var serverSnapshots []zfs.SnapshotEntry
err = d.Decode(&serverSnapshots)
panicOn(err)
clientSnapshots, err := zfs.ListSnapshots(ds)
panicOn(err)
var toSend *zfs.SnapshotEntry
if sourceSs != "" {
for i, s := range clientSnapshots {
if s.Snapshot == sourceSs {
toSend = &s
clientSnapshots = clientSnapshots[:i+1]
break
}
}
} else {
toSend = &clientSnapshots[len(clientSnapshots)-1]
logf(VERBOSE, "zsync: our latest: %s@%s\n", toSend.Dataset, toSend.Snapshot)
}
if toSend == nil {
logf(INFO, "zsync: no snapshot to send\n")
return
}
latest := latestCommon(serverSnapshots, clientSnapshots)
if latest != nil {
logf(VERBOSE, "zsync: snapshot in common: %s@%s\n", latest.Dataset, latest.Snapshot)
if toSend.Snapshot == latest.Snapshot {
logf(INFO, "zsync: nothing to send (destination in sync)\n")
return
}
} else {
logf(VERBOSE, "zsync: remote dataset missing or no snapshots in common\n")
}
params := []string{"send"}
if opts.Recursive {
params = append(params, "-R")
}
if latest != nil {
params = append(params, "-I", "@"+latest.Snapshot)
}
params = append(params, ds+"@"+toSend.Snapshot)
sendCmd := exec.Command("zfs", params...)
stream, err := sendCmd.StdoutPipe()
panicOn(err)
sendStderr, err := sendCmd.StderrPipe()
panicOn(err)
go printLines("zfs send: ", sendStderr)
err = sendCmd.Start()
panicOn(err)
params = nil
if opts.Rollback {
params = append(params, "-F")
}
if opts.NoMount {
params = append(params, "-u")
}
params = append(params, serverDs)
sc := Command{Command: CmdReceive, Params: params}
err = e.Encode(sc)
panicOn(err)
logf(VERBOSE, "zsync: sending %s@%s\n", toSend.Dataset, toSend.Snapshot)
t0 := time.Now()
bufout := bufio.NewWriterSize(stdin, opts.bufferBytes)
chunkout := ChunkedWriter{bufout}
tot, err := io.Copy(chunkout, stream)
panicOn(err)
err = chunkout.Flush()
panicOn(err)
err = bufout.Flush()
panicOn(err)
err = sendCmd.Wait()
panicOn(err)
err = d.Decode(&command)
panicOn(err)
stdin.Close()
err = sshCmd.Wait()
panicOn(err)
td := time.Since(t0)
logf(INFO, "zsync: sent %s@%s; %sB in %.2f seconds (%sB/s)\n", toSend.Dataset, toSend.Snapshot, toSi(int(tot)), td.Seconds(), toSi(int(float64(tot)/td.Seconds())))
}
func latestCommon(o, n []zfs.SnapshotEntry) *zfs.SnapshotEntry {
for i := len(n) - 1; i >= 0; i-- {
latest := n[i]
for j := len(o) - 1; j >= 0; j-- {
if o[j].Snapshot == latest.Snapshot {
return &o[j]
}
}
}
return nil
}
func toSi(n int) string {
if n > 1e9 {
return fmt.Sprintf("%.01fG", float64(n)/1e9)
} else if n > 1e6 {
return fmt.Sprintf("%.01fM", float64(n)/1e6)
} else if n > 1e3 {
return fmt.Sprintf("%.01fk", float64(n)/1e3)
} else {
return fmt.Sprintf("%d", n)
}
}