mirror of
https://github.com/jkaninda/mysql-bkup.git
synced 2025-12-08 14:39:41 +01:00
Compare commits
27 Commits
v1.2.26
...
b6192f4c42
| Author | SHA1 | Date | |
|---|---|---|---|
| b6192f4c42 | |||
| d5061453b0 | |||
| 0bc7497512 | |||
| 489dfdf842 | |||
| 696477fe5c | |||
|
|
56a8b51660 | ||
| c76a00139c | |||
| 0f43871765 | |||
| 9ba6abe3f4 | |||
|
|
764583d88f | ||
|
|
dbf4dc596a | ||
|
|
06c89a9b78 | ||
| ec8bdd806c | |||
|
|
828b11c6dd | ||
| 1d01e13909 | |||
| bd65db2418 | |||
| 75b809511e | |||
| fc028a2c55 | |||
| 7fa0c6a118 | |||
| 661702a97e | |||
| dd5f33f17d | |||
| b7cdfebd9c | |||
| 4b93becdf2 | |||
| 748cccec58 | |||
| 3e8bfabc44 | |||
| 777b59fd7c | |||
|
|
2b25f39c0a |
@@ -27,6 +27,7 @@ linters:
|
||||
- gosimple
|
||||
- govet
|
||||
- ineffassign
|
||||
# - lll
|
||||
- misspell
|
||||
- nakedret
|
||||
- prealloc
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
FROM golang:1.23.4 AS build
|
||||
FROM golang:1.24.0 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,11 +44,12 @@ 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`)")
|
||||
BackupCmd.PersistentFlags().StringP("config", "c", "", "Configuration file for multi database backup. (e.g: `/backup/config.yaml`)")
|
||||
BackupCmd.PersistentFlags().BoolP("disable-compression", "", false, "Disable backup compression")
|
||||
BackupCmd.PersistentFlags().BoolP("all", "a", false, "Backup all databases")
|
||||
|
||||
}
|
||||
|
||||
@@ -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.
|
||||
|
||||
@@ -8,51 +8,62 @@ nav_order: 11
|
||||
|
||||
# Multiple Backup Schedules
|
||||
|
||||
You can configure multiple backup schedules with different configurations by using a configuration file.
|
||||
|
||||
This file can be mounted into the container at `/config/config.yaml`, `/config/config.yml`, or specified via the `BACKUP_CONFIG_FILE` environment variable.
|
||||
This tool supports running multiple database backup schedules within the same container.
|
||||
You can configure these schedules with different settings using a **configuration file**. This flexibility allows you to manage backups for multiple databases efficiently.
|
||||
|
||||
---
|
||||
|
||||
## Configuration File
|
||||
## Configuration File Setup
|
||||
|
||||
The configuration file allows you to define multiple databases and their respective backup settings.
|
||||
The configuration file can be mounted into the container at `/config/config.yaml`, `/config/config.yml`, or specified via the `BACKUP_CONFIG_FILE` environment variable.
|
||||
|
||||
Below is an example configuration file:
|
||||
### Key Features:
|
||||
- **Global Environment Variables**: Use these for databases that share the same configuration.
|
||||
- **Database-Specific Overrides**: Override global settings for individual databases by specifying them in the configuration file or using the database name as a suffix in the variable name (e.g., `DB_HOST_DATABASE1`).
|
||||
- **Global Cron Expression**: Define a global `cronExpression` in the configuration file to schedule backups for all databases. If omitted, backups will run immediately.
|
||||
- **Configuration File Path**: Specify the configuration file path using:
|
||||
- The `BACKUP_CONFIG_FILE` environment variable.
|
||||
- The `--config` or `-c` flag for the backup command.
|
||||
|
||||
---
|
||||
|
||||
## Configuration File Example
|
||||
|
||||
Below is an example configuration file (`config.yaml`) that defines multiple databases and their respective backup settings:
|
||||
|
||||
```yaml
|
||||
# Optional: Define a global cron expression for scheduled backups
|
||||
# cronExpression: "@every 20m"
|
||||
# Optional: Define a global cron expression for scheduled backups.
|
||||
# Example: "@every 20m" (runs every 20 minutes). If omitted, backups run immediately.
|
||||
cronExpression: ""
|
||||
|
||||
databases:
|
||||
- host: mysql1
|
||||
port: 3306
|
||||
name: database1
|
||||
user: database1
|
||||
password: password
|
||||
path: /s3-path/database1 # For SSH or FTP, define the full path (e.g., /home/toto/backup/)
|
||||
- host: mysql1 # Optional: Overrides DB_HOST or uses DB_HOST_DATABASE1.
|
||||
port: 3306 # Optional: Default is 5432. Overrides DB_PORT or uses DB_PORT_DATABASE1.
|
||||
name: database1 # Required: Database name.
|
||||
user: database1 # Optional: Overrides DB_USERNAME or uses DB_USERNAME_DATABASE1.
|
||||
password: password # Optional: Overrides DB_PASSWORD or uses DB_PASSWORD_DATABASE1.
|
||||
path: /s3-path/database1 # Required: Backup path for SSH, FTP, or S3 (e.g., /home/toto/backup/).
|
||||
|
||||
- host: mysql2
|
||||
port: 3306
|
||||
name: lldap
|
||||
user: lldap
|
||||
password: password
|
||||
path: /s3-path/lldap # For SSH or FTP, define the full path (e.g., /home/toto/backup/)
|
||||
- host: mysql2 # Optional: Overrides DB_HOST or uses DB_HOST_LLAP.
|
||||
port: 3306 # Optional: Default is 5432. Overrides DB_PORT or uses DB_PORT_LLAP.
|
||||
name: lldap # Required: Database name.
|
||||
user: lldap # Optional: Overrides DB_USERNAME or uses DB_USERNAME_LLAP.
|
||||
password: password # Optional: Overrides DB_PASSWORD or uses DB_PASSWORD_LLAP.
|
||||
path: /s3-path/lldap # Required: Backup path for SSH, FTP, or S3 (e.g., /home/toto/backup/).
|
||||
|
||||
- host: mysql3
|
||||
port: 3306
|
||||
name: keycloak
|
||||
user: keycloak
|
||||
password: password
|
||||
path: /s3-path/keycloak # For SSH or FTP, define the full path (e.g., /home/toto/backup/)
|
||||
- host: mysql3 # Optional: Overrides DB_HOST or uses DB_HOST_KEYCLOAK.
|
||||
port: 3306 # Optional: Default is 5432. Overrides DB_PORT or uses DB_PORT_KEYCLOAK.
|
||||
name: keycloak # Required: Database name.
|
||||
user: keycloak # Optional: Overrides DB_USERNAME or uses DB_USERNAME_KEYCLOAK.
|
||||
password: password # Optional: Overrides DB_PASSWORD or uses DB_PASSWORD_KEYCLOAK.
|
||||
path: /s3-path/keycloak # Required: Backup path for SSH, FTP, or S3 (e.g., /home/toto/backup/).
|
||||
|
||||
- host: mysql4
|
||||
port: 3306
|
||||
name: joplin
|
||||
user: joplin
|
||||
password: password
|
||||
path: /s3-path/joplin # For SSH or FTP, define the full path (e.g., /home/toto/backup/)
|
||||
- host: mysql4 # Optional: Overrides DB_HOST or uses DB_HOST_JOPLIN.
|
||||
port: 3306 # Optional: Default is 5432. Overrides DB_PORT or uses DB_PORT_JOPLIN.
|
||||
name: joplin # Required: Database name.
|
||||
user: joplin # Optional: Overrides DB_USERNAME or uses DB_USERNAME_JOPLIN.
|
||||
password: password # Optional: Overrides DB_PASSWORD or uses DB_PASSWORD_JOPLIN.
|
||||
path: /s3-path/joplin # Required: Backup path for SSH, FTP, or S3 (e.g., /home/toto/backup/).
|
||||
```
|
||||
|
||||
---
|
||||
@@ -88,9 +99,5 @@ networks:
|
||||
|
||||
---
|
||||
|
||||
## Key Notes
|
||||
|
||||
- **Global Cron Expression**: You can define a global `cronExpression` in the configuration file to schedule backups for all databases. If omitted, backups will run immediately.
|
||||
- **Database-Specific Paths**: For SSH or FTP storage, ensure the `path` field contains the full remote path (e.g., `/home/toto/backup/`).
|
||||
- **Environment Variables**: Use the `BACKUP_CONFIG_FILE` environment variable to specify the path to the configuration file.
|
||||
- **Security**: Avoid hardcoding sensitive information like passwords in the configuration file. Use environment variables or secrets management tools instead.
|
||||
|
||||
|
||||
@@ -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
|
||||
|
||||
5
go.mod
5
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
|
||||
)
|
||||
|
||||
|
||||
12
go.sum
12
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,6 +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.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=
|
||||
@@ -66,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=
|
||||
|
||||
15
pkg/azure.go
15
pkg/azure.go
@@ -27,6 +27,7 @@ package pkg
|
||||
import (
|
||||
"fmt"
|
||||
"github.com/jkaninda/go-storage/pkg/azure"
|
||||
goutils "github.com/jkaninda/go-utils"
|
||||
"github.com/jkaninda/mysql-bkup/utils"
|
||||
|
||||
"os"
|
||||
@@ -36,10 +37,13 @@ import (
|
||||
|
||||
func azureBackup(db *dbConfig, config *BackupConfig) {
|
||||
utils.Info("Backup database to Azure Blob Storage")
|
||||
startTime = time.Now().Format(utils.TimeFormat())
|
||||
|
||||
// Backup database
|
||||
BackupDatabase(db, config.backupFileName, disableCompression)
|
||||
err := BackupDatabase(db, config.backupFileName, disableCompression, config.all)
|
||||
if err != nil {
|
||||
recoverMode(err, "Error backing up database")
|
||||
return
|
||||
}
|
||||
finalFileName := config.backupFileName
|
||||
if config.encryption {
|
||||
encryptBackup(config)
|
||||
@@ -87,6 +91,8 @@ func azureBackup(db *dbConfig, config *BackupConfig) {
|
||||
utils.Info("Backup size: %s", utils.ConvertBytes(uint64(backupSize)))
|
||||
utils.Info("Uploading backup archive to Azure Blob storage ... done ")
|
||||
|
||||
duration := goutils.FormatDuration(time.Since(startTime), 0)
|
||||
|
||||
// Send notification
|
||||
utils.NotifySuccess(&utils.NotificationData{
|
||||
File: finalFileName,
|
||||
@@ -94,12 +100,11 @@ func azureBackup(db *dbConfig, config *BackupConfig) {
|
||||
Database: db.dbName,
|
||||
Storage: config.storage,
|
||||
BackupLocation: filepath.Join(config.remotePath, finalFileName),
|
||||
StartTime: startTime,
|
||||
EndTime: time.Now().Format(utils.TimeFormat()),
|
||||
Duration: duration,
|
||||
})
|
||||
// Delete temp
|
||||
deleteTemp()
|
||||
utils.Info("Backup completed successfully")
|
||||
utils.Info("Backup successfully completed in %s", duration)
|
||||
}
|
||||
func azureRestore(db *dbConfig, conf *RestoreConfig) {
|
||||
utils.Info("Restore database from Azure Blob storage")
|
||||
|
||||
176
pkg/backup.go
176
pkg/backup.go
@@ -29,10 +29,10 @@ import (
|
||||
"fmt"
|
||||
"github.com/jkaninda/encryptor"
|
||||
"github.com/jkaninda/go-storage/pkg/local"
|
||||
goutils "github.com/jkaninda/go-utils"
|
||||
"github.com/jkaninda/mysql-bkup/utils"
|
||||
"github.com/robfig/cron/v3"
|
||||
"github.com/spf13/cobra"
|
||||
"log"
|
||||
"os"
|
||||
"os/exec"
|
||||
"path/filepath"
|
||||
@@ -71,13 +71,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))
|
||||
|
||||
@@ -107,10 +111,15 @@ func multiBackupTask(databases []Database, bkConfig *BackupConfig) {
|
||||
// BackupTask backups database
|
||||
func BackupTask(db *dbConfig, config *BackupConfig) {
|
||||
utils.Info("Starting backup task...")
|
||||
startTime = time.Now()
|
||||
prefix := db.dbName
|
||||
if config.all {
|
||||
prefix = "all_databases"
|
||||
}
|
||||
// Generate file name
|
||||
backupFileName := fmt.Sprintf("%s_%s.sql.gz", db.dbName, time.Now().Format("20060102_150405"))
|
||||
backupFileName := fmt.Sprintf("%s_%s.sql.gz", prefix, time.Now().Format("20060102_150405"))
|
||||
if config.disableCompression {
|
||||
backupFileName = fmt.Sprintf("%s_%s.sql", db.dbName, time.Now().Format("20060102_150405"))
|
||||
backupFileName = fmt.Sprintf("%s_%s.sql", prefix, time.Now().Format("20060102_150405"))
|
||||
}
|
||||
config.backupFileName = backupFileName
|
||||
switch config.storage {
|
||||
@@ -118,7 +127,7 @@ func BackupTask(db *dbConfig, config *BackupConfig) {
|
||||
localBackup(db, config)
|
||||
case "s3", "S3":
|
||||
s3Backup(db, config)
|
||||
case "ssh", "SSH", "remote":
|
||||
case "ssh", "SSH", "remote", "sftp":
|
||||
sshBackup(db, config)
|
||||
case "ftp", "FTP":
|
||||
ftpBackup(db, config)
|
||||
@@ -145,6 +154,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")
|
||||
@@ -155,7 +165,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...")
|
||||
@@ -185,79 +199,79 @@ func startMultiBackup(bkConfig *BackupConfig, configFile string) {
|
||||
}
|
||||
|
||||
// BackupDatabase backup database
|
||||
func BackupDatabase(db *dbConfig, backupFileName string, disableCompression bool) {
|
||||
func BackupDatabase(db *dbConfig, backupFileName string, disableCompression, all bool) error {
|
||||
storagePath = os.Getenv("STORAGE_PATH")
|
||||
|
||||
utils.Info("Starting database backup...")
|
||||
|
||||
err := os.Setenv("MYSQL_PWD", db.dbPassword)
|
||||
if err != nil {
|
||||
return
|
||||
if err := testDatabaseConnection(db); err != nil {
|
||||
return fmt.Errorf("database connection failed: %w", err)
|
||||
}
|
||||
testDatabaseConnection(db)
|
||||
// Backup Database database
|
||||
utils.Info("Backing up database...")
|
||||
|
||||
// Verify is compression is disabled
|
||||
if disableCompression {
|
||||
// Execute mysqldump
|
||||
cmd := exec.Command("mysqldump",
|
||||
"-h", db.dbHost,
|
||||
"-P", db.dbPort,
|
||||
"-u", db.dbUserName,
|
||||
db.dbName,
|
||||
)
|
||||
output, err := cmd.Output()
|
||||
if err != nil {
|
||||
utils.Fatal(err.Error())
|
||||
}
|
||||
|
||||
// save output
|
||||
file, err := os.Create(filepath.Join(tmpPath, backupFileName))
|
||||
if err != nil {
|
||||
utils.Fatal(err.Error())
|
||||
}
|
||||
defer func(file *os.File) {
|
||||
err := file.Close()
|
||||
if err != nil {
|
||||
utils.Fatal(err.Error())
|
||||
}
|
||||
}(file)
|
||||
|
||||
_, err = file.Write(output)
|
||||
if err != nil {
|
||||
utils.Fatal(err.Error())
|
||||
}
|
||||
utils.Info("Database has been backed up")
|
||||
|
||||
dumpArgs := []string{fmt.Sprintf("--defaults-file=%s", mysqlClientConfig)}
|
||||
if all {
|
||||
dumpArgs = append(dumpArgs, "--all-databases", "--single-transaction", "--routines", "--triggers")
|
||||
} else {
|
||||
// Execute mysqldump
|
||||
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)
|
||||
}
|
||||
gzipCmd := exec.Command("gzip")
|
||||
gzipCmd.Stdin = stdout
|
||||
gzipCmd.Stdout, err = os.Create(filepath.Join(tmpPath, backupFileName))
|
||||
err = gzipCmd.Start()
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
if err := cmd.Run(); err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
if err := gzipCmd.Wait(); err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
utils.Info("Database has been backed up")
|
||||
|
||||
dumpArgs = append(dumpArgs, db.dbName)
|
||||
}
|
||||
|
||||
backupPath := filepath.Join(tmpPath, backupFileName)
|
||||
if disableCompression {
|
||||
return runCommandAndSaveOutput("mysqldump", dumpArgs, backupPath)
|
||||
}
|
||||
return runCommandWithCompression("mysqldump", dumpArgs, backupPath)
|
||||
}
|
||||
|
||||
func runCommandAndSaveOutput(command string, args []string, outputPath string) error {
|
||||
cmd := exec.Command(command, args...)
|
||||
output, err := cmd.Output()
|
||||
if err != nil {
|
||||
return fmt.Errorf("failed to execute %s: %v, output: %s", command, err, string(output))
|
||||
}
|
||||
|
||||
return os.WriteFile(outputPath, output, 0644)
|
||||
}
|
||||
|
||||
func runCommandWithCompression(command string, args []string, outputPath string) error {
|
||||
cmd := exec.Command(command, args...)
|
||||
stdout, err := cmd.StdoutPipe()
|
||||
if err != nil {
|
||||
return fmt.Errorf("failed to create stdout pipe: %w", err)
|
||||
}
|
||||
|
||||
gzipCmd := exec.Command("gzip")
|
||||
gzipCmd.Stdin = stdout
|
||||
gzipFile, err := os.Create(outputPath)
|
||||
if err != nil {
|
||||
return fmt.Errorf("failed to create gzip file: %w", err)
|
||||
}
|
||||
defer func(gzipFile *os.File) {
|
||||
err := gzipFile.Close()
|
||||
if err != nil {
|
||||
utils.Error("Error closing gzip file: %v", err)
|
||||
}
|
||||
}(gzipFile)
|
||||
gzipCmd.Stdout = gzipFile
|
||||
|
||||
if err := gzipCmd.Start(); err != nil {
|
||||
return fmt.Errorf("failed to start gzip: %w", err)
|
||||
}
|
||||
if err := cmd.Run(); err != nil {
|
||||
return fmt.Errorf("failed to execute %s: %w", command, err)
|
||||
}
|
||||
if err := gzipCmd.Wait(); err != nil {
|
||||
return fmt.Errorf("failed to wait for gzip completion: %w", err)
|
||||
}
|
||||
|
||||
utils.Info("Database has been backed up")
|
||||
return nil
|
||||
}
|
||||
func localBackup(db *dbConfig, config *BackupConfig) {
|
||||
utils.Info("Backup database to local storage")
|
||||
startTime = time.Now().Format(utils.TimeFormat())
|
||||
BackupDatabase(db, config.backupFileName, disableCompression)
|
||||
err := BackupDatabase(db, config.backupFileName, disableCompression, config.all)
|
||||
if err != nil {
|
||||
recoverMode(err, "Error backing up database")
|
||||
return
|
||||
}
|
||||
finalFileName := config.backupFileName
|
||||
if config.encryption {
|
||||
encryptBackup(config)
|
||||
@@ -279,6 +293,8 @@ func localBackup(db *dbConfig, config *BackupConfig) {
|
||||
utils.Info("Backup name is %s", finalFileName)
|
||||
utils.Info("Backup size: %s", utils.ConvertBytes(uint64(backupSize)))
|
||||
utils.Info("Backup saved in %s", filepath.Join(storagePath, finalFileName))
|
||||
duration := goutils.FormatDuration(time.Since(startTime), 0)
|
||||
|
||||
// Send notification
|
||||
utils.NotifySuccess(&utils.NotificationData{
|
||||
File: finalFileName,
|
||||
@@ -286,8 +302,7 @@ func localBackup(db *dbConfig, config *BackupConfig) {
|
||||
Database: db.dbName,
|
||||
Storage: config.storage,
|
||||
BackupLocation: filepath.Join(storagePath, finalFileName),
|
||||
StartTime: startTime,
|
||||
EndTime: time.Now().Format(utils.TimeFormat()),
|
||||
Duration: duration,
|
||||
})
|
||||
// Delete old backup
|
||||
if config.prune {
|
||||
@@ -299,7 +314,7 @@ func localBackup(db *dbConfig, config *BackupConfig) {
|
||||
}
|
||||
// Delete temp
|
||||
deleteTemp()
|
||||
utils.Info("Backup completed successfully")
|
||||
utils.Info("Backup successfully completed in %s", duration)
|
||||
}
|
||||
|
||||
func encryptBackup(config *BackupConfig) {
|
||||
@@ -331,3 +346,18 @@ 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)
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -30,6 +30,7 @@ import (
|
||||
"github.com/spf13/cobra"
|
||||
"os"
|
||||
"strconv"
|
||||
"strings"
|
||||
)
|
||||
|
||||
type Database struct {
|
||||
@@ -41,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 {
|
||||
@@ -75,6 +77,7 @@ type BackupConfig struct {
|
||||
publicKey string
|
||||
storage string
|
||||
cronExpression string
|
||||
all bool
|
||||
}
|
||||
type FTPConfig struct {
|
||||
host string
|
||||
@@ -113,7 +116,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")
|
||||
@@ -127,6 +130,11 @@ func initDbConfig(cmd *cobra.Command) *dbConfig {
|
||||
}
|
||||
|
||||
func getDatabase(database Database) *dbConfig {
|
||||
// Set default values from environment variables if not provided
|
||||
database.User = getEnvOrDefault(database.User, "DB_USERNAME", database.Name, "")
|
||||
database.Password = getEnvOrDefault(database.Password, "DB_PASSWORD", database.Name, "")
|
||||
database.Host = getEnvOrDefault(database.Host, "DB_HOST", database.Name, "")
|
||||
database.Port = getEnvOrDefault(database.Port, "DB_PORT", database.Name, "3306")
|
||||
return &dbConfig{
|
||||
dbHost: database.Host,
|
||||
dbPort: database.Port,
|
||||
@@ -136,6 +144,31 @@ 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 != "" {
|
||||
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)
|
||||
}
|
||||
|
||||
// loadSSHConfig loads the SSH configuration from environment variables
|
||||
func loadSSHConfig() (*SSHConfig, error) {
|
||||
utils.GetEnvVariable("SSH_HOST", "SSH_HOST_NAME")
|
||||
@@ -224,6 +257,7 @@ func initBackupConfig(cmd *cobra.Command) *BackupConfig {
|
||||
prune = true
|
||||
}
|
||||
disableCompression, _ = cmd.Flags().GetBool("disable-compression")
|
||||
all, _ := cmd.Flags().GetBool("all")
|
||||
_, _ = cmd.Flags().GetString("mode")
|
||||
passphrase := os.Getenv("GPG_PASSPHRASE")
|
||||
_ = utils.GetEnv(cmd, "path", "AWS_S3_PATH")
|
||||
@@ -249,6 +283,7 @@ func initBackupConfig(cmd *cobra.Command) *BackupConfig {
|
||||
config.publicKey = publicKeyFile
|
||||
config.usingKey = usingKey
|
||||
config.cronExpression = cronExpression
|
||||
config.all = all
|
||||
return &config
|
||||
}
|
||||
|
||||
|
||||
@@ -27,6 +27,7 @@ package pkg
|
||||
import (
|
||||
"bytes"
|
||||
"fmt"
|
||||
goutils "github.com/jkaninda/go-utils"
|
||||
"github.com/jkaninda/mysql-bkup/utils"
|
||||
"gopkg.in/yaml.v3"
|
||||
"os"
|
||||
@@ -65,27 +66,31 @@ func deleteTemp() {
|
||||
}
|
||||
}
|
||||
|
||||
// TestDatabaseConnection tests the database connection
|
||||
func testDatabaseConnection(db *dbConfig) {
|
||||
err := os.Setenv("MYSQL_PWD", db.dbPassword)
|
||||
if err != nil {
|
||||
return
|
||||
// TestDatabaseConnection tests the database connection
|
||||
func testDatabaseConnection(db *dbConfig) error {
|
||||
// Create the mysql client config file
|
||||
if err := createMysqlClientConfigFile(*db); err != nil {
|
||||
return fmt.Errorf(err.Error())
|
||||
}
|
||||
utils.Info("Connecting to %s database ...", db.dbName)
|
||||
// Set database name for notification error
|
||||
utils.DatabaseName = db.dbName
|
||||
cmd := exec.Command("mariadb", "-h", db.dbHost, "-P", db.dbPort, "-u", db.dbUserName, db.dbName, "-e", "quit")
|
||||
|
||||
// Prepare the command to test the database connection
|
||||
//cmd := exec.Command("mariadb", "-h", db.dbHost, "-P", db.dbPort, "-u", db.dbUserName, db.dbName, "-e", "quit")
|
||||
cmd := exec.Command("mariadb", fmt.Sprintf("--defaults-file=%s", mysqlClientConfig), db.dbName, "-e", "quit")
|
||||
// Capture the output
|
||||
var out bytes.Buffer
|
||||
cmd.Stdout = &out
|
||||
cmd.Stderr = &out
|
||||
err = cmd.Run()
|
||||
if err != nil {
|
||||
utils.Fatal("Error testing database connection: %v\nOutput: %s", err, out.String())
|
||||
|
||||
// Run the command
|
||||
if err := cmd.Run(); err != nil {
|
||||
return fmt.Errorf("failed to connect to database %s: %v, output: %s", db.dbName, err, out.String())
|
||||
}
|
||||
utils.Info("Successfully connected to %s database", db.dbName)
|
||||
|
||||
utils.Info("Successfully connected to %s database", db.dbName)
|
||||
return nil
|
||||
}
|
||||
|
||||
// checkPubKeyFile checks gpg public key
|
||||
@@ -183,3 +188,16 @@ func RemoveLastExtension(filename string) string {
|
||||
}
|
||||
return filename
|
||||
}
|
||||
|
||||
// Create mysql client config file
|
||||
func createMysqlClientConfigFile(db dbConfig) error {
|
||||
caCertPath := goutils.GetStringEnvWithDefault("DB_SSL_CA", "/etc/ssl/certs/ca-certificates.crt")
|
||||
sslMode := goutils.GetStringEnvWithDefault("DB_SSL_MODE", "0")
|
||||
// Create the mysql client config file
|
||||
mysqlClientConfigFile := filepath.Join(tmpPath, "my.cnf")
|
||||
mysqlClientConfig := fmt.Sprintf("[client]\nhost=%s\nport=%s\nuser=%s\npassword=%s\nssl-ca=%s\nssl=%s\n", db.dbHost, db.dbPort, db.dbUserName, db.dbPassword, caCertPath, sslMode)
|
||||
if err := os.WriteFile(mysqlClientConfigFile, []byte(mysqlClientConfig), 0644); err != nil {
|
||||
return fmt.Errorf("failed to create mysql client config file: %v", err)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
@@ -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, false)
|
||||
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)
|
||||
|
||||
@@ -28,6 +28,7 @@ import (
|
||||
"fmt"
|
||||
"github.com/jkaninda/go-storage/pkg/ftp"
|
||||
"github.com/jkaninda/go-storage/pkg/ssh"
|
||||
goutils "github.com/jkaninda/go-utils"
|
||||
"github.com/jkaninda/mysql-bkup/utils"
|
||||
|
||||
"os"
|
||||
@@ -37,9 +38,12 @@ import (
|
||||
|
||||
func sshBackup(db *dbConfig, config *BackupConfig) {
|
||||
utils.Info("Backup database to Remote server")
|
||||
startTime = time.Now().Format(utils.TimeFormat())
|
||||
// Backup database
|
||||
BackupDatabase(db, config.backupFileName, disableCompression)
|
||||
err := BackupDatabase(db, config.backupFileName, disableCompression, config.all)
|
||||
if err != nil {
|
||||
recoverMode(err, "Error backing up database")
|
||||
return
|
||||
}
|
||||
finalFileName := config.backupFileName
|
||||
if config.encryption {
|
||||
encryptBackup(config)
|
||||
@@ -91,6 +95,8 @@ func sshBackup(db *dbConfig, config *BackupConfig) {
|
||||
|
||||
}
|
||||
utils.Info("Uploading backup archive to remote storage ... done ")
|
||||
duration := goutils.FormatDuration(time.Since(startTime), 0)
|
||||
|
||||
// Send notification
|
||||
utils.NotifySuccess(&utils.NotificationData{
|
||||
File: finalFileName,
|
||||
@@ -98,12 +104,11 @@ func sshBackup(db *dbConfig, config *BackupConfig) {
|
||||
Database: db.dbName,
|
||||
Storage: config.storage,
|
||||
BackupLocation: filepath.Join(config.remotePath, finalFileName),
|
||||
StartTime: startTime,
|
||||
EndTime: time.Now().Format(utils.TimeFormat()),
|
||||
Duration: duration,
|
||||
})
|
||||
// Delete temp
|
||||
deleteTemp()
|
||||
utils.Info("Backup completed successfully")
|
||||
utils.Info("Backup successfully completed in %s", duration)
|
||||
|
||||
}
|
||||
func remoteRestore(db *dbConfig, conf *RestoreConfig) {
|
||||
@@ -153,10 +158,13 @@ func ftpRestore(db *dbConfig, conf *RestoreConfig) {
|
||||
}
|
||||
func ftpBackup(db *dbConfig, config *BackupConfig) {
|
||||
utils.Info("Backup database to the remote FTP server")
|
||||
startTime = time.Now().Format(utils.TimeFormat())
|
||||
|
||||
// Backup database
|
||||
BackupDatabase(db, config.backupFileName, disableCompression)
|
||||
err := BackupDatabase(db, config.backupFileName, disableCompression, config.all)
|
||||
if err != nil {
|
||||
recoverMode(err, "Error backing up database")
|
||||
return
|
||||
}
|
||||
finalFileName := config.backupFileName
|
||||
if config.encryption {
|
||||
encryptBackup(config)
|
||||
@@ -203,6 +211,7 @@ func ftpBackup(db *dbConfig, config *BackupConfig) {
|
||||
utils.Info("Backup name is %s", finalFileName)
|
||||
utils.Info("Backup size: %s", utils.ConvertBytes(uint64(backupSize)))
|
||||
utils.Info("Uploading backup archive to the remote FTP server ... done ")
|
||||
duration := goutils.FormatDuration(time.Since(startTime), 0)
|
||||
|
||||
// Send notification
|
||||
utils.NotifySuccess(&utils.NotificationData{
|
||||
@@ -211,10 +220,9 @@ func ftpBackup(db *dbConfig, config *BackupConfig) {
|
||||
Database: db.dbName,
|
||||
Storage: config.storage,
|
||||
BackupLocation: filepath.Join(config.remotePath, finalFileName),
|
||||
StartTime: startTime,
|
||||
EndTime: time.Now().Format(utils.TimeFormat()),
|
||||
Duration: duration,
|
||||
})
|
||||
// Delete temp
|
||||
deleteTemp()
|
||||
utils.Info("Backup completed successfully")
|
||||
utils.Info("Backup successfully completed in %s", duration)
|
||||
}
|
||||
|
||||
@@ -25,6 +25,7 @@ SOFTWARE.
|
||||
package pkg
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"github.com/jkaninda/encryptor"
|
||||
"github.com/jkaninda/go-storage/pkg/local"
|
||||
"github.com/jkaninda/mysql-bkup/utils"
|
||||
@@ -114,20 +115,19 @@ func RestoreDatabase(db *dbConfig, conf *RestoreConfig) {
|
||||
}
|
||||
|
||||
if utils.FileExists(filepath.Join(tmpPath, conf.file)) {
|
||||
err := os.Setenv("MYSQL_PWD", db.dbPassword)
|
||||
err = testDatabaseConnection(db)
|
||||
if err != nil {
|
||||
return
|
||||
utils.Fatal("Error connecting to the database %v", err)
|
||||
}
|
||||
testDatabaseConnection(db)
|
||||
utils.Info("Restoring database...")
|
||||
|
||||
extension := filepath.Ext(filepath.Join(tmpPath, conf.file))
|
||||
// Restore from compressed file / .sql.gz
|
||||
// Restore from a compressed file / .sql.gz
|
||||
if extension == ".gz" {
|
||||
str := "zcat " + filepath.Join(tmpPath, conf.file) + " | mariadb -h " + db.dbHost + " -P " + db.dbPort + " -u " + db.dbUserName + " " + db.dbName
|
||||
_, err := exec.Command("sh", "-c", str).Output()
|
||||
str := "zcat " + filepath.Join(tmpPath, conf.file) + " | mariadb " + fmt.Sprintf("--defaults-file=%s", mysqlClientConfig) + " " + db.dbName
|
||||
output, err := exec.Command("sh", "-c", str).Output()
|
||||
if err != nil {
|
||||
utils.Fatal("Error, in restoring the database %v", err)
|
||||
utils.Fatal("Error, in restoring the database %v output: %v", err, output)
|
||||
}
|
||||
utils.Info("Restoring database... done")
|
||||
utils.Info("Database has been restored")
|
||||
@@ -135,11 +135,11 @@ func RestoreDatabase(db *dbConfig, conf *RestoreConfig) {
|
||||
deleteTemp()
|
||||
|
||||
} else if extension == ".sql" {
|
||||
// Restore from sql file
|
||||
str := "cat " + filepath.Join(tmpPath, conf.file) + " | mariadb -h " + db.dbHost + " -P " + db.dbPort + " -u " + db.dbUserName + " " + db.dbName
|
||||
_, err := exec.Command("sh", "-c", str).Output()
|
||||
// Restore from SQL file
|
||||
str := "cat " + filepath.Join(tmpPath, conf.file) + " | mariadb " + fmt.Sprintf("--defaults-file=%s", mysqlClientConfig) + " " + db.dbName
|
||||
output, err := exec.Command("sh", "-c", str).Output()
|
||||
if err != nil {
|
||||
utils.Fatal("Error in restoring the database %v", err)
|
||||
utils.Fatal("Error, in restoring the database %v output: %v", err, output)
|
||||
}
|
||||
utils.Info("Restoring database... done")
|
||||
utils.Info("Database has been restored")
|
||||
|
||||
14
pkg/s3.go
14
pkg/s3.go
@@ -27,6 +27,7 @@ package pkg
|
||||
import (
|
||||
"fmt"
|
||||
"github.com/jkaninda/go-storage/pkg/s3"
|
||||
goutils "github.com/jkaninda/go-utils"
|
||||
"github.com/jkaninda/mysql-bkup/utils"
|
||||
|
||||
"os"
|
||||
@@ -37,9 +38,12 @@ import (
|
||||
func s3Backup(db *dbConfig, config *BackupConfig) {
|
||||
|
||||
utils.Info("Backup database to s3 storage")
|
||||
startTime = time.Now().Format(utils.TimeFormat())
|
||||
// Backup database
|
||||
BackupDatabase(db, config.backupFileName, disableCompression)
|
||||
err := BackupDatabase(db, config.backupFileName, disableCompression, config.all)
|
||||
if err != nil {
|
||||
recoverMode(err, "Error backing up database")
|
||||
return
|
||||
}
|
||||
finalFileName := config.backupFileName
|
||||
if config.encryption {
|
||||
encryptBackup(config)
|
||||
@@ -91,6 +95,7 @@ func s3Backup(db *dbConfig, config *BackupConfig) {
|
||||
}
|
||||
utils.Info("Backup saved in %s", filepath.Join(config.remotePath, finalFileName))
|
||||
utils.Info("Uploading backup archive to remote storage S3 ... done ")
|
||||
duration := goutils.FormatDuration(time.Since(startTime), 0)
|
||||
// Send notification
|
||||
utils.NotifySuccess(&utils.NotificationData{
|
||||
File: finalFileName,
|
||||
@@ -98,12 +103,11 @@ func s3Backup(db *dbConfig, config *BackupConfig) {
|
||||
Database: db.dbName,
|
||||
Storage: config.storage,
|
||||
BackupLocation: filepath.Join(config.remotePath, finalFileName),
|
||||
StartTime: startTime,
|
||||
EndTime: time.Now().Format(utils.TimeFormat()),
|
||||
Duration: duration,
|
||||
})
|
||||
// Delete temp
|
||||
deleteTemp()
|
||||
utils.Info("Backup completed successfully")
|
||||
utils.Info("Backup successfully completed in %s", duration)
|
||||
|
||||
}
|
||||
func s3Restore(db *dbConfig, conf *RestoreConfig) {
|
||||
|
||||
16
pkg/var.go
16
pkg/var.go
@@ -24,6 +24,11 @@ SOFTWARE.
|
||||
|
||||
package pkg
|
||||
|
||||
import (
|
||||
"path/filepath"
|
||||
"time"
|
||||
)
|
||||
|
||||
const tmpPath = "/tmp/backup"
|
||||
const gpgHome = "/config/gnupg"
|
||||
const gpgExtension = "gpg"
|
||||
@@ -39,7 +44,9 @@ var (
|
||||
encryption = false
|
||||
usingKey = false
|
||||
backupSize int64 = 0
|
||||
startTime string
|
||||
startTime = time.Now()
|
||||
backupRescueMode = false
|
||||
mysqlClientConfig = filepath.Join(tmpPath, "my.cnf")
|
||||
)
|
||||
|
||||
// dbHVars Required environment variables for database
|
||||
@@ -59,13 +66,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>
|
||||
|
||||
@@ -52,8 +52,7 @@
|
||||
<h3>Backup Details:</h3>
|
||||
<ul>
|
||||
<li><strong>Database Name:</strong> {{.Database}}</li>
|
||||
<li><strong>Backup Start Time:</strong> {{.StartTime}}</li>
|
||||
<li><strong>Backup End Time:</strong> {{.EndTime}}</li>
|
||||
<li><strong>Backup Duration:</strong> {{.Duration}}</li>
|
||||
<li><strong>Backup Storage:</strong> {{.Storage}}</li>
|
||||
<li><strong>Backup Location:</strong> {{.BackupLocation}}</li>
|
||||
<li><strong>Backup Size:</strong> {{.BackupSize}}</li>
|
||||
|
||||
@@ -6,8 +6,7 @@ Please find the details below:
|
||||
|
||||
Backup Details:
|
||||
- Database Name: {{.Database}}
|
||||
- Backup Start Time: {{.StartTime}}
|
||||
- Backup EndTime: {{.EndTime}}
|
||||
- Backup Duration: {{.Duration}}
|
||||
- Backup Storage: {{.Storage}}
|
||||
- Backup Location: {{.BackupLocation}}
|
||||
- Backup Size: {{.BackupSize}}
|
||||
|
||||
@@ -39,8 +39,7 @@ type NotificationData struct {
|
||||
File string
|
||||
BackupSize string
|
||||
Database string
|
||||
StartTime string
|
||||
EndTime string
|
||||
Duration string
|
||||
Storage string
|
||||
BackupLocation string
|
||||
BackupReference string
|
||||
@@ -84,3 +83,13 @@ func backupReference() string {
|
||||
const templatePath = "/config/templates"
|
||||
|
||||
var DatabaseName = ""
|
||||
var vars = []string{
|
||||
"TG_TOKEN",
|
||||
"TG_CHAT_ID",
|
||||
}
|
||||
var mailVars = []string{
|
||||
"MAIL_HOST",
|
||||
"MAIL_PORT",
|
||||
"MAIL_FROM",
|
||||
"MAIL_TO",
|
||||
}
|
||||
|
||||
@@ -107,19 +107,6 @@ func sendMessage(msg string) error {
|
||||
}
|
||||
func NotifySuccess(notificationData *NotificationData) {
|
||||
notificationData.BackupReference = backupReference()
|
||||
var vars = []string{
|
||||
"TG_TOKEN",
|
||||
"TG_CHAT_ID",
|
||||
}
|
||||
var mailVars = []string{
|
||||
"MAIL_HOST",
|
||||
"MAIL_PORT",
|
||||
"MAIL_USERNAME",
|
||||
"MAIL_PASSWORD",
|
||||
"MAIL_FROM",
|
||||
"MAIL_TO",
|
||||
}
|
||||
|
||||
// Email notification
|
||||
err := CheckEnvVars(mailVars)
|
||||
if err == nil {
|
||||
@@ -147,18 +134,6 @@ func NotifySuccess(notificationData *NotificationData) {
|
||||
}
|
||||
}
|
||||
func NotifyError(error string) {
|
||||
var vars = []string{
|
||||
"TG_TOKEN",
|
||||
"TG_CHAT_ID",
|
||||
}
|
||||
var mailVars = []string{
|
||||
"MAIL_HOST",
|
||||
"MAIL_PORT",
|
||||
"MAIL_USERNAME",
|
||||
"MAIL_PASSWORD",
|
||||
"MAIL_FROM",
|
||||
"MAIL_TO",
|
||||
}
|
||||
|
||||
// Email notification
|
||||
err := CheckEnvVars(mailVars)
|
||||
|
||||
Reference in New Issue
Block a user