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()
|
||||
}
|
||||
@@ -58,11 +58,23 @@ type Settings struct {
|
||||
SSLCertificateID int `json:"ssl_certificate_id"`
|
||||
StaleThresholdDays int `json:"stale_threshold_days"`
|
||||
AllowedVolumePaths string `json:"allowed_volume_paths"` // JSON array of allowed absolute paths
|
||||
WildcardDNS bool `json:"wildcard_dns"`
|
||||
DNSProvider string `json:"dns_provider"`
|
||||
CloudflareAPIToken string `json:"cloudflare_api_token"`
|
||||
CloudflareZoneID string `json:"cloudflare_zone_id"`
|
||||
UpdatedAt string `json:"updated_at"`
|
||||
WildcardDNS bool `json:"wildcard_dns"`
|
||||
DNSProvider string `json:"dns_provider"`
|
||||
CloudflareAPIToken string `json:"cloudflare_api_token"`
|
||||
CloudflareZoneID string `json:"cloudflare_zone_id"`
|
||||
BackupEnabled bool `json:"backup_enabled"`
|
||||
BackupIntervalHours int `json:"backup_interval_hours"`
|
||||
BackupRetentionCount int `json:"backup_retention_count"`
|
||||
UpdatedAt string `json:"updated_at"`
|
||||
}
|
||||
|
||||
// Backup represents a backup metadata record.
|
||||
type Backup struct {
|
||||
ID string `json:"id"`
|
||||
Filename string `json:"filename"`
|
||||
SizeBytes int64 `json:"size_bytes"`
|
||||
BackupType string `json:"backup_type"` // "manual" or "auto"
|
||||
CreatedAt string `json:"created_at"`
|
||||
}
|
||||
|
||||
// DNSRecord tracks a DNS record managed by the application.
|
||||
|
||||
@@ -7,23 +7,28 @@ import (
|
||||
// GetSettings returns the global settings (single-row pattern, always row id=1).
|
||||
func (s *Store) GetSettings() (Settings, error) {
|
||||
var st Settings
|
||||
var wildcardDNS int
|
||||
var wildcardDNS, backupEnabled int
|
||||
err := s.db.QueryRow(
|
||||
`SELECT domain, server_ip, network, subdomain_pattern, notification_url,
|
||||
npm_url, npm_email, npm_password, webhook_secret, polling_interval,
|
||||
base_volume_path, ssl_certificate_id, stale_threshold_days,
|
||||
allowed_volume_paths, wildcard_dns, dns_provider,
|
||||
cloudflare_api_token, cloudflare_zone_id, updated_at
|
||||
cloudflare_api_token, cloudflare_zone_id,
|
||||
backup_enabled, backup_interval_hours, backup_retention_count,
|
||||
updated_at
|
||||
FROM settings WHERE id = 1`,
|
||||
).Scan(&st.Domain, &st.ServerIP, &st.Network, &st.SubdomainPattern, &st.NotificationURL,
|
||||
&st.NpmURL, &st.NpmEmail, &st.NpmPassword, &st.WebhookSecret, &st.PollingInterval,
|
||||
&st.BaseVolumePath, &st.SSLCertificateID, &st.StaleThresholdDays,
|
||||
&st.AllowedVolumePaths, &wildcardDNS, &st.DNSProvider,
|
||||
&st.CloudflareAPIToken, &st.CloudflareZoneID, &st.UpdatedAt)
|
||||
&st.CloudflareAPIToken, &st.CloudflareZoneID,
|
||||
&backupEnabled, &st.BackupIntervalHours, &st.BackupRetentionCount,
|
||||
&st.UpdatedAt)
|
||||
if err != nil {
|
||||
return Settings{}, fmt.Errorf("query settings: %w", err)
|
||||
}
|
||||
st.WildcardDNS = wildcardDNS != 0
|
||||
st.BackupEnabled = backupEnabled != 0
|
||||
return st, nil
|
||||
}
|
||||
|
||||
@@ -34,19 +39,27 @@ func (s *Store) UpdateSettings(st Settings) error {
|
||||
if st.WildcardDNS {
|
||||
wildcardDNS = 1
|
||||
}
|
||||
backupEnabled := 0
|
||||
if st.BackupEnabled {
|
||||
backupEnabled = 1
|
||||
}
|
||||
_, err := s.db.Exec(
|
||||
`UPDATE settings SET
|
||||
domain=?, server_ip=?, network=?, subdomain_pattern=?, notification_url=?,
|
||||
npm_url=?, npm_email=?, npm_password=?, webhook_secret=?, polling_interval=?,
|
||||
base_volume_path=?, ssl_certificate_id=?, stale_threshold_days=?,
|
||||
allowed_volume_paths=?, wildcard_dns=?, dns_provider=?,
|
||||
cloudflare_api_token=?, cloudflare_zone_id=?, updated_at=?
|
||||
cloudflare_api_token=?, cloudflare_zone_id=?,
|
||||
backup_enabled=?, backup_interval_hours=?, backup_retention_count=?,
|
||||
updated_at=?
|
||||
WHERE id = 1`,
|
||||
st.Domain, st.ServerIP, st.Network, st.SubdomainPattern, st.NotificationURL,
|
||||
st.NpmURL, st.NpmEmail, st.NpmPassword, st.WebhookSecret, st.PollingInterval,
|
||||
st.BaseVolumePath, st.SSLCertificateID, st.StaleThresholdDays,
|
||||
st.AllowedVolumePaths, wildcardDNS, st.DNSProvider,
|
||||
st.CloudflareAPIToken, st.CloudflareZoneID, st.UpdatedAt,
|
||||
st.CloudflareAPIToken, st.CloudflareZoneID,
|
||||
backupEnabled, st.BackupIntervalHours, st.BackupRetentionCount,
|
||||
st.UpdatedAt,
|
||||
)
|
||||
if err != nil {
|
||||
return fmt.Errorf("update settings: %w", err)
|
||||
|
||||
@@ -95,6 +95,10 @@ func (s *Store) runMigrations() error {
|
||||
`ALTER TABLE settings ADD COLUMN dns_provider TEXT NOT NULL DEFAULT ''`,
|
||||
`ALTER TABLE settings ADD COLUMN cloudflare_api_token TEXT NOT NULL DEFAULT ''`,
|
||||
`ALTER TABLE settings ADD COLUMN cloudflare_zone_id TEXT NOT NULL DEFAULT ''`,
|
||||
// Add backup management fields to settings (2026-04-02).
|
||||
`ALTER TABLE settings ADD COLUMN backup_enabled INTEGER NOT NULL DEFAULT 0`,
|
||||
`ALTER TABLE settings ADD COLUMN backup_interval_hours INTEGER NOT NULL DEFAULT 24`,
|
||||
`ALTER TABLE settings ADD COLUMN backup_retention_count INTEGER NOT NULL DEFAULT 10`,
|
||||
}
|
||||
|
||||
for _, m := range migrations {
|
||||
@@ -313,6 +317,14 @@ CREATE TABLE IF NOT EXISTS dns_records (
|
||||
created_at TEXT NOT NULL DEFAULT (datetime('now')),
|
||||
updated_at TEXT NOT NULL DEFAULT (datetime('now'))
|
||||
);
|
||||
|
||||
CREATE TABLE IF NOT EXISTS backups (
|
||||
id TEXT PRIMARY KEY,
|
||||
filename TEXT NOT NULL UNIQUE,
|
||||
size_bytes INTEGER NOT NULL DEFAULT 0,
|
||||
backup_type TEXT NOT NULL DEFAULT 'manual',
|
||||
created_at TEXT NOT NULL DEFAULT (datetime('now'))
|
||||
);
|
||||
`
|
||||
|
||||
// Now returns the current time formatted for SQLite storage.
|
||||
|
||||
Reference in New Issue
Block a user