Skip to content

Commit

Permalink
Add support for db restore for postgres (#338)
Browse files Browse the repository at this point in the history
Signed-off-by: Vishal Anarse <vishal@civo.com>
Co-authored-by: Vishal Anarse <vishal@civo.com>
  • Loading branch information
vishalanarase and Vishal Anarse authored Sep 1, 2023
1 parent 9852272 commit 5885a7a
Show file tree
Hide file tree
Showing 5 changed files with 89 additions and 3 deletions.
4 changes: 4 additions & 0 deletions cmd/database/database.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ func init() {
DBCmd.AddCommand(dbSizeCmd)
DBCmd.AddCommand(dbEngineCmd)
DBCmd.AddCommand(dbBackupCmd)
DBCmd.AddCommand(dbRestoreCmd)

dbCredentialCmd.Flags().BoolVarP(&connectionString, "connection-string", "c", false, "show the connection string for the database")

Expand All @@ -49,4 +50,7 @@ func init() {
dbUpdateCmd.Flags().IntVarP(&nodes, "nodes", "", 0, "the number of nodes for the database")
dbUpdateCmd.Flags().StringVarP(&firewallID, "firewall", "", "", "the firewall to use for the database")
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.MarkFlagRequired("backup")
}
67 changes: 67 additions & 0 deletions cmd/database/database_restore.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
package database

import (
"fmt"

"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 backup 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>",
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
}

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

if utility.UserConfirmedRestore(db.Name, common.DefaultYes, backup) {
config := &civogo.RestoreDatabaseRequest{
Software: db.Software,
Backup: backup,
}
_, err = client.RestoreDatabase(db.ID, config)
if err != nil {
utility.Error("%s", err)
os.Exit(1)
}
ow := utility.NewOutputWriter()
switch common.OutputFormat {
case "json":
ow.WriteSingleObjectJSON(common.PrettySet)
case "custom":
ow.WriteCustomOutput(common.OutputFields)
default:
fmt.Printf("Restoring database %s from from backup %s\n", utility.Green(db.Name), utility.Green(backup))
}
} else {
fmt.Println("Aborted")
}

},
}
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.44
github.com/civo/civogo v0.3.45
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.44 h1:HGrnnD47gpYm96MFGwJjOomxrOf40Zvo/WG5JbqlmQA=
github.com/civo/civogo v0.3.44/go.mod h1:54lv/FOf7gh6wE9ZwVdw4yBehk8V1CvU9aWa4v6dvW0=
github.com/civo/civogo v0.3.45 h1:H9rrCKhO3vAQ249koZODdE/ESAXWAZQaYuQafqzJZr8=
github.com/civo/civogo v0.3.45/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
15 changes: 15 additions & 0 deletions utility/confirmation.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,21 @@ func UserConfirmedUnassign(resourceType string, ignoringConfirmed bool, objectTo
return true
}

// UserConfirmedUnassign builds a message to ask the user to confirm unassign
// a resource and then sends it through to AskForConfirm to
// parses and verifies user input.
func UserConfirmedRestore(resourceType string, ignoringConfirmed bool, objectToDelete string) bool {
if !ignoringConfirmed {
message := fmt.Sprintf("restore db %s from %s backup", Green(resourceType), Green(objectToDelete))
err := AskForConfirm(message)
if err != nil {
return false
}
}

return true
}

// UserConfirmedOverwrite builds a message to ask the user to confirm overwrite config
// and then sends it through to AskForConfirm to
// parses and verifies user input.
Expand Down

0 comments on commit 5885a7a

Please sign in to comment.