docker-volume-backup/cmd/backup/main.go

64 lines
1.5 KiB
Go
Raw Normal View History

// Copyright 2021-2022 - Offen Authors <hioffen@posteo.de>
2021-08-22 18:07:32 +02:00
// SPDX-License-Identifier: MPL-2.0
package main
import (
"fmt"
"os"
)
func main() {
2021-08-24 11:39:27 +02:00
s, err := newScript()
if err != nil {
panic(err)
}
unlock, err := s.lock("/var/lock/dockervolumebackup.lock")
defer s.must(unlock())
s.must(err)
defer func() {
if pArg := recover(); pArg != nil {
if err, ok := pArg.(error); ok {
2021-12-18 09:56:05 +01:00
if hookErr := s.runHooks(err); hookErr != nil {
s.logger.Error(
fmt.Sprintf("An error occurred calling the registered hooks: %s", hookErr),
)
}
os.Exit(1)
}
panic(pArg)
}
2021-12-18 09:56:05 +01:00
if err := s.runHooks(nil); err != nil {
s.logger.Error(
fmt.Sprintf(
"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 {
2024-01-25 19:44:28 +01:00
restartContainers, err := s.stopContainersAndServices()
// The mechanism for restarting containers is not using hooks as it
// should happen as soon as possible (i.e. before uploading backups or
// similar).
2021-08-26 16:22:24 +02:00
defer func() {
s.must(restartContainers())
}()
2021-08-24 11:39:27 +02:00
if err != nil {
return err
}
return s.createArchive()
})())
2021-08-24 11:39:27 +02:00
s.must(s.withLabeledCommands(lifecyclePhaseProcess, s.encryptArchive)())
s.must(s.withLabeledCommands(lifecyclePhaseCopy, s.copyArchive)())
s.must(s.withLabeledCommands(lifecyclePhasePrune, s.pruneBackups)())
2021-12-17 20:45:15 +01:00
}