2016-05-22 13:04:37 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2023-08-21 11:16:46 +00:00
|
|
|
_ "embed"
|
2016-05-22 13:04:37 +00:00
|
|
|
"fmt"
|
|
|
|
"os"
|
|
|
|
"os/exec"
|
|
|
|
"path"
|
2016-06-25 23:33:28 +00:00
|
|
|
"regexp"
|
2016-05-22 13:04:37 +00:00
|
|
|
"strings"
|
|
|
|
"time"
|
|
|
|
|
2023-08-21 11:16:46 +00:00
|
|
|
"github.com/Luzifer/go_helpers/v2/env"
|
|
|
|
"github.com/Luzifer/go_helpers/v2/str"
|
|
|
|
"github.com/Luzifer/go_helpers/v2/which"
|
|
|
|
"github.com/Luzifer/rconfig/v2"
|
|
|
|
"github.com/pkg/errors"
|
2018-10-08 14:43:53 +00:00
|
|
|
"github.com/rifflock/lfshook"
|
|
|
|
|
2016-05-22 13:04:37 +00:00
|
|
|
"github.com/mitchellh/go-homedir"
|
|
|
|
"github.com/nightlyone/lockfile"
|
2023-08-21 11:16:46 +00:00
|
|
|
"github.com/sirupsen/logrus"
|
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
logDirPerms = 0o750
|
|
|
|
messageChanSize = 10
|
2016-05-22 13:04:37 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
|
|
|
cfg = struct {
|
|
|
|
ConfigFile string `flag:"config-file,f" default:"~/.config/duplicity-backup.yaml" description:"Configuration for this duplicity wrapper"`
|
|
|
|
LockFile string `flag:"lock-file,l" default:"~/.config/duplicity-backup.lock" description:"File to hold the lock for this wrapper execution"`
|
|
|
|
|
|
|
|
RestoreTime string `flag:"time,t" description:"The time from which to restore or list files"`
|
|
|
|
|
2018-10-08 14:43:53 +00:00
|
|
|
DryRun bool `flag:"dry-run,n" default:"false" description:"Do a test-run without changes"`
|
|
|
|
Silent bool `flag:"silent,s" default:"false" description:"Do not print to stdout, only write to logfile (for example useful for crons)"`
|
|
|
|
LogLevel string `flag:"log-level" default:"info" description:"Verbosity of logs to use (debug, info, warning, error, ...)"`
|
2016-05-30 05:32:46 +00:00
|
|
|
|
2016-05-22 13:04:37 +00:00
|
|
|
VersionAndExit bool `flag:"version" default:"false" description:"Print version and exit"`
|
|
|
|
}{}
|
|
|
|
|
|
|
|
duplicityBinary string
|
|
|
|
|
2023-08-21 11:16:46 +00:00
|
|
|
//go:embed help.txt
|
|
|
|
helpText string
|
|
|
|
|
2016-05-22 13:04:37 +00:00
|
|
|
version = "dev"
|
|
|
|
)
|
|
|
|
|
2023-08-21 11:16:46 +00:00
|
|
|
func initApp() error {
|
|
|
|
rconfig.AutoEnv(true)
|
2018-10-08 14:52:45 +00:00
|
|
|
if err := rconfig.Parse(&cfg); err != nil {
|
2023-08-21 11:16:46 +00:00
|
|
|
logrus.WithError(err).Fatal("Error while parsing arguments")
|
2016-05-22 13:04:37 +00:00
|
|
|
}
|
|
|
|
|
2023-08-21 11:16:46 +00:00
|
|
|
l, err := logrus.ParseLevel(cfg.LogLevel)
|
|
|
|
if err != nil {
|
|
|
|
return errors.Wrap(err, "parsing log-level")
|
2018-10-08 14:43:53 +00:00
|
|
|
}
|
2023-08-21 11:16:46 +00:00
|
|
|
logrus.SetLevel(l)
|
2018-10-08 14:43:53 +00:00
|
|
|
|
2016-05-22 13:04:37 +00:00
|
|
|
if cfg.ConfigFile, err = homedir.Expand(cfg.ConfigFile); err != nil {
|
2023-08-21 11:16:46 +00:00
|
|
|
return errors.Wrap(err, "expanding config-file path")
|
2016-05-22 13:04:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if cfg.LockFile, err = homedir.Expand(cfg.LockFile); err != nil {
|
2023-08-21 11:16:46 +00:00
|
|
|
return errors.Wrap(err, "expanding lock-file path")
|
2016-05-22 13:04:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if duplicityBinary, err = which.FindInPath("duplicity"); err != nil {
|
2023-08-21 11:16:46 +00:00
|
|
|
return errors.Wrap(err, "finding duplicity binary in $PATH")
|
2016-05-30 05:32:46 +00:00
|
|
|
}
|
2023-08-21 11:16:46 +00:00
|
|
|
|
|
|
|
return nil
|
2016-05-22 13:04:37 +00:00
|
|
|
}
|
|
|
|
|
2023-08-21 11:16:46 +00:00
|
|
|
//nolint:gocyclo // Slightly too complex, makes no sense to split
|
2016-05-22 13:04:37 +00:00
|
|
|
func main() {
|
|
|
|
var (
|
|
|
|
err error
|
|
|
|
config *configFile
|
|
|
|
)
|
|
|
|
|
2023-08-21 11:16:46 +00:00
|
|
|
if err = initApp(); err != nil {
|
|
|
|
logrus.WithError(err).Fatal("initializing app")
|
|
|
|
}
|
|
|
|
|
|
|
|
if cfg.VersionAndExit {
|
|
|
|
logrus.WithField("version", version).Info("duplicity-backup")
|
|
|
|
os.Exit(0)
|
|
|
|
}
|
|
|
|
|
2016-05-22 13:04:37 +00:00
|
|
|
lock, err := lockfile.New(cfg.LockFile)
|
|
|
|
if err != nil {
|
2023-08-21 11:16:46 +00:00
|
|
|
logrus.WithError(err).Fatal("initializing lockfile")
|
2016-05-22 13:04:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// If no command is passed assume we're requesting "help"
|
|
|
|
argv := rconfig.Args()
|
|
|
|
if len(argv) == 1 || argv[1] == "help" {
|
2023-08-21 11:16:46 +00:00
|
|
|
if _, err = os.Stderr.WriteString(helpText); err != nil {
|
|
|
|
logrus.WithError(err).Fatal("printing help to stderr")
|
|
|
|
}
|
2016-05-22 13:04:37 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// Get configuration
|
|
|
|
configSource, err := os.Open(cfg.ConfigFile)
|
|
|
|
if err != nil {
|
2023-08-21 11:16:46 +00:00
|
|
|
logrus.WithError(err).Fatalf("opening configuration file %s", cfg.ConfigFile)
|
2016-05-22 13:04:37 +00:00
|
|
|
}
|
2023-08-21 11:16:46 +00:00
|
|
|
defer configSource.Close() //nolint:errcheck // If this errors the file will be closed by process exit
|
|
|
|
|
2016-05-22 13:04:37 +00:00
|
|
|
config, err = loadConfigFile(configSource)
|
|
|
|
if err != nil {
|
2023-08-21 11:16:46 +00:00
|
|
|
logrus.WithError(err).Fatal("reading configuration file")
|
2016-05-22 13:04:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Initialize logfile
|
2023-08-21 11:16:46 +00:00
|
|
|
if err = os.MkdirAll(config.LogDirectory, logDirPerms); err != nil {
|
|
|
|
logrus.WithError(err).Fatal("creating log dir")
|
2018-10-08 14:06:28 +00:00
|
|
|
}
|
|
|
|
|
2016-05-22 13:04:37 +00:00
|
|
|
logFilePath := path.Join(config.LogDirectory, time.Now().Format("duplicity-backup_2006-01-02_15-04-05.txt"))
|
2023-08-21 11:16:46 +00:00
|
|
|
logFile, err := os.Create(logFilePath) //#nosec:G304 // That's a log file we just created the path for
|
2018-10-08 14:43:53 +00:00
|
|
|
if err != nil {
|
2023-08-21 11:16:46 +00:00
|
|
|
logrus.WithError(err).Fatalf("opening logfile %s", logFilePath)
|
2016-05-22 13:04:37 +00:00
|
|
|
}
|
2023-08-21 11:16:46 +00:00
|
|
|
defer logFile.Close() //nolint:errcheck // If this errors the file will be closed by process exit
|
2016-05-22 13:04:37 +00:00
|
|
|
|
2018-10-08 14:43:53 +00:00
|
|
|
// Hook into logging and write to file
|
2023-08-21 11:16:46 +00:00
|
|
|
logrus.AddHook(lfshook.NewHook(logFile, nil))
|
2018-10-08 14:43:53 +00:00
|
|
|
|
2023-08-21 11:16:46 +00:00
|
|
|
logrus.Infof("++++ duplicity-backup %s started with command '%s'", version, argv[1])
|
2016-05-22 13:04:37 +00:00
|
|
|
|
|
|
|
if err := lock.TryLock(); err != nil {
|
2023-08-21 11:16:46 +00:00
|
|
|
logrus.WithError(err).Error("acquiring lock")
|
2016-05-22 13:04:37 +00:00
|
|
|
return
|
|
|
|
}
|
2023-08-21 11:16:46 +00:00
|
|
|
defer func() {
|
|
|
|
if err = lock.Unlock(); err != nil {
|
|
|
|
logrus.WithError(err).Error("releasing log")
|
|
|
|
}
|
|
|
|
}()
|
2016-05-22 13:04:37 +00:00
|
|
|
|
|
|
|
if err := execute(config, argv[1:]); err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2016-05-23 09:59:23 +00:00
|
|
|
if config.Cleanup.Type != "none" && str.StringInSlice(argv[1], removeCommands) {
|
2023-08-21 11:16:46 +00:00
|
|
|
logrus.Info("++++ Starting removal of old backups")
|
2016-05-22 13:04:37 +00:00
|
|
|
|
2016-05-23 09:54:45 +00:00
|
|
|
if err := execute(config, []string{commandRemove}); err != nil {
|
2016-05-22 13:04:37 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-05-23 09:54:45 +00:00
|
|
|
if err := config.Notify(argv[1], true, nil); err != nil {
|
2023-08-21 11:16:46 +00:00
|
|
|
logrus.WithError(err).Error("sending notifications")
|
2016-05-23 08:41:20 +00:00
|
|
|
} else {
|
2023-08-21 11:16:46 +00:00
|
|
|
logrus.Info("notifications sent")
|
2016-05-23 08:41:20 +00:00
|
|
|
}
|
|
|
|
|
2023-08-21 11:16:46 +00:00
|
|
|
logrus.Info("++++ Backup finished successfully")
|
2016-05-22 13:04:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func execute(config *configFile, argv []string) error {
|
|
|
|
var (
|
2016-05-23 08:41:20 +00:00
|
|
|
err error
|
|
|
|
commandLine, tmpEnv []string
|
2016-06-25 23:33:28 +00:00
|
|
|
logFilter *regexp.Regexp
|
2016-05-22 13:04:37 +00:00
|
|
|
)
|
2023-08-21 11:16:46 +00:00
|
|
|
|
2016-06-25 23:33:28 +00:00
|
|
|
commandLine, tmpEnv, logFilter, err = config.GenerateCommand(argv, cfg.RestoreTime)
|
2016-05-22 13:04:37 +00:00
|
|
|
if err != nil {
|
2023-08-21 11:16:46 +00:00
|
|
|
logrus.WithError(err).Error("generating command")
|
2016-05-22 13:04:37 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2023-08-21 11:16:46 +00:00
|
|
|
procEnv := env.ListToMap(os.Environ())
|
|
|
|
for k, v := range env.ListToMap(tmpEnv) {
|
|
|
|
procEnv[k] = v
|
2016-05-23 08:41:20 +00:00
|
|
|
}
|
|
|
|
|
2016-05-22 13:04:37 +00:00
|
|
|
// Ensure duplicity is talking to us
|
|
|
|
commandLine = append([]string{"-v3"}, commandLine...)
|
|
|
|
|
|
|
|
if cfg.DryRun {
|
|
|
|
commandLine = append([]string{"--dry-run"}, commandLine...)
|
|
|
|
}
|
|
|
|
|
2023-08-21 11:16:46 +00:00
|
|
|
logrus.Debugf("Command: %s %s", duplicityBinary, strings.Join(commandLine, " "))
|
2016-05-22 13:04:37 +00:00
|
|
|
|
2023-08-21 11:16:46 +00:00
|
|
|
msgChan := make(chan string, messageChanSize)
|
2016-06-25 23:33:28 +00:00
|
|
|
go func(c chan string, logFilter *regexp.Regexp) {
|
2016-05-23 11:19:25 +00:00
|
|
|
for l := range c {
|
2016-06-25 23:33:28 +00:00
|
|
|
if logFilter == nil || logFilter.MatchString(l) {
|
2023-08-21 11:16:46 +00:00
|
|
|
logrus.Info(l)
|
2016-06-25 23:33:28 +00:00
|
|
|
}
|
2016-05-23 11:19:25 +00:00
|
|
|
}
|
2016-06-25 23:33:28 +00:00
|
|
|
}(msgChan, logFilter)
|
2016-05-23 11:19:25 +00:00
|
|
|
|
2016-05-23 14:04:50 +00:00
|
|
|
output := newMessageChanWriter(msgChan)
|
2018-10-08 14:06:28 +00:00
|
|
|
cmd := exec.Command(duplicityBinary, commandLine...) // #nosec G204
|
2016-05-22 13:04:37 +00:00
|
|
|
cmd.Stdout = output
|
|
|
|
cmd.Stderr = output
|
2023-08-21 11:16:46 +00:00
|
|
|
cmd.Env = env.MapToList(procEnv)
|
2016-05-22 13:04:37 +00:00
|
|
|
err = cmd.Run()
|
|
|
|
|
2016-05-23 11:19:25 +00:00
|
|
|
close(msgChan)
|
|
|
|
|
2016-05-22 13:04:37 +00:00
|
|
|
if err != nil {
|
2023-08-21 11:16:46 +00:00
|
|
|
logrus.Error("Execution of duplicity command was unsuccessful! (exit-code was non-zero)")
|
2016-05-22 13:04:37 +00:00
|
|
|
} else {
|
2023-08-21 11:16:46 +00:00
|
|
|
logrus.Info("Execution of duplicity command was successful.")
|
2016-05-22 13:04:37 +00:00
|
|
|
}
|
|
|
|
|
2016-05-23 09:54:45 +00:00
|
|
|
if err != nil {
|
2023-08-21 11:16:46 +00:00
|
|
|
if nErr := config.Notify(argv[0], false, fmt.Errorf("creating backup: %s", err)); nErr != nil {
|
|
|
|
logrus.WithError(err).Error("Error sending notifications")
|
2016-05-23 09:54:45 +00:00
|
|
|
} else {
|
2023-08-21 11:16:46 +00:00
|
|
|
logrus.Info("Notifications sent")
|
2016-05-23 08:41:20 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-08-21 11:16:46 +00:00
|
|
|
return errors.Wrap(err, "running duplicity")
|
2016-05-23 08:41:20 +00:00
|
|
|
}
|