Add ability to restore specific snapshots
Some checks failed
continuous-integration/drone/push Build is failing

This commit is contained in:
IamTheFij 2023-04-25 14:02:28 -07:00
parent 8a8bc23376
commit 95fea5ef30
3 changed files with 18 additions and 8 deletions

6
job.go
View File

@ -203,7 +203,7 @@ func (j Job) Logger() *log.Logger {
return GetLogger(j.Name) return GetLogger(j.Name)
} }
func (j Job) RunRestore() error { func (j Job) RunRestore(snapshot string) error {
logger := j.Logger() logger := j.Logger()
restic := j.NewRestic() restic := j.NewRestic()
@ -219,6 +219,10 @@ func (j Job) RunRestore() error {
Env: nil, Env: nil,
} }
if backupTask, ok := exTask.(BackupFilesTask); ok {
backupTask.snapshot = snapshot
}
if err := exTask.RunRestore(taskCfg); err != nil { if err := exTask.RunRestore(taskCfg); err != nil {
return fmt.Errorf("failed running job %s: %w", j.Name, err) return fmt.Errorf("failed running job %s: %w", j.Name, err)
} }

12
main.go
View File

@ -160,7 +160,7 @@ func runBackupJobs(jobs []Job, names string) error {
return filterJobErr return filterJobErr
} }
func runRestoreJobs(jobs []Job, names string) error { func runRestoreJobs(jobs []Job, names string, snapshot string) error {
if names == "" { if names == "" {
return nil return nil
} }
@ -173,7 +173,7 @@ func runRestoreJobs(jobs []Job, names string) error {
jobs, filterJobErr := FilterJobs(jobs, namesSlice) jobs, filterJobErr := FilterJobs(jobs, namesSlice)
for _, job := range jobs { for _, job := range jobs {
if err := job.RunRestore(); err != nil { if err := job.RunRestore(snapshot); err != nil {
return err return err
} }
} }
@ -185,6 +185,7 @@ type Flags struct {
showVersion bool showVersion bool
backup string backup string
restore string restore string
restoreSnapshot string
once bool once bool
healthCheckAddr string healthCheckAddr string
metricsPushGateway string metricsPushGateway string
@ -199,19 +200,20 @@ func readFlags() Flags {
flag.StringVar(&flags.healthCheckAddr, "addr", "0.0.0.0:8080", "address to bind health check API") flag.StringVar(&flags.healthCheckAddr, "addr", "0.0.0.0:8080", "address to bind health check API")
flag.StringVar(&flags.metricsPushGateway, "push-gateway", "", "url of push gateway service for batch runs (optional)") flag.StringVar(&flags.metricsPushGateway, "push-gateway", "", "url of push gateway service for batch runs (optional)")
flag.StringVar(&JobBaseDir, "base-dir", JobBaseDir, "Base dir to create intermediate job files like SQL dumps.") flag.StringVar(&JobBaseDir, "base-dir", JobBaseDir, "Base dir to create intermediate job files like SQL dumps.")
flag.StringVar(&flags.restoreSnapshot, "snapshot", "latest", "the snapshot to restore")
flag.Parse() flag.Parse()
return flags return flags
} }
func runSpecifiedJobs(jobs []Job, backupJobs, restoreJobs string) error { func runSpecifiedJobs(jobs []Job, backupJobs, restoreJobs, snapshot string) error {
// Run specified backup jobs // Run specified backup jobs
if err := runBackupJobs(jobs, backupJobs); err != nil { if err := runBackupJobs(jobs, backupJobs); err != nil {
return fmt.Errorf("Failed running backup jobs: %w", err) return fmt.Errorf("Failed running backup jobs: %w", err)
} }
// Run specified restore jobs // Run specified restore jobs
if err := runRestoreJobs(jobs, restoreJobs); err != nil { if err := runRestoreJobs(jobs, restoreJobs, snapshot); err != nil {
return fmt.Errorf("Failed running restore jobs: %w", err) return fmt.Errorf("Failed running restore jobs: %w", err)
} }
@ -251,7 +253,7 @@ func main() {
log.Fatalf("Failed to read jobs from files: %v", err) log.Fatalf("Failed to read jobs from files: %v", err)
} }
if err := runSpecifiedJobs(jobs, flags.backup, flags.restore); err != nil { if err := runSpecifiedJobs(jobs, flags.backup, flags.restore, flags.restoreSnapshot); err != nil {
log.Fatal(err) log.Fatal(err)
} }

View File

@ -226,6 +226,7 @@ type BackupFilesTask struct {
BackupOpts *BackupOpts `hcl:"backup_opts,block"` BackupOpts *BackupOpts `hcl:"backup_opts,block"`
RestoreOpts *RestoreOpts `hcl:"restore_opts,block"` RestoreOpts *RestoreOpts `hcl:"restore_opts,block"`
name string name string
snapshot string
} }
func (t BackupFilesTask) RunBackup(cfg TaskConfig) error { func (t BackupFilesTask) RunBackup(cfg TaskConfig) error {
@ -248,8 +249,11 @@ func (t BackupFilesTask) RunRestore(cfg TaskConfig) error {
t.RestoreOpts = &RestoreOpts{} //nolint:exhaustruct t.RestoreOpts = &RestoreOpts{} //nolint:exhaustruct
} }
// TODO: Make the snapshot configurable if t.snapshot == "" {
if err := cfg.Restic.Restore("latest", *t.RestoreOpts); err != nil { t.snapshot = "latest"
}
if err := cfg.Restic.Restore(t.snapshot, *t.RestoreOpts); err != nil {
err = fmt.Errorf("failed restoring paths: %w", err) err = fmt.Errorf("failed restoring paths: %w", err)
cfg.Logger.Print(err) cfg.Logger.Print(err)