Skip to content

Commit

Permalink
Support manual backup
Browse files Browse the repository at this point in the history
Signed-off-by: Vishal Anarse <vishal@civo.com>
  • Loading branch information
Vishal Anarse committed Oct 28, 2023
1 parent c0a0868 commit 604ebfa
Show file tree
Hide file tree
Showing 4 changed files with 98 additions and 55 deletions.
3 changes: 2 additions & 1 deletion cmd/database/database_backup.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (

var name, schedule string
var count int
var manual bool

// dbBackupCmd is the root command for the db backup subcommand
var dbBackupCmd = &cobra.Command{
Expand All @@ -33,9 +34,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().BoolVarP(&manual, "manual", "m", false, "set only if backup is manual/instant")

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

// Update cmd options
dbBackupUpdateCmd.Flags().StringVarP(&name, "name", "n", "", "name of the database backup")
Expand Down
89 changes: 53 additions & 36 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> --manual`,
Short: "Create a new database backup",
Args: cobra.MinimumNArgs(1),
Run: func(cmd *cobra.Command, args []string) {
utility.EnsureCurrentRegion()

Expand All @@ -38,53 +39,69 @@ 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 !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.IsManual = manual
}

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 !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": bk.Scheduled.Name,
})
}

switch common.OutputFormat {
case "json":
ow.WriteSingleObjectJSON(common.PrettySet)
Expand Down
59 changes: 42 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,58 @@ 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 := ""

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.Schedule != "" {
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 {
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()
fmt.Println("Scheduled Backups:")
osbk.WriteTable()
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

0 comments on commit 604ebfa

Please sign in to comment.