-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
omegion
committed
Mar 1, 2021
1 parent
bf44d7c
commit 0b072a0
Showing
19 changed files
with
404 additions
and
204 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
package local | ||
|
||
import ( | ||
db "github.com/omegion/go-db-backup/pkg/database" | ||
"github.com/spf13/cobra" | ||
"log" | ||
) | ||
|
||
func setupImportCommand(cmd *cobra.Command) { | ||
cmd.Flags().String("file", "", "Backup file path") | ||
if err := cmd.MarkFlagRequired("file"); err != nil { | ||
log.Fatalf("Lethal damage: %s\n\n", err) | ||
} | ||
} | ||
|
||
func Import() *cobra.Command { | ||
cmd := &cobra.Command{ | ||
Use: "import", | ||
Short: "Import database backup from local", | ||
RunE: func(cmd *cobra.Command, args []string) error { | ||
dbType, _ := cmd.Flags().GetString("type") | ||
file, _ := cmd.Flags().GetString("file") | ||
host, _ := cmd.Flags().GetString("host") | ||
port, _ := cmd.Flags().GetString("port") | ||
databaseName, _ := cmd.Flags().GetString("database") | ||
username, _ := cmd.Flags().GetString("username") | ||
password, _ := cmd.Flags().GetString("password") | ||
|
||
options := db.Options{ | ||
Type: dbType, | ||
Host: host, | ||
Port: port, | ||
Name: databaseName, | ||
Username: username, | ||
Password: password, | ||
} | ||
|
||
database, err := GetDatabaseByType(options) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
_, err = database.Import(file) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
return nil | ||
}, | ||
} | ||
|
||
setupImportCommand(cmd) | ||
SetupExportCommand(cmd) | ||
|
||
return cmd | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
package local | ||
|
||
import "github.com/spf13/cobra" | ||
|
||
func Main() *cobra.Command { | ||
cmd := &cobra.Command{ | ||
Use: "local", | ||
Short: "dump Management", | ||
Long: "CLI command to manage backups", | ||
} | ||
|
||
cmd.AddCommand(Export()) | ||
cmd.AddCommand(Import()) | ||
|
||
return cmd | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
package s3 | ||
|
||
import ( | ||
"log" | ||
|
||
"github.com/omegion/go-db-backup/cmd/db-backup/command/local" | ||
db "github.com/omegion/go-db-backup/pkg/database" | ||
"github.com/omegion/go-db-backup/pkg/storage" | ||
"github.com/spf13/cobra" | ||
) | ||
|
||
func setupExportCommand(cmd *cobra.Command) { | ||
cmd.Flags().String("bucket", "", "Bucket name") | ||
if err := cmd.MarkFlagRequired("bucket"); err != nil { | ||
log.Fatalf("Lethal damage: %s\n\n", err) | ||
} | ||
|
||
cmd.Flags().String("endpoint", "", "S3 custom endpoint") | ||
if err := cmd.MarkFlagRequired("endpoint"); err != nil { | ||
log.Fatalf("Lethal damage: %s\n\n", err) | ||
} | ||
} | ||
|
||
func Export() *cobra.Command { | ||
cmd := &cobra.Command{ | ||
Use: "export", | ||
Short: "Export database to S3 bucket.", | ||
RunE: func(cmd *cobra.Command, args []string) error { | ||
dbType, _ := cmd.Flags().GetString("type") | ||
host, _ := cmd.Flags().GetString("host") | ||
port, _ := cmd.Flags().GetString("port") | ||
databaseName, _ := cmd.Flags().GetString("database") | ||
username, _ := cmd.Flags().GetString("username") | ||
password, _ := cmd.Flags().GetString("password") | ||
bucketName, _ := cmd.Flags().GetString("bucket") | ||
endpointURL, _ := cmd.Flags().GetString("endpoint") | ||
|
||
options := db.Options{ | ||
Type: dbType, | ||
Host: host, | ||
Port: port, | ||
Name: databaseName, | ||
Username: username, | ||
Password: password, | ||
} | ||
|
||
database, err := local.GetDatabaseByType(options) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
backup, err := database.Export() | ||
if err != nil { | ||
return err | ||
} | ||
|
||
err = backup.Save(&storage.S3{ | ||
Bucket: bucketName, | ||
EndpointURL: endpointURL, | ||
}) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
return nil | ||
}, | ||
} | ||
|
||
local.SetupExportCommand(cmd) | ||
setupExportCommand(cmd) | ||
|
||
return cmd | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
package s3 | ||
|
||
import "github.com/spf13/cobra" | ||
|
||
func Main() *cobra.Command { | ||
cmd := &cobra.Command{ | ||
Use: "s3", | ||
Short: "dump Management", | ||
Long: "CLI command to manage backups", | ||
} | ||
|
||
cmd.AddCommand(Export()) | ||
cmd.AddCommand(Import()) | ||
|
||
return cmd | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.