1
0
Fork 0
mirror of https://github.com/Luzifer/duplicity-backup.git synced 2024-11-14 01:42:52 +00:00
duplicity-backup/configfile.go

426 lines
12 KiB
Go
Raw Permalink Normal View History

2016-05-22 13:04:37 +00:00
package main
import (
2016-06-25 12:11:51 +00:00
"bytes"
2016-05-22 13:04:37 +00:00
"fmt"
"io"
"os"
2016-06-25 23:33:28 +00:00
"regexp"
2016-05-22 13:04:37 +00:00
"strconv"
2016-06-25 12:11:51 +00:00
"text/template"
2016-05-22 13:04:37 +00:00
valid "github.com/asaskevich/govalidator"
"github.com/pkg/errors"
2016-05-22 13:04:37 +00:00
"gopkg.in/yaml.v2"
)
const (
2016-06-25 23:33:28 +00:00
commandBackup = "backup"
commandFullBackup = "full"
commandIncrBackup = "incr"
commandCleanup = "cleanup"
commandList = "list-current-files"
commandRestore = "restore"
commandStatus = "status"
commandVerify = "verify"
commandRemove = "__remove_old"
commandListChangedFiles = "list-changed-files"
)
var (
notifyCommands = []string{
commandBackup,
commandRemove,
commandFullBackup,
commandIncrBackup,
}
removeCommands = []string{
commandBackup,
commandCleanup,
}
)
2016-05-22 13:04:37 +00:00
type configFile struct {
RootPath string `yaml:"root" valid:"required"`
Hostname string `yaml:"hostname"`
Destination string `yaml:"dest" valid:"required"`
FTPPassword string `yaml:"ftp_password"`
AWS struct {
AccessKeyID string `yaml:"access_key_id"`
SecretAccessKey string `yaml:"secret_access_key"`
StorageClass string `yaml:"storage_class"`
} `yaml:"aws"`
GoogleCloud struct {
AccessKeyID string `yaml:"access_key_id"`
SecretAccessKey string `yaml:"secret_access_key"`
} `yaml:"google_cloud"`
Swift struct {
Username string `yaml:"username"`
Password string `yaml:"password"`
AuthURL string `yaml:"auth_url"`
AuthVersion int `yaml:"auth_version"`
} `yaml:"swift"`
Include []string `yaml:"inclist"`
Exclude []string `yaml:"exclist"`
IncExcFile string `yaml:"incexcfile" valid:"customFileExistsValidator"`
2016-05-22 13:04:37 +00:00
ExcludeDeviceFiles bool `yaml:"excdevicefiles"`
Encryption struct {
Enable bool `yaml:"enable"`
Passphrase string `yaml:"passphrase"`
GPGEncryptionKey string `yaml:"gpg_encryption_key"`
GPGSignKey string `yaml:"gpg_sign_key"`
HideKeyID bool `yaml:"hide_key_id"`
SecretKeyRing string `yaml:"secret_keyring"`
} `yaml:"encryption"`
StaticBackupOptions []string `yaml:"static_options"`
Cleanup struct {
Type string `yaml:"type"`
Value string `yaml:"value"`
} `yaml:"cleanup"`
LogDirectory string `yaml:"logdir" valid:"required"`
Notifications struct {
Slack struct {
HookURL string `yaml:"hook_url"`
Channel string `yaml:"channel"`
Username string `yaml:"username"`
Emoji string `yaml:"emoji"`
} `yaml:"slack"`
MonDash struct {
2016-05-23 08:41:20 +00:00
BoardURL string `yaml:"board"`
Token string `yaml:"token"`
Freshness int64 `yaml:"freshness"`
2016-05-22 13:04:37 +00:00
} `yaml:"mondash"`
} `yaml:"notifications"`
}
2016-05-22 14:05:06 +00:00
func init() {
valid.CustomTypeTagMap.Set("customFileExistsValidator", valid.CustomTypeValidator(func(i interface{}, context interface{}) bool {
if v, ok := i.(string); ok {
2016-05-22 14:05:06 +00:00
_, err := os.Stat(v)
return v == "" || err == nil
}
2016-05-22 14:05:06 +00:00
return false
}))
}
2016-05-22 13:04:37 +00:00
func (c *configFile) validate() error {
result, err := valid.ValidateStruct(c)
if !result || err != nil {
return errors.Wrap(err, "validating config")
2016-05-22 13:04:37 +00:00
}
if c.Encryption.Enable && c.Encryption.GPGSignKey != "" && c.Encryption.Passphrase == "" {
return errors.New("With gpg_sign_key passphrase is required")
}
if c.Encryption.Enable && c.Encryption.GPGEncryptionKey == "" && c.Encryption.Passphrase == "" {
return errors.New("Encryption is enabled but no encryption key or passphrase is specified")
}
if c.Destination[0:2] == "s3" && (c.AWS.AccessKeyID == "" || c.AWS.SecretAccessKey == "") {
return errors.New("Destination is S3 but AWS credentials are not configured")
}
if c.Destination[0:2] == "gs" && (c.GoogleCloud.AccessKeyID == "" || c.GoogleCloud.SecretAccessKey == "") {
return errors.New("Destination is S3 but AWS credentials are not configured")
}
return nil
}
2016-06-25 12:11:51 +00:00
func getTemplateFuncMap() template.FuncMap {
return template.FuncMap{
"env": func(name string, v ...string) string {
defaultValue := ""
if len(v) > 0 {
defaultValue = v[0]
}
if value, ok := os.LookupEnv(name); ok {
return value
}
return defaultValue
},
}
}
2016-05-22 13:04:37 +00:00
func loadConfigFile(in io.Reader) (*configFile, error) {
fileContent, err := io.ReadAll(in)
2016-05-22 13:04:37 +00:00
if err != nil {
return nil, errors.Wrap(err, "reading config file content")
2016-05-22 13:04:37 +00:00
}
2016-06-25 12:11:51 +00:00
buf := bytes.NewBuffer([]byte{})
tpl, err := template.New("config file").Funcs(getTemplateFuncMap()).Parse(string(fileContent))
if err != nil {
return nil, errors.Wrap(err, "parsing config file as template")
2016-06-25 12:11:51 +00:00
}
if err := tpl.Execute(buf, nil); err != nil {
return nil, errors.Wrap(err, "rendering config file template")
2016-06-25 12:11:51 +00:00
}
hostname, _ := os.Hostname() // #nosec G104
2016-05-22 14:20:36 +00:00
res := &configFile{
Hostname: hostname,
}
2016-06-25 12:11:51 +00:00
if err := yaml.Unmarshal(buf.Bytes(), res); err != nil {
return nil, errors.Wrap(err, "unmarshalling config")
2016-05-22 13:04:37 +00:00
}
return res, res.validate()
}
//nolint:funlen // Is just a list of parameter groups
2016-06-25 23:33:28 +00:00
func (c *configFile) GenerateCommand(argv []string, time string) (commandLine []string, env []string, logfilter *regexp.Regexp, err error) {
2016-05-22 13:04:37 +00:00
var (
tmpEnv []string
2016-05-22 13:04:37 +00:00
option, root, dest string
addTime bool
command = argv[0]
)
switch command {
case commandBackup:
option = "inc"
2016-05-22 13:04:37 +00:00
root = c.RootPath
dest = c.Destination
commandLine, env, err = c.generateFullCommand(option, time, root, dest, addTime, "")
2016-06-25 23:33:28 +00:00
case commandListChangedFiles:
option = "inc"
2016-06-25 23:33:28 +00:00
root = c.RootPath
dest = c.Destination
commandLine, env, err = c.generateFullCommand(option, time, root, dest, addTime, "")
commandLine = append([]string{"--dry-run", "--verbosity", "8"}, commandLine...)
logfilter = regexp.MustCompile(`^[ADM] `)
2016-05-23 19:11:02 +00:00
case commandFullBackup:
option = command
root = c.RootPath
dest = c.Destination
commandLine, env, err = c.generateFullCommand(option, time, root, dest, addTime, "")
2016-05-23 19:11:02 +00:00
case commandIncrBackup:
option = command
root = c.RootPath
dest = c.Destination
commandLine, env, err = c.generateFullCommand(option, time, root, dest, addTime, "")
case commandCleanup:
2016-05-22 14:05:06 +00:00
option = command
2016-05-22 13:04:37 +00:00
commandLine, env, err = c.generateLiteCommand(option, time, addTime)
case commandList:
2016-05-22 14:05:06 +00:00
option = command
2016-05-22 13:04:37 +00:00
commandLine, env, err = c.generateLiteCommand(option, time, addTime)
case commandRestore:
2016-05-22 13:04:37 +00:00
addTime = true
2016-05-22 14:05:06 +00:00
option = command
2016-05-22 13:04:37 +00:00
root = c.Destination
restoreFile := ""
switch len(argv) {
case 3: //nolint:gomnd // Simple count of arguments
2016-05-22 13:04:37 +00:00
restoreFile = argv[1]
dest = argv[2]
case 2: //nolint:gomnd // Simple count of arguments
2016-05-22 13:04:37 +00:00
dest = argv[1]
default:
err = errors.New("You need to specify one or more parameters: See help message")
return commandLine, env, logfilter, err
2016-05-22 13:04:37 +00:00
}
commandLine, env, err = c.generateFullCommand(option, time, root, dest, addTime, restoreFile)
case commandStatus:
2016-05-22 13:04:37 +00:00
option = "collection-status"
commandLine, env, err = c.generateLiteCommand(option, time, addTime)
case commandVerify:
2016-05-22 14:05:06 +00:00
option = command
2016-05-22 13:04:37 +00:00
root = c.Destination
dest = c.RootPath
commandLine, env, err = c.generateFullCommand(option, time, root, dest, addTime, "")
case commandRemove:
2016-05-22 13:04:37 +00:00
commandLine, env, err = c.generateRemoveCommand()
2016-05-22 13:04:37 +00:00
default:
err = fmt.Errorf("did not understand command '%s', please see 'help' for details what to do", command)
return commandLine, env, logfilter, err
2016-05-22 13:04:37 +00:00
}
// Add destination credentials
tmpEnv = c.generateCredentialExport()
env = append(env, tmpEnv...)
commandLine = c.cleanSlice(commandLine)
env = c.cleanSlice(env)
2016-05-22 13:04:37 +00:00
return commandLine, env, logfilter, err
}
2016-05-22 13:04:37 +00:00
func (*configFile) cleanSlice(in []string) []string {
out := []string{}
for _, i := range in {
2016-05-22 13:04:37 +00:00
if i != "" {
out = append(out, i)
2016-05-22 13:04:37 +00:00
}
}
return out
2016-05-22 13:04:37 +00:00
}
func (c *configFile) generateCredentialExport() []string {
env := []string{}
2016-05-22 13:04:37 +00:00
if c.AWS.AccessKeyID != "" {
env = append(env, "AWS_ACCESS_KEY_ID="+c.AWS.AccessKeyID)
env = append(env, "AWS_SECRET_ACCESS_KEY="+c.AWS.SecretAccessKey)
}
if c.GoogleCloud.AccessKeyID != "" {
env = append(env, "GS_ACCESS_KEY_ID="+c.GoogleCloud.AccessKeyID)
env = append(env, "GS_SECRET_ACCESS_KEY="+c.GoogleCloud.SecretAccessKey)
}
if c.Swift.Username != "" {
env = append(env, "SWIFT_USERNAME="+c.Swift.Username)
env = append(env, "SWIFT_PASSWORD="+c.Swift.Password)
env = append(env, "SWIFT_AUTHURL="+c.Swift.AuthURL)
2016-05-22 14:11:57 +00:00
env = append(env, "SWIFT_AUTHVERSION="+strconv.Itoa(c.Swift.AuthVersion))
2016-05-22 13:04:37 +00:00
}
if c.FTPPassword != "" {
env = append(env, "FTP_PASSWORD="+c.FTPPassword)
}
return env
2016-05-22 13:04:37 +00:00
}
func (c *configFile) generateRemoveCommand() ([]string, []string, error) {
var commandLine, env, tmpArg, tmpEnv []string
2016-05-22 13:04:37 +00:00
// Assemble command
commandLine = append(commandLine, c.Cleanup.Type, c.Cleanup.Value)
// Static Options
commandLine = append(commandLine, c.StaticBackupOptions...)
// Encryption options
tmpArg, tmpEnv = c.generateEncryption(c.Cleanup.Type)
commandLine = append(commandLine, tmpArg...)
env = append(env, tmpEnv...)
// Enforce cleanup
commandLine = append(commandLine, "--force")
// Remote repo
commandLine = append(commandLine, c.Destination)
return commandLine, env, nil
2016-05-22 13:04:37 +00:00
}
//revive:disable-next-line:flag-parameter // Keeping for the sake of simplicity
func (c *configFile) generateLiteCommand(option, time string, addTime bool) ([]string, []string, error) {
var commandLine, env, tmpArg, tmpEnv []string
2016-05-22 13:04:37 +00:00
// Assemble command
commandLine = append(commandLine, option)
// Static Options
commandLine = append(commandLine, c.StaticBackupOptions...)
if addTime && time != "" {
commandLine = append(commandLine, "--time", time)
}
// Encryption options
tmpArg, tmpEnv = c.generateEncryption(option)
commandLine = append(commandLine, tmpArg...)
env = append(env, tmpEnv...)
// Remote repo
commandLine = append(commandLine, c.Destination)
return commandLine, env, nil
2016-05-22 13:04:37 +00:00
}
//revive:disable-next-line:flag-parameter // Keeping for the sake of simplicity
func (c *configFile) generateFullCommand(option, time, root, dest string, addTime bool, restoreFile string) ([]string, []string, error) {
var commandLine, env, tmpArg, tmpEnv []string
2016-05-22 13:04:37 +00:00
// Assemble command
commandLine = append(commandLine, option)
// Static Options
commandLine = append(commandLine, c.StaticBackupOptions...)
if addTime && time != "" {
commandLine = append(commandLine, "--time", time)
}
if restoreFile != "" {
commandLine = append(commandLine, "--file-to-restore", restoreFile)
}
// AWS Storage Class (empty if not used, will get stripped)
commandLine = append(commandLine, c.AWS.StorageClass)
// Encryption options
tmpArg, tmpEnv = c.generateEncryption(option)
commandLine = append(commandLine, tmpArg...)
env = append(env, tmpEnv...)
// Includes / Excludes
tmpArg, tmpEnv = c.generateIncludeExclude()
commandLine = append(commandLine, tmpArg...)
env = append(env, tmpEnv...)
// Source / Destination
commandLine = append(commandLine, root, dest)
return commandLine, env, nil
2016-05-22 13:04:37 +00:00
}
func (c *configFile) generateIncludeExclude() ([]string, []string) {
var arguments, env []string
2016-05-22 13:04:37 +00:00
if c.ExcludeDeviceFiles {
arguments = append(arguments, "--exclude-device-files")
}
for _, exc := range c.Exclude {
arguments = append(arguments, "--exclude="+exc)
}
for _, inc := range c.Include {
arguments = append(arguments, "--include="+inc)
}
if c.IncExcFile != "" {
arguments = append(arguments, "--include-globbing-filelist", c.IncExcFile)
}
if len(c.Include) > 0 || c.IncExcFile != "" {
arguments = append(arguments, "--exclude=**")
}
return arguments, env
2016-05-22 13:04:37 +00:00
}
func (c *configFile) generateEncryption(command string) ([]string, []string) {
var arguments, env []string
2016-05-22 13:04:37 +00:00
if !c.Encryption.Enable {
arguments = append(arguments, "--no-encryption")
return arguments, env
2016-05-22 13:04:37 +00:00
}
if c.Encryption.Passphrase != "" {
env = append(env, "PASSPHRASE="+c.Encryption.Passphrase)
}
if c.Encryption.GPGEncryptionKey != "" {
if c.Encryption.HideKeyID {
arguments = append(arguments, "--hidden-encrypt-key="+c.Encryption.GPGEncryptionKey)
} else {
arguments = append(arguments, "--encrypt-key="+c.Encryption.GPGEncryptionKey)
}
}
if c.Encryption.GPGSignKey != "" && command != "restore" {
arguments = append(arguments, "--sign-key="+c.Encryption.GPGSignKey)
}
if c.Encryption.GPGEncryptionKey != "" && c.Encryption.SecretKeyRing != "" {
arguments = append(arguments, "--encrypt-secret-keyring="+c.Encryption.SecretKeyRing)
}
return arguments, env
2016-05-22 13:04:37 +00:00
}