MCPcopy Create free account
hub / github.com/cloudnative-pg/cloudnative-pg / NewCmd

Function NewCmd

internal/cmd/plugin/backup/cmd.go:67–232  ·  view source on GitHub ↗

NewCmd creates the new "backup" subcommand

()

Source from the content-addressed store, hash-verified

65
66// NewCmd creates the new "backup" subcommand
67func NewCmd() *cobra.Command {
68 var backupName, backupTarget, backupMethod, online, immediateCheckpoint, waitForArchive, pluginName string
69 var pluginParameters pluginParameters
70
71 backupMethods := []string{
72 string(apiv1.BackupMethodBarmanObjectStore),
73 string(apiv1.BackupMethodVolumeSnapshot),
74 string(apiv1.BackupMethodPlugin),
75 }
76
77 backupSubcommand := &cobra.Command{
78 Use: "backup CLUSTER",
79 Short: "Request an on-demand backup for a PostgreSQL Cluster",
80 GroupID: plugin.GroupIDDatabase,
81 Args: plugin.RequiresArguments(1),
82 ValidArgsFunction: func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
83 return plugin.CompleteClusters(cmd.Context(), args, toComplete), cobra.ShellCompDirectiveNoFileComp
84 },
85 RunE: func(cmd *cobra.Command, args []string) error {
86 clusterName := args[0]
87
88 if len(backupName) == 0 {
89 backupName = fmt.Sprintf(
90 "%s-%s",
91 clusterName,
92 pgTime.ToCompactISO8601(time.Now()),
93 )
94 }
95
96 // Check if the backup target is correct
97 allowedBackupTargets := []string{
98 "",
99 string(apiv1.BackupTargetPrimary),
100 string(apiv1.BackupTargetStandby),
101 }
102 if !slices.Contains(allowedBackupTargets, backupTarget) {
103 return fmt.Errorf("backup-target: %s is not supported by the backup command", backupTarget)
104 }
105
106 // Check if the backup method is correct
107 allowedBackupMethods := backupMethods
108 allowedBackupMethods = append(allowedBackupMethods, "")
109 if !slices.Contains(allowedBackupMethods, backupMethod) {
110 return fmt.Errorf("backup-method: %s is not supported by the backup command", backupMethod)
111 }
112
113 if backupMethod == string(apiv1.BackupMethodPlugin) {
114 if len(pluginName) == 0 {
115 return fmt.Errorf("plugin-name is required when backup method in %s",
116 apiv1.BackupMethodPlugin)
117 }
118 } else {
119 if len(pluginName) > 0 {
120 return fmt.Errorf("plugin-name is allowed only when backup method in %s",
121 apiv1.BackupMethodPlugin)
122 }
123
124 if len(pluginParameters) > 0 {

Callers 1

mainFunction · 0.92

Calls 6

RequiresArgumentsFunction · 0.92
CompleteClustersFunction · 0.92
createBackupFunction · 0.85
JoinMethod · 0.80
GetMethod · 0.45

Tested by

no test coverage detected