-
Notifications
You must be signed in to change notification settings - Fork 69
/
plugin.go
290 lines (248 loc) · 6.09 KB
/
plugin.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
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
package main
import (
"archive/tar"
"io"
"os"
"path/filepath"
"strings"
fmt "github.com/jhunt/go-ansi"
"github.com/starkandwayne/shield/plugin"
)
func main() {
p := FSPlugin{
Name: "Local Filesystem Plugin",
Author: "Stark & Wayne",
Version: "1.0.0",
Features: plugin.PluginFeatures{
Target: "yes",
Store: "no",
},
Example: `
{
"base_dir" : "/path/to/backup" # REQUIRED
"include" : "*.txt", # UNIX glob of files to include in backup
"exclude" : "*.o" # ... and another for what to exclude
}
`,
Defaults: `
{
}
`,
Fields: []plugin.Field{
plugin.Field{
Mode: "target",
Name: "base_dir",
Type: "abspath",
Title: "Base Directory",
Help: "Absolute path of the directory to backup.",
Example: "/srv/www/htdocs",
Required: true,
},
plugin.Field{
Mode: "target",
Name: "include",
Type: "string",
Title: "Files to Include",
Help: "Only files that match this pattern will be included in the backup archive. If not specified, all files will be included.",
},
plugin.Field{
Mode: "target",
Name: "exclude",
Type: "abspath",
Title: "Files to Exclude",
Help: "Files that match this pattern will be excluded from the backup archive. If not specified, no files will be excluded.",
},
},
}
plugin.DEBUG("fs plugin starting up...")
plugin.Run(p)
}
type FSPlugin plugin.PluginInfo
type FSConfig struct {
Include string
Exclude string
BasePath string
}
func (cfg *FSConfig) Match(path string) bool {
if cfg.Exclude != "" {
if ok, err := filepath.Match(cfg.Exclude, path); ok && err == nil {
return false
}
}
if cfg.Include != "" {
if ok, err := filepath.Match(cfg.Include, path); ok && err == nil {
return true
}
return false
}
return true
}
func (p FSPlugin) Meta() plugin.PluginInfo {
return plugin.PluginInfo(p)
}
func getFSConfig(endpoint plugin.ShieldEndpoint) (*FSConfig, error) {
include, err := endpoint.StringValueDefault("include", "")
if err != nil {
return nil, err
}
exclude, err := endpoint.StringValueDefault("exclude", "")
if err != nil {
return nil, err
}
base_dir, err := endpoint.StringValue("base_dir")
if err != nil {
return nil, err
}
return &FSConfig{
Include: include,
Exclude: exclude,
BasePath: base_dir,
}, nil
}
func (p FSPlugin) Validate(endpoint plugin.ShieldEndpoint) error {
var (
s string
err error
fail bool
)
s, err = endpoint.StringValue("base_dir")
if err != nil {
fmt.Printf("@R{\u2717 base_dir %s}\n", err)
fail = true
} else {
fmt.Printf("@G{\u2713 base_dir} files in @C{%s} will be backed up\n", s)
}
s, err = endpoint.StringValueDefault("include", "")
if err != nil {
fmt.Printf("@R{\u2717 include %s}\n", err)
fail = true
} else if s == "" {
fmt.Printf("@G{\u2713 include} all files will be included\n")
} else {
fmt.Printf("@G{\u2713 include} only files matching @C{%s} will be backed up\n", s)
}
s, err = endpoint.StringValueDefault("exclude", "")
if err != nil {
fmt.Printf("@R{\u2717 base_dir %s}\n", err)
fail = true
} else if s == "" {
fmt.Printf("@G{\u2713 exclude} no files will be excluded\n")
} else {
fmt.Printf("@G{\u2713 exclude} files matching @C{%s} will be skipped\n", s)
}
if fail {
return fmt.Errorf("fs: invalid configuration")
}
return nil
}
func (p FSPlugin) Backup(endpoint plugin.ShieldEndpoint) error {
cfg, err := getFSConfig(endpoint)
if err != nil {
return err
}
archive := tar.NewWriter(os.Stdout)
walker := func(path string, info os.FileInfo, err error) error {
if info == nil {
return fmt.Errorf("fs: base directory invalid")
}
if !cfg.Match(info.Name()) {
if info.IsDir() {
return filepath.SkipDir
}
return nil
}
baseRelative := strings.TrimPrefix(strings.Replace(path, cfg.BasePath, "", 1), "/")
if baseRelative == "" { /* musta been cfg.BasePath or cfg.BasePath + '/' */
return nil
}
link := ""
if info.Mode()&os.ModeType == os.ModeSymlink {
link, err = os.Readlink(path)
if err != nil {
return err
}
}
header, err := tar.FileInfoHeader(info, link)
if err != nil {
return err
}
header.Name = baseRelative
if err := archive.WriteHeader(header); err != nil {
return err
}
if info.Mode().IsDir() || link != "" {
return nil
}
if info.Mode().IsRegular() {
f, err := os.Open(path)
if err != nil {
return err
}
io.Copy(archive, f)
return nil
}
return fmt.Errorf("unable to archive special file '%s'", path)
}
if err := filepath.Walk(cfg.BasePath, walker); err != nil {
return err
}
return archive.Close()
}
func (p FSPlugin) Restore(endpoint plugin.ShieldEndpoint) error {
cfg, err := getFSConfig(endpoint)
if err != nil {
return err
}
if err := os.MkdirAll(cfg.BasePath, 0777); err != nil {
return err
}
archive := tar.NewReader(os.Stdin)
for {
header, err := archive.Next()
if err != nil {
if err == io.EOF {
return nil
}
return err
}
info := header.FileInfo()
if err != nil {
return err
}
path := fmt.Sprintf("%s/%s", cfg.BasePath, header.Name)
if info.Mode().IsDir() {
if err := os.MkdirAll(path, 0777); err != nil {
return err
}
} else if info.Mode().IsRegular() {
f, err := os.OpenFile(path, os.O_RDWR|os.O_CREATE, info.Mode())
if err != nil {
return err
}
if _, err := io.Copy(f, archive); err != nil {
return err
}
} else {
return fmt.Errorf("unable to unpack special file '%s'", path)
}
/* put things back the way they were... */
if err := os.Chtimes(path, header.AccessTime, header.ModTime); err != nil {
return err
}
if err := os.Chown(path, header.Uid, header.Gid); err != nil {
return err
}
if err := os.Chmod(path, info.Mode()); err != nil {
return err
}
}
}
func (p FSPlugin) Store(endpoint plugin.ShieldEndpoint) (string, int64, error) {
return "", 0, plugin.UNIMPLEMENTED
}
func (p FSPlugin) Retrieve(endpoint plugin.ShieldEndpoint, file string) error {
return plugin.UNIMPLEMENTED
}
func (p FSPlugin) Purge(endpoint plugin.ShieldEndpoint, file string) error {
return plugin.UNIMPLEMENTED
}