| 43 | } |
| 44 | |
| 45 | func (a *Application) BackupToFile(ctx context.Context, dir string, name string) error { |
| 46 | if dir == "" { |
| 47 | return fmt.Errorf("backup location is required") |
| 48 | } |
| 49 | if !filepath.IsAbs(dir) { |
| 50 | return ErrBackupPathRelative |
| 51 | } |
| 52 | |
| 53 | filePath := filepath.Join(dir, name) |
| 54 | file, err := fsutil.CreateFile(filePath) |
| 55 | if err != nil { |
| 56 | slog.Error("Failed to create backup file", "app", a.Settings.Name, "filename", filePath, "error", err) |
| 57 | return fmt.Errorf("creating backup file: %w", err) |
| 58 | } |
| 59 | defer file.Close() |
| 60 | |
| 61 | err = a.backupToWriter(ctx, file) |
| 62 | a.saveOperationResult(ctx, func(s *State) { s.RecordBackup(a.Settings.Name, err) }) |
| 63 | |
| 64 | if err != nil { |
| 65 | if !errors.Is(err, ErrUnpauseFailed) { |
| 66 | os.Remove(filePath) |
| 67 | } |
| 68 | slog.Error("Backup failed", "app", a.Settings.Name, "filename", filePath, "error", err) |
| 69 | return err |
| 70 | } |
| 71 | |
| 72 | slog.Info("Created backup file", "app", a.Settings.Name, "filename", filePath) |
| 73 | |
| 74 | return nil |
| 75 | } |
| 76 | |
| 77 | func (a *Application) TrimBackups() error { |
| 78 | if a.Settings.Backup.Path == "" { |