2022-03-26 05:44:04 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"errors"
|
|
|
|
"fmt"
|
|
|
|
"io/fs"
|
|
|
|
"log"
|
|
|
|
"os"
|
|
|
|
"strings"
|
|
|
|
)
|
|
|
|
|
|
|
|
type TaskConfig struct {
|
|
|
|
BackupPaths []string
|
|
|
|
Env map[string]string
|
|
|
|
Logger *log.Logger
|
|
|
|
Restic *Restic
|
|
|
|
}
|
|
|
|
|
|
|
|
// ExecutableTask is a task to be run before or after backup/retore.
|
|
|
|
type ExecutableTask interface {
|
|
|
|
RunBackup(cfg TaskConfig) error
|
|
|
|
RunRestore(cfg TaskConfig) error
|
|
|
|
Name() string
|
|
|
|
}
|
|
|
|
|
|
|
|
// JobTaskScript is a sript to be executed as part of a job task.
|
|
|
|
type JobTaskScript struct {
|
|
|
|
OnBackup string `hcl:"on_backup,optional"`
|
|
|
|
OnRestore string `hcl:"on_restore,optional"`
|
|
|
|
Cwd string `hcl:"cwd,optional"`
|
|
|
|
env map[string]string
|
|
|
|
name string
|
|
|
|
}
|
|
|
|
|
|
|
|
func (t JobTaskScript) run(script string, cfg TaskConfig) error {
|
|
|
|
if script == "" {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
env := MergeEnvMap(cfg.Env, t.env)
|
|
|
|
if env == nil {
|
|
|
|
env = map[string]string{}
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := RunShell(script, t.Cwd, env, cfg.Logger); err != nil {
|
|
|
|
return fmt.Errorf("failed running task script %s: %w", t.Name(), err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// RunBackup runs script on backup.
|
|
|
|
func (t JobTaskScript) RunBackup(cfg TaskConfig) error {
|
|
|
|
return t.run(t.OnBackup, cfg)
|
|
|
|
}
|
|
|
|
|
|
|
|
// RunRestore script on restore.
|
|
|
|
func (t JobTaskScript) RunRestore(cfg TaskConfig) error {
|
|
|
|
return t.run(t.OnRestore, cfg)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (t JobTaskScript) Name() string {
|
|
|
|
return t.name
|
|
|
|
}
|
|
|
|
|
|
|
|
func (t *JobTaskScript) SetName(name string) {
|
|
|
|
t.name = name
|
|
|
|
}
|
|
|
|
|
|
|
|
// JobTaskMySQL is a sqlite backup task that performs required pre and post tasks.
|
|
|
|
type JobTaskMySQL struct {
|
2022-04-15 17:25:29 +00:00
|
|
|
Port int `hcl:"port,optional"`
|
|
|
|
Name string `hcl:"name,label"`
|
|
|
|
Hostname string `hcl:"hostname,optional"`
|
|
|
|
Database string `hcl:"database,optional"`
|
|
|
|
Username string `hcl:"username,optional"`
|
|
|
|
Password string `hcl:"password,optional"`
|
|
|
|
Tables []string `hcl:"tables,optional"`
|
|
|
|
NoTablespaces bool `hcl:"no_tablespaces,optional"`
|
|
|
|
DumpToPath string `hcl:"dump_to"`
|
2022-03-26 05:44:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (t JobTaskMySQL) Paths() []string {
|
|
|
|
return []string{t.DumpToPath}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (t JobTaskMySQL) Validate() error {
|
2022-03-29 05:01:18 +00:00
|
|
|
if t.DumpToPath == "" {
|
|
|
|
return fmt.Errorf("task %s is missing dump_to path: %w", t.Name, ErrMissingField)
|
|
|
|
}
|
|
|
|
|
2022-03-26 05:44:04 +00:00
|
|
|
if s, err := os.Stat(t.DumpToPath); err != nil {
|
|
|
|
if !errors.Is(err, fs.ErrNotExist) {
|
2022-03-29 05:01:18 +00:00
|
|
|
return fmt.Errorf("task %s: invalid dump_to: could not stat path: %v: %w", t.Name, err, ErrInvalidConfigValue)
|
2022-03-26 05:44:04 +00:00
|
|
|
}
|
|
|
|
} else if s.Mode().IsDir() {
|
2022-03-29 05:01:18 +00:00
|
|
|
return fmt.Errorf("task %s: dump_to cannot be a directory: %w", t.Name, ErrInvalidConfigValue)
|
2022-03-26 05:44:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if len(t.Tables) > 0 && t.Database == "" {
|
|
|
|
return fmt.Errorf(
|
2022-03-29 05:01:18 +00:00
|
|
|
"task %s is invalid. Must specify a database to use tables: %w",
|
2022-03-26 05:44:04 +00:00
|
|
|
t.Name,
|
|
|
|
ErrMissingField,
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (t JobTaskMySQL) GetPreTask() ExecutableTask {
|
|
|
|
command := []string{"mysqldump", "--result-file", t.DumpToPath}
|
|
|
|
|
|
|
|
if t.Hostname != "" {
|
|
|
|
command = append(command, "--host", t.Hostname)
|
|
|
|
}
|
|
|
|
|
2022-04-14 16:14:16 +00:00
|
|
|
if t.Port != 0 {
|
|
|
|
command = append(command, "--port", fmt.Sprintf("%d", t.Port))
|
|
|
|
}
|
|
|
|
|
2022-03-26 05:44:04 +00:00
|
|
|
if t.Username != "" {
|
|
|
|
command = append(command, "--user", t.Username)
|
|
|
|
}
|
|
|
|
|
|
|
|
if t.Password != "" {
|
2022-04-15 17:52:26 +00:00
|
|
|
command = append(command, fmt.Sprintf("--password=%s", t.Password))
|
2022-03-26 05:44:04 +00:00
|
|
|
}
|
|
|
|
|
2022-04-15 17:25:29 +00:00
|
|
|
if t.NoTablespaces {
|
|
|
|
command = append(command, "--no-tablespaces")
|
|
|
|
}
|
|
|
|
|
2022-03-26 05:44:04 +00:00
|
|
|
if t.Database != "" {
|
|
|
|
command = append(command, t.Database)
|
2022-04-15 17:25:29 +00:00
|
|
|
} else {
|
|
|
|
command = append(command, "--all-databases")
|
2022-03-26 05:44:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
command = append(command, t.Tables...)
|
|
|
|
|
|
|
|
return JobTaskScript{
|
|
|
|
name: t.Name,
|
|
|
|
env: nil,
|
|
|
|
Cwd: ".",
|
|
|
|
OnBackup: strings.Join(command, " "),
|
|
|
|
OnRestore: "",
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (t JobTaskMySQL) GetPostTask() ExecutableTask {
|
|
|
|
command := []string{"mysql"}
|
|
|
|
|
|
|
|
if t.Hostname != "" {
|
|
|
|
command = append(command, "--host", t.Hostname)
|
|
|
|
}
|
|
|
|
|
|
|
|
if t.Username != "" {
|
|
|
|
command = append(command, "--user", t.Username)
|
|
|
|
}
|
|
|
|
|
|
|
|
if t.Password != "" {
|
|
|
|
command = append(command, "--password", t.Password)
|
|
|
|
}
|
|
|
|
|
|
|
|
command = append(command, "<", t.DumpToPath)
|
|
|
|
|
|
|
|
return JobTaskScript{
|
|
|
|
name: t.Name,
|
|
|
|
env: nil,
|
|
|
|
Cwd: ".",
|
|
|
|
OnBackup: "",
|
|
|
|
OnRestore: strings.Join(command, " "),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// JobTaskSqlite is a sqlite backup task that performs required pre and post tasks.
|
|
|
|
type JobTaskSqlite struct {
|
|
|
|
Name string `hcl:"name,label"`
|
|
|
|
Path string `hcl:"path"`
|
|
|
|
DumpToPath string `hcl:"dump_to"`
|
|
|
|
}
|
|
|
|
|
|
|
|
func (t JobTaskSqlite) Paths() []string {
|
|
|
|
return []string{t.DumpToPath}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (t JobTaskSqlite) Validate() error {
|
2022-03-29 05:01:18 +00:00
|
|
|
if t.DumpToPath == "" {
|
|
|
|
return fmt.Errorf("task %s is missing dump_to path: %w", t.Name, ErrMissingField)
|
|
|
|
}
|
|
|
|
|
2022-03-26 05:44:04 +00:00
|
|
|
if s, err := os.Stat(t.DumpToPath); err != nil {
|
|
|
|
if !errors.Is(err, fs.ErrNotExist) {
|
2022-03-29 05:01:18 +00:00
|
|
|
return fmt.Errorf("task %s: invalid dump_to: could not stat path: %v: %w", t.Name, err, ErrInvalidConfigValue)
|
2022-03-26 05:44:04 +00:00
|
|
|
}
|
|
|
|
} else if s.Mode().IsDir() {
|
2022-03-29 05:01:18 +00:00
|
|
|
return fmt.Errorf("task %s: dump_to cannot be a directory: %w", t.Name, ErrInvalidConfigValue)
|
2022-03-26 05:44:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (t JobTaskSqlite) GetPreTask() ExecutableTask {
|
|
|
|
return JobTaskScript{
|
|
|
|
name: t.Name,
|
|
|
|
env: nil,
|
|
|
|
Cwd: ".",
|
|
|
|
OnBackup: fmt.Sprintf("sqlite3 '%s' '.backup %s'", t.Path, t.DumpToPath),
|
|
|
|
OnRestore: "",
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (t JobTaskSqlite) GetPostTask() ExecutableTask {
|
|
|
|
return JobTaskScript{
|
|
|
|
name: t.Name,
|
|
|
|
env: nil,
|
|
|
|
Cwd: ".",
|
|
|
|
OnBackup: "",
|
|
|
|
OnRestore: fmt.Sprintf("cp '%s' '%s'", t.DumpToPath, t.Path),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
type BackupFilesTask struct {
|
2022-03-29 05:53:49 +00:00
|
|
|
Paths []string `hcl:"paths"`
|
2022-03-26 05:44:04 +00:00
|
|
|
BackupOpts *BackupOpts `hcl:"backup_opts,block"`
|
|
|
|
RestoreOpts *RestoreOpts `hcl:"restore_opts,block"`
|
|
|
|
name string
|
|
|
|
}
|
|
|
|
|
|
|
|
func (t BackupFilesTask) RunBackup(cfg TaskConfig) error {
|
|
|
|
if t.BackupOpts == nil {
|
2022-11-10 21:30:30 +00:00
|
|
|
t.BackupOpts = &BackupOpts{} //nolint:exhaustruct
|
2022-03-26 05:44:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if err := cfg.Restic.Backup(cfg.BackupPaths, *t.BackupOpts); err != nil {
|
2022-03-29 05:53:49 +00:00
|
|
|
err = fmt.Errorf("failed backing up paths: %w", err)
|
2022-04-13 05:28:28 +00:00
|
|
|
cfg.Logger.Print(err)
|
2022-03-26 05:44:04 +00:00
|
|
|
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (t BackupFilesTask) RunRestore(cfg TaskConfig) error {
|
|
|
|
if t.RestoreOpts == nil {
|
2022-11-10 21:30:30 +00:00
|
|
|
t.RestoreOpts = &RestoreOpts{} //nolint:exhaustruct
|
2022-03-26 05:44:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// TODO: Make the snapshot configurable
|
|
|
|
if err := cfg.Restic.Restore("latest", *t.RestoreOpts); err != nil {
|
2022-03-29 05:53:49 +00:00
|
|
|
err = fmt.Errorf("failed restoring paths: %w", err)
|
2022-04-13 05:28:28 +00:00
|
|
|
cfg.Logger.Print(err)
|
2022-03-26 05:44:04 +00:00
|
|
|
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (t BackupFilesTask) Name() string {
|
|
|
|
return t.name
|
|
|
|
}
|
|
|
|
|
|
|
|
func (t *BackupFilesTask) SetName(name string) {
|
|
|
|
t.name = name
|
|
|
|
}
|
|
|
|
|
2022-03-29 05:01:18 +00:00
|
|
|
func (t *BackupFilesTask) Validate() error {
|
|
|
|
if len(t.Paths) == 0 {
|
|
|
|
return fmt.Errorf("backup config doesn't include any paths: %w", ErrInvalidConfigValue)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2022-03-26 05:44:04 +00:00
|
|
|
// JobTask represents a single task within a backup job.
|
|
|
|
type JobTask struct {
|
|
|
|
Name string `hcl:"name,label"`
|
|
|
|
PreScripts []JobTaskScript `hcl:"pre_script,block"`
|
|
|
|
PostScripts []JobTaskScript `hcl:"post_script,block"`
|
2022-11-03 23:10:33 +00:00
|
|
|
MySQL []JobTaskMySQL `hcl:"mysql,block"`
|
|
|
|
Sqlite []JobTaskSqlite `hcl:"sqlite,block"`
|
2022-03-26 05:44:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (t JobTask) Validate() error {
|
2022-03-29 05:01:18 +00:00
|
|
|
if t.Name == "" {
|
|
|
|
return fmt.Errorf("task is missing a name: %w", ErrMissingField)
|
|
|
|
}
|
|
|
|
|
2022-03-26 05:44:04 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (t JobTask) GetPreTasks() []ExecutableTask {
|
|
|
|
allTasks := []ExecutableTask{}
|
|
|
|
|
2022-11-03 23:10:33 +00:00
|
|
|
for _, task := range t.MySQL {
|
|
|
|
allTasks = append(allTasks, task.GetPreTask())
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, task := range t.Sqlite {
|
|
|
|
allTasks = append(allTasks, task.GetPreTask())
|
|
|
|
}
|
|
|
|
|
2022-03-26 05:44:04 +00:00
|
|
|
for _, exTask := range t.PreScripts {
|
|
|
|
exTask.SetName(t.Name)
|
|
|
|
allTasks = append(allTasks, exTask)
|
|
|
|
}
|
|
|
|
|
|
|
|
return allTasks
|
|
|
|
}
|
|
|
|
|
|
|
|
func (t JobTask) GetPostTasks() []ExecutableTask {
|
|
|
|
allTasks := []ExecutableTask{}
|
|
|
|
|
|
|
|
for _, exTask := range t.PostScripts {
|
|
|
|
exTask.SetName(t.Name)
|
|
|
|
allTasks = append(allTasks, exTask)
|
|
|
|
}
|
|
|
|
|
2022-11-03 23:10:33 +00:00
|
|
|
for _, task := range t.MySQL {
|
|
|
|
allTasks = append(allTasks, task.GetPostTask())
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, task := range t.Sqlite {
|
|
|
|
allTasks = append(allTasks, task.GetPostTask())
|
|
|
|
}
|
|
|
|
|
2022-03-26 05:44:04 +00:00
|
|
|
return allTasks
|
|
|
|
}
|