Documentation ¶
Overview ¶
Package repository implements a restic repository on top of a backend. In the following the abstractions used for this package are listed. More information can be found in the restic design document.
File ¶
A file is a named handle for some data saved in the backend. For the local backend, this corresponds to actual files saved to disk. Usually, the SHA256 hash of the content is used for a file's name (hexadecimal, in lower-case ASCII characters). An exception is the file `config`. Most files are encrypted before being saved in a backend. This means that the name is the hash of the ciphertext.
Blob ¶
A blob is a number of bytes that has a type (data or tree). Blobs are identified by an ID, which is the SHA256 hash of the blobs' contents. One or more blobs are bundled together in a Pack and then saved to the backend. Blobs are always encrypted before being bundled in a Pack.
Pack ¶
A Pack is a File in the backend that contains one or more (encrypted) blobs, followed by a header at the end of the Pack. The header is encrypted and contains the ID, type, length and offset for each blob contained in the Pack.
Index ¶
- Variables
- func FilesInParallel(ctx context.Context, repo restic.Lister, t restic.FileType, n uint, ...) error
- func Repack(ctx context.Context, repo restic.Repository, packs restic.IDSet, ...) (obsoletePacks restic.IDSet, err error)
- func SaveIndex(ctx context.Context, repo restic.Repository, index *Index) (restic.ID, error)
- func TestBackend(t testing.TB) (be restic.Backend, cleanup func())
- func TestOpenLocal(t testing.TB, dir string) (r restic.Repository)
- func TestRepository(t testing.TB) (r restic.Repository, cleanup func())
- func TestRepositoryWithBackend(t testing.TB, be restic.Backend) (r restic.Repository, cleanup func())
- func TestUseLowSecurityKDFParameters(t logger)
- type Index
- func DecodeIndex(buf []byte) (idx *Index, err error)
- func DecodeOldIndex(buf []byte) (idx *Index, err error)
- func LoadIndex(ctx context.Context, repo restic.Repository, id restic.ID) (*Index, error)
- func LoadIndexWithDecoder(ctx context.Context, repo restic.Repository, id restic.ID, ...) (idx *Index, err error)
- func NewIndex() *Index
- func (idx *Index) AddToSupersedes(ids ...restic.ID) error
- func (idx *Index) Count(t restic.BlobType) (n uint)
- func (idx *Index) Dump(w io.Writer) error
- func (idx *Index) Each(ctx context.Context) <-chan restic.PackedBlob
- func (idx *Index) Encode(w io.Writer) error
- func (idx *Index) Final() bool
- func (idx *Index) Finalize(w io.Writer) error
- func (idx *Index) Has(id restic.ID, tpe restic.BlobType) bool
- func (idx *Index) ID() (restic.ID, error)
- func (idx *Index) ListPack(id restic.ID) (list []restic.PackedBlob)
- func (idx *Index) Lookup(id restic.ID, tpe restic.BlobType) (blobs []restic.PackedBlob, err error)
- func (idx *Index) LookupSize(id restic.ID, tpe restic.BlobType) (plaintextLength uint, err error)
- func (idx *Index) Packs() restic.IDSet
- func (idx *Index) SetID(id restic.ID) error
- func (idx *Index) Store(blob restic.PackedBlob)
- func (idx *Index) Supersedes() restic.IDs
- type Key
- func AddKey(ctx context.Context, s *Repository, password string, template *crypto.Key) (*Key, error)
- func LoadKey(ctx context.Context, s *Repository, name string) (k *Key, err error)
- func OpenKey(ctx context.Context, s *Repository, name string, password string) (*Key, error)
- func SearchKey(ctx context.Context, s *Repository, password string, maxKeys int) (*Key, error)
- type MasterIndex
- func (mi *MasterIndex) All() []*Index
- func (mi *MasterIndex) Count(t restic.BlobType) (n uint)
- func (mi *MasterIndex) Each(ctx context.Context) <-chan restic.PackedBlob
- func (mi *MasterIndex) FullIndexes() []*Index
- func (mi *MasterIndex) Has(id restic.ID, tpe restic.BlobType) bool
- func (mi *MasterIndex) Insert(idx *Index)
- func (mi *MasterIndex) ListPack(id restic.ID) (list []restic.PackedBlob)
- func (mi *MasterIndex) Lookup(id restic.ID, tpe restic.BlobType) (blobs []restic.PackedBlob, err error)
- func (mi *MasterIndex) LookupSize(id restic.ID, tpe restic.BlobType) (uint, error)
- func (mi *MasterIndex) NotFinalIndexes() []*Index
- func (mi *MasterIndex) RebuildIndex(packBlacklist restic.IDSet) (*Index, error)
- func (mi *MasterIndex) Remove(index *Index)
- func (mi *MasterIndex) Store(pb restic.PackedBlob)
- type Packer
- type ParallelIDWorkFunc
- type ParallelWorkFunc
- type Repository
- func (r *Repository) Backend() restic.Backend
- func (r *Repository) Close() error
- func (r *Repository) Config() restic.Config
- func (r *Repository) Delete(ctx context.Context) error
- func (r *Repository) Encrypt(ciphertext, plaintext []byte) ([]byte, error)
- func (r *Repository) Flush() error
- func (r *Repository) Index() restic.Index
- func (r *Repository) Init(ctx context.Context, password string) error
- func (r *Repository) Key() *crypto.Key
- func (r *Repository) KeyName() string
- func (r *Repository) List(ctx context.Context, t restic.FileType) <-chan restic.ID
- func (r *Repository) ListPack(ctx context.Context, id restic.ID) ([]restic.Blob, int64, error)
- func (r *Repository) LoadAndDecrypt(ctx context.Context, t restic.FileType, id restic.ID) ([]byte, error)
- func (r *Repository) LoadBlob(ctx context.Context, t restic.BlobType, id restic.ID, buf []byte) (int, error)
- func (r *Repository) LoadIndex(ctx context.Context) error
- func (r *Repository) LoadJSONUnpacked(ctx context.Context, t restic.FileType, id restic.ID, item interface{}) (err error)
- func (r *Repository) LoadTree(ctx context.Context, id restic.ID) (*restic.Tree, error)
- func (r *Repository) LookupBlobSize(id restic.ID, tpe restic.BlobType) (uint, error)
- func (r *Repository) PrefixLength(t restic.FileType) (int, error)
- func (r *Repository) SaveAndEncrypt(ctx context.Context, t restic.BlobType, data []byte, id *restic.ID) (restic.ID, error)
- func (r *Repository) SaveBlob(ctx context.Context, t restic.BlobType, buf []byte, id restic.ID) (restic.ID, error)
- func (r *Repository) SaveFullIndex(ctx context.Context) error
- func (r *Repository) SaveIndex(ctx context.Context) error
- func (r *Repository) SaveJSONUnpacked(ctx context.Context, t restic.FileType, item interface{}) (restic.ID, error)
- func (r *Repository) SaveTree(ctx context.Context, t *restic.Tree) (restic.ID, error)
- func (r *Repository) SaveUnpacked(ctx context.Context, t restic.FileType, p []byte) (id restic.ID, err error)
- func (r *Repository) SearchKey(ctx context.Context, password string, maxKeys int) error
- func (r *Repository) SetIndex(i restic.Index)
- type Saver
Constants ¶
This section is empty.
Variables ¶
var ( // ErrNoKeyFound is returned when no key for the repository could be decrypted. ErrNoKeyFound = errors.New("wrong password or no key found") // ErrMaxKeysReached is returned when the maximum number of keys was checked and no key could be found. ErrMaxKeysReached = errors.New("maximum number of keys reached") )
var ( // KDFTimeout specifies the maximum runtime for the KDF. KDFTimeout = 500 * time.Millisecond // KDFMemory limits the memory the KDF is allowed to use. KDFMemory = 60 )
var ErrOldIndexFormat = errors.New("index has old format")
ErrOldIndexFormat means an index with the old format was detected.
var IndexFull = func(idx *Index) bool { idx.m.Lock() defer idx.m.Unlock() debug.Log("checking whether index %p is full", idx) packs := len(idx.pack) age := time.Now().Sub(idx.created) if age > indexMaxAge { debug.Log("index %p is old enough", idx, age) return true } if packs < indexMinBlobs || age < indexMinAge { debug.Log("index %p only has %d packs or is too young (%v)", idx, packs, age) return false } if packs > indexMaxBlobs { debug.Log("index %p has %d packs", idx, packs) return true } debug.Log("index %p is not full", idx) return false }
IndexFull returns true iff the index is "full enough" to be saved as a preliminary index.
var KDFParams *crypto.KDFParams
KDFParams tracks the parameters used for the KDF. If not set, it will be calibrated on the first run of AddKey().
Functions ¶
func FilesInParallel ¶
func FilesInParallel(ctx context.Context, repo restic.Lister, t restic.FileType, n uint, f ParallelWorkFunc) error
FilesInParallel runs n workers of f in parallel, on the IDs that repo.List(t) yield. If f returns an error, the process is aborted and the first error is returned.
func Repack ¶ added in v0.3.0
func Repack(ctx context.Context, repo restic.Repository, packs restic.IDSet, keepBlobs restic.BlobSet, p *restic.Progress) (obsoletePacks restic.IDSet, err error)
Repack takes a list of packs together with a list of blobs contained in these packs. Each pack is loaded and the blobs listed in keepBlobs is saved into a new pack. Returned is the list of obsolete packs which can then be removed.
func TestBackend ¶
TestBackend returns a fully configured in-memory backend.
func TestOpenLocal ¶ added in v0.3.0
func TestOpenLocal(t testing.TB, dir string) (r restic.Repository)
TestOpenLocal opens a local repository.
func TestRepository ¶
func TestRepository(t testing.TB) (r restic.Repository, cleanup func())
TestRepository returns a repository initialized with a test password on an in-memory backend. When the environment variable RESTIC_TEST_REPO is set to a non-existing directory, a local backend is created there and this is used instead. The directory is not removed, but left there for inspection.
func TestRepositoryWithBackend ¶
func TestRepositoryWithBackend(t testing.TB, be restic.Backend) (r restic.Repository, cleanup func())
TestRepositoryWithBackend returns a repository initialized with a test password. If be is nil, an in-memory backend is used. A constant polynomial is used for the chunker and low-security test parameters.
func TestUseLowSecurityKDFParameters ¶ added in v0.3.0
func TestUseLowSecurityKDFParameters(t logger)
TestUseLowSecurityKDFParameters configures low-security KDF parameters for testing.
Types ¶
type Index ¶
type Index struct {
// contains filtered or unexported fields
}
Index holds a lookup table for id -> pack.
func DecodeIndex ¶
DecodeIndex loads and unserializes an index from rd.
func DecodeOldIndex ¶
DecodeOldIndex loads and unserializes an index in the old format from rd.
func LoadIndexWithDecoder ¶
func LoadIndexWithDecoder(ctx context.Context, repo restic.Repository, id restic.ID, fn func([]byte) (*Index, error)) (idx *Index, err error)
LoadIndexWithDecoder loads the index and decodes it with fn.
func (*Index) AddToSupersedes ¶
AddToSupersedes adds the ids to the list of indexes superseded by this index. If the index has already been finalized, an error is returned.
func (*Index) Each ¶
func (idx *Index) Each(ctx context.Context) <-chan restic.PackedBlob
Each returns a channel that yields all blobs known to the index. When the context is cancelled, the background goroutine terminates. This blocks any modification of the index.
func (*Index) Final ¶
Final returns true iff the index is already written to the repository, it is finalized.
func (*Index) ID ¶
ID returns the ID of the index, if available. If the index is not yet finalized, an error is returned.
func (*Index) ListPack ¶
func (idx *Index) ListPack(id restic.ID) (list []restic.PackedBlob)
ListPack returns a list of blobs contained in a pack.
func (*Index) LookupSize ¶
LookupSize returns the length of the plaintext content of the blob with the given id.
func (*Index) SetID ¶
SetID sets the ID the index has been written to. This requires that Finalize() has been called before, otherwise an error is returned.
func (*Index) Store ¶
func (idx *Index) Store(blob restic.PackedBlob)
Store remembers the id and pack in the index. An existing entry will be silently overwritten.
func (*Index) Supersedes ¶
Supersedes returns the list of indexes this index supersedes, if any.
type Key ¶
type Key struct { Created time.Time `json:"created"` Username string `json:"username"` Hostname string `json:"hostname"` KDF string `json:"kdf"` N int `json:"N"` R int `json:"r"` P int `json:"p"` Salt []byte `json:"salt"` Data []byte `json:"data"` // contains filtered or unexported fields }
Key represents an encrypted master key for a repository.
func AddKey ¶
func AddKey(ctx context.Context, s *Repository, password string, template *crypto.Key) (*Key, error)
AddKey adds a new key to an already existing repository.
type MasterIndex ¶
type MasterIndex struct {
// contains filtered or unexported fields
}
MasterIndex is a collection of indexes and IDs of chunks that are in the process of being saved.
func (*MasterIndex) Count ¶
func (mi *MasterIndex) Count(t restic.BlobType) (n uint)
Count returns the number of blobs of type t in the index.
func (*MasterIndex) Each ¶ added in v0.7.0
func (mi *MasterIndex) Each(ctx context.Context) <-chan restic.PackedBlob
Each returns a channel that yields all blobs known to the index. When the context is cancelled, the background goroutine terminates. This blocks any modification of the index.
func (*MasterIndex) FullIndexes ¶
func (mi *MasterIndex) FullIndexes() []*Index
FullIndexes returns all indexes that are full.
func (*MasterIndex) Insert ¶
func (mi *MasterIndex) Insert(idx *Index)
Insert adds a new index to the MasterIndex.
func (*MasterIndex) ListPack ¶
func (mi *MasterIndex) ListPack(id restic.ID) (list []restic.PackedBlob)
ListPack returns the list of blobs in a pack. The first matching index is returned, or nil if no index contains information about the pack id.
func (*MasterIndex) Lookup ¶
func (mi *MasterIndex) Lookup(id restic.ID, tpe restic.BlobType) (blobs []restic.PackedBlob, err error)
Lookup queries all known Indexes for the ID and returns the first match.
func (*MasterIndex) LookupSize ¶
LookupSize queries all known Indexes for the ID and returns the first match.
func (*MasterIndex) NotFinalIndexes ¶
func (mi *MasterIndex) NotFinalIndexes() []*Index
NotFinalIndexes returns all indexes that have not yet been saved.
func (*MasterIndex) RebuildIndex ¶
func (mi *MasterIndex) RebuildIndex(packBlacklist restic.IDSet) (*Index, error)
RebuildIndex combines all known indexes to a new index, leaving out any packs whose ID is contained in packBlacklist. The new index contains the IDs of all known indexes in the "supersedes" field.
func (*MasterIndex) Remove ¶
func (mi *MasterIndex) Remove(index *Index)
Remove deletes an index from the MasterIndex.
func (*MasterIndex) Store ¶ added in v0.3.3
func (mi *MasterIndex) Store(pb restic.PackedBlob)
Store remembers the id and pack in the index.
type ParallelIDWorkFunc ¶
ParallelIDWorkFunc gets one restic.ID to work on. If an error is returned, processing stops. When the context is cancelled the function should return.
type ParallelWorkFunc ¶
ParallelWorkFunc gets one file ID to work on. If an error is returned, processing stops. When the contect is cancelled the function should return.
func ParallelWorkFuncParseID ¶
func ParallelWorkFuncParseID(f ParallelIDWorkFunc) ParallelWorkFunc
ParallelWorkFuncParseID converts a function that takes a restic.ID to a function that takes a string. Filenames that do not parse as a restic.ID are ignored.
type Repository ¶
type Repository struct {
// contains filtered or unexported fields
}
Repository is used to access a repository in a backend.
func (*Repository) Backend ¶
func (r *Repository) Backend() restic.Backend
Backend returns the backend for the repository.
func (*Repository) Close ¶
func (r *Repository) Close() error
Close closes the repository by closing the backend.
func (*Repository) Config ¶
func (r *Repository) Config() restic.Config
Config returns the repository configuration.
func (*Repository) Delete ¶
func (r *Repository) Delete(ctx context.Context) error
Delete calls backend.Delete() if implemented, and returns an error otherwise.
func (*Repository) Encrypt ¶
func (r *Repository) Encrypt(ciphertext, plaintext []byte) ([]byte, error)
Encrypt encrypts and authenticates the plaintext and saves the result in ciphertext.
func (*Repository) Index ¶
func (r *Repository) Index() restic.Index
Index returns the currently used MasterIndex.
func (*Repository) Init ¶
func (r *Repository) Init(ctx context.Context, password string) error
Init creates a new master key with the supplied password, initializes and saves the repository config.
func (*Repository) KeyName ¶
func (r *Repository) KeyName() string
KeyName returns the name of the current key in the backend.
func (*Repository) ListPack ¶
ListPack returns the list of blobs saved in the pack id and the length of the file as stored in the backend.
func (*Repository) LoadAndDecrypt ¶
func (r *Repository) LoadAndDecrypt(ctx context.Context, t restic.FileType, id restic.ID) ([]byte, error)
LoadAndDecrypt loads and decrypts data identified by t and id from the backend.
func (*Repository) LoadBlob ¶
func (r *Repository) LoadBlob(ctx context.Context, t restic.BlobType, id restic.ID, buf []byte) (int, error)
LoadBlob loads a blob of type t from the repository to the buffer. buf must be large enough to hold the encrypted blob, since it is used as scratch space.
func (*Repository) LoadIndex ¶
func (r *Repository) LoadIndex(ctx context.Context) error
LoadIndex loads all index files from the backend in parallel and stores them in the master index. The first error that occurred is returned.
func (*Repository) LoadJSONUnpacked ¶
func (r *Repository) LoadJSONUnpacked(ctx context.Context, t restic.FileType, id restic.ID, item interface{}) (err error)
LoadJSONUnpacked decrypts the data and afterwards calls json.Unmarshal on the item.
func (*Repository) LookupBlobSize ¶
LookupBlobSize returns the size of blob id.
func (*Repository) PrefixLength ¶
func (r *Repository) PrefixLength(t restic.FileType) (int, error)
PrefixLength returns the number of bytes required so that all prefixes of all IDs of type t are unique.
func (*Repository) SaveAndEncrypt ¶
func (r *Repository) SaveAndEncrypt(ctx context.Context, t restic.BlobType, data []byte, id *restic.ID) (restic.ID, error)
SaveAndEncrypt encrypts data and stores it to the backend as type t. If data is small enough, it will be packed together with other small blobs.
func (*Repository) SaveBlob ¶ added in v0.3.0
func (r *Repository) SaveBlob(ctx context.Context, t restic.BlobType, buf []byte, id restic.ID) (restic.ID, error)
SaveBlob saves a blob of type t into the repository. If id is the null id, it will be computed and returned.
func (*Repository) SaveFullIndex ¶
func (r *Repository) SaveFullIndex(ctx context.Context) error
SaveFullIndex saves all full indexes in the backend.
func (*Repository) SaveIndex ¶
func (r *Repository) SaveIndex(ctx context.Context) error
SaveIndex saves all new indexes in the backend.
func (*Repository) SaveJSONUnpacked ¶
func (r *Repository) SaveJSONUnpacked(ctx context.Context, t restic.FileType, item interface{}) (restic.ID, error)
SaveJSONUnpacked serialises item as JSON and encrypts and saves it in the backend as type t, without a pack. It returns the storage hash.
func (*Repository) SaveTree ¶ added in v0.3.0
SaveTree stores a tree into the repository and returns the ID. The ID is checked against the index. The tree is only stored when the index does not contain the ID.
func (*Repository) SaveUnpacked ¶
func (r *Repository) SaveUnpacked(ctx context.Context, t restic.FileType, p []byte) (id restic.ID, err error)
SaveUnpacked encrypts data and stores it in the backend. Returned is the storage hash.
func (*Repository) SearchKey ¶
SearchKey finds a key with the supplied password, afterwards the config is read and parsed. It tries at most maxKeys key files in the repo.
func (*Repository) SetIndex ¶
func (r *Repository) SetIndex(i restic.Index)
SetIndex instructs the repository to use the given index.