2019-11-07 19:05:39 +00:00
package main
import (
"flag"
"fmt"
2020-05-16 08:59:30 +00:00
"os"
2019-11-07 19:05:39 +00:00
"github.com/VictoriaMetrics/VictoriaMetrics/lib/backup/actions"
"github.com/VictoriaMetrics/VictoriaMetrics/lib/backup/common"
"github.com/VictoriaMetrics/VictoriaMetrics/lib/backup/fslocal"
"github.com/VictoriaMetrics/VictoriaMetrics/lib/buildinfo"
2020-02-10 11:26:18 +00:00
"github.com/VictoriaMetrics/VictoriaMetrics/lib/envflag"
2020-08-16 14:05:52 +00:00
"github.com/VictoriaMetrics/VictoriaMetrics/lib/flagutil"
2019-11-07 19:05:39 +00:00
"github.com/VictoriaMetrics/VictoriaMetrics/lib/logger"
)
var (
src = flag . String ( "src" , "" , "Source path with backup on the remote storage. " +
2021-09-29 09:10:26 +00:00
"Example: gs://bucket/path/to/backup/dir, s3://bucket/path/to/backup/dir or fs:///path/to/local/backup/dir" )
2019-11-07 19:05:39 +00:00
storageDataPath = flag . String ( "storageDataPath" , "victoria-metrics-data" , "Destination path where backup must be restored. " +
2020-05-16 06:02:27 +00:00
"VictoriaMetrics must be stopped when restoring from backup. -storageDataPath dir can be non-empty. In this case the contents of -storageDataPath dir " +
"is synchronized with -src contents, i.e. it works like 'rsync --delete'" )
2020-01-09 13:24:26 +00:00
concurrency = flag . Int ( "concurrency" , 10 , "The number of concurrent workers. Higher concurrency may reduce restore duration" )
2020-08-16 14:05:52 +00:00
maxBytesPerSecond = flagutil . NewBytes ( "maxBytesPerSecond" , 0 , "The maximum download speed. There is no limit if it is set to 0" )
2020-02-04 13:46:13 +00:00
skipBackupCompleteCheck = flag . Bool ( "skipBackupCompleteCheck" , false , "Whether to skip checking for 'backup complete' file in -src. This may be useful for restoring from old backups, which were created without 'backup complete' file" )
2019-11-07 19:05:39 +00:00
)
func main ( ) {
2020-05-16 08:59:30 +00:00
// Write flags and help message to stdout, since it is easier to grep or pipe.
flag . CommandLine . SetOutput ( os . Stdout )
2019-11-07 19:05:39 +00:00
flag . Usage = usage
2020-02-10 11:26:18 +00:00
envflag . Parse ( )
2019-11-07 19:05:39 +00:00
buildinfo . Init ( )
2020-08-11 19:54:13 +00:00
logger . Init ( )
2019-11-07 19:05:39 +00:00
srcFS , err := newSrcFS ( )
if err != nil {
logger . Fatalf ( "%s" , err )
}
dstFS , err := newDstFS ( )
if err != nil {
logger . Fatalf ( "%s" , err )
}
a := & actions . Restore {
2020-01-09 13:24:26 +00:00
Concurrency : * concurrency ,
Src : srcFS ,
Dst : dstFS ,
SkipBackupCompleteCheck : * skipBackupCompleteCheck ,
2019-11-07 19:05:39 +00:00
}
if err := a . Run ( ) ; err != nil {
logger . Fatalf ( "cannot restore from backup: %s" , err )
}
2020-10-09 12:31:39 +00:00
srcFS . MustStop ( )
2020-10-09 12:11:28 +00:00
dstFS . MustStop ( )
2019-11-07 19:05:39 +00:00
}
func usage ( ) {
const s = `
vmrestore restores VictoriaMetrics data from backups made by vmbackup .
2021-04-20 17:16:17 +00:00
See the docs at https : //docs.victoriametrics.com/vmrestore.html .
2019-11-07 19:05:39 +00:00
`
2020-12-03 19:40:30 +00:00
flagutil . Usage ( s )
2019-11-07 19:05:39 +00:00
}
func newDstFS ( ) ( * fslocal . FS , error ) {
if len ( * storageDataPath ) == 0 {
return nil , fmt . Errorf ( "`-storageDataPath` cannot be empty" )
}
fs := & fslocal . FS {
2019-11-19 18:31:52 +00:00
Dir : * storageDataPath ,
2020-08-16 14:05:52 +00:00
MaxBytesPerSecond : maxBytesPerSecond . N ,
2019-11-19 18:31:52 +00:00
}
if err := fs . Init ( ) ; err != nil {
2020-06-30 19:58:18 +00:00
return nil , fmt . Errorf ( "cannot initialize local fs: %w" , err )
2019-11-07 19:05:39 +00:00
}
return fs , nil
}
func newSrcFS ( ) ( common . RemoteFS , error ) {
fs , err := actions . NewRemoteFS ( * src )
if err != nil {
2020-06-30 19:58:18 +00:00
return nil , fmt . Errorf ( "cannot parse `-src`=%q: %w" , * src , err )
2019-11-07 19:05:39 +00:00
}
return fs , nil
}