Skip to content

Commit

Permalink
Merge pull request #371 from civo/db-bk-delete
Browse files Browse the repository at this point in the history
Add support to get and delete database backup
  • Loading branch information
vishalanarase authored Mar 18, 2024
2 parents c677e70 + 9a7a1f5 commit c76243c
Show file tree
Hide file tree
Showing 10 changed files with 222 additions and 28 deletions.
2 changes: 2 additions & 0 deletions cmd/database/database_backup.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ func init() {
dbBackupCmd.AddCommand(dbBackupCreateCmd)
dbBackupCmd.AddCommand(dbBackupListCmd)
dbBackupCmd.AddCommand(dbBackupUpdateCmd)
dbBackupCmd.AddCommand(dbBackupShowCmd)
dbBackupCmd.AddCommand(dbBackupDeleteCmd)

// Create cmd options
dbBackupCreateCmd.Flags().StringVarP(&name, "name", "n", "", "name of the database backup")
Expand Down
10 changes: 8 additions & 2 deletions cmd/database/database_backup_create.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ import (
var dbBackupCreateCmd = &cobra.Command{
Use: "create",
Aliases: []string{"new", "add"},
Example: `Scheduled: civo database backup create <DATABASE-NAME/ID> --name <BACKUP_NAME> --schedule <SCHEDULE>\n
Manual: civo database backup create <DATABASE-NAME/ID> --name <BACKUP_NAME> --type manual`,
Example: ` Scheduled: civo database backup create <DATABASE-NAME/ID> --name <BACKUP-NAME> --schedule <SCHEDULE>
Manual: civo database backup create <DATABASE-NAME/ID> --name <BACKUP_NAME> --type manual`,
Short: "Create a new database backup",
Args: cobra.MinimumNArgs(1),
Run: func(cmd *cobra.Command, args []string) {
Expand All @@ -39,6 +39,11 @@ var dbBackupCreateCmd = &cobra.Command{
os.Exit(1)
}

if name == "" {
utility.Error("Name must be specified")
os.Exit(1)
}

db, err := client.FindDatabase(args[0])
if err != nil {
utility.Error("Database %s", err)
Expand Down Expand Up @@ -91,6 +96,7 @@ var dbBackupCreateCmd = &cobra.Command{
"database_name": bk.DatabaseName,
"software": bk.Software,
"name": name,
"id": bk.ID,
})
}

Expand Down
108 changes: 108 additions & 0 deletions cmd/database/database_backup_delete.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
package database

import (
"errors"
"fmt"
"strings"

pluralize "github.com/alejandrojnm/go-pluralize"
"github.com/civo/civogo"
"github.com/civo/cli/common"
"github.com/civo/cli/config"
"github.com/civo/cli/utility"

"os"

"github.com/spf13/cobra"
)

var backupList []utility.ObjecteList
var dbBackupDeleteCmd = &cobra.Command{
Use: "delete",
Aliases: []string{"rm", "remove", "destroy"},
Short: "Delete a manual database backup",
Example: "civo database backup delete <DATABASE-NAME/ID> <BACKUP-NAME/ID>",
Args: cobra.MinimumNArgs(1),
Run: func(cmd *cobra.Command, args []string) {
utility.EnsureCurrentRegion()

client, err := config.CivoAPIClient()
if err != nil {
utility.Error("Creating the connection to Civo's API failed with %s", err)
os.Exit(1)
}

if common.RegionSet != "" {
client.Region = common.RegionSet
}

if len(args) == 2 {
bk, err := client.FindDatabaseBackup(args[0], args[1])
if err != nil {
if errors.Is(err, civogo.ZeroMatchesError) {
utility.Error("sorry there is no %s Database in your account", utility.Red(args[0]))
os.Exit(1)
}
if errors.Is(err, civogo.MultipleMatchesError) {
utility.Error("sorry we found more than one database with that name in your account")
os.Exit(1)
}
}
backupList = append(backupList, utility.ObjecteList{ID: bk.ID, Name: bk.Name})
} else {
for idx, v := range args {
if idx == 0 {
continue
}
bk, err := client.FindDatabaseBackup(args[0], v)
if err == nil {
backupList = append(backupList, utility.ObjecteList{ID: bk.ID, Name: bk.Name})
}
}
}

dbNameList := []string{}
for _, v := range backupList {
dbNameList = append(dbNameList, v.Name)
}

if utility.UserConfirmedDeletion(pluralize.Pluralize(len(backupList), "Database Backup"), common.DefaultYes, strings.Join(dbNameList, ", ")) {

for _, v := range backupList {
db, err := client.FindDatabaseBackup(args[0], v.ID)
if err != nil {
utility.Error("%s", err)
os.Exit(1)
}
_, err = client.DeleteDatabaseBackup(args[0], db.ID)
if err != nil {
utility.Error("Error deleting the Database backup: %s", err)
os.Exit(1)
}
}

ow := utility.NewOutputWriter()

for _, v := range backupList {
ow.StartLine()
ow.AppendDataWithLabel("id", v.ID, "ID")
ow.AppendDataWithLabel("backup", v.Name, "Backup")
}

switch common.OutputFormat {
case "json":
if len(backupList) == 1 {
ow.WriteSingleObjectJSON(common.PrettySet)
} else {
ow.WriteMultipleObjectsJSON(common.PrettySet)
}
case "custom":
ow.WriteCustomOutput(common.OutputFields)
default:
fmt.Printf("The %s (%s) has been deleted\n", pluralize.Pluralize(len(backupList), "database backup"), utility.Green(strings.Join(dbNameList, ", ")))
}
} else {
fmt.Println("Operation aborted")
}
},
}
20 changes: 12 additions & 8 deletions cmd/database/database_backup_list.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,14 +61,14 @@ func postgresScheduledBackups(backups *civogo.PaginatedDatabaseBackup) {
}
printMsg = true
ow.StartLine()
ow.AppendDataWithLabel("name", bk.Name, "Name")
ow.AppendDataWithLabel("schedule", bk.Schedule, "Schedule")

ow.AppendDataWithLabel("database_id", utility.TrimID(bk.DatabaseID), "Database ID")
ow.AppendDataWithLabel("database_name", bk.DatabaseName, "Database Name")

ow.AppendDataWithLabel("software", bk.Software, "Software")
ow.AppendDataWithLabel("schedule", bk.Schedule, "Schedule")
ow.AppendDataWithLabel("backup_name", bk.Name, "Backup Name")

ow.AppendDataWithLabel("backup", bk.Backup, "Backup")
ow.AppendDataWithLabel("status", bk.Status, "Status")

if common.OutputFormat == "json" || common.OutputFormat == "custom" {
ow.AppendDataWithLabel("database_id", bk.DatabaseID, "Database ID")
Expand Down Expand Up @@ -97,13 +97,17 @@ func postgresManualBackups(backups *civogo.PaginatedDatabaseBackup) {
}
printMsg = true
ow.StartLine()
ow.AppendDataWithLabel("id", utility.TrimID(bk.ID), "ID")
ow.AppendDataWithLabel("name", bk.Name, "Name")

ow.AppendDataWithLabel("database_id", utility.TrimID(bk.DatabaseID), "Database ID")
ow.AppendDataWithLabel("database_name", bk.DatabaseName, "Database Name")

ow.AppendDataWithLabel("software", bk.Software, "Software")
ow.AppendDataWithLabel("backup", bk.Backup, "Backup")
ow.AppendDataWithLabel("status", bk.Status, "Status")

if common.OutputFormat == "json" || common.OutputFormat == "custom" {
ow.AppendDataWithLabel("id", bk.ID, "ID")
ow.AppendDataWithLabel("database_id", bk.DatabaseID, "Database ID")
}
}
Expand All @@ -127,18 +131,18 @@ func mysqlBackups(backups *civogo.PaginatedDatabaseBackup) {
for _, bk := range backups.Items {
printMsg = true
ow.StartLine()
ow.AppendDataWithLabel("id", utility.TrimID(bk.ID), "ID")
ow.AppendDataWithLabel("name", bk.Name, "Name")

ow.AppendDataWithLabel("database_id", utility.TrimID(bk.DatabaseID), "Database ID")
ow.AppendDataWithLabel("database_name", bk.DatabaseName, "Database Name")

ow.AppendDataWithLabel("backup_id", utility.TrimID(bk.ID), "Backup ID")
ow.AppendDataWithLabel("backup_name", bk.Name, "Backup Name")
ow.AppendDataWithLabel("software", bk.Software, "Software")
ow.AppendDataWithLabel("status", bk.Status, "Status")

if common.OutputFormat == "json" || common.OutputFormat == "custom" {
ow.AppendDataWithLabel("id", bk.ID, "ID")
ow.AppendDataWithLabel("database_id", bk.DatabaseID, "Database ID")
ow.AppendDataWithLabel("database_name", bk.DatabaseName, "Database Name")
}
}

Expand Down
70 changes: 70 additions & 0 deletions cmd/database/database_backup_show.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
package database

import (
"os"

"github.com/civo/cli/common"
"github.com/civo/cli/config"
"github.com/civo/cli/utility"
"github.com/spf13/cobra"
)

var dbBackupShowCmd = &cobra.Command{
Use: "show",
Example: `civo database backup show <DATABASE-NAME/ID> <BACKUP-NAME/ID>`,
Aliases: []string{"get", "inspect"},
Args: cobra.MinimumNArgs(2),
Short: "Show details of a database backup",
Run: func(cmd *cobra.Command, args []string) {
utility.EnsureCurrentRegion()

client, err := config.CivoAPIClient()
if common.RegionSet != "" {
client.Region = common.RegionSet
}
if err != nil {
utility.Error("Creating the connection to Civo's API failed with %s", err)
os.Exit(1)
}

if common.RegionSet != "" {
client.Region = common.RegionSet
}

bk, err := client.GetDatabaseBackup(args[0], args[1])
if err != nil {
utility.Error("Database backup %s", err)
os.Exit(1)
}

ow := utility.NewOutputWriter()

ow.StartLine()

if !bk.IsScheduled {
ow.AppendDataWithLabel("id", utility.TrimID(bk.ID), "ID")
}
ow.AppendDataWithLabel("name", bk.Name, "Name")
ow.AppendDataWithLabel("schedule", bk.Schedule, "Schedule")

ow.AppendDataWithLabel("database_id", utility.TrimID(bk.DatabaseID), "Database ID")
ow.AppendDataWithLabel("database_name", bk.DatabaseName, "Database Name")

ow.AppendDataWithLabel("software", bk.Software, "Software")
ow.AppendDataWithLabel("status", bk.Status, "Status")

if common.OutputFormat == "json" || common.OutputFormat == "custom" {
ow.AppendDataWithLabel("id", bk.ID, "ID")
ow.AppendDataWithLabel("database_id", bk.DatabaseID, "Database ID")
}

switch common.OutputFormat {
case "json":
ow.WriteMultipleObjectsJSON(common.PrettySet)
case "custom":
ow.WriteCustomOutput(common.OutputFields)
default:
ow.WriteKeyValues()
}
},
}
2 changes: 1 addition & 1 deletion cmd/database/database_backup_update.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ var dbBackupUpdateCmd = &cobra.Command{
Use: "update",
Aliases: []string{"modify", "change"},
Short: "Update a scheduled database backup",
Example: "civo database backup update <DATABASE-NAME/ID> --name <NEW_BACKUP-NAME> --schedule <SCHEDULE>",
Example: "civo database backup update <DATABASE-NAME/ID> --name <NEW-BACKUP-NAME> --schedule <SCHEDULE>",
Args: cobra.MinimumNArgs(1),
Run: func(cmd *cobra.Command, args []string) {
utility.EnsureCurrentRegion()
Expand Down
17 changes: 8 additions & 9 deletions cmd/database/database_delete.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ import (
"github.com/spf13/cobra"
)

var databaseList []utility.ObjecteList
var dbDeleteCmd = &cobra.Command{
Use: "delete",
Aliases: []string{"rm", "remove", "destroy"},
Expand Down Expand Up @@ -48,24 +47,24 @@ var dbDeleteCmd = &cobra.Command{
os.Exit(1)
}
}
databaseList = append(databaseList, utility.ObjecteList{ID: db.ID, Name: db.Name})
backupList = append(backupList, utility.ObjecteList{ID: db.ID, Name: db.Name})
} else {
for _, v := range args {
db, err := client.FindDatabase(v)
if err == nil {
databaseList = append(databaseList, utility.ObjecteList{ID: db.ID, Name: db.Name})
backupList = append(backupList, utility.ObjecteList{ID: db.ID, Name: db.Name})
}
}
}

dbNameList := []string{}
for _, v := range databaseList {
for _, v := range backupList {
dbNameList = append(dbNameList, v.Name)
}

if utility.UserConfirmedDeletion(pluralize.Pluralize(len(databaseList), "Database"), common.DefaultYes, strings.Join(dbNameList, ", ")) {
if utility.UserConfirmedDeletion(pluralize.Pluralize(len(backupList), "Database"), common.DefaultYes, strings.Join(dbNameList, ", ")) {

for _, v := range databaseList {
for _, v := range backupList {
db, err := client.FindDatabase(v.ID)
if err != nil {
utility.Error("%s", err)
Expand All @@ -80,23 +79,23 @@ var dbDeleteCmd = &cobra.Command{

ow := utility.NewOutputWriter()

for _, v := range databaseList {
for _, v := range backupList {
ow.StartLine()
ow.AppendDataWithLabel("id", v.ID, "ID")
ow.AppendDataWithLabel("database", v.Name, "Database")
}

switch common.OutputFormat {
case "json":
if len(databaseList) == 1 {
if len(backupList) == 1 {
ow.WriteSingleObjectJSON(common.PrettySet)
} else {
ow.WriteMultipleObjectsJSON(common.PrettySet)
}
case "custom":
ow.WriteCustomOutput(common.OutputFields)
default:
fmt.Printf("The %s (%s) has been deleted\n", pluralize.Pluralize(len(databaseList), "database"), utility.Green(strings.Join(dbNameList, ", ")))
fmt.Printf("The %s (%s) has been deleted\n", pluralize.Pluralize(len(backupList), "database"), utility.Green(strings.Join(dbNameList, ", ")))
}
} else {
fmt.Println("Operation aborted")
Expand Down
15 changes: 10 additions & 5 deletions cmd/database/database_restore.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,16 +43,21 @@ var dbRestoreCmd = &cobra.Command{
os.Exit(1)
}

bk, err := client.GetDatabaseBackup(args[0], backup)
if err != nil {
utility.Error("Database backup %s", err)
os.Exit(1)
}

if utility.UserConfirmedRestore(db.Name, common.DefaultYes, backup) {
config := &civogo.RestoreDatabaseRequest{
Software: db.Software,
Name: restoreName,
Backup: backup,
Region: client.Region,
Name: restoreName,
Backup: bk.Name,
Region: client.Region,
}
_, err = client.RestoreDatabase(db.ID, config)
if err != nil {
utility.Error("%s", err)
utility.Error("Failed to restore %s", err)
os.Exit(1)
}
ow := utility.NewOutputWriter()
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ require (
github.com/bradfitz/iter v0.0.0-20191230175014-e8f45d346db8 // indirect
github.com/briandowns/spinner v1.11.1
github.com/c4milo/unpackit v0.0.0-20170704181138-4ed373e9ef1c // indirect
github.com/civo/civogo v0.3.63
github.com/civo/civogo v0.3.64
github.com/dsnet/compress v0.0.1 // indirect
github.com/fatih/color v1.13.0 // indirect
github.com/google/go-github v17.0.0+incompatible // indirect
Expand Down
Loading

0 comments on commit c76243c

Please sign in to comment.