wal-g

Форк
0
/
backup_fetch.go 
121 строка · 4.9 Кб
1
package gp
2

3
import (
4
	"fmt"
5

6
	"github.com/wal-g/wal-g/internal/databases/greenplum"
7

8
	"github.com/spf13/cobra"
9
	"github.com/spf13/viper"
10
	"github.com/wal-g/tracelog"
11
	"github.com/wal-g/wal-g/internal"
12
	conf "github.com/wal-g/wal-g/internal/config"
13
)
14

15
const (
16
	backupFetchShortDescription  = "Fetches a backup from storage"
17
	targetUserDataDescription    = "Fetch storage backup which has the specified user data"
18
	restorePointDescription      = "Fetch storage backup w/ restore point specified by name"
19
	restorePointTSDescription    = "Fetch storage backup w/ restore point time less or equal to the provided timestamp"
20
	restoreConfigPathDescription = "Path to the cluster restore configuration"
21
	fetchContentIdsDescription   = "If set, WAL-G will fetch only the specified segments"
22
	fetchModeDescription         = "Backup fetch mode. default: do the backup unpacking " +
23
		"and prepare the configs [unpack+prepare], unpack: backup unpacking only, prepare: config preparation only."
24
	inPlaceFlagDescription = "Perform the backup fetch in-place (without the restore config)"
25
	restoreOnlyDescription = `[Experimental] Downloads only databases specified by passed names from default tablespace.
26
Always downloads system databases.`
27
)
28

29
var fetchTargetUserData string
30
var restorePointTS string
31
var restorePoint string
32
var restoreConfigPath string
33
var fetchContentIds *[]int
34
var fetchModeStr string
35
var inPlaceRestore bool
36
var partialRestoreArgs []string
37

38
var backupFetchCmd = &cobra.Command{
39
	Use:   "backup-fetch [backup_name | --target-user-data <data> | --restore-point <name>]",
40
	Short: backupFetchShortDescription, // TODO : improve description
41
	Args:  cobra.RangeArgs(0, 1),
42
	Run: func(cmd *cobra.Command, args []string) {
43
		internal.ConfigureLimiters()
44

45
		if !inPlaceRestore && restoreConfigPath == "" {
46
			tracelog.ErrorLogger.Fatalf(
47
				"No restore config was specified. Either specify one via the --restore-config flag or add the --in-place flag to restore in-place.")
48
		}
49

50
		if fetchTargetUserData == "" {
51
			fetchTargetUserData = viper.GetString(conf.FetchTargetUserDataSetting)
52
		}
53

54
		storage, err := internal.ConfigureStorage()
55
		tracelog.ErrorLogger.FatalOnError(err)
56

57
		if restorePoint != "" && restorePointTS != "" {
58
			tracelog.ErrorLogger.Fatalf("can't use both --restore-point and --restore-point-ts")
59
		}
60

61
		if restorePointTS != "" {
62
			restorePoint, err = greenplum.FindRestorePointBeforeTS(restorePointTS, storage.RootFolder())
63
			tracelog.ErrorLogger.FatalOnError(err)
64
		}
65

66
		targetBackupSelector, err := createTargetFetchBackupSelector(cmd, args, fetchTargetUserData, restorePoint)
67
		tracelog.ErrorLogger.FatalOnError(err)
68

69
		logsDir := viper.GetString(conf.GPLogsDirectory)
70

71
		if len(*fetchContentIds) > 0 {
72
			tracelog.InfoLogger.Printf("Will perform fetch operations only on the specified segments: %v", *fetchContentIds)
73
		}
74

75
		fetchMode, err := greenplum.NewBackupFetchMode(fetchModeStr)
76
		tracelog.ErrorLogger.FatalOnError(err)
77

78
		internal.HandleBackupFetch(storage.RootFolder(), targetBackupSelector,
79
			greenplum.NewGreenplumBackupFetcher(restoreConfigPath, inPlaceRestore, logsDir, *fetchContentIds, fetchMode, restorePoint,
80
				partialRestoreArgs))
81
	},
82
}
83

84
// create the BackupSelector to select the backup to fetch
85
func createTargetFetchBackupSelector(cmd *cobra.Command,
86
	args []string, targetUserData, restorePoint string) (internal.BackupSelector, error) {
87
	targetName := ""
88
	if len(args) >= 1 {
89
		targetName = args[0]
90
	}
91

92
	// if target restore point is provided without the backup name, then
93
	// choose the latest backup up to the specified restore point name
94
	if restorePoint != "" && targetUserData == "" && targetName == "" {
95
		tracelog.InfoLogger.Printf("Restore point %s is specified without the backup name or target user data, "+
96
			"will search for a matching backup", restorePoint)
97
		return greenplum.NewRestorePointBackupSelector(restorePoint), nil
98
	}
99

100
	backupSelector, err := internal.NewTargetBackupSelector(targetUserData, targetName, greenplum.NewGenericMetaFetcher())
101
	if err != nil {
102
		fmt.Println(cmd.UsageString())
103
		return nil, err
104
	}
105
	return backupSelector, nil
106
}
107

108
func init() {
109
	backupFetchCmd.Flags().StringVar(&fetchTargetUserData, "target-user-data",
110
		"", targetUserDataDescription)
111
	backupFetchCmd.Flags().StringVar(&restorePointTS, "restore-point-ts", "", restorePointTSDescription)
112
	backupFetchCmd.Flags().StringVar(&restorePoint, "restore-point", "", restorePointDescription)
113
	backupFetchCmd.Flags().StringVar(&restoreConfigPath, "restore-config",
114
		"", restoreConfigPathDescription)
115
	backupFetchCmd.Flags().BoolVar(&inPlaceRestore, "in-place", false, inPlaceFlagDescription)
116
	fetchContentIds = backupFetchCmd.Flags().IntSlice("content-ids", []int{}, fetchContentIdsDescription)
117
	backupFetchCmd.Flags().StringSliceVar(&partialRestoreArgs, "restore-only", nil, restoreOnlyDescription)
118

119
	backupFetchCmd.Flags().StringVar(&fetchModeStr, "mode", "default", fetchModeDescription)
120
	cmd.AddCommand(backupFetchCmd)
121
}
122

Использование cookies

Мы используем файлы cookie в соответствии с Политикой конфиденциальности и Политикой использования cookies.

Нажимая кнопку «Принимаю», Вы даете АО «СберТех» согласие на обработку Ваших персональных данных в целях совершенствования нашего веб-сайта и Сервиса GitVerse, а также повышения удобства их использования.

Запретить использование cookies Вы можете самостоятельно в настройках Вашего браузера.