2019-11-07 20:05:39 +01:00
package main
import (
"flag"
"fmt"
2024-04-24 13:47:57 +02:00
"net/url"
2019-11-07 20:05:39 +01:00
"os"
2022-05-06 17:04:09 +02:00
"path/filepath"
2020-04-24 21:35:03 +02:00
"strings"
2021-12-02 10:55:58 +01:00
"time"
2019-11-07 20:05:39 +01: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 19:09:10 +01:00
"github.com/VictoriaMetrics/VictoriaMetrics/lib/backup/fsnil"
2019-11-07 20:05:39 +01:00
"github.com/VictoriaMetrics/VictoriaMetrics/lib/buildinfo"
2020-02-10 12:26:18 +01:00
"github.com/VictoriaMetrics/VictoriaMetrics/lib/envflag"
2020-08-16 16:05:52 +02:00
"github.com/VictoriaMetrics/VictoriaMetrics/lib/flagutil"
2021-12-02 10:55:58 +01:00
"github.com/VictoriaMetrics/VictoriaMetrics/lib/httpserver"
2019-11-07 20:05:39 +01:00
"github.com/VictoriaMetrics/VictoriaMetrics/lib/logger"
2022-07-21 18:58:22 +02:00
"github.com/VictoriaMetrics/VictoriaMetrics/lib/pushmetrics"
2022-05-04 21:12:03 +02:00
"github.com/VictoriaMetrics/VictoriaMetrics/lib/snapshot"
2024-02-09 03:03:20 +01:00
"github.com/VictoriaMetrics/VictoriaMetrics/lib/snapshot/snapshotutil"
2019-11-07 20:05:39 +01:00
)
var (
2021-12-02 10:55:58 +01:00
httpListenAddr = flag . String ( "httpListenAddr" , ":8420" , "TCP address for exporting metrics at /metrics page" )
2020-04-24 21:35:03 +02:00
storageDataPath = flag . String ( "storageDataPath" , "victoria-metrics-data" , "Path to VictoriaMetrics data. Must match -storageDataPath from VictoriaMetrics or vmstorage" )
2024-04-18 03:11:03 +02:00
snapshotName = flag . String ( "snapshotName" , "" , "Name for the snapshot to backup. See https://docs.victoriametrics.com/single-server-victoriametrics/#how-to-work-with-snapshots. There is no need in setting -snapshotName if -snapshot.createURL is set" )
2024-04-24 13:47:57 +02: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 07:40:30 +02:00
"Example: http://victoriametrics:8428/snapshot/create . There is no need in setting -snapshotName if -snapshot.createURL is set" )
2024-04-24 13:47:57 +02:00
snapshotDeleteURL = flag . String ( "snapshot.deleteURL" , "" , "VictoriaMetrics delete snapshot url. Optional. Will be generated from -snapshot.createURL if not provided. " +
2021-05-18 00:12:34 +02:00
"All created snapshots will be automatically deleted. Example: http://victoriametrics:8428/snapshot/delete" )
2020-04-24 21:35:03 +02:00
dst = flag . String ( "dst" , "" , "Where to put the backup on the remote storage. " +
2022-12-12 20:17:22 +01:00
"Example: gs://bucket/path/to/backup, s3://bucket/path/to/backup, azblob://container/path/to/backup or fs:///path/to/local/backup/dir\n" +
2019-11-07 20:05:39 +01: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 19:31:52 +01: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 16:05:52 +02:00
maxBytesPerSecond = flagutil . NewBytes ( "maxBytesPerSecond" , 0 , "The maximum upload speed. There is no limit if it is set to 0" )
2019-11-07 20:05:39 +01:00
)
func main ( ) {
2020-05-16 10:59:30 +02:00
// Write flags and help message to stdout, since it is easier to grep or pipe.
flag . CommandLine . SetOutput ( os . Stdout )
2019-11-07 20:05:39 +01:00
flag . Usage = usage
2023-01-18 20:35:21 +01:00
flagutil . RegisterSecretFlag ( "snapshot.createURL" )
flagutil . RegisterSecretFlag ( "snapshot.deleteURL" )
2020-02-10 12:26:18 +01:00
envflag . Parse ( )
2019-11-07 20:05:39 +01:00
buildinfo . Init ( )
2020-08-11 21:54:13 +02:00
logger . Init ( )
2019-11-07 20:05:39 +01:00
2023-03-25 05:49:58 +01:00
// Storing snapshot delete function to be able to call it in case
// of error since logger.Fatal will exit the program without
// calling deferred functions.
2023-03-25 06:03:11 +01:00
// See https://github.com/VictoriaMetrics/VictoriaMetrics/issues/2055
deleteSnapshot := func ( ) { }
2023-03-25 05:49:58 +01:00
2024-04-24 13:47:57 +02:00
if len ( * snapshotCreateURL ) > 0 {
// create net/url object
createURL , err := url . Parse ( * snapshotCreateURL )
if err != nil {
logger . Fatalf ( "cannot parse snapshotCreateURL: %s" , err )
}
2021-05-07 07:40:30 +02: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" )
}
2024-04-24 13:47:57 +02:00
logger . Infof ( "Snapshot create url %s" , createURL . Redacted ( ) )
if len ( * snapshotDeleteURL ) <= 0 {
err := flag . Set ( "snapshot.deleteURL" , strings . Replace ( * snapshotCreateURL , "/create" , "/delete" , 1 ) )
2020-04-24 21:35:03 +02:00
if err != nil {
logger . Fatalf ( "Failed to set snapshot.deleteURL flag: %v" , err )
}
}
2024-04-24 13:47:57 +02:00
deleteURL , err := url . Parse ( * snapshotDeleteURL )
if err != nil {
logger . Fatalf ( "cannot parse snapshotDeleteURL: %s" , err )
}
logger . Infof ( "Snapshot delete url %s" , deleteURL . Redacted ( ) )
2020-04-24 21:35:03 +02:00
2024-04-24 13:47:57 +02:00
name , err := snapshot . Create ( createURL . String ( ) )
2020-04-24 21:35:03 +02:00
if err != nil {
2024-04-24 13:47:57 +02:00
logger . Fatalf ( "cannot create snapshot: %s" , err )
2020-04-24 21:35:03 +02:00
}
err = flag . Set ( "snapshotName" , name )
if err != nil {
2020-11-23 16:09:59 +01:00
logger . Fatalf ( "cannot set snapshotName flag: %v" , err )
2020-04-24 21:35:03 +02:00
}
2023-03-25 05:49:58 +01:00
deleteSnapshot = func ( ) {
2024-04-24 13:47:57 +02:00
err := snapshot . Delete ( deleteURL . String ( ) , name )
2020-04-24 21:35:03 +02:00
if err != nil {
2024-04-24 13:47:57 +02:00
logger . Fatalf ( "cannot delete snapshot: %s" , err )
2020-04-24 21:35:03 +02:00
}
2023-03-25 05:49:58 +01:00
}
2022-05-04 21:12:03 +02:00
}
2020-04-24 21:35:03 +02:00
2024-02-09 02:15:04 +01:00
listenAddrs := [ ] string { * httpListenAddr }
go httpserver . Serve ( listenAddrs , nil , nil )
2021-12-02 10:55:58 +01:00
2024-01-15 12:37:02 +01:00
pushmetrics . Init ( )
2023-03-25 06:03:11 +01:00
err := makeBackup ( )
deleteSnapshot ( )
if err != nil {
logger . Fatalf ( "cannot create backup: %s" , err )
}
2024-01-15 12:37:02 +01:00
pushmetrics . Stop ( )
2023-03-25 06:03:11 +01:00
startTime := time . Now ( )
2024-02-09 02:15:04 +01:00
logger . Infof ( "gracefully shutting down http server for metrics at %q" , listenAddrs )
if err := httpserver . Stop ( listenAddrs ) ; err != nil {
2023-03-25 06:03:11 +01:00
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 ( ) )
}
func makeBackup ( ) error {
2019-11-07 20:05:39 +01:00
dstFS , err := newDstFS ( )
if err != nil {
2023-03-25 06:03:11 +01:00
return err
2019-11-07 20:05:39 +01:00
}
2023-08-14 02:17:12 +02:00
if * snapshotName == "" {
// Make server-side copy from -origin to -dst
originFS , err := newRemoteOriginFS ( )
if err != nil {
return err
}
a := & actions . RemoteBackupCopy {
Concurrency : * concurrency ,
Src : originFS ,
Dst : dstFS ,
}
if err := a . Run ( ) ; err != nil {
return err
}
originFS . MustStop ( )
} else {
// Make backup from srcFS to -dst
srcFS , err := newSrcFS ( )
if err != nil {
return err
}
originFS , err := newOriginFS ( )
if err != nil {
return err
}
a := & actions . Backup {
Concurrency : * concurrency ,
Src : srcFS ,
Dst : dstFS ,
Origin : originFS ,
}
if err := a . Run ( ) ; err != nil {
return err
}
srcFS . MustStop ( )
originFS . MustStop ( )
2019-11-07 20:05:39 +01:00
}
2020-10-09 14:31:39 +02:00
dstFS . MustStop ( )
2023-03-25 06:03:11 +01:00
return nil
2019-11-07 20:05:39 +01:00
}
func usage ( ) {
const s = `
2023-01-04 06:52:40 +01:00
vmbackup performs backups for VictoriaMetrics data from instant snapshots to gcs , s3 , azblob
2019-11-07 20:05:39 +01:00
or local filesystem . Backed up data can be restored with vmrestore .
2024-04-18 01:57:04 +02:00
See the docs at https : //docs.victoriametrics.com/vmbackup/ .
2019-11-07 20:05:39 +01:00
`
2020-12-03 20:40:30 +01:00
flagutil . Usage ( s )
2019-11-07 20:05:39 +01:00
}
func newSrcFS ( ) ( * fslocal . FS , error ) {
2024-02-09 03:03:20 +01:00
if err := snapshotutil . Validate ( * snapshotName ) ; err != nil {
2023-10-25 21:24:01 +02:00
return nil , fmt . Errorf ( "invalid -snapshotName=%q: %w" , * snapshotName , err )
2023-08-14 02:17:12 +02:00
}
2023-05-03 10:48:53 +02:00
snapshotPath := filepath . Join ( * storageDataPath , "snapshots" , * snapshotName )
2019-11-07 20:05:39 +01:00
// Verify the snapshot exists.
f , err := os . Open ( snapshotPath )
if err != nil {
2020-06-30 21:58:18 +02:00
return nil , fmt . Errorf ( "cannot open snapshot at %q: %w" , snapshotPath , err )
2019-11-07 20:05:39 +01:00
}
fi , err := f . Stat ( )
_ = f . Close ( )
if err != nil {
2020-06-30 21:58:18 +02:00
return nil , fmt . Errorf ( "cannot stat %q: %w" , snapshotPath , err )
2019-11-07 20:05:39 +01:00
}
if ! fi . IsDir ( ) {
return nil , fmt . Errorf ( "snapshot %q must be a directory" , snapshotPath )
}
fs := & fslocal . FS {
2019-11-19 19:31:52 +01:00
Dir : snapshotPath ,
2022-12-15 04:26:24 +01:00
MaxBytesPerSecond : maxBytesPerSecond . IntN ( ) ,
2019-11-19 19:31:52 +01:00
}
if err := fs . Init ( ) ; err != nil {
2020-06-30 21:58:18 +02:00
return nil , fmt . Errorf ( "cannot initialize fs: %w" , err )
2019-11-07 20:05:39 +01:00
}
return fs , nil
}
func newDstFS ( ) ( common . RemoteFS , error ) {
fs , err := actions . NewRemoteFS ( * dst )
if err != nil {
2020-06-30 21:58:18 +02:00
return nil , fmt . Errorf ( "cannot parse `-dst`=%q: %w" , * dst , err )
2019-11-07 20:05:39 +01:00
}
2022-05-06 17:04:09 +02:00
if hasFilepathPrefix ( * dst , * storageDataPath ) {
return nil , fmt . Errorf ( "-dst=%q can not point to the directory with VictoriaMetrics data (aka -storageDataPath=%q)" , * dst , * storageDataPath )
}
2019-11-07 20:05:39 +01:00
return fs , nil
}
2022-05-06 17:04:09 +02:00
func hasFilepathPrefix ( path , prefix string ) bool {
if ! strings . HasPrefix ( path , "fs://" ) {
return false
}
path = path [ len ( "fs://" ) : ]
pathAbs , err := filepath . Abs ( path )
if err != nil {
return false
}
prefixAbs , err := filepath . Abs ( prefix )
if err != nil {
return false
}
2023-08-16 14:45:35 +02:00
if prefixAbs == pathAbs {
return true
}
rel , err := filepath . Rel ( prefixAbs , pathAbs )
if err != nil {
// if paths can't be related - they don't match
return false
}
if i := strings . Index ( rel , "." ) ; i == 0 {
// if path can be related only with . as first char - they still don't match
return false
}
// if paths are related - it is a match
return true
2022-05-06 17:04:09 +02:00
}
2020-10-28 19:09:10 +01:00
func newOriginFS ( ) ( common . OriginFS , error ) {
2019-11-07 20:05:39 +01:00
if len ( * origin ) == 0 {
2020-10-28 19:09:10 +01:00
return & fsnil . FS { } , nil
2019-11-07 20:05:39 +01:00
}
fs , err := actions . NewRemoteFS ( * origin )
if err != nil {
2020-06-30 21:58:18 +02:00
return nil , fmt . Errorf ( "cannot parse `-origin`=%q: %w" , * origin , err )
2019-11-07 20:05:39 +01:00
}
return fs , nil
}
2023-08-14 02:17:12 +02:00
func newRemoteOriginFS ( ) ( common . RemoteFS , error ) {
if len ( * origin ) == 0 {
return nil , fmt . Errorf ( "-origin cannot be empty when -snapshotName and -snapshot.createURL aren't set" )
}
fs , err := actions . NewRemoteFS ( * origin )
if err != nil {
return nil , fmt . Errorf ( "cannot parse `-origin`=%q: %w" , * origin , err )
}
return fs , nil
}