Skip to content

Commit 39f4f87

Browse files
authored
feat: Add --dry-run to backup command (#438)
Restic supports --dry-run for backups since 0.13.0 [1] and this adds support for that. [1] restic/restic@bc97a3d
1 parent bd36bbe commit 39f4f87

File tree

3 files changed

+13
-4
lines changed

3 files changed

+13
-4
lines changed

cmd/backup.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,11 @@ var backupCmd = &cobra.Command{
1818
err := lock.Lock()
1919
CheckErr(err)
2020
defer lock.Unlock()
21+
dry, _ := cmd.Flags().GetBool("dry-run")
2122

2223
selected, err := internal.GetAllOrSelected(cmd, false)
2324
CheckErr(err)
25+
2426
errors := 0
2527
for _, name := range selected {
2628
var splitted = strings.Split(name, "@")
@@ -29,7 +31,7 @@ var backupCmd = &cobra.Command{
2931
specificBackend = splitted[1]
3032
}
3133
location, _ := internal.GetLocation(splitted[0])
32-
errs := location.Backup(false, specificBackend)
34+
errs := location.Backup(false, dry, specificBackend)
3335
for _, err := range errs {
3436
colors.Error.Printf("%s\n\n", err)
3537
errors++
@@ -44,4 +46,5 @@ var backupCmd = &cobra.Command{
4446
func init() {
4547
rootCmd.AddCommand(backupCmd)
4648
internal.AddFlagsToCommand(backupCmd, false)
49+
backupCmd.Flags().Bool("dry-run", false, "do not write changes, show what would be affected")
4750
}

docs/pages/cli/backup.md

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,14 @@
11
# Backup
22

33
```bash
4-
autorestic backup [-l, --location] [-a, --all]
4+
autorestic backup [-l, --location] [-a, --all] [--dry-run]
55
```
66

77
Performs a backup of all locations if the `-a` flag is passed. To only backup some locations pass one or more `-l` or `--location` flags.
88

9+
The `--dry-run` flag will do a dry run showing what would have been deleted, but won't touch the actual data.
10+
11+
912
```bash
1013
# All
1114
autorestic backup -a

internal/location.go

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -168,7 +168,7 @@ func (l Location) getLocationTags() string {
168168
return buildTag("location", l.name)
169169
}
170170

171-
func (l Location) Backup(cron bool, specificBackend string) []error {
171+
func (l Location) Backup(cron bool, dry bool, specificBackend string) []error {
172172
var errors []error
173173
var backends []string
174174
colors.PrimaryPrint(" Backing up location \"%s\" ", l.name)
@@ -228,6 +228,9 @@ func (l Location) Backup(cron bool, specificBackend string) []error {
228228
if cron {
229229
cmd = append(cmd, "--tag", buildTag("cron"))
230230
}
231+
if dry {
232+
cmd = append(cmd, "--dry-run")
233+
}
231234
cmd = append(cmd, "--tag", l.getLocationTags())
232235
backupOptions := ExecuteOptions{
233236
Envs: env,
@@ -447,7 +450,7 @@ func (l Location) RunCron() error {
447450
now := time.Now()
448451
if now.After(next) {
449452
lock.SetCron(l.name, now.Unix())
450-
errs := l.Backup(true, "")
453+
errs := l.Backup(true, false, "")
451454
if len(errs) > 0 {
452455
return fmt.Errorf("Failed to backup location \"%s\":\n%w", l.name, errors.Join(errs...))
453456
}

0 commit comments

Comments
 (0)