-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
dedicated mysql and postgres dump and restore commands
- Loading branch information
Showing
16 changed files
with
252 additions
and
24 deletions.
There are no files selected for viewing
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,47 @@ | ||
package cmd | ||
|
||
import ( | ||
"log" | ||
|
||
"github.com/allaboutapps/backup-ns/internal/lib" | ||
"github.com/spf13/cobra" | ||
) | ||
|
||
// mysqlRestoreCmd represents the restore command | ||
var mysqlRestoreCmd = &cobra.Command{ | ||
Use: "restore", | ||
Short: "Connects to the live mysql/mariadb container and creates a database restore", | ||
// Long: `...`, | ||
Run: func(_ *cobra.Command, _ []string) { | ||
config := lib.LoadConfig() | ||
|
||
if config.DryRun { | ||
log.Println("Dry run mode is active, write operations are skipped!") | ||
} | ||
|
||
if !config.MySQL.Enabled { | ||
log.Fatal("BAK_DB_MYSQL=true must be set.") | ||
} | ||
|
||
runMySQLRestore(config) | ||
}, | ||
} | ||
|
||
func init() { | ||
mysqlCmd.AddCommand(mysqlRestoreCmd) | ||
} | ||
|
||
func runMySQLRestore(config lib.Config) { | ||
if err := lib.EnsureResourceAvailable(config.Namespace, config.MySQL.ExecResource); err != nil { | ||
log.Fatal(err) | ||
} | ||
if err := lib.EnsureMySQLAvailable(config.Namespace, config.MySQL); err != nil { | ||
log.Fatal(err) | ||
} | ||
|
||
if err := lib.RestoreMySQL(config.Namespace, config.DryRun, config.MySQL); err != nil { | ||
log.Fatal(err) | ||
} | ||
|
||
log.Printf("Finished mysql restore in namespace='%s'!", config.Namespace) | ||
} |
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,48 @@ | ||
package cmd | ||
|
||
import ( | ||
"log" | ||
|
||
"github.com/allaboutapps/backup-ns/internal/lib" | ||
"github.com/spf13/cobra" | ||
) | ||
|
||
// postgresRestoreCmd represents the dump command | ||
var postgresRestoreCmd = &cobra.Command{ | ||
Use: "restore", | ||
Short: "Connects to the live postgres container and restores the database dump", | ||
// Long: `...`, | ||
Run: func(_ *cobra.Command, _ []string) { | ||
config := lib.LoadConfig() | ||
|
||
if config.DryRun { | ||
log.Println("Dry run mode is active, write operations are skipped!") | ||
} | ||
|
||
if !config.Postgres.Enabled { | ||
log.Fatal("BAK_DB_POSTGRES=true must be set.") | ||
} | ||
|
||
runPostgresRestore(config) | ||
}, | ||
} | ||
|
||
func init() { | ||
postgresCmd.AddCommand(postgresRestoreCmd) | ||
} | ||
|
||
func runPostgresRestore(config lib.Config) { | ||
if err := lib.EnsureResourceAvailable(config.Namespace, config.Postgres.ExecResource); err != nil { | ||
log.Fatal(err) | ||
} | ||
if err := lib.EnsurePostgresAvailable(config.Namespace, config.Postgres); err != nil { | ||
log.Fatal(err) | ||
} | ||
|
||
if err := lib.RestorePostgres(config.Namespace, config.DryRun, config.Postgres); err != nil { | ||
log.Fatal(err) | ||
} | ||
|
||
log.Printf("Finished postgres restore in namespace='%s'!", config.Namespace) | ||
|
||
} |
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
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
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
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,20 @@ | ||
#!/bin/bash | ||
|
||
# inject default MYSQL_PWD into current env (before cmds are visible in logs) | ||
export MYSQL_PWD="{{.Password}}" | ||
|
||
set -Eeox pipefail | ||
|
||
# ensure the dump file exists... | ||
[ -s {{.DumpFile}} ] || exit 1 | ||
|
||
# print dump file info | ||
ls -lha {{.DumpFile}} | ||
|
||
# restore from dump file | ||
gzip -dc {{.DumpFile}} | mysql \ | ||
--host={{.Host}} \ | ||
--port={{.Port}} \ | ||
--user={{.User}} \ | ||
--default-character-set={{.DefaultCharacterSet}} \ | ||
{{.DB}} |
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
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,15 @@ | ||
#!/bin/bash | ||
|
||
# inject default PGPASSWORD into current env (before cmds are visible in logs) | ||
export PGPASSWORD="{{.Password}}" | ||
|
||
set -Eeox pipefail | ||
|
||
# ensure the dump file exists... | ||
[ -s {{.DumpFile}} ] || exit 1 | ||
|
||
# print dump file info | ||
ls -lha {{.DumpFile}} | ||
|
||
# restore from dump file | ||
gzip -dc {{.DumpFile}} | psql --host {{.Host}} --port {{.Port}} --username={{.User}} {{.DB}} |
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.