NewCmd creates the new "backup" subcommand
()
| 65 | |
| 66 | // NewCmd creates the new "backup" subcommand |
| 67 | func 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 { |
no test coverage detected