Documentation ¶
Index ¶
Constants ¶
const ( SQLite3Main = "main" PagesPerStep = 5 StepSleep = 50 * time.Millisecond DefaultDBName = "backup.db" )
Variables ¶
var ( ErrNotEnabled = errors.New("the backup manager is not enabled") ErrInvalidStorageDSN = errors.New("could not parse storage dsn, specify scheme:///relative/path/") ErrNotADirectory = errors.New("incorrectly configured: backup storage is not a directory") ErrNilSQLite3Conn = errors.New("could not fetch underlying sqlite3 connection for backup") )
Functions ¶
Types ¶
type Backup ¶
Backup is an interface that enables different types of database backups, e.g. for backuping up a leveldb database (or multiple leveldb databases) vs a sqlite database. It is expected that the backup is written to the temporary directory specified by the input variable. It is unnecessary to compress the contents of the backup as the backup manager will compress the contents before transferring them to the backup storage location. Once the backup is complete (error or not) the tmpdir is removed.
type Config ¶
type Config struct { // If false, the backup manager will not run. Enabled bool `default:"false"` // The interval between backups. Interval time.Duration `default:"24h"` // The path to a local disk directory to store compressed backups, e.g. // file:///rel/path/ or a cloud location such as s3://bucket. StorageDSN string `split_words:"true" required:"true"` // Temporary directory to perform local backup to. If not set, then the OS tmpdir // is used. Backups are generated in this folder and then moved to the storage // location. This folder needs enough space to contain the complete backup. TempDir string `required:"false"` // Prefix specifies the filename of the backup, e.g.g prefix-200601021504.tgz Prefix string `default:"backup"` // The number of previous backup versions to keep. Keep int `default:"1"` }
Configure the backup manager routine to ensure it is backing up the correct databases at the correct interval; removing old backups as necessary and storing the backups in the correct storage location.
func (Config) ArchiveName ¶
ArchiveName returns the name of the next archive using the current timestamp.
type FileStorage ¶
type FileStorage struct {
// contains filtered or unexported fields
}
FileStorage stores archived backups on a local disk. This is primarily used by mounting a second volume on a Kubernetes pod and saving the backups there or by writing to a network or RAID protected dis.
func NewFileStorage ¶
func NewFileStorage(root, prefix string) (_ *FileStorage, err error)
NewFileStorage creates the specified root directory if it does not exist. All storage archives will be opened relative to the root directory.
func (*FileStorage) ListArchives ¶
func (s *FileStorage) ListArchives() (paths []string, err error)
ListArchives returns all backup archives in the FileStorage directory ordered by date ascending using string sorting that depends on the backup archive name format: prefix-YYYYmmddHHMM.tgz
func (*FileStorage) Open ¶
func (s *FileStorage) Open(name string) (_ io.WriteCloser, err error)
Open a file on disk and return the file for writting a gzip stream to.
func (*FileStorage) Remove ¶
func (s *FileStorage) Remove(name string) error
Remove a file from the backup directory.
type LevelDB ¶
LevelDB implements a single leveldb backup that takes a snapshot of the database and writes it to a second leveldb database at the temporary directory location.
type Manager ¶
Manager runs as an independent service which periodically backs up a database to a compressed backup location on disk or to cloud storage. At a specified interval, the manager runs a db-specific backup routine that generally clones the database to a tmp directory; it then compresses the clone and moves it to a final backup location.
func New ¶
Return a new backup manager ready to be run. NOTE: the backup manager is not started on new; it must explicitly be run for the backup routine to be effective. This follows the startup/shutdown services model.
func (*Manager) MkdirTemp ¶
Create a temporary direcory in the configured path. If no configured directory is specified then os.MkdirTemp is used. It is the callers responsibility to cleanup the directory that was created.
type MemoryStorage ¶
type MemoryStorage struct {
// contains filtered or unexported fields
}
Memory storage is primarily used for testing and writes backups to a bytes buffer. If /dev/null is specified as the "root" path, then a noop-closer is returned.
func (*MemoryStorage) ListArchives ¶
func (s *MemoryStorage) ListArchives() ([]string, error)
func (*MemoryStorage) Open ¶
func (s *MemoryStorage) Open(name string) (io.WriteCloser, error)
func (*MemoryStorage) Remove ¶
func (s *MemoryStorage) Remove(name string) error