From 2db06f0ef85eed298d4010e33e80ecfcfd48b92f Mon Sep 17 00:00:00 2001 From: Aliaksandr Valialkin Date: Tue, 19 Nov 2019 18:35:31 +0200 Subject: [PATCH] lib/backup: prevent from restoring to directory which is in use by VictoriaMetrics during the restore --- lib/backup/actions/restore.go | 11 +++++++++++ lib/backup/fscommon/fscommon.go | 5 +++++ 2 files changed, 16 insertions(+) diff --git a/lib/backup/actions/restore.go b/lib/backup/actions/restore.go index 6422ebc5f..e310993b6 100644 --- a/lib/backup/actions/restore.go +++ b/lib/backup/actions/restore.go @@ -8,6 +8,7 @@ import ( "github.com/VictoriaMetrics/VictoriaMetrics/lib/backup/common" "github.com/VictoriaMetrics/VictoriaMetrics/lib/backup/fslocal" + "github.com/VictoriaMetrics/VictoriaMetrics/lib/fs" "github.com/VictoriaMetrics/VictoriaMetrics/lib/logger" ) @@ -34,6 +35,16 @@ type Restore struct { func (r *Restore) Run() error { startTime := time.Now() + // Make sure VictoriaMetrics doesn't run during the restore process. + if err := fs.MkdirAllIfNotExist(r.Dst.Dir); err != nil { + return fmt.Errorf("cannot create dir %q: %s", r.Dst.Dir, err) + } + flockF, err := fs.CreateFlockFile(r.Dst.Dir) + if err != nil { + return fmt.Errorf("cannot create lock file in %q; make sure VictoriaMetrics doesn't use the dir; error: %s", r.Dst.Dir, err) + } + defer fs.MustClose(flockF) + concurrency := r.Concurrency src := r.Src dst := r.Dst diff --git a/lib/backup/fscommon/fscommon.go b/lib/backup/fscommon/fscommon.go index c01822515..157c9a31e 100644 --- a/lib/backup/fscommon/fscommon.go +++ b/lib/backup/fscommon/fscommon.go @@ -72,6 +72,11 @@ func appendFilesInternal(dst []string, d *os.File) ([]string, error) { if name == "." || name == ".." { continue } + if name == "flock.lock" { + // Do not take into account flock.lock files, since they are used + // for preventing from concurrent access. + continue + } path := dir + "/" + name if fi.IsDir() { // Process directory