|
| 1 | +package archive |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + "path" |
| 6 | + "path/filepath" |
| 7 | + |
| 8 | + "github.com/4nkitd/gobackup/config" |
| 9 | + "github.com/4nkitd/gobackup/helper" |
| 10 | + "github.com/4nkitd/gobackup/logger" |
| 11 | +) |
| 12 | + |
| 13 | +// Run archive |
| 14 | +func Run(model config.ModelConfig) (err error) { |
| 15 | + if model.Archive == nil { |
| 16 | + return nil |
| 17 | + } |
| 18 | + |
| 19 | + logger.Info("------------- Archives -------------") |
| 20 | + |
| 21 | + helper.MkdirP(model.DumpPath) |
| 22 | + |
| 23 | + includes := model.Archive.GetStringSlice("includes") |
| 24 | + includes = cleanPaths(includes) |
| 25 | + |
| 26 | + excludes := model.Archive.GetStringSlice("excludes") |
| 27 | + excludes = cleanPaths(excludes) |
| 28 | + |
| 29 | + if len(includes) == 0 { |
| 30 | + return fmt.Errorf("archive.includes have no config") |
| 31 | + } |
| 32 | + logger.Info("=> includes", len(includes), "rules") |
| 33 | + |
| 34 | + opts := options(model.DumpPath, excludes, includes) |
| 35 | + helper.Exec("tar", opts...) |
| 36 | + |
| 37 | + logger.Info("------------- Archives -------------\n") |
| 38 | + |
| 39 | + return nil |
| 40 | +} |
| 41 | + |
| 42 | +func options(dumpPath string, excludes, includes []string) (opts []string) { |
| 43 | + tarPath := path.Join(dumpPath, "archive.tar") |
| 44 | + if helper.IsGnuTar { |
| 45 | + opts = append(opts, "--ignore-failed-read") |
| 46 | + } |
| 47 | + opts = append(opts, "-cPf", tarPath) |
| 48 | + |
| 49 | + for _, exclude := range excludes { |
| 50 | + opts = append(opts, "--exclude="+filepath.Clean(exclude)) |
| 51 | + } |
| 52 | + |
| 53 | + opts = append(opts, includes...) |
| 54 | + |
| 55 | + return opts |
| 56 | +} |
| 57 | + |
| 58 | +func cleanPaths(paths []string) (results []string) { |
| 59 | + for _, p := range paths { |
| 60 | + results = append(results, filepath.Clean(p)) |
| 61 | + } |
| 62 | + return |
| 63 | +} |
0 commit comments