Skip to content

Commit

Permalink
Add support for mysql backup and restore
Browse files Browse the repository at this point in the history
Signed-off-by: Vishal Anarase <[email protected]>
  • Loading branch information
Vishal Anarase committed Jan 31, 2024
1 parent 8995e3d commit 11ac765
Show file tree
Hide file tree
Showing 9 changed files with 120 additions and 74 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,4 @@ temporal*
.DS*
get.sh
.vscode
staging.civo.json
2 changes: 2 additions & 0 deletions cmd/database/database.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,5 +52,7 @@ func init() {
dbUpdateCmd.Flags().StringVarP(&updatedName, "name", "n", "", "the new name for the database")

dbRestoreCmd.Flags().StringVarP(&backup, "backup", "b", "", "the backup name which you can restore database")
dbRestoreCmd.Flags().StringVarP(&restoreName, "name", "n", "", "name of the restore")
dbRestoreCmd.MarkFlagRequired("backup")
dbRestoreCmd.MarkFlagRequired("name")
}
1 change: 0 additions & 1 deletion cmd/database/database_backup.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,5 +40,4 @@ func init() {
// Update cmd options
dbBackupUpdateCmd.Flags().StringVarP(&name, "name", "n", "", "name of the database backup")
dbBackupUpdateCmd.Flags().StringVarP(&schedule, "schedule", "s", "", "schedule of the database backup in the form of cronjob")
dbBackupUpdateCmd.Flags().IntVarP(&count, "count", "c", 0, "number of backups to keep")
}
5 changes: 2 additions & 3 deletions cmd/database/database_backup_create.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,9 +89,8 @@ var dbBackupCreateCmd = &cobra.Command{
"database_id": bk.DatabaseID,
"database_name": bk.DatabaseName,
"software": bk.Software,
"name": bk.Scheduled.Name,
"schedule": bk.Scheduled.Schedule,
"count": fmt.Sprintf("%d", count),
"name": bk.Name,
"schedule": bk.Schedule,
})
} else {
ow = utility.NewOutputWriterWithMap(map[string]string{
Expand Down
159 changes: 102 additions & 57 deletions cmd/database/database_backup_list.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ package database
import (
"fmt"
"os"
"strings"

"github.com/civo/civogo"
"github.com/civo/cli/common"
"github.com/civo/cli/config"
"github.com/civo/cli/utility"
Expand Down Expand Up @@ -42,70 +42,115 @@ var dbBackupListCmd = &cobra.Command{
os.Exit(1)
}

if backups.DatabaseID == "" {
return
switch db.Software {
case "PostgreSQL":
postgresScheduledBackups(backups)
postgresManualBackups(backups)
case "MySQL":
mysqlBackups(backups)
}
},
}

func postgresScheduledBackups(backups *civogo.PaginatedDatabaseBackup) {
ow := utility.NewOutputWriter()
printMsg := false
for _, bk := range backups.Items {
if !bk.IsScheduled {
continue
}
printMsg = true
ow.StartLine()
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")

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

switch common.OutputFormat {
case "json":
ow.WriteMultipleObjectsJSON(common.PrettySet)
case "custom":
ow.WriteCustomOutput(common.OutputFields)
default:
if printMsg {
fmt.Println("Scheduled backup")
}
ow.WriteTable()
}
}

func postgresManualBackups(backups *civogo.PaginatedDatabaseBackup) {
ow := utility.NewOutputWriter()
printMsg := false
for _, bk := range backups.Items {
if bk.IsScheduled {
continue
}
printMsg = true
ow.StartLine()
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")

odb := utility.NewOutputWriter()
ombk := utility.NewOutputWriter()
osbk := utility.NewOutputWriter()
mbk := ""
sbk := ""
isConfiguredScheduled := false
isConfiguredManual := false

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 common.OutputFormat == "json" || common.OutputFormat == "custom" {
ow.AppendDataWithLabel("database_id", bk.DatabaseID, "Database ID")
}
}

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")
switch common.OutputFormat {
case "json":
ow.WriteMultipleObjectsJSON(common.PrettySet)
case "custom":
ow.WriteCustomOutput(common.OutputFields)
default:
if printMsg {
fmt.Println("Manual backups")
}
ow.WriteTable()
}
}

func mysqlBackups(backups *civogo.PaginatedDatabaseBackup) {
ow := utility.NewOutputWriter()
printMsg := false
for _, bk := range backups.Items {
printMsg = true
ow.StartLine()
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" {
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")
ow.AppendDataWithLabel("id", bk.ID, "ID")
ow.AppendDataWithLabel("database_id", bk.DatabaseID, "Database ID")
ow.AppendDataWithLabel("database_name", bk.DatabaseName, "Database Name")
}
}

switch common.OutputFormat {
case "json":
odb.WriteMultipleObjectsJSON(common.PrettySet)
case "custom":
odb.WriteCustomOutput(common.OutputFields)
default:
if isConfiguredScheduled {
fmt.Println("Scheduled Backups:")
}
osbk.WriteTable()
if isConfiguredManual {
fmt.Println("Manual Backups:")
}
ombk.WriteTable()
switch common.OutputFormat {
case "json":
ow.WriteMultipleObjectsJSON(common.PrettySet)
case "custom":
ow.WriteCustomOutput(common.OutputFields)
default:
if printMsg {
fmt.Println("Manual backups")
}
},
ow.WriteTable()
}
}
12 changes: 4 additions & 8 deletions 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> --count <COUNT>",
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 Expand Up @@ -48,9 +48,6 @@ var dbBackupUpdateCmd = &cobra.Command{
if schedule != "" {
backupUpdateConfig.Schedule = schedule
}
if count >= 0 {
backupUpdateConfig.Count = int32(count)
}
if name != "" {
backupUpdateConfig.Name = name
}
Expand All @@ -69,17 +66,16 @@ var dbBackupUpdateCmd = &cobra.Command{
"database_id": bk.DatabaseID,
"database_name": bk.DatabaseName,
"software": bk.Software,
"name": bk.Scheduled.Name,
"schedule": bk.Scheduled.Schedule,
"count": fmt.Sprintf("%d", count),
"backup_name": bk.Name,
"schedule": bk.Schedule,
})
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 update\n", utility.Green(bk.Scheduled.Name), utility.Green(bk.DatabaseName))
fmt.Printf("Database backup (%s) for database %s has been update\n", utility.Green(bk.Name), utility.Green(bk.DatabaseName))
}
},
}
8 changes: 6 additions & 2 deletions cmd/database/database_restore.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,16 @@ import (
"github.com/spf13/cobra"
)

var backup string
var (
backup string
restoreName string
)

var dbRestoreCmd = &cobra.Command{
Use: "restore",
Aliases: []string{"reset", "restores"},
Short: "Restore a database",
Example: "civo db restore <DATABASE-NAME/ID> --backup <BACKUP-NAME>",
Example: "civo db restore <DATABASE-NAME/ID> --name <RESTORE-NAME> --backup <BACKUP-NAME>",
Args: cobra.MinimumNArgs(1),
Run: func(cmd *cobra.Command, args []string) {
utility.EnsureCurrentRegion()
Expand All @@ -43,6 +46,7 @@ var dbRestoreCmd = &cobra.Command{
if utility.UserConfirmedRestore(db.Name, common.DefaultYes, backup) {
config := &civogo.RestoreDatabaseRequest{
Software: db.Software,
Name: restoreName,
Backup: backup,
Region: client.Region,
}
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.56
github.com/civo/civogo v0.3.58
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
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.56 h1:t5u/OwExb2ibVmIXsUprV5m++bAqBRLO9uMcodNHZek=
github.com/civo/civogo v0.3.56/go.mod h1:54lv/FOf7gh6wE9ZwVdw4yBehk8V1CvU9aWa4v6dvW0=
github.com/civo/civogo v0.3.58 h1:FikCbwAKB5ovD8+NmSi7rzYBSpC4nUpGVpCyngEkTo4=
github.com/civo/civogo v0.3.58/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 11ac765

Please sign in to comment.