Files
skillshare/internal/backup/delete.go
T
Willie 86dca7924f feat(backup): browse and restore file history, delete snapshots
skillshare backs up AGENTS.md, CLAUDE.md and shared-file locations before
rewriting them, but nothing could list or restore those copies, and a
target snapshot could not be deleted on its own. The file backups now
record why they were taken in their name (<time>.<reason>.bak; older ones
have none), and the server and CLI can list them, show a version and
restore it, saving the current content first. A link is only replaced
when the caller asks for it.

Also:
- DELETE /api/backups/{timestamp} and `backup --delete`.
- Agents snapshots (<target>-agents) could not be restored from the
  dashboard: the name was looked up as a target.
- Project mode uses the project's backups folder for snapshots and lists
  only file backups inside the project.
- MCP backups report which servers they changed and when.

Refs #299
2026-09-28 19:23:16 +08:00

72 lines
2.1 KiB
Go

package backup
import (
"errors"
"fmt"
"os"
"path/filepath"
"regexp"
"strings"
)
// timestampPattern matches a snapshot folder name, e.g. 2024-01-15_14-30-45.
var timestampPattern = regexp.MustCompile(`^\d{4}-\d{2}-\d{2}_\d{2}-\d{2}-\d{2}$`)
var (
// ErrInvalidTimestamp is returned for a name that is not a snapshot folder name.
ErrInvalidTimestamp = errors.New("invalid backup timestamp")
// ErrNotFound is returned when no snapshot has the timestamp.
ErrNotFound = errors.New("backup not found")
)
// ValidTimestamp reports whether ts has the snapshot folder name format, so it
// can never name a path outside the backup directory.
func ValidTimestamp(ts string) bool {
return timestampPattern.MatchString(ts)
}
// DeleteInDir removes the snapshot folder named timestamp from backupDir.
func DeleteInDir(backupDir, timestamp string) error {
if !ValidTimestamp(timestamp) {
return fmt.Errorf("%w: %q", ErrInvalidTimestamp, timestamp)
}
if backupDir == "" {
return fmt.Errorf("cannot determine backup directory: home directory not found")
}
path := filepath.Join(backupDir, timestamp)
info, err := os.Lstat(path)
if os.IsNotExist(err) || err == nil && !info.IsDir() {
return fmt.Errorf("%w: %s", ErrNotFound, timestamp)
}
if err != nil {
return fmt.Errorf("inspect backup %s: %w", timestamp, err)
}
if err := os.RemoveAll(path); err != nil {
return fmt.Errorf("delete backup %s: %w", timestamp, err)
}
return nil
}
// TotalSizeInDir returns the total size of all backups in backupDir in bytes.
func TotalSizeInDir(backupDir string) (int64, error) {
backups, err := ListInDir(backupDir)
if err != nil {
return 0, err
}
var total int64
for _, b := range backups {
total += dirSize(b.Path)
}
return total, nil
}
// AgentsEntrySuffix ends the name of an agents snapshot inside a backup
// ("<target>-agents"); skills snapshots use the bare target name.
const AgentsEntrySuffix = "-agents"
// AgentsEntryTarget returns the target an agents snapshot entry belongs to.
func AgentsEntryTarget(entry string) (string, bool) {
name, ok := strings.CutSuffix(entry, AgentsEntrySuffix)
return name, ok && name != ""
}