Skip to content

Commit 6300a8f

Browse files
committed
feat: add backup all databases
1 parent cd827a9 commit 6300a8f

File tree

13 files changed

+175
-105
lines changed

13 files changed

+175
-105
lines changed

cmd/backup.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ func init() {
5050
BackupCmd.PersistentFlags().StringP("cron-expression", "e", "", "Backup cron expression (e.g., `0 0 * * *` or `@daily`)")
5151
BackupCmd.PersistentFlags().StringP("config", "c", "", "Configuration file for multi database backup. (e.g: `/backup/config.yaml`)")
5252
BackupCmd.PersistentFlags().BoolP("disable-compression", "", false, "Disable backup compression")
53-
BackupCmd.PersistentFlags().BoolP("all", "a", false, "Backup all databases")
54-
BackupCmd.PersistentFlags().BoolP("single-file", "", false, "Backup all databases in a single file")
53+
BackupCmd.PersistentFlags().BoolP("all-databases", "a", false, "Backup all databases")
54+
BackupCmd.PersistentFlags().BoolP("all-in-one", "A", false, "Backup all databases in a single file")
5555

5656
}

docs/how-tos/backup-all.md

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
---
2+
title: Backup all databases in the server
3+
layout: default
4+
parent: How Tos
5+
nav_order: 12
6+
---
7+
8+
# Backup All Databases
9+
10+
MySQL-Bkup supports backing up all databases on the server using the `--all-databases` (`-a`) flag. By default, this creates separate backup files for each database. If you prefer a single backup file, you can use the `--all-in-on`e (`-A`) flag.
11+
12+
Backing up all databases is useful for creating a snapshot of the entire database server, whether for disaster recovery or migration purposes.
13+
## Backup Modes
14+
15+
### Separate Backup Files (Default)
16+
17+
Using --all-databases without --all-in-one creates individual backup files for each database.
18+
19+
- Creates separate backup files for each database.
20+
- Provides more flexibility in restoring individual databases or tables.
21+
- Can be more manageable in cases where different databases have different retention policies.
22+
- Might take slightly longer due to multiple file operations.
23+
- It is the default behavior when using the `--all-databases` flag.
24+
- It does not backup system databases (`information_schema`, `performance_schema`, `mysql`, `sys`, `innodb`).
25+
26+
**Command:**
27+
28+
```bash
29+
docker run --rm --network your_network_name \
30+
-v $PWD/backup:/backup/ \
31+
-e "DB_HOST=dbhost" \
32+
-e "DB_PORT=3306" \
33+
-e "DB_USERNAME=username" \
34+
-e "DB_PASSWORD=password" \
35+
jkaninda/mysql-bkup backup --all-databases
36+
```
37+
### Single Backup File
38+
39+
Using --all-in-one (-A) creates a single backup file containing all databases.
40+
41+
- Creates a single backup file containing all databases.
42+
- Easier to manage if you need to restore everything at once.
43+
- Faster to back up and restore in bulk.
44+
- Can be problematic if you only need to restore a specific database or table.
45+
- It is recommended to use this option for disaster recovery purposes.
46+
- It backups system databases as well.
47+
48+
```bash
49+
docker run --rm --network your_network_name \
50+
-v $PWD/backup:/backup/ \
51+
-e "DB_HOST=dbhost" \
52+
-e "DB_PORT=3306" \
53+
-e "DB_USERNAME=username" \
54+
-e "DB_PASSWORD=password" \
55+
jkaninda/mysql-bkup backup --all-in-one
56+
```
57+
58+
### When to Use Which?
59+
60+
- Use `--all-in-one` if you want a quick, simple backup for disaster recovery where you'll restore everything at once.
61+
- Use `--all-databases` if you need granularity in restoring specific databases or tables without affecting others.

docs/how-tos/mutli-backup.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,8 @@ Below is an example configuration file (`config.yaml`) that defines multiple dat
3434
```yaml
3535
# Optional: Define a global cron expression for scheduled backups.
3636
# Example: "@every 20m" (runs every 20 minutes). If omitted, backups run immediately.
37-
cronExpression: ""
38-
37+
cronExpression: "" # Optional: Define a global cron expression for scheduled backups.
38+
backupRescueMode: false # Optional: Set to true to enable rescue mode for backups.
3939
databases:
4040
- host: mysql1 # Optional: Overrides DB_HOST or uses DB_HOST_DATABASE1.
4141
port: 3306 # Optional: Default is 5432. Overrides DB_PORT or uses DB_PORT_DATABASE1.

docs/how-tos/receive-notification.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
title: Receive notifications
33
layout: default
44
parent: How Tos
5-
nav_order: 12
5+
nav_order: 13
66
---
77

88
# Receive Notifications

docs/reference/index.md

Lines changed: 23 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -6,28 +6,30 @@ nav_order: 3
66

77
# Configuration Reference
88

9-
Backup, restore, and migration targets, schedules, and retention policies are configured using **environment variables** or **CLI flags**.
10-
11-
---
9+
MySQL backup, restore, and migration processes can be configured using **environment variables** or **CLI flags**.
1210

1311
## CLI Utility Usage
1412

15-
| Option | Short Flag | Description |
16-
|-------------------------|------------|-------------------------------------------------------------------------------|
17-
| `pg-bkup` | `bkup` | CLI utility for managing PostgreSQL backups. |
18-
| `backup` | | Perform a backup operation. |
19-
| `restore` | | Perform a restore operation. |
20-
| `migrate` | | Migrate a database from one instance to another. |
21-
| `--storage` | `-s` | Storage type (`local`, `s3`, `ssh`, etc.). Default: `local`. |
22-
| `--file` | `-f` | File name for restoration. |
23-
| `--path` | | Path for storage (e.g., `/custom_path` for S3 or `/home/foo/backup` for SSH). |
24-
| `--config` | `-c` | Configuration file for multi database backup. (e.g: `/backup/config.yaml`). |
25-
| `--dbname` | `-d` | Database name. |
26-
| `--port` | `-p` | Database port. Default: `3306`. |
27-
| `--disable-compression` | | Disable compression for database backups. |
28-
| `--cron-expression` | `-e` | Cron expression for scheduled backups (e.g., `0 0 * * *` or `@daily`). |
29-
| `--help` | `-h` | Display help message and exit. |
30-
| `--version` | `-V` | Display version information and exit. |
13+
The `mysql-bkup` CLI provides commands and options to manage MySQL backups efficiently.
14+
15+
| Option | Short Flag | Description |
16+
|-------------------------|------------|-----------------------------------------------------------------------------------------|
17+
| `mysql-bkup` | `bkup` | CLI tool for managing MySQL backups, restoration, and migration. |
18+
| `backup` | | Executes a backup operation. |
19+
| `restore` | | Restores a database from a backup file. |
20+
| `migrate` | | Migrates a database from one instance to another. |
21+
| `--storage` | `-s` | Specifies the storage type (`local`, `s3`, `ssh`, etc.). Default: `local`. |
22+
| `--file` | `-f` | Defines the backup file name for restoration. |
23+
| `--path` | | Sets the storage path (e.g., `/custom_path` for S3 or `/home/foo/backup` for SSH). |
24+
| `--config` | `-c` | Provides a configuration file for multi-database backups (e.g., `/backup/config.yaml`). |
25+
| `--dbname` | `-d` | Specifies the database name to back up or restore. |
26+
| `--port` | `-p` | Defines the database port. Default: `3306`. |
27+
| `--disable-compression` | | Disables compression for database backups. |
28+
| `--cron-expression` | `-e` | Schedules backups using a cron expression (e.g., `0 0 * * *` or `@daily`). |
29+
| `--all-databases` | `-a` | Backs up all databases separately (e.g., `backup --all-databases`). |
30+
| `--all-in-one` | `-A` | Backs up all databases in a single file (e.g., `backup --all-databases --single-file`). |
31+
| `--help` | `-h` | Displays the help message and exits. |
32+
| `--version` | `-V` | Shows version information and exits. |
3133

3234
---
3335

@@ -40,6 +42,8 @@ Backup, restore, and migration targets, schedules, and retention policies are co
4042
| `DB_NAME` | Optional (if provided via `-d` flag) | Database name. |
4143
| `DB_USERNAME` | Required | Database username. |
4244
| `DB_PASSWORD` | Required | Database password. |
45+
| `DB_SSL_CA` | Optional | Database client CA certificate file |
46+
| `DB_SSL_MODE` | Optional(`0 or 1`) default: `0` | Database client Enable CA validation |
4347
| `AWS_ACCESS_KEY` | Required for S3 storage | AWS S3 Access Key. |
4448
| `AWS_SECRET_KEY` | Required for S3 storage | AWS S3 Secret Key. |
4549
| `AWS_BUCKET_NAME` | Required for S3 storage | AWS S3 Bucket Name. |

pkg/azure.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ func azureBackup(db *dbConfig, config *BackupConfig) {
3939
utils.Info("Backup database to Azure Blob Storage")
4040

4141
// Backup database
42-
err := BackupDatabase(db, config.backupFileName, disableCompression, config.all, config.singleFile)
42+
err := BackupDatabase(db, config.backupFileName, disableCompression, config.all, config.allInOne)
4343
if err != nil {
4444
recoverMode(err, "Error backing up database")
4545
return

pkg/backup.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,7 @@ func multiBackupTask(databases []Database, bkConfig *BackupConfig) {
112112

113113
// createBackupTask backup task
114114
func createBackupTask(db *dbConfig, config *BackupConfig) {
115-
if config.all && !config.singleFile {
115+
if config.all && !config.allInOne {
116116
backupAll(db, config)
117117
} else {
118118
backupTask(db, config)
@@ -141,7 +141,7 @@ func backupTask(db *dbConfig, config *BackupConfig) {
141141
utils.Info("Starting backup task...")
142142
startTime = time.Now()
143143
prefix := db.dbName
144-
if config.all && config.singleFile {
144+
if config.all && config.allInOne {
145145
prefix = "all_databases"
146146
}
147147
// Generate file name
@@ -302,7 +302,7 @@ func runCommandWithCompression(command string, args []string, outputPath string)
302302
// localBackup backup database to local storage
303303
func localBackup(db *dbConfig, config *BackupConfig) {
304304
utils.Info("Backup database to local storage")
305-
err := BackupDatabase(db, config.backupFileName, disableCompression, config.all, config.singleFile)
305+
err := BackupDatabase(db, config.backupFileName, disableCompression, config.all, config.allInOne)
306306
if err != nil {
307307
recoverMode(err, "Error backing up database")
308308
return

pkg/config.go

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ type BackupConfig struct {
7878
storage string
7979
cronExpression string
8080
all bool
81-
singleFile bool
81+
allInOne bool
8282
}
8383
type FTPConfig struct {
8484
host string
@@ -253,13 +253,17 @@ func initBackupConfig(cmd *cobra.Command) *BackupConfig {
253253
remotePath := utils.GetEnvVariable("REMOTE_PATH", "SSH_REMOTE_PATH")
254254
storage = utils.GetEnv(cmd, "storage", "STORAGE")
255255
prune := false
256+
configFile := os.Getenv("BACKUP_CONFIG_FILE")
256257
backupRetention := utils.GetIntEnv("BACKUP_RETENTION_DAYS")
257258
if backupRetention > 0 {
258259
prune = true
259260
}
260261
disableCompression, _ = cmd.Flags().GetBool("disable-compression")
261-
all, _ := cmd.Flags().GetBool("all")
262-
singleFile, _ := cmd.Flags().GetBool("single-file")
262+
all, _ := cmd.Flags().GetBool("all-databases")
263+
allInOne, _ := cmd.Flags().GetBool("all-in-one")
264+
if allInOne {
265+
all = true
266+
}
263267
_, _ = cmd.Flags().GetString("mode")
264268
passphrase := os.Getenv("GPG_PASSPHRASE")
265269
_ = utils.GetEnv(cmd, "path", "AWS_S3_PATH")
@@ -273,6 +277,10 @@ func initBackupConfig(cmd *cobra.Command) *BackupConfig {
273277
encryption = true
274278
usingKey = false
275279
}
280+
dbName := os.Getenv("DB_NAME")
281+
if dbName == "" && !all && configFile == "" {
282+
utils.Fatal("Database name is required, use DB_NAME environment variable or -d flag")
283+
}
276284
// Initialize backup configs
277285
config := BackupConfig{}
278286
config.backupRetention = backupRetention
@@ -286,7 +294,7 @@ func initBackupConfig(cmd *cobra.Command) *BackupConfig {
286294
config.usingKey = usingKey
287295
config.cronExpression = cronExpression
288296
config.all = all
289-
config.singleFile = singleFile
297+
config.allInOne = allInOne
290298
return &config
291299
}
292300

pkg/helper.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ import (
3737
)
3838

3939
func intro() {
40-
fmt.Println("Starting MySQL Backup...")
40+
fmt.Println("Starting MYSQL-BKUP...")
4141
fmt.Printf("Version: %s\n", utils.Version)
4242
fmt.Println("Copyright (c) 2024 Jonas Kaninda")
4343
}

pkg/remote.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ import (
3939
func sshBackup(db *dbConfig, config *BackupConfig) {
4040
utils.Info("Backup database to Remote server")
4141
// Backup database
42-
err := BackupDatabase(db, config.backupFileName, disableCompression, config.all, config.singleFile)
42+
err := BackupDatabase(db, config.backupFileName, disableCompression, config.all, config.allInOne)
4343
if err != nil {
4444
recoverMode(err, "Error backing up database")
4545
return
@@ -160,7 +160,7 @@ func ftpBackup(db *dbConfig, config *BackupConfig) {
160160
utils.Info("Backup database to the remote FTP server")
161161

162162
// Backup database
163-
err := BackupDatabase(db, config.backupFileName, disableCompression, config.all, config.singleFile)
163+
err := BackupDatabase(db, config.backupFileName, disableCompression, config.all, config.allInOne)
164164
if err != nil {
165165
recoverMode(err, "Error backing up database")
166166
return

0 commit comments

Comments
 (0)