2022-02-13 10:52:19 +01:00
|
|
|
// Copyright 2021-2022 - Offen Authors <hioffen@posteo.de>
|
2021-08-22 18:07:32 +02:00
|
|
|
// SPDX-License-Identifier: MPL-2.0
|
|
|
|
|
2021-08-21 19:05:49 +02:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"os"
|
|
|
|
)
|
|
|
|
|
|
|
|
func main() {
|
2021-08-24 11:39:27 +02:00
|
|
|
s, err := newScript()
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
2022-03-25 18:26:34 +01:00
|
|
|
unlock, err := s.lock("/var/lock/dockervolumebackup.lock")
|
|
|
|
defer unlock()
|
|
|
|
s.must(err)
|
|
|
|
|
2021-09-12 08:54:33 +02:00
|
|
|
defer func() {
|
2021-11-08 19:10:10 +01:00
|
|
|
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 {
|
2021-11-08 19:10:10 +01:00
|
|
|
s.logger.Errorf("An error occurred calling the registered hooks: %s", hookErr)
|
|
|
|
}
|
2021-09-12 08:54:33 +02:00
|
|
|
os.Exit(1)
|
|
|
|
}
|
2021-11-08 19:10:10 +01:00
|
|
|
panic(pArg)
|
|
|
|
}
|
|
|
|
|
2021-12-18 09:56:05 +01:00
|
|
|
if err := s.runHooks(nil); err != nil {
|
2021-11-08 19:10:10 +01:00
|
|
|
s.logger.Errorf(
|
|
|
|
"Backup procedure ran successfully, but an error ocurred calling the registered hooks: %v",
|
|
|
|
err,
|
|
|
|
)
|
|
|
|
os.Exit(1)
|
2021-09-12 08:54:33 +02:00
|
|
|
}
|
2021-11-08 19:10:10 +01:00
|
|
|
s.logger.Info("Finished running backup tasks.")
|
2021-09-12 08:54:33 +02:00
|
|
|
}()
|
|
|
|
|
2021-08-24 11:39:27 +02:00
|
|
|
s.must(func() error {
|
2022-02-22 07:53:33 +01:00
|
|
|
runPostCommands, err := s.runCommands()
|
|
|
|
defer func() {
|
|
|
|
s.must(runPostCommands())
|
|
|
|
}()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2021-08-24 11:39:27 +02:00
|
|
|
restartContainers, err := s.stopContainers()
|
2021-11-08 19:10:10 +01:00
|
|
|
// 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.takeBackup()
|
|
|
|
}())
|
|
|
|
|
2021-11-08 19:10:10 +01:00
|
|
|
s.must(s.encryptBackup())
|
|
|
|
s.must(s.copyBackup())
|
2022-02-13 10:52:19 +01:00
|
|
|
s.must(s.pruneBackups())
|
2021-12-17 20:45:15 +01:00
|
|
|
}
|