Commit 3654e8d1 authored by ale's avatar ale

Allow to optionally set cache dirs for restic

Cache directories are sharded by source name, enabling re-use.
parent d19fa308
Pipeline #4679 failed with stages
in 6 minutes and 1 second
......@@ -26,6 +26,7 @@ type resticRepository struct {
excludes []string
excludeFiles []string
autoPrune bool
cacheDirPath string
initialized sync.Once
}
......@@ -96,6 +97,11 @@ func newResticRepository(params map[string]interface{}) (Repository, error) {
autoPrune = b
}
var cacheDir string
if s, ok := params["cache_dir"].(string); ok {
cacheDir = s
}
tmpf, err := ioutil.TempFile("", "restic-pw-")
if err != nil {
return nil, err
......@@ -116,6 +122,7 @@ func newResticRepository(params map[string]interface{}) (Repository, error) {
excludes: ex,
excludeFiles: exf,
autoPrune: autoPrune,
cacheDirPath: cacheDir,
}, nil
}
......@@ -152,10 +159,22 @@ func resticBackupTags(backup *Backup, ds *Dataset) string {
return fmt.Sprintf("--tag dataset_id=%s --tag backup_id=%s", ds.ID, backup.ID)
}
func (r *resticRepository) cacheArgs(ds *Dataset) string {
if r.cacheDirPath == "" {
return "--no-cache"
}
dir := filepath.Join(r.cacheDirPath, strings.Replace(ds.Source, "/", "_", -1))
if _, err := os.Stat(dir); os.IsNotExist(err) {
os.MkdirAll(dir, 0700)
}
return fmt.Sprintf("--cache-dir %s", dir)
}
func (r *resticRepository) BackupCmd(backup *Backup, ds *Dataset, inputFile string) string {
return fmt.Sprintf(
"%s backup --no-cache --exclude-caches --one-file-system %s --files-from %s",
"%s backup %s --exclude-caches --one-file-system %s --files-from %s",
r.resticCmd(),
r.cacheArgs(ds),
resticBackupTags(backup, ds),
inputFile,
)
......@@ -210,8 +229,9 @@ func (r *resticRepository) BackupStreamCmd(backup *Backup, ds *Dataset) string {
fakePath := datasetStdinPath(ds)
return fmt.Sprintf(
// The --force prevents restic from trying to find a previous snapshot.
"%s backup --no-cache %s --force --stdin --stdin-filename %s",
"%s backup %s %s --force --stdin --stdin-filename %s",
r.resticCmd(),
r.cacheArgs(ds),
resticBackupTags(backup, ds),
fakePath,
)
......
......@@ -42,7 +42,7 @@ func (p Params) GetBool(key string) (bool, bool) {
// operation. Backups are initiated autonomously by individual hosts,
// so each Backup belongs to a single Host.
type Backup struct {
// Backup ID (globally unique identifier).
// Unique identifier.
ID string `json:"id"`
// Timestamp (backup start).
......@@ -75,9 +75,7 @@ type Atom struct {
// databases (the atom we're interested in), which we might want to
// restore independently.
type Dataset struct {
// Name of the dataset (path-like). Will be prepended to atom
// paths.
//Name string `json:"name"`
// Unique identifier.
ID string `json:"id"`
// Source is the name of the source that created this Dataset,
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment