2022-02-13 10:52:19 +01:00
|
|
|
// Copyright 2022 - Offen Authors <hioffen@posteo.de>
|
|
|
|
// SPDX-License-Identifier: MPL-2.0
|
|
|
|
|
|
|
|
package main
|
|
|
|
|
2022-05-08 11:20:38 +02:00
|
|
|
import (
|
2022-12-22 14:37:51 +01:00
|
|
|
"crypto/x509"
|
|
|
|
"encoding/pem"
|
2022-05-08 11:20:38 +02:00
|
|
|
"fmt"
|
2022-10-12 21:15:17 +02:00
|
|
|
"os"
|
2022-05-08 11:20:38 +02:00
|
|
|
"regexp"
|
2023-08-24 19:33:47 +02:00
|
|
|
"strconv"
|
2022-05-08 11:20:38 +02:00
|
|
|
"time"
|
2024-02-16 15:35:42 +01:00
|
|
|
|
|
|
|
"github.com/offen/docker-volume-backup/internal/errwrap"
|
2022-05-08 11:20:38 +02:00
|
|
|
)
|
2022-02-13 10:52:19 +01:00
|
|
|
|
|
|
|
// Config holds all configuration values that are expected to be set
|
|
|
|
// by users.
|
|
|
|
type Config struct {
|
2023-08-19 19:20:13 +02:00
|
|
|
AwsS3BucketName string `split_words:"true"`
|
|
|
|
AwsS3Path string `split_words:"true"`
|
|
|
|
AwsEndpoint string `split_words:"true" default:"s3.amazonaws.com"`
|
|
|
|
AwsEndpointProto string `split_words:"true" default:"https"`
|
|
|
|
AwsEndpointInsecure bool `split_words:"true"`
|
|
|
|
AwsEndpointCACert CertDecoder `envconfig:"AWS_ENDPOINT_CA_CERT"`
|
|
|
|
AwsStorageClass string `split_words:"true"`
|
|
|
|
AwsAccessKeyID string `envconfig:"AWS_ACCESS_KEY_ID"`
|
|
|
|
AwsSecretAccessKey string `split_words:"true"`
|
|
|
|
AwsIamRoleEndpoint string `split_words:"true"`
|
|
|
|
AwsPartSize int64 `split_words:"true"`
|
|
|
|
BackupCompression CompressionType `split_words:"true" default:"gz"`
|
2023-09-03 16:49:52 +02:00
|
|
|
GzipParallelism WholeNumber `split_words:"true" default:"1"`
|
2023-08-19 19:20:13 +02:00
|
|
|
BackupSources string `split_words:"true" default:"/backup"`
|
|
|
|
BackupFilename string `split_words:"true" default:"backup-%Y-%m-%dT%H-%M-%S.{{ .Extension }}"`
|
|
|
|
BackupFilenameExpand bool `split_words:"true"`
|
|
|
|
BackupLatestSymlink string `split_words:"true"`
|
|
|
|
BackupArchive string `split_words:"true" default:"/archive"`
|
2024-02-06 21:05:38 +01:00
|
|
|
BackupCronExpression string `split_words:"true" default:"@daily"`
|
2023-08-19 19:20:13 +02:00
|
|
|
BackupRetentionDays int32 `split_words:"true" default:"-1"`
|
|
|
|
BackupPruningLeeway time.Duration `split_words:"true" default:"1m"`
|
|
|
|
BackupPruningPrefix string `split_words:"true"`
|
2024-01-31 12:17:41 +01:00
|
|
|
BackupStopContainerLabel string `split_words:"true"`
|
|
|
|
BackupStopDuringBackupLabel string `split_words:"true" default:"true"`
|
|
|
|
BackupStopServiceTimeout time.Duration `split_words:"true" default:"5m"`
|
2023-08-19 19:20:13 +02:00
|
|
|
BackupFromSnapshot bool `split_words:"true"`
|
|
|
|
BackupExcludeRegexp RegexpDecoder `split_words:"true"`
|
2023-08-27 19:19:11 +02:00
|
|
|
BackupSkipBackendsFromPrune []string `split_words:"true"`
|
2023-08-19 19:20:13 +02:00
|
|
|
GpgPassphrase string `split_words:"true"`
|
|
|
|
NotificationURLs []string `envconfig:"NOTIFICATION_URLS"`
|
|
|
|
NotificationLevel string `split_words:"true" default:"error"`
|
|
|
|
EmailNotificationRecipient string `split_words:"true"`
|
|
|
|
EmailNotificationSender string `split_words:"true" default:"noreply@nohost"`
|
|
|
|
EmailSMTPHost string `envconfig:"EMAIL_SMTP_HOST"`
|
|
|
|
EmailSMTPPort int `envconfig:"EMAIL_SMTP_PORT" default:"587"`
|
|
|
|
EmailSMTPUsername string `envconfig:"EMAIL_SMTP_USERNAME"`
|
|
|
|
EmailSMTPPassword string `envconfig:"EMAIL_SMTP_PASSWORD"`
|
|
|
|
WebdavUrl string `split_words:"true"`
|
|
|
|
WebdavUrlInsecure bool `split_words:"true"`
|
|
|
|
WebdavPath string `split_words:"true" default:"/"`
|
|
|
|
WebdavUsername string `split_words:"true"`
|
|
|
|
WebdavPassword string `split_words:"true"`
|
|
|
|
SSHHostName string `split_words:"true"`
|
|
|
|
SSHPort string `split_words:"true" default:"22"`
|
|
|
|
SSHUser string `split_words:"true"`
|
|
|
|
SSHPassword string `split_words:"true"`
|
|
|
|
SSHIdentityFile string `split_words:"true" default:"/root/.ssh/id_rsa"`
|
|
|
|
SSHIdentityPassphrase string `split_words:"true"`
|
|
|
|
SSHRemotePath string `split_words:"true"`
|
|
|
|
ExecLabel string `split_words:"true"`
|
|
|
|
ExecForwardOutput bool `split_words:"true"`
|
|
|
|
LockTimeout time.Duration `split_words:"true" default:"60m"`
|
|
|
|
AzureStorageAccountName string `split_words:"true"`
|
|
|
|
AzureStoragePrimaryAccountKey string `split_words:"true"`
|
|
|
|
AzureStorageContainerName string `split_words:"true"`
|
|
|
|
AzureStoragePath string `split_words:"true"`
|
|
|
|
AzureStorageEndpoint string `split_words:"true" default:"https://{{ .AccountName }}.blob.core.windows.net/"`
|
2023-08-24 19:33:47 +02:00
|
|
|
DropboxEndpoint string `split_words:"true" default:"https://api.dropbox.com/"`
|
|
|
|
DropboxOAuth2Endpoint string `envconfig:"DROPBOX_OAUTH2_ENDPOINT" default:"https://api.dropbox.com/"`
|
|
|
|
DropboxRefreshToken string `split_words:"true"`
|
|
|
|
DropboxAppKey string `split_words:"true"`
|
|
|
|
DropboxAppSecret string `split_words:"true"`
|
|
|
|
DropboxRemotePath string `split_words:"true"`
|
|
|
|
DropboxConcurrencyLevel NaturalNumber `split_words:"true" default:"6"`
|
2024-02-13 19:21:57 +01:00
|
|
|
source string
|
|
|
|
additionalEnvVars map[string]string
|
2022-02-13 10:52:19 +01:00
|
|
|
}
|
2022-05-08 11:20:38 +02:00
|
|
|
|
2023-08-19 19:20:13 +02:00
|
|
|
type CompressionType string
|
|
|
|
|
|
|
|
func (c *CompressionType) Decode(v string) error {
|
|
|
|
switch v {
|
|
|
|
case "gz", "zst":
|
|
|
|
*c = CompressionType(v)
|
|
|
|
return nil
|
|
|
|
default:
|
2024-02-16 15:35:42 +01:00
|
|
|
return errwrap.Wrap(nil, fmt.Sprintf("error decoding compression type %s", v))
|
2023-08-19 19:20:13 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *CompressionType) String() string {
|
|
|
|
return string(*c)
|
|
|
|
}
|
|
|
|
|
2022-12-22 14:37:51 +01:00
|
|
|
type CertDecoder struct {
|
|
|
|
Cert *x509.Certificate
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *CertDecoder) Decode(v string) error {
|
|
|
|
if v == "" {
|
|
|
|
return nil
|
|
|
|
}
|
2023-08-27 18:14:55 +02:00
|
|
|
content, err := os.ReadFile(v)
|
2022-12-22 14:37:51 +01:00
|
|
|
if err != nil {
|
|
|
|
content = []byte(v)
|
|
|
|
}
|
|
|
|
block, _ := pem.Decode(content)
|
|
|
|
cert, err := x509.ParseCertificate(block.Bytes)
|
|
|
|
if err != nil {
|
2024-02-16 15:35:42 +01:00
|
|
|
return errwrap.Wrap(err, "error parsing certificate")
|
2022-12-22 14:37:51 +01:00
|
|
|
}
|
|
|
|
*c = CertDecoder{Cert: cert}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2022-05-08 11:20:38 +02:00
|
|
|
type RegexpDecoder struct {
|
|
|
|
Re *regexp.Regexp
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r *RegexpDecoder) Decode(v string) error {
|
|
|
|
if v == "" {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
re, err := regexp.Compile(v)
|
|
|
|
if err != nil {
|
2024-02-16 15:35:42 +01:00
|
|
|
return errwrap.Wrap(err, fmt.Sprintf("error compiling given regexp `%s`", v))
|
2022-05-08 11:20:38 +02:00
|
|
|
}
|
|
|
|
*r = RegexpDecoder{Re: re}
|
|
|
|
return nil
|
|
|
|
}
|
2023-08-24 19:33:47 +02:00
|
|
|
|
2023-09-03 16:49:52 +02:00
|
|
|
// NaturalNumber is a type that can be used to decode a positive, non-zero natural number
|
2023-08-24 19:33:47 +02:00
|
|
|
type NaturalNumber int
|
|
|
|
|
|
|
|
func (n *NaturalNumber) Decode(v string) error {
|
|
|
|
asInt, err := strconv.Atoi(v)
|
|
|
|
if err != nil {
|
2024-02-16 15:35:42 +01:00
|
|
|
return errwrap.Wrap(nil, fmt.Sprintf("error converting %s to int", v))
|
2023-08-24 19:33:47 +02:00
|
|
|
}
|
|
|
|
if asInt <= 0 {
|
2024-02-16 15:35:42 +01:00
|
|
|
return errwrap.Wrap(nil, fmt.Sprintf("expected a natural number, got %d", asInt))
|
2023-08-24 19:33:47 +02:00
|
|
|
}
|
|
|
|
*n = NaturalNumber(asInt)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (n *NaturalNumber) Int() int {
|
|
|
|
return int(*n)
|
|
|
|
}
|
2023-09-03 16:49:52 +02:00
|
|
|
|
|
|
|
// WholeNumber is a type that can be used to decode a positive whole number, including zero
|
|
|
|
type WholeNumber int
|
|
|
|
|
|
|
|
func (n *WholeNumber) Decode(v string) error {
|
|
|
|
asInt, err := strconv.Atoi(v)
|
|
|
|
if err != nil {
|
2024-02-16 15:35:42 +01:00
|
|
|
return errwrap.Wrap(nil, fmt.Sprintf("error converting %s to int", v))
|
2023-09-03 16:49:52 +02:00
|
|
|
}
|
|
|
|
if asInt < 0 {
|
2024-02-16 15:35:42 +01:00
|
|
|
return errwrap.Wrap(nil, fmt.Sprintf("expected a whole, positive number, including zero. Got %d", asInt))
|
2023-09-03 16:49:52 +02:00
|
|
|
}
|
|
|
|
*n = WholeNumber(asInt)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (n *WholeNumber) Int() int {
|
|
|
|
return int(*n)
|
|
|
|
}
|
2024-02-13 19:21:57 +01:00
|
|
|
|
|
|
|
type envVarLookup struct {
|
|
|
|
ok bool
|
|
|
|
key string
|
|
|
|
value string
|
|
|
|
}
|
|
|
|
|
|
|
|
// applyEnv sets the values in `additionalEnvVars` as environment variables.
|
|
|
|
// It returns a function that reverts all values that have been set to its
|
|
|
|
// previous state.
|
|
|
|
func (c *Config) applyEnv() (func() error, error) {
|
|
|
|
lookups := []envVarLookup{}
|
|
|
|
|
|
|
|
unset := func() error {
|
|
|
|
for _, lookup := range lookups {
|
|
|
|
if !lookup.ok {
|
|
|
|
if err := os.Unsetenv(lookup.key); err != nil {
|
2024-02-16 15:35:42 +01:00
|
|
|
return errwrap.Wrap(err, fmt.Sprintf("error unsetting env var %s", lookup.key))
|
2024-02-13 19:21:57 +01:00
|
|
|
}
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
if err := os.Setenv(lookup.key, lookup.value); err != nil {
|
2024-02-16 15:35:42 +01:00
|
|
|
return errwrap.Wrap(err, fmt.Sprintf("error setting back env var %s", lookup.key))
|
2024-02-13 19:21:57 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
for key, value := range c.additionalEnvVars {
|
|
|
|
current, ok := os.LookupEnv(key)
|
|
|
|
lookups = append(lookups, envVarLookup{ok: ok, key: key, value: current})
|
|
|
|
if err := os.Setenv(key, value); err != nil {
|
2024-02-16 15:35:42 +01:00
|
|
|
return unset, errwrap.Wrap(err, "error setting env var")
|
2024-02-13 19:21:57 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return unset, nil
|
|
|
|
}
|