mirror of
https://github.com/runkids/skillshare.git
synced 2026-11-20 12:23:22 +00:00
Target folder backups were capped at a fixed 10 snapshots and 500 MB, and the dashboard could only remove them one at a time. Users with larger targets or little disk space had no way to tune either. The global config now takes backup.max_count and backup.max_size_mb (0 = no limit); sync, the dashboard's sync and backup --cleanup apply them, while the 30-day limit stays fixed and project snapshots keep the defaults. The Target folders tab shows the limits in its toolbar, opens a panel to change them, and adds Delete all behind a confirmation. File, MCP and Hooks backups are not touched.
200 lines
5.0 KiB
Go
200 lines
5.0 KiB
Go
package backup
|
|
|
|
import (
|
|
"errors"
|
|
"fmt"
|
|
"os"
|
|
"path/filepath"
|
|
"strings"
|
|
"time"
|
|
|
|
"skillshare/internal/config"
|
|
)
|
|
|
|
// CleanupConfig holds backup cleanup configuration
|
|
type CleanupConfig struct {
|
|
MaxAge time.Duration // Maximum age of backups to keep (0 = no limit)
|
|
MaxCount int // Maximum number of backups to keep (0 = no limit)
|
|
MaxSizeMB int64 // Maximum total size in MB (0 = no limit)
|
|
}
|
|
|
|
// DefaultCleanupConfig returns sensible defaults for cleanup
|
|
func DefaultCleanupConfig() CleanupConfig {
|
|
return CleanupConfig{
|
|
MaxAge: 30 * 24 * time.Hour, // 30 days
|
|
MaxCount: 10, // Keep last 10 backups
|
|
MaxSizeMB: 500, // 500 MB max
|
|
}
|
|
}
|
|
|
|
// RetentionConfig returns the defaults with the limits set in the global
|
|
// config's backup section. A nil config, or a negative limit, keeps the default.
|
|
func RetentionConfig(cfg *config.Config) CleanupConfig {
|
|
c := DefaultCleanupConfig()
|
|
if cfg == nil {
|
|
return c
|
|
}
|
|
if n := cfg.Backup.MaxCount; n != nil && *n >= 0 {
|
|
c.MaxCount = *n
|
|
}
|
|
if n := cfg.Backup.MaxSizeMB; n != nil && *n >= 0 {
|
|
c.MaxSizeMB = *n
|
|
}
|
|
return c
|
|
}
|
|
|
|
// Cleanup removes old backups from the global backup dir.
|
|
// Returns the number of backups removed and any error encountered.
|
|
func Cleanup(cfg CleanupConfig) (int, error) {
|
|
return CleanupInDir(BackupDir(), cfg)
|
|
}
|
|
|
|
// CleanupInDir removes old backups from the specified directory based on the
|
|
// configuration. Returns the number of backups removed.
|
|
func CleanupInDir(backupDir string, cfg CleanupConfig) (int, error) {
|
|
backups, err := ListInDir(backupDir)
|
|
if err != nil {
|
|
return 0, err
|
|
}
|
|
|
|
if len(backups) == 0 {
|
|
return 0, nil
|
|
}
|
|
|
|
removed := 0
|
|
now := time.Now()
|
|
var totalSize int64
|
|
var cleanupErrs []error
|
|
|
|
// Backups are sorted by date (newest first)
|
|
for i, backup := range backups {
|
|
shouldRemove := false
|
|
|
|
// Check age (skip if MaxAge is 0)
|
|
if cfg.MaxAge > 0 && now.Sub(backup.Date) > cfg.MaxAge {
|
|
shouldRemove = true
|
|
}
|
|
|
|
// Check count - keep most recent N backups (skip if MaxCount is 0)
|
|
if cfg.MaxCount > 0 && i >= cfg.MaxCount {
|
|
shouldRemove = true
|
|
}
|
|
|
|
// Check size - remove if total exceeds limit (skip if MaxSizeMB is 0).
|
|
// i > 0 always keeps the newest backup: a single snapshot larger than
|
|
// the cap must not leave the user with no restore point at all.
|
|
size := dirSize(backup.Path)
|
|
if !shouldRemove && cfg.MaxSizeMB > 0 && i > 0 &&
|
|
totalSize+size > cfg.MaxSizeMB*1024*1024 {
|
|
shouldRemove = true
|
|
}
|
|
|
|
if shouldRemove {
|
|
if err := os.RemoveAll(backup.Path); err != nil {
|
|
cleanupErrs = append(cleanupErrs, fmt.Errorf("remove backup %s: %w", backup.Timestamp, err))
|
|
totalSize += size
|
|
continue
|
|
}
|
|
removed++
|
|
continue
|
|
}
|
|
totalSize += size
|
|
}
|
|
|
|
// Clean up empty timestamp directories
|
|
if err := cleanEmptyDirs(backupDir); err != nil {
|
|
cleanupErrs = append(cleanupErrs, err)
|
|
}
|
|
|
|
return removed, errors.Join(cleanupErrs...)
|
|
}
|
|
|
|
// CleanupByAge removes backups older than the specified duration.
|
|
// Returns the number of backups removed.
|
|
func CleanupByAge(maxAge time.Duration) (int, error) {
|
|
return Cleanup(CleanupConfig{MaxAge: maxAge})
|
|
}
|
|
|
|
// CleanupByCount keeps only the N most recent backups.
|
|
// Returns the number of backups removed.
|
|
func CleanupByCount(maxCount int) (int, error) {
|
|
return Cleanup(CleanupConfig{MaxCount: maxCount})
|
|
}
|
|
|
|
// dirSize calculates the total size of a directory
|
|
func dirSize(path string) int64 {
|
|
size, _ := Stats(path)
|
|
return size
|
|
}
|
|
|
|
// Stats returns the total size in bytes and the number of files under path,
|
|
// in a single walk.
|
|
func Stats(path string) (size int64, files int) {
|
|
filepath.Walk(path, func(_ string, info os.FileInfo, err error) error {
|
|
if err != nil {
|
|
return nil
|
|
}
|
|
if !info.IsDir() {
|
|
size += info.Size()
|
|
files++
|
|
}
|
|
return nil
|
|
})
|
|
return size, files
|
|
}
|
|
|
|
// cleanEmptyDirs removes empty directories in the backup directory.
|
|
func cleanEmptyDirs(path string) error {
|
|
if path == "" {
|
|
return nil
|
|
}
|
|
|
|
entries, err := os.ReadDir(path)
|
|
if err != nil {
|
|
if os.IsNotExist(err) {
|
|
return nil
|
|
}
|
|
return fmt.Errorf("read backup directory: %w", err)
|
|
}
|
|
|
|
var cleanupErrs []error
|
|
for _, entry := range entries {
|
|
if strings.HasPrefix(entry.Name(), stagingDirPrefix) {
|
|
continue
|
|
}
|
|
if entry.IsDir() {
|
|
subPath := filepath.Join(path, entry.Name())
|
|
subEntries, err := os.ReadDir(subPath)
|
|
if err != nil {
|
|
cleanupErrs = append(cleanupErrs, fmt.Errorf("read backup timestamp %s: %w", entry.Name(), err))
|
|
continue
|
|
}
|
|
if len(subEntries) == 0 {
|
|
if err := os.Remove(subPath); err != nil && !os.IsNotExist(err) {
|
|
cleanupErrs = append(cleanupErrs, fmt.Errorf("remove empty backup timestamp %s: %w", entry.Name(), err))
|
|
}
|
|
}
|
|
}
|
|
}
|
|
return errors.Join(cleanupErrs...)
|
|
}
|
|
|
|
// Size returns the total size of a backup directory in bytes
|
|
func Size(path string) int64 {
|
|
return dirSize(path)
|
|
}
|
|
|
|
// TotalSize returns the total size of all backups in bytes
|
|
func TotalSize() (int64, error) {
|
|
backups, err := List()
|
|
if err != nil {
|
|
return 0, err
|
|
}
|
|
|
|
var total int64
|
|
for _, b := range backups {
|
|
total += dirSize(b.Path)
|
|
}
|
|
return total, nil
|
|
}
|