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"
|
2024-02-10 12:10:16 +01:00
|
|
|
"runtime"
|
2024-02-06 21:05:38 +01:00
|
|
|
"syscall"
|
|
|
|
|
|
|
|
"github.com/robfig/cron/v3"
|
2021-08-21 19:05:49 +02:00
|
|
|
)
|
|
|
|
|
2024-02-09 10:24:28 +01:00
|
|
|
type command struct {
|
|
|
|
logger *slog.Logger
|
|
|
|
}
|
|
|
|
|
|
|
|
func newCommand() *command {
|
|
|
|
return &command{
|
|
|
|
logger: slog.New(slog.NewTextHandler(os.Stdout, nil)),
|
2021-08-24 11:39:27 +02:00
|
|
|
}
|
2024-02-09 10:24:28 +01:00
|
|
|
}
|
2021-08-24 11:39:27 +02:00
|
|
|
|
2024-02-09 10:24:28 +01:00
|
|
|
func (c *command) must(err error) {
|
2024-02-06 21:05:38 +01:00
|
|
|
if err != nil {
|
2024-02-09 10:24:28 +01:00
|
|
|
c.logger.Error(
|
|
|
|
fmt.Sprintf("Fatal error running command: %v", err),
|
|
|
|
"error",
|
|
|
|
err,
|
|
|
|
)
|
|
|
|
os.Exit(1)
|
2024-02-06 21:05:38 +01:00
|
|
|
}
|
2024-02-09 10:24:28 +01:00
|
|
|
}
|
2024-02-06 21:05:38 +01:00
|
|
|
|
2024-02-09 10:24:28 +01:00
|
|
|
func runScript(c *Config) (err error) {
|
2024-02-01 18:14:18 +01:00
|
|
|
defer func() {
|
2024-02-09 10:24:28 +01:00
|
|
|
if derr := recover(); derr != nil {
|
|
|
|
err = fmt.Errorf("runScript: unexpected panic running script: %v", err)
|
2024-02-06 21:05:38 +01:00
|
|
|
}
|
2024-02-01 18:14:18 +01:00
|
|
|
}()
|
2022-03-25 18:26:34 +01:00
|
|
|
|
2024-02-09 10:24:28 +01:00
|
|
|
s, err := newScript(c)
|
|
|
|
if err != nil {
|
|
|
|
err = fmt.Errorf("runScript: error instantiating script: %w", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
runErr := func() (err error) {
|
|
|
|
unlock, err := s.lock("/var/lock/dockervolumebackup.lock")
|
|
|
|
if err != nil {
|
|
|
|
err = fmt.Errorf("runScript: error acquiring file lock: %w", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
defer func() {
|
|
|
|
derr := unlock()
|
|
|
|
if err == nil && derr != nil {
|
|
|
|
err = fmt.Errorf("runScript: error releasing file lock: %w", derr)
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
|
|
|
scriptErr := func() error {
|
|
|
|
if err := s.withLabeledCommands(lifecyclePhaseArchive, func() (err error) {
|
|
|
|
restartContainersAndServices, 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).
|
|
|
|
defer func() {
|
|
|
|
derr := restartContainersAndServices()
|
|
|
|
if err == nil {
|
|
|
|
err = derr
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
if err != nil {
|
|
|
|
return
|
2021-11-08 19:10:10 +01:00
|
|
|
}
|
2024-02-09 10:24:28 +01:00
|
|
|
err = s.createArchive()
|
|
|
|
return
|
|
|
|
})(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2024-02-06 21:05:38 +01:00
|
|
|
|
2024-02-09 10:24:28 +01:00
|
|
|
if err := s.withLabeledCommands(lifecyclePhaseProcess, s.encryptArchive)(); err != nil {
|
|
|
|
return err
|
2021-09-12 08:54:33 +02:00
|
|
|
}
|
2024-02-09 10:24:28 +01:00
|
|
|
if err := s.withLabeledCommands(lifecyclePhaseCopy, s.copyArchive)(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if err := s.withLabeledCommands(lifecyclePhasePrune, s.pruneBackups)(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}()
|
2021-11-08 19:10:10 +01:00
|
|
|
|
2024-02-09 10:24:28 +01:00
|
|
|
if hookErr := s.runHooks(scriptErr); hookErr != nil {
|
|
|
|
if scriptErr != nil {
|
|
|
|
return fmt.Errorf(
|
|
|
|
"runScript: error %w executing the script followed by %w calling the registered hooks",
|
|
|
|
scriptErr,
|
|
|
|
hookErr,
|
|
|
|
)
|
|
|
|
}
|
|
|
|
return fmt.Errorf(
|
|
|
|
"runScript: the script ran successfully, but an error occurred calling the registered hooks: %w",
|
|
|
|
hookErr,
|
2021-11-08 19:10:10 +01:00
|
|
|
)
|
2021-09-12 08:54:33 +02:00
|
|
|
}
|
2024-02-09 10:24:28 +01:00
|
|
|
if scriptErr != nil {
|
|
|
|
return fmt.Errorf("runScript: error running script: %w", scriptErr)
|
2021-08-24 11:39:27 +02:00
|
|
|
}
|
2024-02-09 10:24:28 +01:00
|
|
|
return nil
|
|
|
|
}()
|
2024-02-06 21:05:38 +01:00
|
|
|
|
2024-02-09 10:24:28 +01:00
|
|
|
if runErr != nil {
|
|
|
|
s.logger.Error(
|
|
|
|
fmt.Sprintf("Script run failed: %v", runErr), "error", runErr,
|
|
|
|
)
|
|
|
|
}
|
|
|
|
return runErr
|
2024-02-06 21:05:38 +01:00
|
|
|
}
|
|
|
|
|
2024-02-10 12:10:16 +01:00
|
|
|
func (c *command) runInForeground(profileCronExpression string) error {
|
2024-02-06 21:05:38 +01:00
|
|
|
cr := cron.New(
|
|
|
|
cron.WithParser(
|
|
|
|
cron.NewParser(
|
|
|
|
cron.SecondOptional | cron.Minute | cron.Hour | cron.Dom | cron.Month | cron.Dow | cron.Descriptor,
|
|
|
|
),
|
|
|
|
),
|
|
|
|
)
|
|
|
|
|
2024-02-09 10:24:28 +01:00
|
|
|
addJob := func(config *Config, name string) error {
|
|
|
|
if _, err := cr.AddFunc(config.BackupCronExpression, func() {
|
|
|
|
c.logger.Info(
|
|
|
|
fmt.Sprintf(
|
|
|
|
"Now running script on schedule %s",
|
|
|
|
config.BackupCronExpression,
|
|
|
|
),
|
|
|
|
)
|
|
|
|
if err := runScript(config); err != nil {
|
|
|
|
c.logger.Error(
|
|
|
|
fmt.Sprintf(
|
|
|
|
"Unexpected error running schedule %s: %v",
|
|
|
|
config.BackupCronExpression,
|
|
|
|
err,
|
|
|
|
),
|
|
|
|
"error",
|
|
|
|
err,
|
|
|
|
)
|
2024-02-06 21:05:38 +01:00
|
|
|
}
|
2024-02-09 10:24:28 +01:00
|
|
|
}); err != nil {
|
|
|
|
return fmt.Errorf("addJob: error adding schedule %s: %w", config.BackupCronExpression, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
c.logger.Info(fmt.Sprintf("Successfully scheduled backup %s with expression %s", name, config.BackupCronExpression))
|
|
|
|
if ok := checkCronSchedule(config.BackupCronExpression); !ok {
|
|
|
|
c.logger.Warn(
|
|
|
|
fmt.Sprintf("Scheduled cron expression %s will never run, is this intentional?", config.BackupCronExpression),
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
2024-02-06 21:05:38 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
cs, err := loadEnvFiles("/etc/dockervolumebackup/conf.d")
|
|
|
|
if err != nil {
|
|
|
|
if !os.IsNotExist(err) {
|
2024-02-09 10:24:28 +01:00
|
|
|
return fmt.Errorf("runInForeground: could not load config from environment files: %w", err)
|
2024-02-06 21:05:38 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
c, err := loadEnvVars()
|
|
|
|
if err != nil {
|
2024-02-09 10:24:28 +01:00
|
|
|
return fmt.Errorf("runInForeground: could not load config from environment variables: %w", err)
|
2024-02-06 21:05:38 +01:00
|
|
|
} else {
|
2024-02-09 10:24:28 +01:00
|
|
|
err = addJob(c, "from environment")
|
2024-02-06 21:05:38 +01:00
|
|
|
if err != nil {
|
2024-02-09 10:24:28 +01:00
|
|
|
return fmt.Errorf("runInForeground: error adding job from env: %w", err)
|
2024-02-06 21:05:38 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
2024-02-09 10:24:28 +01:00
|
|
|
c.logger.Info("/etc/dockervolumebackup/conf.d was found, using configuration files from this directory.")
|
|
|
|
for _, config := range cs {
|
|
|
|
err = addJob(config.config, config.name)
|
2024-02-06 21:05:38 +01:00
|
|
|
if err != nil {
|
2024-02-09 10:24:28 +01:00
|
|
|
return fmt.Errorf("runInForeground: error adding jobs from conf files: %w", err)
|
2024-02-06 21:05:38 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-02-10 12:10:16 +01:00
|
|
|
if profileCronExpression != "" {
|
|
|
|
if _, err := cr.AddFunc(profileCronExpression, func() {
|
|
|
|
memStats := runtime.MemStats{}
|
|
|
|
runtime.ReadMemStats(&memStats)
|
|
|
|
c.logger.Info(
|
|
|
|
"Collecting runtime information",
|
|
|
|
"num_goroutines",
|
|
|
|
runtime.NumGoroutine(),
|
|
|
|
"memory_heap_alloc",
|
|
|
|
formatBytes(memStats.HeapAlloc, false),
|
|
|
|
"memory_heap_inuse",
|
|
|
|
formatBytes(memStats.HeapInuse, false),
|
|
|
|
"memory_heap_sys",
|
|
|
|
formatBytes(memStats.HeapSys, false),
|
|
|
|
"memory_heap_objects",
|
|
|
|
memStats.HeapObjects,
|
|
|
|
)
|
|
|
|
}); err != nil {
|
|
|
|
return fmt.Errorf("runInForeground: error adding profiling job: %w", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-02-06 21:05:38 +01:00
|
|
|
var quit = make(chan os.Signal, 1)
|
|
|
|
signal.Notify(quit, syscall.SIGTERM, syscall.SIGINT)
|
|
|
|
cr.Start()
|
|
|
|
<-quit
|
|
|
|
ctx := cr.Stop()
|
|
|
|
<-ctx.Done()
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2024-02-09 10:24:28 +01:00
|
|
|
func (c *command) runAsCommand() error {
|
|
|
|
config, err := loadEnvVars()
|
2024-02-06 21:05:38 +01:00
|
|
|
if err != nil {
|
2024-02-09 10:24:28 +01:00
|
|
|
return fmt.Errorf("runAsCommand: error loading env vars: %w", err)
|
2024-02-06 21:05:38 +01:00
|
|
|
}
|
2024-02-09 10:24:28 +01:00
|
|
|
err = runScript(config)
|
2024-02-06 21:05:38 +01:00
|
|
|
if err != nil {
|
2024-02-09 10:24:28 +01:00
|
|
|
return fmt.Errorf("runAsCommand: error running script: %w", err)
|
2024-02-06 21:05:38 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func main() {
|
2024-02-09 10:24:28 +01:00
|
|
|
foreground := flag.Bool("foreground", false, "run the tool in the foreground")
|
2024-02-10 12:10:16 +01:00
|
|
|
profile := flag.String("profile", "", "collect runtime metrics and log them periodically on the given cron expression")
|
2024-02-06 21:05:38 +01:00
|
|
|
flag.Parse()
|
|
|
|
|
2024-02-09 10:24:28 +01:00
|
|
|
c := newCommand()
|
|
|
|
if *foreground {
|
2024-02-10 12:10:16 +01:00
|
|
|
c.must(c.runInForeground(*profile))
|
2024-02-06 21:05:38 +01:00
|
|
|
} else {
|
2024-02-09 10:24:28 +01:00
|
|
|
c.must(c.runAsCommand())
|
2024-02-06 21:05:38 +01:00
|
|
|
}
|
2021-12-17 20:45:15 +01:00
|
|
|
}
|