2019-11-07 19:05:39 +00:00
package main
import (
"flag"
"fmt"
"os"
2020-04-24 19:35:03 +00:00
"strings"
2021-12-02 09:55:58 +00:00
"time"
2019-11-07 19:05:39 +00:00
2020-04-24 19:35:03 +00:00
"github.com/VictoriaMetrics/VictoriaMetrics/app/vmbackup/snapshot"
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"
2020-10-28 18:09:10 +00:00
"github.com/VictoriaMetrics/VictoriaMetrics/lib/backup/fsnil"
2019-11-07 19:05:39 +00:00
"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"
2021-12-02 09:55:58 +00:00
"github.com/VictoriaMetrics/VictoriaMetrics/lib/httpserver"
2019-11-07 19:05:39 +00:00
"github.com/VictoriaMetrics/VictoriaMetrics/lib/logger"
)
var (
2021-12-02 09:55:58 +00:00
httpListenAddr = flag . String ( "httpListenAddr" , ":8420" , "TCP address for exporting metrics at /metrics page" )
2020-04-24 19:35:03 +00:00
storageDataPath = flag . String ( "storageDataPath" , "victoria-metrics-data" , "Path to VictoriaMetrics data. Must match -storageDataPath from VictoriaMetrics or vmstorage" )
2021-05-07 05:40:30 +00:00
snapshotName = flag . String ( "snapshotName" , "" , "Name for the snapshot to backup. See https://docs.victoriametrics.com/Single-server-VictoriaMetrics.html#how-to-work-with-snapshots. There is no need in setting -snapshotName if -snapshot.createURL is set" )
2020-09-09 18:10:34 +00:00
snapshotCreateURL = flag . String ( "snapshot.createURL" , "" , "VictoriaMetrics create snapshot url. When this is given a snapshot will automatically be created during backup. " +
2021-05-07 05:40:30 +00:00
"Example: http://victoriametrics:8428/snapshot/create . There is no need in setting -snapshotName if -snapshot.createURL is set" )
2020-09-09 18:10:34 +00:00
snapshotDeleteURL = flag . String ( "snapshot.deleteURL" , "" , "VictoriaMetrics delete snapshot url. Optional. Will be generated from -snapshot.createURL if not provided. " +
2021-05-17 22:12:34 +00:00
"All created snapshots will be automatically deleted. Example: http://victoriametrics:8428/snapshot/delete" )
2020-04-24 19:35:03 +00:00
dst = flag . String ( "dst" , "" , "Where to put the 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\n" +
2019-11-07 19:05:39 +00:00
"-dst can point to the previous backup. In this case incremental backup is performed, i.e. only changed data is uploaded" )
2019-11-19 18:31:52 +00:00
origin = flag . String ( "origin" , "" , "Optional origin directory on the remote storage with old backup for server-side copying when performing full backup. This speeds up full backups" )
concurrency = flag . Int ( "concurrency" , 10 , "The number of concurrent workers. Higher concurrency may reduce backup duration" )
2020-08-16 14:05:52 +00:00
maxBytesPerSecond = flagutil . NewBytes ( "maxBytesPerSecond" , 0 , "The maximum upload speed. There is no limit if it is set to 0" )
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
2020-04-24 19:35:03 +00:00
if len ( * snapshotCreateURL ) > 0 {
2021-05-07 05:40:30 +00:00
if len ( * snapshotName ) > 0 {
logger . Fatalf ( "-snapshotName shouldn't be set if -snapshot.createURL is set, since snapshots are created automatically in this case" )
}
2020-04-24 19:35:03 +00:00
logger . Infof ( "Snapshot create url %s" , * snapshotCreateURL )
if len ( * snapshotDeleteURL ) <= 0 {
err := flag . Set ( "snapshot.deleteURL" , strings . Replace ( * snapshotCreateURL , "/create" , "/delete" , 1 ) )
if err != nil {
logger . Fatalf ( "Failed to set snapshot.deleteURL flag: %v" , err )
}
}
logger . Infof ( "Snapshot delete url %s" , * snapshotDeleteURL )
name , err := snapshot . Create ( * snapshotCreateURL )
if err != nil {
2020-11-23 15:09:59 +00:00
logger . Fatalf ( "cannot create snapshot: %s" , err )
2020-04-24 19:35:03 +00:00
}
err = flag . Set ( "snapshotName" , name )
if err != nil {
2020-11-23 15:09:59 +00:00
logger . Fatalf ( "cannot set snapshotName flag: %v" , err )
2020-04-24 19:35:03 +00:00
}
defer func ( ) {
err := snapshot . Delete ( * snapshotDeleteURL , name )
if err != nil {
2020-11-23 15:09:59 +00:00
logger . Fatalf ( "cannot delete snapshot: %s" , err )
2020-04-24 19:35:03 +00:00
}
} ( )
}
2021-12-02 09:55:58 +00:00
logger . Infof ( "starting http server for exporting metrics at http://%q/metrics" , * httpListenAddr )
go httpserver . Serve ( * httpListenAddr , nil )
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 )
}
originFS , err := newOriginFS ( )
if err != nil {
logger . Fatalf ( "%s" , err )
}
a := & actions . Backup {
Concurrency : * concurrency ,
Src : srcFS ,
Dst : dstFS ,
Origin : originFS ,
}
if err := a . Run ( ) ; err != nil {
logger . Fatalf ( "cannot create backup: %s" , err )
}
2020-10-09 12:11:28 +00:00
srcFS . MustStop ( )
2020-10-09 12:31:39 +00:00
dstFS . MustStop ( )
originFS . MustStop ( )
2021-12-02 09:55:58 +00:00
startTime := time . Now ( )
logger . Infof ( "gracefully shutting down http server for metrics at %q" , * httpListenAddr )
if err := httpserver . Stop ( * httpListenAddr ) ; err != nil {
logger . Fatalf ( "cannot stop http server for metrics: %s" , err )
}
logger . Infof ( "successfully shut down http server for metrics in %.3f seconds" , time . Since ( startTime ) . Seconds ( ) )
2019-11-07 19:05:39 +00:00
}
func usage ( ) {
const s = `
vmbackup performs backups for VictoriaMetrics data from instant snapshots to gcs , s3
or local filesystem . Backed up data can be restored with vmrestore .
2021-05-18 13:37:28 +00:00
See the docs at https : //docs.victoriametrics.com/vmbackup.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 newSrcFS ( ) ( * fslocal . FS , error ) {
if len ( * snapshotName ) == 0 {
2020-04-24 19:35:03 +00:00
return nil , fmt . Errorf ( "`-snapshotName` or `-snapshot.createURL` must be provided" )
2019-11-07 19:05:39 +00:00
}
snapshotPath := * storageDataPath + "/snapshots/" + * snapshotName
// Verify the snapshot exists.
f , err := os . Open ( snapshotPath )
if err != nil {
2020-06-30 19:58:18 +00:00
return nil , fmt . Errorf ( "cannot open snapshot at %q: %w" , snapshotPath , err )
2019-11-07 19:05:39 +00:00
}
fi , err := f . Stat ( )
_ = f . Close ( )
if err != nil {
2020-06-30 19:58:18 +00:00
return nil , fmt . Errorf ( "cannot stat %q: %w" , snapshotPath , err )
2019-11-07 19:05:39 +00:00
}
if ! fi . IsDir ( ) {
return nil , fmt . Errorf ( "snapshot %q must be a directory" , snapshotPath )
}
fs := & fslocal . FS {
2019-11-19 18:31:52 +00:00
Dir : snapshotPath ,
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 fs: %w" , err )
2019-11-07 19:05:39 +00:00
}
return fs , nil
}
func newDstFS ( ) ( common . RemoteFS , error ) {
fs , err := actions . NewRemoteFS ( * dst )
if err != nil {
2020-06-30 19:58:18 +00:00
return nil , fmt . Errorf ( "cannot parse `-dst`=%q: %w" , * dst , err )
2019-11-07 19:05:39 +00:00
}
return fs , nil
}
2020-10-28 18:09:10 +00:00
func newOriginFS ( ) ( common . OriginFS , error ) {
2019-11-07 19:05:39 +00:00
if len ( * origin ) == 0 {
2020-10-28 18:09:10 +00:00
return & fsnil . FS { } , nil
2019-11-07 19:05:39 +00:00
}
fs , err := actions . NewRemoteFS ( * origin )
if err != nil {
2020-06-30 19:58:18 +00:00
return nil , fmt . Errorf ( "cannot parse `-origin`=%q: %w" , * origin , err )
2019-11-07 19:05:39 +00:00
}
return fs , nil
}