Skip to content

Commit

Permalink
Merge pull request #5147 from planetscale/vtbackup-init
Browse files Browse the repository at this point in the history
vtbackup: Make initial backup idempotent again.
  • Loading branch information
sougou authored Aug 30, 2019
2 parents f68189a + 3a05a41 commit f2ff0ae
Showing 1 changed file with 8 additions and 7 deletions.
15 changes: 8 additions & 7 deletions go/cmd/vtbackup/vtbackup.go
Original file line number Diff line number Diff line change
Expand Up @@ -468,13 +468,21 @@ func parseBackupTime(name string) (time.Time, error) {
}

func shouldBackup(ctx context.Context, topoServer *topo.Server, backupStorage backupstorage.BackupStorage, backupDir string) (bool, error) {
// Look for the most recent, complete backup.
backups, err := backupStorage.ListBackups(ctx, backupDir)
if err != nil {
return false, fmt.Errorf("can't list backups: %v", err)
}
lastBackup := lastCompleteBackup(ctx, backups)

// Check preconditions for initial_backup mode.
if *initialBackup {
// Check if any backups for the shard already exist in this backup storage location.
if lastBackup != nil {
log.Infof("At least one complete backup already exists, so there's no need to seed an empty backup. Doing nothing.")
return false, nil
}

// Check whether the shard exists.
_, shardErr := topoServer.GetShard(ctx, *initKeyspace, *initShard)
switch {
Expand Down Expand Up @@ -505,11 +513,6 @@ func shouldBackup(ctx context.Context, topoServer *topo.Server, backupStorage ba
return false, fmt.Errorf("failed to check whether shard %v/%v exists before doing initial backup: %v", *initKeyspace, *initShard, err)
}

// Check if any backups for the shard exist in this backup storage location.
if len(backups) > 0 {
log.Infof("At least one backup already exists, so there's no need to seed an empty backup. Doing nothing.")
return false, nil
}
log.Infof("Shard %v/%v has no existing backups. Creating initial backup.", *initKeyspace, *initShard)
return true, nil
}
Expand All @@ -518,8 +521,6 @@ func shouldBackup(ctx context.Context, topoServer *topo.Server, backupStorage ba
if len(backups) == 0 && !*allowFirstBackup {
return false, fmt.Errorf("no existing backups to restore from; backup is not possible since -initial_backup flag was not enabled")
}
// Look for the most recent, complete backup.
lastBackup := lastCompleteBackup(ctx, backups)
if lastBackup == nil {
if *allowFirstBackup {
// There's no complete backup, but we were told to take one from scratch anyway.
Expand Down

0 comments on commit f2ff0ae

Please sign in to comment.