lib/backup: prevent from restoring to directory which is in use by VictoriaMetrics during the restore

This commit is contained in:
Aliaksandr Valialkin 2019-11-19 18:35:31 +02:00
parent 0094bc4fc9
commit 2db06f0ef8
2 changed files with 16 additions and 0 deletions

View file

@ -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

View file

@ -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