mirror of
https://github.com/jkaninda/mysql-bkup.git
synced 2025-12-08 14:39:41 +01:00
Compare commits
18 Commits
v1.2.27
...
002c93a796
| Author | SHA1 | Date | |
|---|---|---|---|
| 002c93a796 | |||
|
|
907e70d552 | ||
| 696477fe5c | |||
|
|
56a8b51660 | ||
| c76a00139c | |||
| 0f43871765 | |||
| 9ba6abe3f4 | |||
|
|
764583d88f | ||
|
|
dbf4dc596a | ||
|
|
06c89a9b78 | ||
| ec8bdd806c | |||
|
|
828b11c6dd | ||
| 1d01e13909 | |||
| bd65db2418 | |||
| 75b809511e | |||
| fc028a2c55 | |||
| 7fa0c6a118 | |||
| 661702a97e |
@@ -27,6 +27,7 @@ linters:
|
||||
- gosimple
|
||||
- govet
|
||||
- ineffassign
|
||||
# - lll
|
||||
- misspell
|
||||
- nakedret
|
||||
- prealloc
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
FROM golang:1.23.5 AS build
|
||||
FROM golang:1.24.1 AS build
|
||||
WORKDIR /app
|
||||
ARG appVersion=""
|
||||
|
||||
@@ -10,7 +10,7 @@ RUN go mod download
|
||||
# Build
|
||||
RUN CGO_ENABLED=0 GOOS=linux go build -ldflags="-X 'github.com/jkaninda/mysql-bkup/utils.Version=${appVersion}'" -o /app/mysql-bkup
|
||||
|
||||
FROM alpine:3.21.2
|
||||
FROM alpine:3.21.3
|
||||
ENV TZ=UTC
|
||||
ARG WORKDIR="/config"
|
||||
ARG BACKUPDIR="/backup"
|
||||
|
||||
13
README.md
13
README.md
@@ -74,6 +74,7 @@ To run a one time backup, bind your local volume to `/backup` in the container a
|
||||
docker run --rm --network your_network_name \
|
||||
-v $PWD/backup:/backup/ \
|
||||
-e "DB_HOST=dbhost" \
|
||||
-e "DB_PORT=3306" \
|
||||
-e "DB_USERNAME=username" \
|
||||
-e "DB_PASSWORD=password" \
|
||||
jkaninda/mysql-bkup backup -d database_name
|
||||
@@ -87,7 +88,19 @@ Alternatively, pass a `--env-file` in order to use a full config as described be
|
||||
-v $PWD/backup:/backup/ \
|
||||
jkaninda/mysql-bkup backup -d database_name
|
||||
```
|
||||
### Simple restore using Docker CLI
|
||||
|
||||
To restore a database, bind your local volume to `/backup` in the container and run the `restore` command:
|
||||
|
||||
```shell
|
||||
docker run --rm --network your_network_name \
|
||||
-v $PWD/backup:/backup/ \
|
||||
-e "DB_HOST=dbhost" \
|
||||
-e "DB_PORT=3306" \
|
||||
-e "DB_USERNAME=username" \
|
||||
-e "DB_PASSWORD=password" \
|
||||
jkaninda/mysql-bkup restore -d database_name -f backup_file.sql.gz
|
||||
```
|
||||
### Simple backup in docker compose file
|
||||
|
||||
```yaml
|
||||
|
||||
@@ -44,7 +44,7 @@ var BackupCmd = &cobra.Command{
|
||||
}
|
||||
|
||||
func init() {
|
||||
//Backup
|
||||
// Backup
|
||||
BackupCmd.PersistentFlags().StringP("storage", "s", "local", "Define storage: local, s3, ssh, ftp, azure")
|
||||
BackupCmd.PersistentFlags().StringP("path", "P", "", "Storage path without file name. e.g: /custom_path or ssh remote path `/home/foo/backup`")
|
||||
BackupCmd.PersistentFlags().StringP("cron-expression", "e", "", "Backup cron expression (e.g., `0 0 * * *` or `@daily`)")
|
||||
|
||||
@@ -46,7 +46,7 @@ var RestoreCmd = &cobra.Command{
|
||||
}
|
||||
|
||||
func init() {
|
||||
//Restore
|
||||
// Restore
|
||||
RestoreCmd.PersistentFlags().StringP("file", "f", "", "File name of database")
|
||||
RestoreCmd.PersistentFlags().StringP("storage", "s", "local", "Define storage: local, s3, ssh, ftp")
|
||||
RestoreCmd.PersistentFlags().StringP("path", "P", "", "AWS S3 path without file name. eg: /custom_path or ssh remote path `/home/foo/backup`")
|
||||
|
||||
@@ -38,7 +38,6 @@ var rootCmd = &cobra.Command{
|
||||
Example: utils.MainExample,
|
||||
Version: appVersion,
|
||||
}
|
||||
var operation = ""
|
||||
|
||||
// Execute adds all child commands to the root command and sets flags appropriately.
|
||||
// This is called by main.main(). It only needs to happen once to the rootCmd.
|
||||
|
||||
@@ -18,6 +18,7 @@ To run a one-time backup, bind your local volume to `/backup` in the container a
|
||||
docker run --rm --network your_network_name \
|
||||
-v $PWD/backup:/backup/ \
|
||||
-e "DB_HOST=dbhost" \
|
||||
-e "DB_PORT=3306" \
|
||||
-e "DB_USERNAME=username" \
|
||||
-e "DB_PASSWORD=password" \
|
||||
jkaninda/mysql-bkup backup -d database_name
|
||||
@@ -34,6 +35,19 @@ docker run --rm --network your_network_name \
|
||||
jkaninda/mysql-bkup backup -d database_name
|
||||
```
|
||||
|
||||
### Simple restore using Docker CLI
|
||||
|
||||
To restore a database, bind your local volume to `/backup` in the container and run the `restore` command:
|
||||
|
||||
```shell
|
||||
docker run --rm --network your_network_name \
|
||||
-v $PWD/backup:/backup/ \
|
||||
-e "DB_HOST=dbhost" \
|
||||
-e "DB_PORT=3306" \
|
||||
-e "DB_USERNAME=username" \
|
||||
-e "DB_PASSWORD=password" \
|
||||
jkaninda/mysql-bkup restore -d database_name -f backup_file.sql.gz
|
||||
```
|
||||
---
|
||||
|
||||
## Simple Backup Using Docker Compose
|
||||
|
||||
6
go.mod
6
go.mod
@@ -2,14 +2,15 @@ module github.com/jkaninda/mysql-bkup
|
||||
|
||||
go 1.23.2
|
||||
|
||||
require github.com/spf13/pflag v1.0.5 // indirect
|
||||
require github.com/spf13/pflag v1.0.6 // indirect
|
||||
|
||||
require (
|
||||
github.com/go-mail/mail v2.3.1+incompatible
|
||||
github.com/jkaninda/encryptor v0.0.0-20241111100652-926393c9437e
|
||||
github.com/jkaninda/go-storage v0.1.3
|
||||
github.com/jkaninda/go-utils v0.1.1
|
||||
github.com/robfig/cron/v3 v3.0.1
|
||||
github.com/spf13/cobra v1.8.1
|
||||
github.com/spf13/cobra v1.9.1
|
||||
gopkg.in/yaml.v3 v3.0.1
|
||||
)
|
||||
|
||||
@@ -26,7 +27,6 @@ require (
|
||||
github.com/hashicorp/errwrap v1.1.0 // indirect
|
||||
github.com/hashicorp/go-multierror v1.1.1 // indirect
|
||||
github.com/inconshreveable/mousetrap v1.1.0 // indirect
|
||||
github.com/jkaninda/go-utils v0.0.0-20250122060806-26119182077a // indirect
|
||||
github.com/jlaffaye/ftp v0.2.0 // indirect
|
||||
github.com/jmespath/go-jmespath v0.4.0 // indirect
|
||||
github.com/pkg/errors v0.9.1 // indirect
|
||||
|
||||
16
go.sum
16
go.sum
@@ -22,7 +22,7 @@ github.com/bramvdbogaerde/go-scp v1.5.0 h1:a9BinAjTfQh273eh7vd3qUgmBC+bx+3TRDtkZ
|
||||
github.com/bramvdbogaerde/go-scp v1.5.0/go.mod h1:on2aH5AxaFb2G0N5Vsdy6B0Ml7k9HuHSwfo1y0QzAbQ=
|
||||
github.com/cloudflare/circl v1.3.7 h1:qlCDlTPz2n9fu58M0Nh1J/JzcFpfgkFHHX3O35r5vcU=
|
||||
github.com/cloudflare/circl v1.3.7/go.mod h1:sRTcRWXGLrKw6yIGJ+l7amYJFfAXbZG0kBSc8r4zxgA=
|
||||
github.com/cpuguy83/go-md2man/v2 v2.0.4/go.mod h1:tgQtvFlXSQOSOSIRvRPT7W67SCa46tRHOmNcaadrF8o=
|
||||
github.com/cpuguy83/go-md2man/v2 v2.0.6/go.mod h1:oOW0eioCTA6cOiMLiUPZOpcVxMig6NIQQ7OS05n1F4g=
|
||||
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
|
||||
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
|
||||
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
|
||||
@@ -43,10 +43,8 @@ github.com/jkaninda/encryptor v0.0.0-20241111100652-926393c9437e h1:jtFKZHt/PLGQ
|
||||
github.com/jkaninda/encryptor v0.0.0-20241111100652-926393c9437e/go.mod h1:Y1EXpPWQ9PNd7y7E6ez3xgnzZc8fuDWXwX/1/dXNCE4=
|
||||
github.com/jkaninda/go-storage v0.1.3 h1:lEpHVgFLKSvjsi/6tAek96Y07za3vxmsXF2/+jiCMZU=
|
||||
github.com/jkaninda/go-storage v0.1.3/go.mod h1:zVRnLprBk/9AUz2+za6Y03MgoNYrqKLy3edVtjqMaps=
|
||||
github.com/jkaninda/go-utils v0.0.0-20250122054739-d330fecee150 h1:AgcKk58P/Z+u4DBE2MTyZ6kCweA89YUpX7TuttHS3oQ=
|
||||
github.com/jkaninda/go-utils v0.0.0-20250122054739-d330fecee150/go.mod h1:pf0/U6k4JbxlablM2G4eSTZdQ2LFshfAsCK5Q8qNfGo=
|
||||
github.com/jkaninda/go-utils v0.0.0-20250122060806-26119182077a h1:ZTpKujQGhEF266RWkD2cXnCsafk3R2+sGtfbzCQSs1s=
|
||||
github.com/jkaninda/go-utils v0.0.0-20250122060806-26119182077a/go.mod h1:pf0/U6k4JbxlablM2G4eSTZdQ2LFshfAsCK5Q8qNfGo=
|
||||
github.com/jkaninda/go-utils v0.1.1 h1:PMrtXR9d51YzHo85y9Z6YVL0YyBURbRTPemHVbFDqZg=
|
||||
github.com/jkaninda/go-utils v0.1.1/go.mod h1:pf0/U6k4JbxlablM2G4eSTZdQ2LFshfAsCK5Q8qNfGo=
|
||||
github.com/jlaffaye/ftp v0.2.0 h1:lXNvW7cBu7R/68bknOX3MrRIIqZ61zELs1P2RAiA3lg=
|
||||
github.com/jlaffaye/ftp v0.2.0/go.mod h1:is2Ds5qkhceAPy2xD6RLI6hmp/qysSoymZ+Z2uTnspI=
|
||||
github.com/jmespath/go-jmespath v0.4.0 h1:BEgLn5cpjn8UN1mAw4NjwDrS35OdebyEtFe+9YPoQUg=
|
||||
@@ -70,10 +68,10 @@ github.com/robfig/cron/v3 v3.0.1/go.mod h1:eQICP3HwyT7UooqI/z+Ov+PtYAWygg1TEWWzG
|
||||
github.com/rogpeppe/go-internal v1.12.0 h1:exVL4IDcn6na9z1rAb56Vxr+CgyK3nn3O+epU5NdKM8=
|
||||
github.com/rogpeppe/go-internal v1.12.0/go.mod h1:E+RYuTGaKKdloAfM02xzb0FW3Paa99yedzYV+kq4uf4=
|
||||
github.com/russross/blackfriday/v2 v2.1.0/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM=
|
||||
github.com/spf13/cobra v1.8.1 h1:e5/vxKd/rZsfSJMUX1agtjeTDf+qv1/JdBF8gg5k9ZM=
|
||||
github.com/spf13/cobra v1.8.1/go.mod h1:wHxEcudfqmLYa8iTfL+OuZPbBZkmvliBWKIezN3kD9Y=
|
||||
github.com/spf13/pflag v1.0.5 h1:iy+VFUOCP1a+8yFto/drg2CJ5u0yRoB7fZw3DKv/JXA=
|
||||
github.com/spf13/pflag v1.0.5/go.mod h1:McXfInJRrz4CZXVZOBLb0bTZqETkiAhM9Iw0y3An2Bg=
|
||||
github.com/spf13/cobra v1.9.1 h1:CXSaggrXdbHK9CF+8ywj8Amf7PBRmPCOJugH954Nnlo=
|
||||
github.com/spf13/cobra v1.9.1/go.mod h1:nDyEzZ8ogv936Cinf6g1RU9MRY64Ir93oCnqb9wxYW0=
|
||||
github.com/spf13/pflag v1.0.6 h1:jFzHGLGAlb3ruxLB8MhbI6A8+AQX/2eW4qeyNZXNp2o=
|
||||
github.com/spf13/pflag v1.0.6/go.mod h1:McXfInJRrz4CZXVZOBLb0bTZqETkiAhM9Iw0y3An2Bg=
|
||||
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
|
||||
github.com/stretchr/testify v1.9.0 h1:HtqpIVDClZ4nwg75+f6Lvsy/wHu+3BoSGCbBAcpTsTg=
|
||||
github.com/stretchr/testify v1.9.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY=
|
||||
|
||||
@@ -39,7 +39,11 @@ func azureBackup(db *dbConfig, config *BackupConfig) {
|
||||
utils.Info("Backup database to Azure Blob Storage")
|
||||
|
||||
// Backup database
|
||||
BackupDatabase(db, config.backupFileName, disableCompression)
|
||||
err := BackupDatabase(db, config.backupFileName, disableCompression)
|
||||
if err != nil {
|
||||
recoverMode(err, "Error backing up database")
|
||||
return
|
||||
}
|
||||
finalFileName := config.backupFileName
|
||||
if config.encryption {
|
||||
encryptBackup(config)
|
||||
|
||||
@@ -72,13 +72,17 @@ func scheduledMode(db *dbConfig, config *BackupConfig) {
|
||||
|
||||
// Test backup
|
||||
utils.Info("Testing backup configurations...")
|
||||
testDatabaseConnection(db)
|
||||
err := testDatabaseConnection(db)
|
||||
if err != nil {
|
||||
utils.Error("Error connecting to database: %s", db.dbName)
|
||||
utils.Fatal("Error: %s", err)
|
||||
}
|
||||
utils.Info("Testing backup configurations...done")
|
||||
utils.Info("Creating backup job...")
|
||||
// Create a new cron instance
|
||||
c := cron.New()
|
||||
|
||||
_, err := c.AddFunc(config.cronExpression, func() {
|
||||
_, err = c.AddFunc(config.cronExpression, func() {
|
||||
BackupTask(db, config)
|
||||
utils.Info("Next backup time is: %v", utils.CronNextTime(config.cronExpression).Format(timeFormat))
|
||||
|
||||
@@ -147,6 +151,7 @@ func startMultiBackup(bkConfig *BackupConfig, configFile string) {
|
||||
if bkConfig.cronExpression == "" {
|
||||
multiBackupTask(conf.Databases, bkConfig)
|
||||
} else {
|
||||
backupRescueMode = conf.BackupRescueMode
|
||||
// Check if cronExpression is valid
|
||||
if utils.IsValidCronExpression(bkConfig.cronExpression) {
|
||||
utils.Info("Running backup in Scheduled mode")
|
||||
@@ -157,7 +162,11 @@ func startMultiBackup(bkConfig *BackupConfig, configFile string) {
|
||||
// Test backup
|
||||
utils.Info("Testing backup configurations...")
|
||||
for _, db := range conf.Databases {
|
||||
testDatabaseConnection(getDatabase(db))
|
||||
err = testDatabaseConnection(getDatabase(db))
|
||||
if err != nil {
|
||||
recoverMode(err, fmt.Sprintf("Error connecting to database: %s", db.Name))
|
||||
continue
|
||||
}
|
||||
}
|
||||
utils.Info("Testing backup configurations...done")
|
||||
utils.Info("Creating backup job...")
|
||||
@@ -187,16 +196,19 @@ func startMultiBackup(bkConfig *BackupConfig, configFile string) {
|
||||
}
|
||||
|
||||
// BackupDatabase backup database
|
||||
func BackupDatabase(db *dbConfig, backupFileName string, disableCompression bool) {
|
||||
func BackupDatabase(db *dbConfig, backupFileName string, disableCompression bool) error {
|
||||
storagePath = os.Getenv("STORAGE_PATH")
|
||||
|
||||
utils.Info("Starting database backup...")
|
||||
|
||||
err := os.Setenv("MYSQL_PWD", db.dbPassword)
|
||||
if err != nil {
|
||||
return
|
||||
return fmt.Errorf("failed to set MYSQL_PWD environment variable: %v", err)
|
||||
}
|
||||
err = testDatabaseConnection(db)
|
||||
if err != nil {
|
||||
return fmt.Errorf("failed to connect to the database: %v", err)
|
||||
}
|
||||
testDatabaseConnection(db)
|
||||
// Backup Database database
|
||||
utils.Info("Backing up database...")
|
||||
|
||||
@@ -211,24 +223,24 @@ func BackupDatabase(db *dbConfig, backupFileName string, disableCompression bool
|
||||
)
|
||||
output, err := cmd.Output()
|
||||
if err != nil {
|
||||
utils.Fatal(err.Error())
|
||||
return fmt.Errorf("failed to backup database: %v", err)
|
||||
}
|
||||
|
||||
// save output
|
||||
file, err := os.Create(filepath.Join(tmpPath, backupFileName))
|
||||
if err != nil {
|
||||
utils.Fatal(err.Error())
|
||||
return fmt.Errorf("failed to create backup file: %v", err)
|
||||
}
|
||||
defer func(file *os.File) {
|
||||
err := file.Close()
|
||||
if err != nil {
|
||||
utils.Fatal(err.Error())
|
||||
return
|
||||
}
|
||||
}(file)
|
||||
|
||||
_, err = file.Write(output)
|
||||
if err != nil {
|
||||
utils.Fatal(err.Error())
|
||||
return err
|
||||
}
|
||||
utils.Info("Database has been backed up")
|
||||
|
||||
@@ -237,14 +249,14 @@ func BackupDatabase(db *dbConfig, backupFileName string, disableCompression bool
|
||||
cmd := exec.Command("mysqldump", "-h", db.dbHost, "-P", db.dbPort, "-u", db.dbUserName, db.dbName)
|
||||
stdout, err := cmd.StdoutPipe()
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
return fmt.Errorf("failed to backup database: %v", err)
|
||||
}
|
||||
gzipCmd := exec.Command("gzip")
|
||||
gzipCmd.Stdin = stdout
|
||||
gzipCmd.Stdout, err = os.Create(filepath.Join(tmpPath, backupFileName))
|
||||
err = gzipCmd.Start()
|
||||
if err != nil {
|
||||
return
|
||||
return fmt.Errorf("failed to backup database: %v", err)
|
||||
}
|
||||
if err := cmd.Run(); err != nil {
|
||||
log.Fatal(err)
|
||||
@@ -252,13 +264,18 @@ func BackupDatabase(db *dbConfig, backupFileName string, disableCompression bool
|
||||
if err := gzipCmd.Wait(); err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
utils.Info("Database has been backed up")
|
||||
|
||||
}
|
||||
utils.Info("Database has been backed up")
|
||||
return nil
|
||||
}
|
||||
func localBackup(db *dbConfig, config *BackupConfig) {
|
||||
utils.Info("Backup database to local storage")
|
||||
BackupDatabase(db, config.backupFileName, disableCompression)
|
||||
err := BackupDatabase(db, config.backupFileName, disableCompression)
|
||||
if err != nil {
|
||||
recoverMode(err, "Error backing up database")
|
||||
return
|
||||
}
|
||||
finalFileName := config.backupFileName
|
||||
if config.encryption {
|
||||
encryptBackup(config)
|
||||
@@ -333,3 +350,17 @@ func encryptBackup(config *BackupConfig) {
|
||||
}
|
||||
|
||||
}
|
||||
func recoverMode(err error, msg string) {
|
||||
if err != nil {
|
||||
if backupRescueMode {
|
||||
utils.NotifyError(fmt.Sprintf("%s : %v", msg, err))
|
||||
utils.Error("Error: %s", msg)
|
||||
utils.Error("Backup rescue mode is enabled")
|
||||
utils.Error("Backup will continue")
|
||||
} else {
|
||||
utils.Error("Error: %s", msg)
|
||||
utils.Fatal("Error: %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -42,8 +42,9 @@ type Database struct {
|
||||
Path string `yaml:"path"`
|
||||
}
|
||||
type Config struct {
|
||||
Databases []Database `yaml:"databases"`
|
||||
CronExpression string `yaml:"cronExpression"`
|
||||
CronExpression string `yaml:"cronExpression"`
|
||||
BackupRescueMode bool `yaml:"backupRescueMode"`
|
||||
Databases []Database `yaml:"databases"`
|
||||
}
|
||||
|
||||
type dbConfig struct {
|
||||
@@ -114,7 +115,7 @@ func initDbConfig(cmd *cobra.Command) *dbConfig {
|
||||
utils.GetEnv(cmd, "dbname", "DB_NAME")
|
||||
dConf := dbConfig{}
|
||||
dConf.dbHost = os.Getenv("DB_HOST")
|
||||
dConf.dbPort = os.Getenv("DB_PORT")
|
||||
dConf.dbPort = utils.EnvWithDefault("DB_PORT", "3306")
|
||||
dConf.dbName = os.Getenv("DB_NAME")
|
||||
dConf.dbUserName = os.Getenv("DB_USERNAME")
|
||||
dConf.dbPassword = os.Getenv("DB_PASSWORD")
|
||||
@@ -144,15 +145,26 @@ func getDatabase(database Database) *dbConfig {
|
||||
|
||||
// Helper function to get environment variable or use a default value
|
||||
func getEnvOrDefault(currentValue, envKey, suffix, defaultValue string) string {
|
||||
// Return the current value if it's already set
|
||||
if currentValue != "" {
|
||||
return currentValue
|
||||
}
|
||||
|
||||
// Check for suffixed or prefixed environment variables if a suffix is provided
|
||||
if suffix != "" {
|
||||
envSuffix := os.Getenv(fmt.Sprintf("%s_%s", envKey, strings.ToUpper(suffix)))
|
||||
suffixUpper := strings.ToUpper(suffix)
|
||||
envSuffix := os.Getenv(fmt.Sprintf("%s_%s", envKey, suffixUpper))
|
||||
if envSuffix != "" {
|
||||
return envSuffix
|
||||
}
|
||||
|
||||
envPrefix := os.Getenv(fmt.Sprintf("%s_%s", suffixUpper, envKey))
|
||||
if envPrefix != "" {
|
||||
return envPrefix
|
||||
}
|
||||
}
|
||||
|
||||
// Fall back to the default value using a helper function
|
||||
return utils.EnvWithDefault(envKey, defaultValue)
|
||||
}
|
||||
|
||||
|
||||
@@ -66,10 +66,10 @@ func deleteTemp() {
|
||||
}
|
||||
|
||||
// TestDatabaseConnection tests the database connection
|
||||
func testDatabaseConnection(db *dbConfig) {
|
||||
func testDatabaseConnection(db *dbConfig) error {
|
||||
err := os.Setenv("MYSQL_PWD", db.dbPassword)
|
||||
if err != nil {
|
||||
return
|
||||
return fmt.Errorf("failed to set MYSQL_PWD environment variable: %v", err)
|
||||
}
|
||||
utils.Info("Connecting to %s database ...", db.dbName)
|
||||
// Set database name for notification error
|
||||
@@ -81,11 +81,11 @@ func testDatabaseConnection(db *dbConfig) {
|
||||
cmd.Stderr = &out
|
||||
err = cmd.Run()
|
||||
if err != nil {
|
||||
utils.Fatal("Error testing database connection: %v\nOutput: %s", err, out.String())
|
||||
return fmt.Errorf("failed to connect to %s database: %v", db.dbName, err)
|
||||
|
||||
}
|
||||
utils.Info("Successfully connected to %s database", db.dbName)
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// checkPubKeyFile checks gpg public key
|
||||
|
||||
@@ -51,7 +51,10 @@ func StartMigration(cmd *cobra.Command) {
|
||||
conf := &RestoreConfig{}
|
||||
conf.file = backupFileName
|
||||
// Backup source Database
|
||||
BackupDatabase(dbConf, backupFileName, true)
|
||||
err := BackupDatabase(dbConf, backupFileName, true)
|
||||
if err != nil {
|
||||
utils.Fatal("Error backing up database: %s", err)
|
||||
}
|
||||
// Restore source database into target database
|
||||
utils.Info("Restoring [%s] database into [%s] database...", dbConf.dbName, targetDbConf.targetDbName)
|
||||
RestoreDatabase(&newDbConfig, conf)
|
||||
|
||||
@@ -39,7 +39,11 @@ import (
|
||||
func sshBackup(db *dbConfig, config *BackupConfig) {
|
||||
utils.Info("Backup database to Remote server")
|
||||
// Backup database
|
||||
BackupDatabase(db, config.backupFileName, disableCompression)
|
||||
err := BackupDatabase(db, config.backupFileName, disableCompression)
|
||||
if err != nil {
|
||||
recoverMode(err, "Error backing up database")
|
||||
return
|
||||
}
|
||||
finalFileName := config.backupFileName
|
||||
if config.encryption {
|
||||
encryptBackup(config)
|
||||
@@ -156,7 +160,11 @@ func ftpBackup(db *dbConfig, config *BackupConfig) {
|
||||
utils.Info("Backup database to the remote FTP server")
|
||||
|
||||
// Backup database
|
||||
BackupDatabase(db, config.backupFileName, disableCompression)
|
||||
err := BackupDatabase(db, config.backupFileName, disableCompression)
|
||||
if err != nil {
|
||||
recoverMode(err, "Error backing up database")
|
||||
return
|
||||
}
|
||||
finalFileName := config.backupFileName
|
||||
if config.encryption {
|
||||
encryptBackup(config)
|
||||
|
||||
@@ -118,7 +118,10 @@ func RestoreDatabase(db *dbConfig, conf *RestoreConfig) {
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
testDatabaseConnection(db)
|
||||
err = testDatabaseConnection(db)
|
||||
if err != nil {
|
||||
utils.Fatal("Error connecting to the database %v", err)
|
||||
}
|
||||
utils.Info("Restoring database...")
|
||||
|
||||
extension := filepath.Ext(filepath.Join(tmpPath, conf.file))
|
||||
|
||||
@@ -39,7 +39,11 @@ func s3Backup(db *dbConfig, config *BackupConfig) {
|
||||
|
||||
utils.Info("Backup database to s3 storage")
|
||||
// Backup database
|
||||
BackupDatabase(db, config.backupFileName, disableCompression)
|
||||
err := BackupDatabase(db, config.backupFileName, disableCompression)
|
||||
if err != nil {
|
||||
recoverMode(err, "Error backing up database")
|
||||
return
|
||||
}
|
||||
finalFileName := config.backupFileName
|
||||
if config.encryption {
|
||||
encryptBackup(config)
|
||||
|
||||
@@ -42,6 +42,7 @@ var (
|
||||
usingKey = false
|
||||
backupSize int64 = 0
|
||||
startTime = time.Now()
|
||||
backupRescueMode = false
|
||||
)
|
||||
|
||||
// dbHVars Required environment variables for database
|
||||
@@ -61,13 +62,6 @@ var tdbRVars = []string{
|
||||
var dbConf *dbConfig
|
||||
var targetDbConf *targetDbConfig
|
||||
|
||||
// sshVars Required environment variables for SSH remote server storage
|
||||
var sshVars = []string{
|
||||
"SSH_USER",
|
||||
"SSH_HOST_NAME",
|
||||
"SSH_PORT",
|
||||
"REMOTE_PATH",
|
||||
}
|
||||
var ftpVars = []string{
|
||||
"FTP_HOST_NAME",
|
||||
"FTP_USER",
|
||||
|
||||
@@ -60,10 +60,10 @@
|
||||
|
||||
<p>We recommend investigating the issue as soon as possible to prevent potential data loss or service disruptions.</p>
|
||||
|
||||
<p>For more information, visit the <a href="https://jkaninda.github.io/pg-bkup">pg-bkup documentation</a>.</p>
|
||||
<p>For more information, visit the <a href="https://jkaninda.github.io/mysql-bkup">mysql-bkup documentation</a>.</p>
|
||||
|
||||
<footer>
|
||||
© 2024 <a href="https://github.com/jkaninda/pg-bkup">pg-bkup</a> | Automated Backup System
|
||||
© 2024 <a href="https://github.com/jkaninda/mysql-bkup">mysql-bkup</a> | Automated Backup System
|
||||
</footer>
|
||||
</body>
|
||||
</html>
|
||||
|
||||
Reference in New Issue
Block a user