feat: configuration backup management with manual and auto backup
Add backup/restore functionality for the SQLite database. Users can trigger manual backups, configure automatic backups on an interval with retention policies, list/download/delete backups, and restore from any backup. - Backup engine using VACUUM INTO (safe with WAL mode) - Backup metadata tracked in DB, files stored in DATA_DIR/backups/ - Settings: backup_enabled, backup_interval_hours, backup_retention_count - API: POST/GET/DELETE /api/backups, download, restore endpoints - Autobackup via cron scheduler with configurable interval - Retention: prune on startup, after each backup (manual and auto) - Orphan cleanup: removes backup files without metadata on startup - Restore: replaces DB and triggers graceful server shutdown - Settings UI: /settings/backup with toggle, interval, retention config - Backup list with download, delete, restore actions - i18n: English and Russian translations
This commit is contained in:
@@ -0,0 +1,106 @@
|
||||
package store
|
||||
|
||||
import (
|
||||
"database/sql"
|
||||
"errors"
|
||||
"fmt"
|
||||
|
||||
"github.com/google/uuid"
|
||||
)
|
||||
|
||||
// CreateBackup inserts a new backup metadata record.
|
||||
func (s *Store) CreateBackup(b Backup) (Backup, error) {
|
||||
if b.ID == "" {
|
||||
b.ID = uuid.New().String()
|
||||
}
|
||||
b.CreatedAt = Now()
|
||||
|
||||
_, err := s.db.Exec(
|
||||
`INSERT INTO backups (id, filename, size_bytes, backup_type, created_at)
|
||||
VALUES (?, ?, ?, ?, ?)`,
|
||||
b.ID, b.Filename, b.SizeBytes, b.BackupType, b.CreatedAt,
|
||||
)
|
||||
if err != nil {
|
||||
return Backup{}, fmt.Errorf("insert backup: %w", err)
|
||||
}
|
||||
return b, nil
|
||||
}
|
||||
|
||||
// GetBackup returns a backup by ID.
|
||||
func (s *Store) GetBackup(id string) (Backup, error) {
|
||||
var b Backup
|
||||
err := s.db.QueryRow(
|
||||
`SELECT id, filename, size_bytes, backup_type, created_at
|
||||
FROM backups WHERE id = ?`, id,
|
||||
).Scan(&b.ID, &b.Filename, &b.SizeBytes, &b.BackupType, &b.CreatedAt)
|
||||
if errors.Is(err, sql.ErrNoRows) {
|
||||
return Backup{}, fmt.Errorf("backup %s: %w", id, ErrNotFound)
|
||||
}
|
||||
if err != nil {
|
||||
return Backup{}, fmt.Errorf("query backup: %w", err)
|
||||
}
|
||||
return b, nil
|
||||
}
|
||||
|
||||
// ListBackups returns all backups ordered by creation date descending.
|
||||
func (s *Store) ListBackups() ([]Backup, error) {
|
||||
rows, err := s.db.Query(
|
||||
`SELECT id, filename, size_bytes, backup_type, created_at
|
||||
FROM backups ORDER BY created_at DESC`,
|
||||
)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("query backups: %w", err)
|
||||
}
|
||||
defer rows.Close()
|
||||
|
||||
var backups []Backup
|
||||
for rows.Next() {
|
||||
var b Backup
|
||||
if err := rows.Scan(&b.ID, &b.Filename, &b.SizeBytes, &b.BackupType, &b.CreatedAt); err != nil {
|
||||
return nil, fmt.Errorf("scan backup: %w", err)
|
||||
}
|
||||
backups = append(backups, b)
|
||||
}
|
||||
return backups, rows.Err()
|
||||
}
|
||||
|
||||
// DeleteBackup removes a backup metadata record by ID.
|
||||
func (s *Store) DeleteBackup(id string) error {
|
||||
_, err := s.db.Exec(`DELETE FROM backups WHERE id = ?`, id)
|
||||
if err != nil {
|
||||
return fmt.Errorf("delete backup: %w", err)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// CountBackups returns the total number of backup records.
|
||||
func (s *Store) CountBackups() (int, error) {
|
||||
var count int
|
||||
err := s.db.QueryRow(`SELECT COUNT(*) FROM backups`).Scan(&count)
|
||||
if err != nil {
|
||||
return 0, fmt.Errorf("count backups: %w", err)
|
||||
}
|
||||
return count, nil
|
||||
}
|
||||
|
||||
// GetOldestBackups returns the N oldest backups (for pruning).
|
||||
func (s *Store) GetOldestBackups(limit int) ([]Backup, error) {
|
||||
rows, err := s.db.Query(
|
||||
`SELECT id, filename, size_bytes, backup_type, created_at
|
||||
FROM backups ORDER BY created_at ASC LIMIT ?`, limit,
|
||||
)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("query oldest backups: %w", err)
|
||||
}
|
||||
defer rows.Close()
|
||||
|
||||
var backups []Backup
|
||||
for rows.Next() {
|
||||
var b Backup
|
||||
if err := rows.Scan(&b.ID, &b.Filename, &b.SizeBytes, &b.BackupType, &b.CreatedAt); err != nil {
|
||||
return nil, fmt.Errorf("scan backup: %w", err)
|
||||
}
|
||||
backups = append(backups, b)
|
||||
}
|
||||
return backups, rows.Err()
|
||||
}
|
||||
Reference in New Issue
Block a user