backups

package
v0.0.3 Latest Latest
Warning

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

Go to latest
Published: Sep 6, 2019 License: MIT Imports: 10 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Backend

type Backend interface {
	Store(name string, reader io.Reader) error
	Read(name string) (io.Reader, error)
}

Backend provides read and write capabilities to a filesystem-like storage.

type InMemoryBackend

type InMemoryBackend struct {
	Backups map[string][]byte
}

InMemoryBackend stores backups in a slice. This should only be used for testing.

func NewInMemoryBackend

func NewInMemoryBackend() *InMemoryBackend

func (*InMemoryBackend) Read

func (b *InMemoryBackend) Read(name string) (io.Reader, error)

func (*InMemoryBackend) Store

func (b *InMemoryBackend) Store(name string, reader io.Reader) error

type Lock

type Lock struct {
	Name      string    `json:"name"`
	Current   string    `json:"current"`
	Previous  string    `json:"previous"`
	CreatedAt time.Time `json:"created_at"`
}

Lock is a data structure representing a backup's lock file.

A single back up may have many versions. The lock file is used to point to the current version to restore from as well as the previous version as an easy way to roll back. It also provides flexibility to point to an even older version by locking Lock.Current to any version.

func NewLock

func NewLock(name string, current string, previous string) Lock

NewLock creates a new lock with a name, current and previous versions.

func NewLockFromBytes

func NewLockFromBytes(bytes []byte) (Lock, error)

NewLockFromBytes unmarshals a lock file's JSON bytes into a Lock struct.

func (Lock) ID

func (l Lock) ID() string

ID returns the ID this lock should be stored under.

func (Lock) Shift

func (l Lock) Shift(next string) Lock

Shift returns a new Lock advanced forward to the next version.

type Manager

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

Manager performs versioned backup and restoration of files.

func NewManager

func NewManager(backend Backend, versioner Versioner) Manager

NewManager returns a new Manager with the given backend and versioner.

func (Manager) Backup

func (m Manager) Backup(name string, reader io.Reader) error

Backup creates and stores a backup for `name` with the contents of `reader` in the Manager's backend. Backups are versioned and stored under a name in the format "$name_$version.bak", where $name is the name passed to this function and $version is calculated from `m.versioner`.

A lockfile is created for each name and points to the latest stored backup under that name. The lockfile is used to restore from the latest backup.

func (Manager) Restore

func (m Manager) Restore(name string) (io.Reader, error)

Restore attempts to restore the latest backup under `name` by looking for an associated lock and returning an `io.Reader` for the contents of backup that the lock points to. If no lock exists for the given name, this function is unable to find a back up and will return an error.

type NoSuchName

type NoSuchName struct {
	Name string
}

NoSuchName is an error returned by `Backend` when a name does not exist.

func (NoSuchName) Error

func (e NoSuchName) Error() string

type S3Backend

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

S3Backend provides a Backend to AWS S3. This will also work with Digital Ocean Spaces, since this product is also S3-compatible.

func NewS3Backend

func NewS3Backend(session *session.Session, bucket string) S3Backend

NewS3Backend returns an S3Backend with the given session and bucket.

func (S3Backend) Read

func (b S3Backend) Read(name string) (io.Reader, error)

Read attempts to download `name` from S3 and return a reader.

func (S3Backend) Store

func (b S3Backend) Store(name string, reader io.Reader) error

Store stores `reader`'s bytes under `name` in S3 under the configured bucket.

type TimestampVersioner

type TimestampVersioner struct {
}

TimestampVersioner is a Versioner that returns a timestamp that can be used to version a file. It takes a `time.Time` which it uses to create the version.

func NewTimestampVersioner

func NewTimestampVersioner() TimestampVersioner

NewTimestampVersioner returns a new TimestampVersioner for the given time.

func (TimestampVersioner) GetVersion

func (v TimestampVersioner) GetVersion() string

GetVersion returns a filename-safe version string in the format `YmdTHMS`.

For example, if the versioner was created on August 15, 2019 at 15:00:00, the version string would look like `20190815T150000`.

type Versioner

type Versioner interface {
	GetVersion() string
}

Versioner provides methods for generating version strings for backups.

Jump to

Keyboard shortcuts

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