2
2
mirror of https://github.com/octoleo/restic.git synced 2024-10-31 19:02:32 +00:00

cmd_backup: filter non-existing target files/dirs

This commit is contained in:
Alexander Neumann 2015-07-06 23:02:16 +02:00
parent cbe4c92c83
commit 57eb1a4eea

View File

@ -1,6 +1,7 @@
package main
import (
"errors"
"fmt"
"os"
"path/filepath"
@ -202,6 +203,25 @@ func findLatestSnapshot(repo *repository.Repository, targets []string) (backend.
return latestID, nil
}
// filterExisting returns a slice of all existing items, or an error if no
// items exist at all.
func filterExisting(items []string) (result []string, err error) {
for _, item := range items {
_, err := os.Lstat(item)
if err != nil && os.IsNotExist(err) {
continue
}
result = append(result, item)
}
if len(result) == 0 {
return nil, errors.New("all target directories/files do not exist")
}
return
}
func (cmd CmdBackup) Execute(args []string) error {
if len(args) == 0 {
return fmt.Errorf("wrong number of parameters, Usage: %s", cmd.Usage())
@ -215,6 +235,11 @@ func (cmd CmdBackup) Execute(args []string) error {
target = append(target, d)
}
target, err := filterExisting(target)
if err != nil {
return err
}
repo, err := cmd.global.OpenRepository()
if err != nil {
return err