Files
bbs/internal/server/backups.go
T
Claude 0944798461 Backups: the configuration, database and files, daily or now
bbs backup makes one archive (bbs-YYYYMMDD-HHMMSS.tar.gz) of bbs.conf,
the database and the files' directories, while callers are on: the
database as a SQLite file whatever its engine, taken whole at one moment
(SQLite's by VACUUM INTO, a server's in one read-only repeatable-read
transaction: store.Snapshot).  It is written beside its final name and
renamed when whole; past [backup] keep the oldest go.

[backup] says where (beside an SQLite database by default), when (at =
HH:MM, daily, in serve), how many, and whether the files go too; it is
live at a reload.  bbs backup list, and bbs backup restore ARCHIVE -yes
with the BBS stopped: each thing put where this system's configuration
says, what is there renamed aside (.before-restore-STAMP), a server's
database copied into a new empty one; an archive whose names climb out
is refused before anything changes.

The configuration screens' Maintenance has Back up now, the backups
made, and [backup]'s keys.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01AFQhReUqUc7F5srk2hViqr
2026-10-14 20:10:27 +00:00

91 lines
2.4 KiB
Go

package server
import (
"context"
"errors"
"sync"
"time"
"github.com/Neo-Desktop/bbs-sec/internal/backup"
"github.com/Neo-Desktop/bbs-sec/internal/bbs"
)
// Backups makes the BBS's backups ([backup]): the configuration file, the
// database and the files' directories, now or daily.
type Backups struct {
B *bbs.BBS
Config string // the configuration file ("": none)
Dirs []backup.Source // the files' directories, as the BBS began with them
mu sync.Mutex // one at a time
}
// NewBackups are the backups of a BBS begun with cfg (from the file at
// path).
func NewBackups(cfg *Config, path string, b *bbs.BBS) *Backups {
return &Backups{B: b, Config: path, Dirs: Sources(cfg)}
}
// Sources are the directories of files a configuration has.
func Sources(cfg *Config) []backup.Source {
out := []backup.Source{{Name: "files", Path: cfg.FilesDir}, {Name: "chan", Path: cfg.Chan.Dir}}
if cfg.Fido != nil {
out = append(out, backup.Source{Name: "fido", Path: cfg.Fido.Dir})
}
if cfg.Screens != nil && cfg.Screens.Dir != "" {
out = append(out, backup.Source{Name: "screens", Path: cfg.Screens.Dir})
}
if cfg.ScriptsDir != "" {
out = append(out, backup.Source{Name: "scripts", Path: cfg.ScriptsDir})
}
return out
}
// Run makes a backup now, as [backup] says.
func (k *Backups) Run() (*backup.Archive, error) {
k.mu.Lock()
defer k.mu.Unlock()
s := k.B.Conf().Backup
if s.Dir == "" {
return nil, errors.New("no backup directory: [backup] dir")
}
p := &backup.Plan{Dir: s.Dir, Keep: s.Keep, Config: k.Config, Store: k.B.Store}
if s.Files {
p.Dirs = k.Dirs
}
start := time.Now()
a, err := p.Run(start)
if err != nil {
k.B.Log.Printf("backup: %v", err)
return nil, err
}
k.B.Log.Printf("backup: %s, %d bytes, in %v", a.Path, a.Size, time.Since(start).Round(time.Second))
return a, nil
}
// List is the backups there are, the newest first.
func (k *Backups) List() ([]backup.Archive, error) {
return backup.List(k.B.Conf().Backup.Dir)
}
// Schedule makes a backup each day at [backup]'s at (read again each
// minute: a reload changes it), until ctx is done.
func (k *Backups) Schedule(ctx context.Context) {
t := time.NewTicker(time.Minute)
defer t.Stop()
done := ""
for {
select {
case <-ctx.Done():
return
case now := <-t.C:
at := k.B.Conf().Backup.At
day := now.Format("2006-01-02")
if at != "" && now.Format("15:04") == at && done != day {
done = day
k.Run()
}
}
}
}