Skip to content

Commit

Permalink
fix s3 custom bucket
Browse files Browse the repository at this point in the history
  • Loading branch information
omegion committed Mar 1, 2021
1 parent bf44d7c commit 0b072a0
Show file tree
Hide file tree
Showing 19 changed files with 404 additions and 204 deletions.
18 changes: 0 additions & 18 deletions cmd/db-backup/command/dump/main.go

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
package dump
package local

import (
db "github.com/omegion/go-db-backup/pkg/database"
"github.com/omegion/go-db-backup/pkg/store"
"github.com/spf13/cobra"
"log"
)

func setupDumpCommand(cmd *cobra.Command) {
func SetupExportCommand(cmd *cobra.Command) {
cmd.Flags().String("type", "postgres", "Database type")
cmd.Flags().String("host", "", "Host")
if err := cmd.MarkFlagRequired("host"); err != nil {
Expand Down Expand Up @@ -35,7 +34,7 @@ func setupDumpCommand(cmd *cobra.Command) {
}
}

func getDatabaseByType(options db.Options) (db.Database, error) {
func GetDatabaseByType(options db.Options) (db.Database, error) {
if options.Type == "postgres" {
return &db.Postgres{
Host: options.Host,
Expand All @@ -50,10 +49,10 @@ func getDatabaseByType(options db.Options) (db.Database, error) {
}
}

func Local() *cobra.Command {
func Export() *cobra.Command {
cmd := &cobra.Command{
Use: "local",
Short: "Print the version/build number",
Use: "export",
Short: "Export database table to local",
RunE: func(cmd *cobra.Command, args []string) error {
dbType, _ := cmd.Flags().GetString("type")
host, _ := cmd.Flags().GetString("host")
Expand All @@ -69,22 +68,14 @@ func Local() *cobra.Command {
Name: databaseName,
Username: username,
Password: password,
Options: []string{"--no-owner"},
}

database, err := getDatabaseByType(options)
database, err := GetDatabaseByType(options)
if err != nil {
return err
}

result, err := database.Export()
if err != nil {
return err
}

export := store.Local{}

err = result.To(&export)
_, err = database.Export()
if err != nil {
return err
}
Expand All @@ -93,7 +84,7 @@ func Local() *cobra.Command {
},
}

setupDumpCommand(cmd)
SetupExportCommand(cmd)

return cmd
}
56 changes: 56 additions & 0 deletions cmd/db-backup/command/local/import.go
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
}
16 changes: 16 additions & 0 deletions cmd/db-backup/command/local/main.go
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
}
73 changes: 73 additions & 0 deletions cmd/db-backup/command/s3/export.go
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
}
Original file line number Diff line number Diff line change
@@ -1,35 +1,34 @@
package dump
package s3

import (
"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/store"
"github.com/omegion/go-db-backup/pkg/storage"
"github.com/spf13/cobra"
"log"
)

func setupS3Command(cmd *cobra.Command) {
cmd.Flags().String("bucket-name", "", "Bucket Name")
if err := cmd.MarkFlagRequired("bucket-name"); err != nil {
func setupImportCommand(cmd *cobra.Command) {
cmd.Flags().String("path", "", "Backup path in S3")
if err := cmd.MarkFlagRequired("path"); err != nil {
log.Fatalf("Lethal damage: %s\n\n", err)
}

cmd.Flags().String("endpoint-url", "", "Custom Endpoint URL")

}

func S3() *cobra.Command {
func Import() *cobra.Command {
cmd := &cobra.Command{
Use: "s3",
Short: "Dumps database to S3 bucket",
Use: "import",
Short: "Import database backup from S3 bucket.",
RunE: func(cmd *cobra.Command, args []string) error {
dbType, _ := cmd.Flags().GetString("type")
path, _ := cmd.Flags().GetString("path")
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-name")
endpointURL, _ := cmd.Flags().GetString("endpoint-url")
bucketName, _ := cmd.Flags().GetString("bucket")
endpointURL, _ := cmd.Flags().GetString("endpoint")

options := db.Options{
Type: dbType,
Expand All @@ -38,25 +37,28 @@ func S3() *cobra.Command {
Name: databaseName,
Username: username,
Password: password,
Options: []string{"--no-owner"},
}

database, err := getDatabaseByType(options)
database, err := local.GetDatabaseByType(options)
if err != nil {
return err
}

result, err := database.Export()
if err != nil {
return err
backup := db.Backup{
Name: databaseName,
Path: path,
Host: host,
}

export := store.S3{
err = backup.Get(&storage.S3{
Bucket: bucketName,
EndpointURL: endpointURL,
})
if err != nil {
return err
}

err = result.To(&export)
_, err = database.Import(backup.Path)
if err != nil {
return err
}
Expand All @@ -65,8 +67,9 @@ func S3() *cobra.Command {
},
}

setupDumpCommand(cmd)
setupS3Command(cmd)
local.SetupExportCommand(cmd)
setupExportCommand(cmd)
setupImportCommand(cmd)

return cmd
}
16 changes: 16 additions & 0 deletions cmd/db-backup/command/s3/main.go
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
}
13 changes: 11 additions & 2 deletions cmd/db-backup/command/version_test.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,20 @@
package command

import (
"github.com/omegion/go-db-backup/cmd/db-backup/command/local"
"testing"
)

func TestTerraformConfig(t *testing.T) {
_, err := executeCommand(Version())
func TestVersionCommand(t *testing.T) {
_, err := executeCommand(local.Import(),
"--type=postgres",
"--file=2021-03-01T20:57:05+01:00.sql.tar.gz",
"--database=vault1",
"--host=db.omegion.dev",
"--password=p6tNYH*4UmrWCP&C9rB#5RdM",
"--port=2052",
"--username=omegion",
)

if err != nil {
t.Errorf("Command Error: %v", err)
Expand Down
6 changes: 4 additions & 2 deletions cmd/db-backup/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@ package main

import (
"github.com/omegion/go-db-backup/cmd/db-backup/command"
"github.com/omegion/go-db-backup/cmd/db-backup/command/dump"
"github.com/omegion/go-db-backup/cmd/db-backup/command/local"
"github.com/omegion/go-db-backup/cmd/db-backup/command/s3"
"github.com/spf13/cobra"
)

Expand All @@ -15,7 +16,8 @@ func RootCommand() *cobra.Command {
}

cmd.AddCommand(command.Version())
cmd.AddCommand(dump.Dump())
cmd.AddCommand(s3.Main())
cmd.AddCommand(local.Main())

return cmd
}
Expand Down
Loading

0 comments on commit 0b072a0

Please sign in to comment.