Skip to content

Commit

Permalink
Merge pull request #349 from civo/feat/manual-backup
Browse files Browse the repository at this point in the history
Support manual backup
  • Loading branch information
vishalanarase authored Nov 6, 2023
2 parents c0a0868 + ebc54d7 commit 97bdecc
Show file tree
Hide file tree
Showing 6 changed files with 110 additions and 60 deletions.
4 changes: 2 additions & 2 deletions cmd/database/database_backup.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import (
"github.com/spf13/cobra"
)

var name, schedule string
var name, schedule, backupType string
var count int

// dbBackupCmd is the root command for the db backup subcommand
Expand All @@ -33,9 +33,9 @@ func init() {
dbBackupCreateCmd.Flags().StringVarP(&name, "name", "n", "", "name of the database backup")
dbBackupCreateCmd.Flags().StringVarP(&schedule, "schedule", "s", "", "schedule of the database backup in the form of cronjob")
dbBackupCreateCmd.Flags().IntVarP(&count, "count", "c", 1, "number of backups to keep")
dbBackupCreateCmd.Flags().StringVarP(&backupType, "type", "t", "scheduled", "set the type of database backup manual/scheduled")

dbBackupCreateCmd.MarkFlagRequired("name")
dbBackupCreateCmd.MarkFlagRequired("schedule")

// Update cmd options
dbBackupUpdateCmd.Flags().StringVarP(&name, "name", "n", "", "name of the database backup")
Expand Down
91 changes: 54 additions & 37 deletions cmd/database/database_backup_create.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,10 @@ import (
var dbBackupCreateCmd = &cobra.Command{
Use: "create",
Aliases: []string{"new", "add"},
Example: "civo database backup create <DATABASE-NAME/ID> --name <BACKUP_NAME> --schedule <SCHEDULE> --count <COUNT>",
Short: "Create a new database backup",
Args: cobra.MinimumNArgs(1),
Example: `Scheduled: civo database backup create <DATABASE-NAME/ID> --name <BACKUP_NAME> --schedule <SCHEDULE> --count <COUNT>\n
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) {
utility.EnsureCurrentRegion()

Expand All @@ -38,60 +39,76 @@ var dbBackupCreateCmd = &cobra.Command{
os.Exit(1)
}

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

if count <= 0 {
utility.Error("Count must be greater than zero, you have given: %d", count)
os.Exit(1)
}

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

gron := gronx.New()
if !gron.IsValid(schedule) {
utility.Error("Invalid schedule, you have given: %s", schedule)
os.Exit(1)
}

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

backupCreateConfig := civogo.DatabaseBackupCreateRequest{
Name: name,
Schedule: schedule,
Count: int32(count),
Region: client.Region,
backupCreateConfig := civogo.DatabaseBackupCreateRequest{}
if backupType != "manual" {
if common.RegionSet != "" {
client.Region = common.RegionSet
}

if count <= 0 {
utility.Error("Count must be greater than zero, you have given: %d", count)
os.Exit(1)
}

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

gron := gronx.New()
if !gron.IsValid(schedule) {
utility.Error("Invalid schedule, you have given: %s", schedule)
os.Exit(1)
}

backupCreateConfig.Name = name
backupCreateConfig.Schedule = schedule
backupCreateConfig.Count = int32(count)

} else {
backupCreateConfig.Name = name
backupCreateConfig.Type = backupType
}

backupCreateConfig.Region = client.Region
bk, err := client.CreateDatabaseBackup(db.ID, &backupCreateConfig)
if err != nil {
utility.Error("Error creating database %s", err)
os.Exit(1)
}

ow := utility.NewOutputWriterWithMap(map[string]string{
"database_id": bk.DatabaseID,
"database_name": bk.DatabaseName,
"software": bk.Software,
"name": bk.Scheduled.Name,
"schedule": bk.Scheduled.Schedule,
"count": fmt.Sprintf("%d", count),
})
ow := &utility.OutputWriter{}
if backupType != "manual" {
ow = utility.NewOutputWriterWithMap(map[string]string{
"database_id": bk.DatabaseID,
"database_name": bk.DatabaseName,
"software": bk.Software,
"name": bk.Scheduled.Name,
"schedule": bk.Scheduled.Schedule,
"count": fmt.Sprintf("%d", count),
})
} else {
ow = utility.NewOutputWriterWithMap(map[string]string{
"database_id": bk.DatabaseID,
"database_name": bk.DatabaseName,
"software": bk.Software,
"name": name,
})
}

switch common.OutputFormat {
case "json":
ow.WriteSingleObjectJSON(common.PrettySet)
case "custom":
ow.WriteCustomOutput(common.OutputFields)
default:
fmt.Printf("Database backup (%s) for database %s has been created\n", utility.Green(bk.Scheduled.Name), utility.Green(bk.DatabaseName))
fmt.Printf("Database backup (%s) for database %s has been created\n", utility.Green(name), utility.Green(bk.DatabaseName))
}
},
}
67 changes: 50 additions & 17 deletions cmd/database/database_backup_list.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package database
import (
"fmt"
"os"
"strings"

"github.com/civo/cli/common"
"github.com/civo/cli/config"
Expand Down Expand Up @@ -45,34 +46,66 @@ var dbBackupListCmd = &cobra.Command{
return
}

ow := utility.NewOutputWriter()
ow.StartLine()
ow.AppendDataWithLabel("database_id", utility.TrimID(backups.DatabaseID), "Database ID")
ow.AppendDataWithLabel("database_name", backups.DatabaseName, "Database Name")
ow.AppendDataWithLabel("name", backups.Scheduled.Name, "Backup Name")
ow.AppendDataWithLabel("schedule", backups.Scheduled.Schedule, "Schedule")
ow.AppendDataWithLabel("count", fmt.Sprintf("%d", backups.Scheduled.Count), "Count")
odb := utility.NewOutputWriter()
ombk := utility.NewOutputWriter()
osbk := utility.NewOutputWriter()
mbk := ""
sbk := ""
isConfiguredScheduled := false
isConfiguredManual := false

bk := ""
for i, backup := range backups.Scheduled.Backups {
bk += backup
if i < len(backups.Scheduled.Backups)-1 {
bk += "\n"
odb.StartLine()
odb.AppendDataWithLabel("database_id", utility.TrimID(backups.DatabaseID), "Database ID")
odb.AppendDataWithLabel("database_name", backups.DatabaseName, "Database Name")
odb.AppendDataWithLabel("software", backups.Software, "Software")

if backups.Scheduled != nil {
isConfiguredScheduled = true
osbk.AppendDataWithLabel("name", backups.Scheduled.Name, "Backup Name")
osbk.AppendDataWithLabel("schedule", backups.Scheduled.Schedule, "Schedule")
osbk.AppendDataWithLabel("count", fmt.Sprintf("%d", backups.Scheduled.Count), "Count")

sbk = strings.TrimSuffix(strings.Join(backups.Scheduled.Backups, ","), ",")
osbk.AppendDataWithLabel("backups", sbk, "Backups")
}

if backups.Manual != nil {
isConfiguredManual = true
for i, m := range backups.Manual {
if i < len(backups.Manual)-1 {
mbk += m.Backup + ", "
} else {
mbk += m.Backup
}
}
ombk.AppendDataWithLabel("backups", mbk, "Backups")
}
ow.AppendDataWithLabel("backups", bk, "Backups")

if common.OutputFormat == "json" || common.OutputFormat == "custom" {
ow.AppendDataWithLabel("software", backups.Software, "Software")
odb.AppendDataWithLabel("database_id", utility.TrimID(backups.DatabaseID), "Database ID")
odb.AppendDataWithLabel("database_name", backups.DatabaseName, "Database Name")
odb.AppendDataWithLabel("software", backups.Software, "Software")
odb.AppendDataWithLabel("schedule_backup_name", backups.Scheduled.Name, "Schedule Backup Name")
odb.AppendDataWithLabel("schedule", backups.Scheduled.Schedule, "Schedule")
odb.AppendDataWithLabel("count", fmt.Sprintf("%d", backups.Scheduled.Count), "Count")
odb.AppendDataWithLabel("scheduled_backups", sbk, "Schedule Backups")
odb.AppendDataWithLabel("manual_backups", mbk, "Manual Backups")
}

switch common.OutputFormat {
case "json":
ow.WriteMultipleObjectsJSON(common.PrettySet)
odb.WriteMultipleObjectsJSON(common.PrettySet)
case "custom":
ow.WriteCustomOutput(common.OutputFields)
odb.WriteCustomOutput(common.OutputFields)
default:
ow.WriteTable()
if isConfiguredScheduled {
fmt.Println("Scheduled Backups:")
}
osbk.WriteTable()
if isConfiguredManual {
fmt.Println("Manual Backups:")
}
ombk.WriteTable()
}
},
}
2 changes: 1 addition & 1 deletion cmd/database/database_backup_update.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import (
var dbBackupUpdateCmd = &cobra.Command{
Use: "update",
Aliases: []string{"modify", "change"},
Short: "Update a database backup",
Short: "Update a scheduled database backup",
Example: "civo database backup update <DATABASE-NAME/ID> --name <NEW_BACKUP-NAME> --schedule <SCHEDULE> --count <COUNT>",
Args: cobra.MinimumNArgs(1),
Run: func(cmd *cobra.Command, args []string) {
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.52
github.com/civo/civogo v0.3.53
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
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -61,8 +61,8 @@ github.com/cespare/xxhash v1.1.0/go.mod h1:XrSqR1VqqWfGrhpAt58auRo0WTKS1nRRg3ghf
github.com/chzyer/logex v1.1.10/go.mod h1:+Ywpsq7O8HXn0nuIou7OrIPyXbp3wmkHB+jjWRnGsAI=
github.com/chzyer/readline v0.0.0-20180603132655-2972be24d48e/go.mod h1:nSuG5e5PlCu98SY8svDHJxuZscDgtXS6KTTbou5AhLI=
github.com/chzyer/test v0.0.0-20180213035817-a1ea475d72b1/go.mod h1:Q3SI9o4m/ZMnBNeIyt5eFwwo7qiLfzFZmjNmxjkiQlU=
github.com/civo/civogo v0.3.52 h1:x300yiN6N8cK3kd79RMxLCzsKCWnm4Nxv8+Nn8hjAEI=
github.com/civo/civogo v0.3.52/go.mod h1:54lv/FOf7gh6wE9ZwVdw4yBehk8V1CvU9aWa4v6dvW0=
github.com/civo/civogo v0.3.53 h1:PYqwTwt9YmeBRKMzfuTXBSPtE0WIsbLzhxD3V36bVlo=
github.com/civo/civogo v0.3.53/go.mod h1:54lv/FOf7gh6wE9ZwVdw4yBehk8V1CvU9aWa4v6dvW0=
github.com/client9/misspell v0.3.4/go.mod h1:qj6jICC3Q7zFZvVWo7KLAzC3yx5G7kyvSDkc90ppPyw=
github.com/coreos/bbolt v1.3.2/go.mod h1:iRUV2dpdMOn7Bo10OQBFzIJO9kkE559Wcmn+qkEiiKk=
github.com/coreos/etcd v3.3.13+incompatible/go.mod h1:uF7uidLiAD3TWHmW31ZFd/JWoc32PjwdhPthX9715RE=
Expand Down

0 comments on commit 97bdecc

Please sign in to comment.