backup

package module
v0.1.1 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jun 25, 2026 License: MIT Imports: 17 Imported by: 0

README

togo

togo-framework/backup

marketplace pkg.go.dev MIT

Scheduled DB + files backups for togo — archive, retain, restore.

Install

togo install togo-framework/backup

The togo answer to Spatie Backup. Bundle a database dump + configured files into a single .tar.gz, keep the newest N, and restore on demand. Pair with scheduler to back up on a cadence.

Configuration

Env Description
BACKUP_SOURCES comma-separated directories/files to include
BACKUP_DB_DRIVER postgres | mysql | sqlite (omit to skip the DB)
BACKUP_DB_DSN DB connection string, or the sqlite file path
BACKUP_DIR output directory for archives (default backups)
BACKUP_KEEP retention — keep the newest N archives (0 = keep all)

Or configure in code:

b, _ := backup.FromKernel(k)
b.Configure(backup.Config{
    Sources:  []string{"storage/app", "uploads"},
    DBDriver: "postgres", DBDSN: os.Getenv("DATABASE_URL"),
    Dir:      "backups", Keep: 7,
})

Usage

rec, err := b.Run(ctx)        // create a backup now → *Backup{ID, Path, Size, ...}
list := b.List()              // newest first
b.Restore(rec.ID, "/restore") // extract the archive (DB restore is a manual psql/mysql step)
b.Prune()                     // enforce retention

Schedule it

scheduler.Register("backup", func(ctx context.Context) error {
    b, _ := backup.FromKernel(k); _, err := b.Run(ctx); return err
})
sched.Schedule("backup", scheduler.DailyAt(3, 0)) // 03:00 nightly

REST API

Method Path Description
POST /api/backup/run create a backup now
GET /api/backup/backups list backups (newest first)
GET /api/backup/backups/{id} one backup record

pg_dump/mysqldump must be on PATH for the respective drivers; sqlite is copied directly. Archives extract path-traversal-safe.


Premium sponsors

ID8 Media  ·  One Studio

Support togo — become a sponsor.

Documentation

Overview

Package backup creates scheduled .tar.gz backups of a togo app's database and configured files (the togo answer to Spatie Backup).

A backup bundles an optional database dump (pg_dump / mysqldump / sqlite copy) plus configured source directories into a single gzip-compressed tar archive written to a destination directory. Retention prunes old archives, and run records are queryable over a Go + REST API. Pair it with the scheduler plugin to run backups on a cadence.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Backup

type Backup struct {
	ID        string    `json:"id"`
	Name      string    `json:"name"`
	Path      string    `json:"path"`
	Size      int64     `json:"size"`
	Status    string    `json:"status"` // done | failed
	WithDB    bool      `json:"with_db"`
	CreatedAt time.Time `json:"created_at"`
	Error     string    `json:"error,omitempty"`
}

Backup is a record of one archive.

type Config

type Config struct {
	Sources  []string // directories/files to include
	DBDriver string   // "postgres" | "mysql" | "sqlite" (empty = no DB dump)
	DBDSN    string   // connection string, or a sqlite file path
	Dir      string   // output directory for archives (default "backups")
	Keep     int      // retention: keep the newest N archives (0 = keep all)
}

Config controls what is backed up and where.

type Service

type Service struct {
	// contains filtered or unexported fields
}

Service is the backup runtime stored on the kernel (k.Get("backup")).

func FromKernel

func FromKernel(k *togo.Kernel) (*Service, bool)

FromKernel returns the backup Service.

func (*Service) Configure

func (s *Service) Configure(c Config) *Service

Configure replaces the backup configuration.

func (*Service) Get

func (s *Service) Get(id string) (*Backup, bool)

Get returns a backup record by id.

func (*Service) List

func (s *Service) List() []*Backup

List returns backup records, newest first.

func (*Service) Prune

func (s *Service) Prune() int

Prune keeps only the newest cfg.Keep successful archives, deleting older files.

func (*Service) Restore

func (s *Service) Restore(id, destDir string) error

Restore extracts a backup archive into destDir. Restoring the database from the included dump is a manual step (run psql/mysql against the dump file).

func (*Service) Run

func (s *Service) Run(ctx context.Context) (*Backup, error)

Run creates a backup archive (DB dump + configured sources) and records it.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL