Compare commits

...

2 Commits

Author SHA1 Message Date
Duru Can Celasun
bb29a98614
feat: Run PreValidate hooks for check cmd (#437)
PreValidate can be used to mount remote directories (e.g. via NFS) so
they must executed first before running any restic commands.

This was done for the backup command in 13aa560, but not for check. This
commit fixes that.
2025-03-22 19:12:36 +01:00
Duru Can Celasun
39f4f87ce3
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] bc97a3d1f9
2025-03-22 19:10:51 +01:00
4 changed files with 35 additions and 10 deletions

View File

@ -18,9 +18,11 @@ var backupCmd = &cobra.Command{
err := lock.Lock()
CheckErr(err)
defer lock.Unlock()
dry, _ := cmd.Flags().GetBool("dry-run")
selected, err := internal.GetAllOrSelected(cmd, false)
CheckErr(err)
errors := 0
for _, name := range selected {
var splitted = strings.Split(name, "@")
@ -29,7 +31,7 @@ var backupCmd = &cobra.Command{
specificBackend = splitted[1]
}
location, _ := internal.GetLocation(splitted[0])
errs := location.Backup(false, specificBackend)
errs := location.Backup(false, dry, specificBackend)
for _, err := range errs {
colors.Error.Printf("%s\n\n", err)
errors++
@ -44,4 +46,5 @@ var backupCmd = &cobra.Command{
func init() {
rootCmd.AddCommand(backupCmd)
internal.AddFlagsToCommand(backupCmd, false)
backupCmd.Flags().Bool("dry-run", false, "do not write changes, show what would be affected")
}

View File

@ -1,11 +1,14 @@
# Backup
```bash
autorestic backup [-l, --location] [-a, --all]
autorestic backup [-l, --location] [-a, --all] [--dry-run]
```
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.
The `--dry-run` flag will do a dry run showing what would have been deleted, but won't touch the actual data.
```bash
# All
autorestic backup -a

View File

@ -188,15 +188,31 @@ func CheckConfig() error {
if !CheckIfResticIsCallable() {
return fmt.Errorf(`%s was not found. Install either with "autorestic install" or manually`, flags.RESTIC_BIN)
}
for name, backend := range c.Backends {
backend.name = name
if err := backend.validate(); err != nil {
cwd, _ := GetPathRelativeToConfig(".")
for name, location := range c.Locations {
location.name = name
// Hooks before location validation
options := ExecuteOptions{
Command: "bash",
Dir: cwd,
Envs: map[string]string{
"AUTORESTIC_LOCATION": location.name,
},
}
if err := location.ExecuteHooks(location.Hooks.PreValidate, options); err != nil {
return err
}
if err := location.validate(); err != nil {
return err
}
}
for name, location := range c.Locations {
location.name = name
if err := location.validate(); err != nil {
for name, backend := range c.Backends {
backend.name = name
if err := backend.validate(); err != nil {
return err
}
}

View File

@ -168,7 +168,7 @@ func (l Location) getLocationTags() string {
return buildTag("location", l.name)
}
func (l Location) Backup(cron bool, specificBackend string) []error {
func (l Location) Backup(cron bool, dry bool, specificBackend string) []error {
var errors []error
var backends []string
colors.PrimaryPrint(" Backing up location \"%s\" ", l.name)
@ -228,6 +228,9 @@ func (l Location) Backup(cron bool, specificBackend string) []error {
if cron {
cmd = append(cmd, "--tag", buildTag("cron"))
}
if dry {
cmd = append(cmd, "--dry-run")
}
cmd = append(cmd, "--tag", l.getLocationTags())
backupOptions := ExecuteOptions{
Envs: env,
@ -447,7 +450,7 @@ func (l Location) RunCron() error {
now := time.Now()
if now.After(next) {
lock.SetCron(l.name, now.Unix())
errs := l.Backup(true, "")
errs := l.Backup(true, false, "")
if len(errs) > 0 {
return fmt.Errorf("Failed to backup location \"%s\":\n%w", l.name, errors.Join(errs...))
}