Merge pull request #163 from benbjohnson/remove-dry-run

Remove -dry-run flag in restore
This commit is contained in:
Ben Johnson
2021-04-18 09:26:01 -06:00
committed by GitHub
2 changed files with 14 additions and 36 deletions

View File

@@ -26,7 +26,6 @@ func (c *RestoreCommand) Run(ctx context.Context, args []string) (err error) {
fs.StringVar(&opt.ReplicaName, "replica", "", "replica name") fs.StringVar(&opt.ReplicaName, "replica", "", "replica name")
fs.StringVar(&opt.Generation, "generation", "", "generation name") fs.StringVar(&opt.Generation, "generation", "", "generation name")
fs.IntVar(&opt.Index, "index", opt.Index, "wal index") 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, "") ifReplicaExists := fs.Bool("if-replica-exists", false, "")
timestampStr := fs.String("timestamp", "", "timestamp") timestampStr := fs.String("timestamp", "", "timestamp")
verbose := fs.Bool("v", false, "verbose output") verbose := fs.Bool("v", false, "verbose output")
@@ -46,11 +45,6 @@ func (c *RestoreCommand) Run(ctx context.Context, args []string) (err error) {
} }
} }
// Verbose output is automatically enabled if dry run is specified.
if opt.DryRun {
*verbose = true
}
// Instantiate logger if verbose output is enabled. // Instantiate logger if verbose output is enabled.
if *verbose { if *verbose {
opt.Logger = log.New(os.Stderr, "", log.LstdFlags|log.Lmicroseconds) opt.Logger = log.New(os.Stderr, "", log.LstdFlags|log.Lmicroseconds)
@@ -173,10 +167,6 @@ Arguments:
Output path of the restored database. Output path of the restored database.
Defaults to original DB path. Defaults to original DB path.
-dry-run
Prints all log output as if it were running but does
not perform actual restore.
-if-replica-exists -if-replica-exists
Returns exit code of 0 if no backups found. Returns exit code of 0 if no backups found.

14
db.go
View File

@@ -1455,14 +1455,12 @@ func RestoreReplica(ctx context.Context, r Replica, opt RestoreOptions) error {
logPrefix = fmt.Sprintf("%s(%s)", db.Path(), r.Name()) logPrefix = fmt.Sprintf("%s(%s)", db.Path(), r.Name())
} }
// Ensure output path does not already exist (unless this is a dry run). // Ensure output path does not already exist.
if !opt.DryRun {
if _, err := os.Stat(opt.OutputPath); err == nil { if _, err := os.Stat(opt.OutputPath); err == nil {
return fmt.Errorf("cannot restore, output path already exists: %s", opt.OutputPath) return fmt.Errorf("cannot restore, output path already exists: %s", opt.OutputPath)
} else if err != nil && !os.IsNotExist(err) { } else if err != nil && !os.IsNotExist(err) {
return err return err
} }
}
// Find lastest snapshot that occurs before timestamp. // Find lastest snapshot that occurs before timestamp.
minWALIndex, err := SnapshotIndexAt(ctx, r, opt.Generation, opt.Timestamp) minWALIndex, err := SnapshotIndexAt(ctx, r, opt.Generation, opt.Timestamp)
@@ -1483,22 +1481,18 @@ func RestoreReplica(ctx context.Context, r Replica, opt RestoreOptions) error {
// Copy snapshot to output path. // Copy snapshot to output path.
logger.Printf("%s: restoring snapshot %s/%08x to %s", logPrefix, opt.Generation, minWALIndex, tmpPath) logger.Printf("%s: restoring snapshot %s/%08x to %s", logPrefix, opt.Generation, minWALIndex, tmpPath)
if !opt.DryRun {
if err := restoreSnapshot(ctx, r, pos.Generation, pos.Index, tmpPath); err != nil { if err := restoreSnapshot(ctx, r, pos.Generation, pos.Index, tmpPath); err != nil {
return fmt.Errorf("cannot restore snapshot: %w", err) return fmt.Errorf("cannot restore snapshot: %w", err)
} }
}
// Restore each WAL file until we reach our maximum index. // Restore each WAL file until we reach our maximum index.
for index := minWALIndex; index <= maxWALIndex; index++ { for index := minWALIndex; index <= maxWALIndex; index++ {
if !opt.DryRun {
if err = restoreWAL(ctx, r, opt.Generation, index, tmpPath); os.IsNotExist(err) && index == minWALIndex && index == maxWALIndex { if err = restoreWAL(ctx, r, opt.Generation, index, tmpPath); os.IsNotExist(err) && index == minWALIndex && index == maxWALIndex {
logger.Printf("%s: no wal available, snapshot only", logPrefix) logger.Printf("%s: no wal available, snapshot only", logPrefix)
break // snapshot file only, ignore error break // snapshot file only, ignore error
} else if err != nil { } else if err != nil {
return fmt.Errorf("cannot restore wal: %w", err) return fmt.Errorf("cannot restore wal: %w", err)
} }
}
if opt.Verbose { if opt.Verbose {
logger.Printf("%s: restored wal %s/%08x", logPrefix, opt.Generation, index) logger.Printf("%s: restored wal %s/%08x", logPrefix, opt.Generation, index)
@@ -1507,11 +1501,9 @@ func RestoreReplica(ctx context.Context, r Replica, opt RestoreOptions) error {
// Copy file to final location. // Copy file to final location.
logger.Printf("%s: renaming database from temporary location", logPrefix) logger.Printf("%s: renaming database from temporary location", logPrefix)
if !opt.DryRun {
if err := os.Rename(tmpPath, opt.OutputPath); err != nil { if err := os.Rename(tmpPath, opt.OutputPath); err != nil {
return err return err
} }
}
return nil return nil
} }
@@ -1740,10 +1732,6 @@ type RestoreOptions struct {
// If zero, database restore to most recent state available. // If zero, database restore to most recent state available.
Timestamp time.Time Timestamp time.Time
// If true, no actual restore is performed.
// Only equivalent log output for a regular restore.
DryRun bool
// Logging settings. // Logging settings.
Logger *log.Logger Logger *log.Logger
Verbose bool Verbose bool