-
Notifications
You must be signed in to change notification settings - Fork 0
/
rsync.go
187 lines (166 loc) · 4.06 KB
/
rsync.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
182
183
184
185
186
187
package main
import (
"fmt"
"os"
"os/exec"
"path"
"path/filepath"
"strings"
)
// Fake only prints the commands out
var Fake = false
// Verbose print outputs of the commands
var Verbose = false
type rsync struct {
rhost string // user@host:/data/backup
lroot string // /home/user/ ..
tmp string
}
func newRsync(lroot, rhost string) *rsync {
return &rsync{
rhost: rhost,
lroot: lroot,
tmp: tmpFolder(),
}
}
// FetchFile takes the remote host name USER@HOST:PATH on the remote server. It
// downloads the file using rsync and places it into a tmp folder. The full path
// on localhost is returned or an error.
// The remote can also be given as PATH on the local filesystem.
func FetchFile(remote string) (string, error) {
tmp := tmpFolder()
localPath := path.Join(tmp, "fetched.config")
cmd := []string{"rsync", "-ravzz", "--links", "--progress"}
cmd = append(cmd, remote)
cmd = append(cmd, localPath)
return localPath, run(cmd)
}
func (r *rsync) Download(download Config) error {
include, exclude, err := r.getFilePath(download)
if err != nil {
return err
}
cmd := r.baseCmd(include, exclude)
cmd = r.toDownload(cmd)
return run(cmd)
}
func (r *rsync) Cleanup() {
os.RemoveAll(r.tmp)
}
func (r *rsync) Upload(upload Config) error {
include, exclude, err := r.getFilePath(upload)
if err != nil {
return err
}
cmd := r.baseCmd(include, exclude)
cmd = r.toUpload(cmd)
return run(cmd)
}
func (r *rsync) getFilePath(c Config) (include string, exclude string, err error) {
if len(c.Includes) == 0 {
return "", "", fmt.Errorf("Invalid config: no include or includefile")
}
var includePath, excludePath string
includePath = filepath.Join(r.tmp, "include.backup")
if err := writeTmp(includePath, c.Includes); err != nil {
return "", "", err
}
if len(c.Excludes) > 0 {
excludePath = filepath.Join(r.tmp, "exclude.backup")
if err := writeTmp(excludePath, c.Excludes); err != nil {
return "", "", err
}
}
return includePath, excludePath, nil
}
func (r *rsync) SyncUpload(sync Config) error {
cmd, err := r.syncCmd(sync)
if err != nil {
return err
}
cmd = r.toUpload(cmd)
return run(cmd)
}
func (r *rsync) SyncDownload(sync Config) error {
cmd, err := r.syncCmd(sync)
if err != nil {
return err
}
cmd = r.toDownload(cmd)
return run(cmd)
}
func (r *rsync) toUpload(cmd []string) []string {
cmd = append(cmd, r.lroot)
cmd = append(cmd, r.rhost)
return cmd
}
func (r *rsync) toDownload(cmd []string) []string {
cmd = append(cmd, r.rhost)
cmd = append(cmd, r.lroot)
return cmd
}
func (r *rsync) syncCmd(sync Config) ([]string, error) {
include, _, err := r.getFilePath(sync)
if err != nil {
return nil, err
}
cmd := r.baseCmd(include, "")
cmd = append(cmd, "--delete")
return cmd, nil
}
func writeTmp(fname string, paths []string) error {
f, err := os.Create(fname)
if err != nil {
return err
}
defer f.Close()
for _, p := range paths {
f.WriteString(p + "\n")
}
return nil
}
func (r *rsync) baseCmd(include, exclude string) []string {
cmd := []string{"rsync", "-ravzz", "--links", "--progress"}
cmd = append(cmd, []string{"--files-from", include}...)
if exclude != "" {
cmd = append(cmd, []string{"--exclude-from", exclude}...)
}
return cmd
}
func preprocess(paths []string, fn func(p string) (string, string)) []string {
prefixed := make([]string, 0, len(paths)*2)
for _, p := range paths {
flag, value := fn(p)
prefixed = append(prefixed, flag)
prefixed = append(prefixed, value)
}
return prefixed
}
func run(c []string) error {
if Verbose {
fmt.Println("command: " + strings.Join(c, " "))
}
if Fake {
cmd := exec.Command(c[0], append([]string{"--dry-run"}, c[1:]...)...)
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
cmd.Run()
return nil
}
cmd := exec.Command(c[0], c[1:]...)
var err error
var out string
if Verbose {
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
err = cmd.Run()
} else {
var buff []byte
buff, err = cmd.CombinedOutput()
out = string(buff)
}
if err != nil {
return fmt.Errorf("error executing command:\n\t-%v\n\t-Error: %s\n\t-Output: %s", c, err, string(out))
}
return nil
}