From 4a17c81b9162be6cbed684567d25440dd3c12037 Mon Sep 17 00:00:00 2001 From: Ben Johnson Date: Sun, 21 Mar 2021 08:04:35 -0600 Subject: [PATCH] Add -if-replica-exists flag to restore This commit adds a flag to change the exit code when restoring from a replica where there is no existing backup. When set, finding no backup will return a `0` exit code. The command will still fail if other errors occur. --- cmd/litestream/restore.go | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/cmd/litestream/restore.go b/cmd/litestream/restore.go index 0baaf89..66c5d00 100644 --- a/cmd/litestream/restore.go +++ b/cmd/litestream/restore.go @@ -27,6 +27,7 @@ func (c *RestoreCommand) Run(ctx context.Context, args []string) (err error) { fs.StringVar(&opt.Generation, "generation", "", "generation name") fs.IntVar(&opt.Index, "index", opt.Index, "wal index") fs.BoolVar(&opt.DryRun, "dry-run", false, "dry run") + ifReplicaExists := fs.Bool("if-replica-exists", false, "") timestampStr := fs.String("timestamp", "", "timestamp") verbose := fs.Bool("v", false, "verbose output") fs.Usage = c.Usage @@ -74,7 +75,12 @@ func (c *RestoreCommand) Run(ctx context.Context, args []string) (err error) { } // Return an error if no matching targets found. + // If optional flag set, return success. Useful for automated recovery. if opt.Generation == "" { + if *ifReplicaExists { + fmt.Println("no matching backups found") + return nil + } return fmt.Errorf("no matching backups found") } @@ -168,6 +174,9 @@ Arguments: Prints all log output as if it were running but does not perform actual restore. + -if-replica-exists + Returns exit code of 0 if no backups found. + -v Verbose output.