mirror of
https://github.com/offen/docker-volume-backup.git
synced 2024-11-14 10:30:27 +01:00
Frederik Ring
b441cf3e2b
* Refactor label command mechanism to be more flexible * Run all steps wrapped in labeled commands * Rename methods to be in line with lifecycle * Deprecate exec-pre and exec-post labels * Add documentation * Use type alias for lifecycle phases * Fix bad imports * Fix command lookup for deprecated labels * Use more generic naming for lifecycle phase * Fail on erroneous post command * Update documentation
59 lines
1.4 KiB
Go
59 lines
1.4 KiB
Go
// Copyright 2021-2022 - Offen Authors <hioffen@posteo.de>
|
|
// SPDX-License-Identifier: MPL-2.0
|
|
|
|
package main
|
|
|
|
import (
|
|
"os"
|
|
)
|
|
|
|
func main() {
|
|
s, err := newScript()
|
|
if err != nil {
|
|
panic(err)
|
|
}
|
|
|
|
unlock, err := s.lock("/var/lock/dockervolumebackup.lock")
|
|
defer unlock()
|
|
s.must(err)
|
|
|
|
defer func() {
|
|
if pArg := recover(); pArg != nil {
|
|
if err, ok := pArg.(error); ok {
|
|
if hookErr := s.runHooks(err); hookErr != nil {
|
|
s.logger.Errorf("An error occurred calling the registered hooks: %s", hookErr)
|
|
}
|
|
os.Exit(1)
|
|
}
|
|
panic(pArg)
|
|
}
|
|
|
|
if err := s.runHooks(nil); err != nil {
|
|
s.logger.Errorf(
|
|
"Backup procedure ran successfully, but an error ocurred calling the registered hooks: %v",
|
|
err,
|
|
)
|
|
os.Exit(1)
|
|
}
|
|
s.logger.Info("Finished running backup tasks.")
|
|
}()
|
|
|
|
s.must(s.withLabeledCommands(lifecyclePhaseArchive, func() error {
|
|
restartContainers, err := s.stopContainers()
|
|
// The mechanism for restarting containers is not using hooks as it
|
|
// should happen as soon as possible (i.e. before uploading backups or
|
|
// similar).
|
|
defer func() {
|
|
s.must(restartContainers())
|
|
}()
|
|
if err != nil {
|
|
return err
|
|
}
|
|
return s.createArchive()
|
|
})())
|
|
|
|
s.must(s.withLabeledCommands(lifecyclePhaseProcess, s.encryptArchive)())
|
|
s.must(s.withLabeledCommands(lifecyclePhaseCopy, s.copyArchive)())
|
|
s.must(s.withLabeledCommands(lifecyclePhasePrune, s.pruneBackups)())
|
|
}
|