Documentation ¶
Overview ¶
Package archiver contains the code which reads files, splits them into chunks and saves the data to the repository.
An Archiver has a number of worker goroutines handling saving the different data structures to the repository, the details are implemented by the FileSaver, BlobSaver, and TreeSaver types.
The main goroutine (the one calling Snapshot()) traverses the directory tree and delegates all work to these worker pools. They return a type (FutureFile, FutureBlob, and FutureTree) which can be resolved later, by calling Wait() on it.
Index ¶
- Constants
- func TestCreateFiles(t testing.TB, target string, dir TestDir)
- func TestEnsureFileContent(ctx context.Context, t testing.TB, repo restic.Repository, filename string, ...)
- func TestEnsureFiles(t testing.TB, target string, dir TestDir)
- func TestEnsureSnapshot(t testing.TB, repo restic.Repository, snapshotID restic.ID, dir TestDir)
- func TestEnsureTree(ctx context.Context, t testing.TB, prefix string, repo restic.Repository, ...)
- func TestSnapshot(t testing.TB, repo restic.Repository, path string, parent *restic.ID) *restic.Snapshot
- func TestWalkFiles(t testing.TB, target string, dir TestDir, fn TestWalkFunc)
- type Archiver
- func (arch *Archiver) Save(ctx context.Context, snPath, target string, previous *restic.Node) (fn FutureNode, excluded bool, err error)
- func (arch *Archiver) SaveDir(ctx context.Context, snPath string, dir string, fi os.FileInfo, ...) (d FutureNode, err error)
- func (arch *Archiver) SaveTree(ctx context.Context, snPath string, atree *Tree, previous *restic.Tree, ...) (FutureNode, int, error)
- func (arch *Archiver) Snapshot(ctx context.Context, targets []string, opts SnapshotOptions) (*restic.Snapshot, restic.ID, error)
- type BlobSaver
- type Buffer
- type BufferPool
- type CompleteFunc
- type ErrorFunc
- type FileSaver
- type FutureNode
- type ItemStats
- type Options
- type SaveBlobFn
- type SaveBlobResponse
- type Saver
- type ScanStats
- type Scanner
- type SelectByNameFunc
- type SelectFunc
- type SnapshotOptions
- type TestDir
- type TestFile
- type TestSymlink
- type TestWalkFunc
- type Tree
- type TreeSaver
Constants ¶
const ( ChangeIgnoreCtime = 1 << iota ChangeIgnoreInode )
Flags for the ChangeIgnoreFlags bitfield.
Variables ¶
This section is empty.
Functions ¶
func TestCreateFiles ¶
TestCreateFiles creates a directory structure described by dir at target, which must already exist. On Windows, symlinks aren't created.
func TestEnsureFileContent ¶
func TestEnsureFileContent(ctx context.Context, t testing.TB, repo restic.Repository, filename string, node *restic.Node, file TestFile)
TestEnsureFileContent checks if the file in the repo is the same as file.
func TestEnsureFiles ¶
TestEnsureFiles tests if the directory structure at target is the same as described in dir.
func TestEnsureSnapshot ¶
TestEnsureSnapshot tests if the snapshot in the repo has exactly the same structure as dir. On Windows, Symlinks are ignored.
func TestEnsureTree ¶
func TestEnsureTree(ctx context.Context, t testing.TB, prefix string, repo restic.Repository, treeID restic.ID, dir TestDir)
TestEnsureTree checks that the tree ID in the repo matches dir. On Windows, Symlinks are ignored.
func TestSnapshot ¶
func TestSnapshot(t testing.TB, repo restic.Repository, path string, parent *restic.ID) *restic.Snapshot
TestSnapshot creates a new snapshot of path.
func TestWalkFiles ¶
func TestWalkFiles(t testing.TB, target string, dir TestDir, fn TestWalkFunc)
TestWalkFiles runs fn for each file/directory in dir, the filename will be constructed with target as the prefix. Symlinks on Windows are ignored.
Types ¶
type Archiver ¶
type Archiver struct { Repo restic.Repository SelectByName SelectByNameFunc Select SelectFunc FS fs.FS Options Options // Error is called for all errors that occur during backup. Error ErrorFunc // CompleteItem is called for all files and dirs once they have been // processed successfully. The parameter item contains the path as it will // be in the snapshot after saving. s contains some statistics about this // particular file/dir. // // Once reading a file has completed successfully (but not saving it yet), // CompleteItem will be called with current == nil. // // CompleteItem may be called asynchronously from several different // goroutines! CompleteItem func(item string, previous, current *restic.Node, s ItemStats, d time.Duration) // StartFile is called when a file is being processed by a worker. StartFile func(filename string) // CompleteBlob is called for all saved blobs for files. CompleteBlob func(bytes uint64) // WithAtime configures if the access time for files and directories should // be saved. Enabling it may result in much metadata, so it's off by // default. WithAtime bool // Flags controlling change detection. See doc/040_backup.rst for details. ChangeIgnoreFlags uint // contains filtered or unexported fields }
Archiver saves a directory structure to the repo.
func (*Archiver) Save ¶
func (arch *Archiver) Save(ctx context.Context, snPath, target string, previous *restic.Node) (fn FutureNode, excluded bool, err error)
Save saves a target (file or directory) to the repo. If the item is excluded, this function returns a nil node and error, with excluded set to true.
Errors and completion needs to be handled by the caller.
snPath is the path within the current snapshot.
func (*Archiver) SaveDir ¶
func (arch *Archiver) SaveDir(ctx context.Context, snPath string, dir string, fi os.FileInfo, previous *restic.Tree, complete CompleteFunc) (d FutureNode, err error)
SaveDir stores a directory in the repo and returns the node. snPath is the path within the current snapshot.
type BlobSaver ¶
type BlobSaver struct {
// contains filtered or unexported fields
}
BlobSaver concurrently saves incoming blobs to the repo.
func NewBlobSaver ¶
NewBlobSaver returns a new blob. A worker pool is started, it is stopped when ctx is cancelled.
func (*BlobSaver) Save ¶
func (s *BlobSaver) Save(ctx context.Context, t restic.BlobType, buf *Buffer, cb func(res SaveBlobResponse))
Save stores a blob in the repo. It checks the index and the known blobs before saving anything. It takes ownership of the buffer passed in.
func (*BlobSaver) TriggerShutdown ¶
func (s *BlobSaver) TriggerShutdown()
type Buffer ¶
type Buffer struct { Data []byte // contains filtered or unexported fields }
Buffer is a reusable buffer. After the buffer has been used, Release should be called so the underlying slice is put back into the pool.
type BufferPool ¶
type BufferPool struct {
// contains filtered or unexported fields
}
BufferPool implements a limited set of reusable buffers.
func NewBufferPool ¶
func NewBufferPool(max int, defaultSize int) *BufferPool
NewBufferPool initializes a new buffer pool. The pool stores at most max items. New buffers are created with defaultSize. Buffers that have grown larger are not put back.
func (*BufferPool) Get ¶
func (pool *BufferPool) Get() *Buffer
Get returns a new buffer, either from the pool or newly allocated.
type CompleteFunc ¶
CompleteFunc is called when the file has been saved.
type ErrorFunc ¶
ErrorFunc is called when an error during archiving occurs. When nil is returned, the archiver continues, otherwise it aborts and passes the error up the call stack.
type FileSaver ¶
type FileSaver struct { CompleteBlob func(bytes uint64) NodeFromFileInfo func(snPath, filename string, fi os.FileInfo) (*restic.Node, error) // contains filtered or unexported fields }
FileSaver concurrently saves incoming files to the repo.
func NewFileSaver ¶
func NewFileSaver(ctx context.Context, wg *errgroup.Group, save SaveBlobFn, pol chunker.Pol, fileWorkers, blobWorkers uint) *FileSaver
NewFileSaver returns a new file saver. A worker pool with fileWorkers is started, it is stopped when ctx is cancelled.
func (*FileSaver) Save ¶
func (s *FileSaver) Save(ctx context.Context, snPath string, target string, file fs.File, fi os.FileInfo, start func(), completeReading func(), complete CompleteFunc) FutureNode
Save stores the file f and returns the data once it has been completed. The file is closed by Save. completeReading is only called if the file was read successfully. complete is always called. If completeReading is called, then this will always happen before calling complete.
func (*FileSaver) TriggerShutdown ¶
func (s *FileSaver) TriggerShutdown()
type FutureNode ¶
type FutureNode struct {
// contains filtered or unexported fields
}
FutureNode holds a reference to a channel that returns a FutureNodeResult or a reference to an already existing result. If the result is available immediatelly, then storing a reference directly requires less memory than using the indirection via a channel.
type ItemStats ¶
type ItemStats struct { DataBlobs int // number of new data blobs added for this item DataSize uint64 // sum of the sizes of all new data blobs DataSizeInRepo uint64 // sum of the bytes added to the repo (including compression and crypto overhead) TreeBlobs int // number of new tree blobs added for this item TreeSize uint64 // sum of the sizes of all new tree blobs TreeSizeInRepo uint64 // sum of the bytes added to the repo (including compression and crypto overhead) }
ItemStats collects some statistics about a particular file or directory.
type Options ¶
type Options struct { // ReadConcurrency sets how many files are read in concurrently. If // it's set to zero, at most two files are read in concurrently (which // turned out to be a good default for most situations). ReadConcurrency uint // SaveBlobConcurrency sets how many blobs are hashed and saved // concurrently. If it's set to zero, the default is the number of CPUs // available in the system. SaveBlobConcurrency uint // SaveTreeConcurrency sets how many trees are marshalled and saved to the // repo concurrently. SaveTreeConcurrency uint }
Options is used to configure the archiver.
func (Options) ApplyDefaults ¶
ApplyDefaults returns a copy of o with the default options set for all unset fields.
type SaveBlobFn ¶
SaveBlobFn saves a blob to a repo.
type SaveBlobResponse ¶
type SaveBlobResponse struct {
// contains filtered or unexported fields
}
type Saver ¶
type Saver interface {
SaveBlob(ctx context.Context, t restic.BlobType, data []byte, id restic.ID, storeDuplicate bool) (restic.ID, bool, int, error)
}
Saver allows saving a blob.
type Scanner ¶
type Scanner struct { FS fs.FS SelectByName SelectByNameFunc Select SelectFunc Error ErrorFunc Result func(item string, s ScanStats) }
Scanner traverses the targets and calls the function Result with cumulated stats concerning the files and folders found. Select is used to decide which items should be included. Error is called when an error occurs.
type SelectByNameFunc ¶
SelectByNameFunc returns true for all items that should be included (files and dirs). If false is returned, files are ignored and dirs are not even walked.
type SelectFunc ¶
SelectFunc returns true for all items that should be included (files and dirs). If false is returned, files are ignored and dirs are not even walked.
type SnapshotOptions ¶
type SnapshotOptions struct { Tags restic.TagList Hostname string Excludes []string Time time.Time ParentSnapshot *restic.Snapshot ProgramVersion string }
SnapshotOptions collect attributes for a new snapshot.
type TestDir ¶
type TestDir map[string]interface{}
TestDir describes a directory structure to create for a test.
type TestFile ¶
type TestFile struct {
Content string
}
TestFile describes a file created for a test.
type TestSymlink ¶
type TestSymlink struct {
Target string
}
TestSymlink describes a symlink created for a test.
func (TestSymlink) String ¶
func (s TestSymlink) String() string
type TestWalkFunc ¶
TestWalkFunc is used by TestWalkFiles to traverse the dir. When an error is returned, traversal stops and the surrounding test is marked as failed.
type Tree ¶
type Tree struct { Nodes map[string]Tree Path string // where the files/dirs to be saved are found FileInfoPath string // where the dir can be found that is not included itself, but its subdirs Root string // parent directory of the tree }
Tree recursively defines how a snapshot should look like when archived.
When `Path` is set, this is a leaf node and the contents of `Path` should be inserted at this point in the tree.
The attribute `Root` is used to distinguish between files/dirs which have the same name, but live in a separate directory on the local file system.
`FileInfoPath` is used to extract metadata for intermediate (=non-leaf) trees.
func (Tree) Leaf ¶
Leaf returns true if this is a leaf node, which means Path is set to a non-empty string and the contents of Path should be inserted at this point in the tree.
type TreeSaver ¶
type TreeSaver struct {
// contains filtered or unexported fields
}
TreeSaver concurrently saves incoming trees to the repo.
func NewTreeSaver ¶
func NewTreeSaver(ctx context.Context, wg *errgroup.Group, treeWorkers uint, saveBlob func(ctx context.Context, t restic.BlobType, buf *Buffer, cb func(res SaveBlobResponse)), errFn ErrorFunc) *TreeSaver
NewTreeSaver returns a new tree saver. A worker pool with treeWorkers is started, it is stopped when ctx is cancelled.
func (*TreeSaver) Save ¶
func (s *TreeSaver) Save(ctx context.Context, snPath string, target string, node *restic.Node, nodes []FutureNode, complete CompleteFunc) FutureNode
Save stores the dir d and returns the data once it has been completed.
func (*TreeSaver) TriggerShutdown ¶
func (s *TreeSaver) TriggerShutdown()