Skip to content

Commit

Permalink
Merge pull request #25 from offen/delete-on-failure
Browse files Browse the repository at this point in the history
Ensure script always tries to remove local artifacts even when backup failed
  • Loading branch information
m90 authored Sep 13, 2021
2 parents 184b7a1 + 53c2570 commit 6cf5cf4
Showing 1 changed file with 30 additions and 7 deletions.
37 changes: 30 additions & 7 deletions cmd/backup/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,15 @@ func main() {
panic(err)
}

defer func() {
if err := recover(); err != nil {
if e, ok := err.(error); ok && strings.Contains(e.Error(), msgBackupFailed) {
os.Exit(1)
}
panic(err)
}
}()

s.must(func() error {
restartContainers, err := s.stopContainers()
defer func() {
Expand All @@ -50,9 +59,14 @@ func main() {
return s.takeBackup()
}())

s.must(s.encryptBackup())
s.must(s.copyBackup())
s.must(s.removeArtifacts())
s.must(func() error {
defer func() {
s.must(s.removeArtifacts())
}()
s.must(s.encryptBackup())
return s.copyBackup()
}())

s.must(s.pruneOldBackups())
s.logger.Info("Finished running backup tasks.")
}
Expand Down Expand Up @@ -95,6 +109,8 @@ type config struct {
EmailSMTPPassword string `envconfig:"EMAIL_SMTP_PASSWORD"`
}

var msgBackupFailed = "backup run failed"

// newScript creates all resources needed for the script to perform actions against
// remote resources like the Docker engine or remote storage locations. All
// reading from env vars or other configuration sources is expected to happen
Expand Down Expand Up @@ -360,10 +376,17 @@ func (s *script) copyBackup() error {

// removeArtifacts removes the backup file from disk.
func (s *script) removeArtifacts() error {
_, err := os.Stat(s.file)
if err != nil {
if os.IsNotExist(err) {
return nil
}
return fmt.Errorf("removeArtifacts: error calling stat on file %s: %w", s.file, err)
}
if err := os.Remove(s.file); err != nil {
return fmt.Errorf("removeArtifacts: error removing file: %w", err)
return fmt.Errorf("removeArtifacts: error removing file %s: %w", s.file, err)
}
s.logger.Info("Removed local artifacts.")
s.logger.Infof("Removed local artifacts %s.", s.file)
return nil
}

Expand Down Expand Up @@ -521,7 +544,7 @@ func (s *script) runHooks(err error, targetLevel string) error {
return nil
}

// must exits the script run non-zero and prematurely in case the given error
// must exits the script run prematurely in case the given error
// is non-nil. If failure hooks have been registered on the script object, they
// will be called, passing the failure and previous log output.
func (s *script) must(err error) {
Expand All @@ -530,7 +553,7 @@ func (s *script) must(err error) {
if hookErr := s.runHooks(err, hookLevelFailure); hookErr != nil {
s.logger.Errorf("An error occurred calling the registered failure hooks: %s", hookErr)
}
os.Exit(1)
panic(errors.New(msgBackupFailed))
}
}

Expand Down

0 comments on commit 6cf5cf4

Please sign in to comment.