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 (
|
2024-02-06 21:05:38 +01:00
|
|
|
"flag"
|
2023-08-10 19:41:03 +02:00
|
|
|
"fmt"
|
2024-02-06 21:05:38 +01:00
|
|
|
"log/slog"
|
2021-08-21 19:05:49 +02:00
|
|
|
"os"
|
2024-02-06 21:05:38 +01:00
|
|
|
"os/signal"
|
|
|
|
"syscall"
|
|
|
|
|
|
|
|
"github.com/robfig/cron/v3"
|
2021-08-21 19:05:49 +02:00
|
|
|
)
|
|
|
|
|
2024-02-06 21:05:38 +01:00
|
|
|
func runScript(c *Config) (ret error) {
|
|
|
|
s, err := newScript(c)
|
2021-08-24 11:39:27 +02:00
|
|
|
if err != nil {
|
2024-02-06 21:05:38 +01:00
|
|
|
return err
|
2021-08-24 11:39:27 +02:00
|
|
|
}
|
|
|
|
|
2022-03-25 18:26:34 +01:00
|
|
|
unlock, err := s.lock("/var/lock/dockervolumebackup.lock")
|
2024-02-06 21:05:38 +01:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2024-02-01 18:14:18 +01:00
|
|
|
defer func() {
|
2024-02-06 21:05:38 +01:00
|
|
|
err = unlock()
|
|
|
|
if err != nil {
|
|
|
|
ret = err
|
|
|
|
}
|
2024-02-01 18:14:18 +01:00
|
|
|
}()
|
2022-03-25 18:26:34 +01:00
|
|
|
|
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 {
|
2024-01-26 20:02:09 +01:00
|
|
|
s.logger.Error(
|
|
|
|
fmt.Sprintf("Executing the script encountered a panic: %v", err),
|
|
|
|
)
|
2021-12-18 09:56:05 +01:00
|
|
|
if hookErr := s.runHooks(err); hookErr != nil {
|
2023-08-10 19:41:03 +02:00
|
|
|
s.logger.Error(
|
|
|
|
fmt.Sprintf("An error occurred calling the registered hooks: %s", hookErr),
|
|
|
|
)
|
2021-11-08 19:10:10 +01:00
|
|
|
}
|
2024-02-06 21:05:38 +01:00
|
|
|
ret = err
|
|
|
|
} else {
|
|
|
|
s.logger.Error(
|
|
|
|
fmt.Sprintf("Executing the script encountered an unrecoverable panic: %v", err),
|
|
|
|
)
|
|
|
|
|
|
|
|
panic(pArg)
|
2021-09-12 08:54:33 +02:00
|
|
|
}
|
2021-11-08 19:10:10 +01:00
|
|
|
}
|
|
|
|
|
2021-12-18 09:56:05 +01:00
|
|
|
if err := s.runHooks(nil); err != nil {
|
2023-08-10 19:41:03 +02:00
|
|
|
s.logger.Error(
|
|
|
|
fmt.Sprintf(
|
|
|
|
"Backup procedure ran successfully, but an error ocurred calling the registered hooks: %v",
|
|
|
|
err,
|
|
|
|
),
|
2021-11-08 19:10:10 +01:00
|
|
|
)
|
2024-02-06 21:05:38 +01:00
|
|
|
ret = err
|
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
|
|
|
}()
|
|
|
|
|
2022-07-10 10:36:56 +02:00
|
|
|
s.must(s.withLabeledCommands(lifecyclePhaseArchive, func() error {
|
2024-01-31 12:17:41 +01:00
|
|
|
restartContainersAndServices, err := s.stopContainersAndServices()
|
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() {
|
2024-01-31 12:17:41 +01:00
|
|
|
s.must(restartContainersAndServices())
|
2021-08-26 16:22:24 +02:00
|
|
|
}()
|
2021-08-24 11:39:27 +02:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2022-07-10 10:36:56 +02:00
|
|
|
return s.createArchive()
|
|
|
|
})())
|
2021-08-24 11:39:27 +02:00
|
|
|
|
2022-07-10 10:36:56 +02:00
|
|
|
s.must(s.withLabeledCommands(lifecyclePhaseProcess, s.encryptArchive)())
|
|
|
|
s.must(s.withLabeledCommands(lifecyclePhaseCopy, s.copyArchive)())
|
|
|
|
s.must(s.withLabeledCommands(lifecyclePhasePrune, s.pruneBackups)())
|
2024-02-06 21:05:38 +01:00
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func runInForeground() error {
|
|
|
|
cr := cron.New(
|
|
|
|
cron.WithParser(
|
|
|
|
cron.NewParser(
|
|
|
|
cron.SecondOptional | cron.Minute | cron.Hour | cron.Dom | cron.Month | cron.Dow | cron.Descriptor,
|
|
|
|
),
|
|
|
|
),
|
|
|
|
)
|
|
|
|
|
|
|
|
addJob := func(c *Config) error {
|
|
|
|
_, err := cr.AddFunc(c.BackupCronExpression, func() {
|
|
|
|
err := runScript(c)
|
|
|
|
if err != nil {
|
|
|
|
slog.Error("unexpected error during backup", "error", err)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
cs, err := loadEnvFiles("/etc/dockervolumebackup/conf.d")
|
|
|
|
if err != nil {
|
|
|
|
if !os.IsNotExist(err) {
|
|
|
|
return fmt.Errorf("could not load config from environment files, error: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
c, err := loadEnvVars()
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("could not load config from environment variables")
|
|
|
|
} else {
|
|
|
|
err = addJob(c)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("could not add cron job, error: %w", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
for _, c := range cs {
|
|
|
|
err = addJob(c)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("could not add cron job, error: %w", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
var quit = make(chan os.Signal, 1)
|
|
|
|
signal.Notify(quit, syscall.SIGTERM, syscall.SIGINT)
|
|
|
|
cr.Start()
|
|
|
|
<-quit
|
|
|
|
ctx := cr.Stop()
|
|
|
|
<-ctx.Done()
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func runAsCommand() error {
|
|
|
|
c, err := loadEnvVars()
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("could not load config from environment variables, error: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
err = runScript(c)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("unexpected error during backup, error: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func main() {
|
|
|
|
serve := flag.Bool("foreground", false, "run the tool in the foreground")
|
|
|
|
flag.Parse()
|
|
|
|
|
|
|
|
var err error
|
|
|
|
if *serve {
|
|
|
|
err = runInForeground()
|
|
|
|
} else {
|
|
|
|
err = runAsCommand()
|
|
|
|
}
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
slog.Error("ran into an issue during execution", "error", err)
|
|
|
|
os.Exit(1)
|
|
|
|
}
|
2021-12-17 20:45:15 +01:00
|
|
|
}
|