Documentation
¶
Overview ¶
Package backup provides path validation helpers for backup operations.
Usage:
absPath, err := backup.ValidateBackupPath(cfg.Backup.Dir, userSuppliedPath)
Package backup provides backup creation, listing, restoration, verification, pruning, and WAL archiving for nSelf projects.
Package backup — streaming encrypted backup to remote destinations.
The pipeline runs three concurrent goroutines:
pg_dump (streaming) | age (encrypt) | rclone (multipart upload)
No temp files are written. Encryption recipients may be age public keys, SSH public keys, or GitHub user keys (fetched via github.com/users/<user>/keys). The upload destination is any rclone-supported remote: s3://, r2://, b2://, gcs://, az://.
Index ¶
- func ArchiveWAL(ctx context.Context, opts ArchiveWALOptions) error
- func ConfigView(cfg *config.Config, format string) (string, error)
- func Create(ctx context.Context, cfg *config.Config, opts CreateOptions) error
- func FormatList(backups []BackupEntry, format string) (string, error)
- func FormatPruneJSON(result *PruneResult, keepDaily int) error
- func FormatStatus(info *StatusInfo, format string) (string, error)
- func InitKey(cfg *config.Config) (string, error)
- func InstallSystemdUnits(cfg *config.Config, opts SystemdInstallOptions) error
- func ListBackups(backupDir string) ([]string, error)
- func Restore(ctx context.Context, cfg *config.Config, opts RestoreOptions) error
- func RestoreFromRemote(ctx context.Context, cfg *config.Config, from, keyPath string) error
- func ScheduleStream(cfg *config.Config, cron, to, recipient, unitDir string, dryRun bool) error
- func ValidateBackupPath(backupDir, userPath string) (string, error)
- type ArchiveWALOptions
- type BackupEntry
- type BackupType
- type CreateOptions
- type ListOptions
- type PruneJSONEntry
- type PruneJSONReport
- type PruneOptions
- type PruneResult
- type RestoreOptions
- type ResumeOptions
- type ResumeState
- type StatusInfo
- type StreamConfig
- type StreamOptions
- type StreamResult
- type SystemdInstallOptions
- type SystemdUnitFiles
- type VerifyOptions
- type VerifyResult
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func ArchiveWAL ¶ added in v1.0.6
func ArchiveWAL(ctx context.Context, opts ArchiveWALOptions) error
ArchiveWAL encrypts and uploads a single WAL file to the remote. This is the core logic for the nself-backup-archive helper binary.
func ConfigView ¶ added in v1.0.6
ConfigView returns the current backup configuration as a formatted string.
func Create ¶ added in v1.0.6
Create performs a backup of the specified type. It writes the backup to the local backup directory and optionally uploads to the configured remote.
func FormatList ¶ added in v1.0.6
func FormatList(backups []BackupEntry, format string) (string, error)
FormatList renders backup entries as a table or JSON string.
func FormatPruneJSON ¶ added in v1.0.6
func FormatPruneJSON(result *PruneResult, keepDaily int) error
FormatPruneJSON prints a PruneResult as JSON to stdout.
func FormatStatus ¶ added in v1.0.6
func FormatStatus(info *StatusInfo, format string) (string, error)
FormatStatus renders status info as a string.
func InitKey ¶ added in v1.0.6
InitKey generates an age keypair for backup encryption. Private key: ~/.config/nself/{project}-age.key (mode 0600) Public key: written to BACKUP_AGE_RECIPIENTS in the project .env.
func InstallSystemdUnits ¶ added in v1.0.6
func InstallSystemdUnits(cfg *config.Config, opts SystemdInstallOptions) error
InstallSystemdUnits renders and writes unit files, then runs `systemctl daemon-reload` and enables each timer. Requires root.
func ListBackups ¶
ListBackups returns the names of all regular files in backupDir. The returned slice is empty (not nil) when the directory contains no files. Directories and other non-regular entries are skipped.
func RestoreFromRemote ¶ added in v1.0.12
RestoreFromRemote decrypts and restores a backup directly from a remote URL. It streams: rclone cat | age -d | pg_restore. No local temp file.
func ScheduleStream ¶ added in v1.0.12
ScheduleStream adds a systemd timer for nself backup stream. It delegates to the existing systemd infrastructure via a dedicated unit.
func ValidateBackupPath ¶
ValidateBackupPath resolves userPath relative to backupDir and verifies that the resolved absolute path is contained within backupDir. This prevents path traversal attacks (e.g. "../../../etc/passwd").
backupDir is the trusted root directory for backups (e.g. "/var/backups"). userPath is the caller-supplied relative or absolute path.
Returns the resolved absolute path on success, or an error if the path escapes backupDir.
Types ¶
type ArchiveWALOptions ¶ added in v1.0.6
type ArchiveWALOptions struct {
WALFile string // path to local WAL file
Remote string // rclone remote path
AgeRecipient string // age public key for encryption
Timeline string // PG timeline ID
MaxRetries int // max upload retries (default: 3)
RetryInterval time.Duration
}
ArchiveWALOptions holds configuration for WAL file archiving.
type BackupEntry ¶ added in v1.0.6
type BackupEntry struct {
ID string `json:"id"`
Date time.Time `json:"date"`
Size int64 `json:"size"`
Type string `json:"type"` // full, metadata, minio, wal
Tag string `json:"tag"` // user tag if present
Encrypted bool `json:"encrypted"` // .age suffix
}
BackupEntry holds parsed metadata for a single backup file.
func List ¶ added in v1.0.6
func List(cfg *config.Config, opts ListOptions) ([]BackupEntry, error)
List returns backup entries from the local backup directory.
type BackupType ¶ added in v1.0.6
type BackupType string
BackupType identifies the kind of backup to create.
const ( BackupTypeFull BackupType = "full" BackupTypeWAL BackupType = "wal" BackupTypeMetadata BackupType = "metadata" BackupTypeMinio BackupType = "minio" BackupTypeAll BackupType = "all" )
type CreateOptions ¶ added in v1.0.6
type CreateOptions struct {
Type BackupType // full, wal, metadata, minio, all
Remote string // remote name override
Encrypt bool // force encryption on
NoEncrypt bool // force encryption off
Tag string // human label for this backup
DryRun bool // preview only
}
CreateOptions holds flags for `nself backup create`.
type ListOptions ¶ added in v1.0.6
type ListOptions struct {
Remote string // filter by remote name
Env string // filter by environment
Since time.Duration // only show backups newer than this
Format string // table or json
}
ListOptions holds flags for `nself backup list`.
type PruneJSONEntry ¶ added in v1.0.6
PruneJSONEntry is one entry in the would_delete or kept arrays.
type PruneJSONReport ¶ added in v1.0.6
type PruneJSONReport struct {
DryRun bool `json:"dry_run"`
KeepDaily int `json:"keep_daily"`
WouldDelete []PruneJSONEntry `json:"would_delete"`
Kept []PruneJSONEntry `json:"kept"`
}
PruneJSONReport is the machine-readable output of `backup prune --format json`.
type PruneOptions ¶ added in v1.0.6
PruneOptions holds flags for `nself backup prune`.
type PruneResult ¶ added in v1.0.6
PruneResult summarizes what was (or would be) pruned.
func Prune ¶ added in v1.0.6
func Prune(cfg *config.Config, opts PruneOptions) (*PruneResult, error)
Prune removes old backups according to the retention policy. Retention: keep last N daily, N weekly (Sundays), N monthly (1st of month). Backups tagged "keep" are never deleted. WAL segments newer than the oldest surviving base backup are never deleted.
type RestoreOptions ¶ added in v1.0.6
type RestoreOptions struct {
BackupID string // backup ID or "latest"
ToDir string // restore to alternate directory
Only []string // pg, minio, metadata — subset to restore
DecryptKey string // path to age identity file
Yes bool // skip confirmation
}
RestoreOptions holds flags for `nself backup restore`.
Note: PointInTime (PITR) is NOT supported in v1.0.9. The field is retained for forward compatibility but any non-empty value returns an explicit error directing users to v1.1.0 + pgbackrest integration. Remove this note when PITR ships.
type ResumeOptions ¶ added in v1.0.12
type ResumeOptions struct {
BackupID string
}
ResumeOptions holds flags for `nself backup resume`.
type ResumeState ¶ added in v1.0.12
type ResumeState struct {
BackupID string `json:"backup_id"`
Destination string `json:"destination"`
Key string `json:"key"`
Recipients []string `json:"recipients"`
StartedAt time.Time `json:"started_at"`
// For rclone-based multipart, we store the partial object key prefix.
// rclone handles actual part tracking internally; we record enough to
// invoke rclone copyto from a local temp file on resume.
PartialPath string `json:"partial_path,omitempty"`
}
ResumeState holds the persisted state for a resumable streaming backup.
type StatusInfo ¶ added in v1.0.6
type StatusInfo struct {
LastRun string `json:"last_run"`
NextRun string `json:"next_run"`
Health string `json:"health"` // healthy, warning, error
TotalSize string `json:"total_size"`
BackupCount int `json:"backup_count"`
RetentionDaily int `json:"retention_daily"`
RetentionWeekly int `json:"retention_weekly"`
RetentionMonthly int `json:"retention_monthly"`
}
StatusInfo holds backup subsystem status.
type StreamConfig ¶ added in v1.0.12
type StreamConfig struct {
// PgURL is the postgres DSN used by pg_dump. If empty, derived from cfg.
PgURL string
// Destination is the rclone remote path, e.g. "s3:bucket/prefix/".
Destination string
// Recipients holds age/SSH public keys (one per entry). May also be
// "github:<username>" to fetch the user's SSH public keys from GitHub.
Recipients []string
// Key is the object name appended to Destination. Auto-generated if empty.
Key string
// ChunkMB is the multipart chunk size. Defaults to 64.
ChunkMB int
}
StreamConfig holds parameters for a streaming encrypted backup.
type StreamOptions ¶ added in v1.0.12
type StreamOptions struct {
To string // destination URL (rclone remote path)
Recipients []string // --recipient flags (may be specified multiple times)
DryRun bool
}
StreamOptions holds CLI flag values for `nself backup stream`.
type StreamResult ¶ added in v1.0.12
type StreamResult struct {
BackupID string `json:"backup_id"`
Destination string `json:"destination"`
StartedAt time.Time `json:"started_at"`
Duration string `json:"duration"`
Encrypted bool `json:"encrypted"`
}
StreamResult is returned by Stream on success.
func Resume ¶ added in v1.0.12
func Resume(ctx context.Context, cfg *config.Config, opts ResumeOptions) (*StreamResult, error)
Resume attempts to complete a previously interrupted streaming backup. Since rclone rcat cannot resume a partial upload, Resume re-streams from pg_dump through the full pipeline. The previous partial upload is overwritten at the same destination key.
func Stream ¶ added in v1.0.12
func Stream(ctx context.Context, cfg *config.Config, opts StreamOptions) (*StreamResult, error)
Stream runs a three-stage concurrent pipeline:
- pg_dump stdout -> pgWriter
- age encrypts pgReader -> encWriter (skipped if no recipients)
- rclone rcat uploads encReader to Destination/Key
All three stages run in parallel goroutines. The first error from any stage cancels the others via context cancellation.
type SystemdInstallOptions ¶ added in v1.0.6
type SystemdInstallOptions struct {
FullAt string // "HH:MM" UTC, e.g. "03:00"
WALEvery string // e.g. "15m"
PruneAt string // "HH:MM" UTC, default "04:00"
VerifyOnDay string // systemd OnCalendar day name (Sun..Sat), default "Sun"
VerifyAt string // "HH:MM", default "05:00"
UnitDir string // default /etc/systemd/system
EnvFile string // default /etc/nself/backup.env
BinaryPath string // default /usr/local/bin/nself
ProjectDir string // working directory for nself commands
Remote string // e.g. b2-nclaw
DryRun bool
}
SystemdInstallOptions controls install of nself-backup systemd units.
type SystemdUnitFiles ¶ added in v1.0.6
SystemdUnitFiles maps filename -> unit contents.
func RenderSystemdUnits ¶ added in v1.0.6
func RenderSystemdUnits(cfg *config.Config, opts SystemdInstallOptions) (SystemdUnitFiles, error)
RenderSystemdUnits produces the unit files needed for unattended backup, verify, and prune without writing to disk. Returns a map suitable for writing into /etc/systemd/system.
type VerifyOptions ¶ added in v1.0.6
type VerifyOptions struct {
BackupID string // backup ID or "latest"
RestoreTest bool // spin up test container and restore
Cleanup bool // remove test container after verify
Keep bool // keep test container for inspection
}
VerifyOptions holds flags for `nself backup verify`.
type VerifyResult ¶ added in v1.0.6
type VerifyResult struct {
BackupID string `json:"backup_id"`
Verified bool `json:"verified"`
StartedAt time.Time `json:"started_at"`
Duration string `json:"duration"`
Method string `json:"method"` // checksum, restore-test
Details string `json:"details"`
}
VerifyResult holds the outcome of a backup verification.
func Verify ¶ added in v1.0.6
func Verify(ctx context.Context, cfg *config.Config, opts VerifyOptions) (*VerifyResult, error)
Verify checks backup integrity, optionally performing a restore test.