mirror of
https://github.com/jkaninda/mysql-bkup.git
synced 2025-12-06 13:39:41 +01:00
feat: add Set default values from environment variables if not provided for multiple backup
This commit is contained in:
@@ -8,51 +8,62 @@ nav_order: 11
|
|||||||
|
|
||||||
# Multiple Backup Schedules
|
# Multiple Backup Schedules
|
||||||
|
|
||||||
You can configure multiple backup schedules with different configurations by using a configuration file.
|
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.
|
||||||
This file can be mounted into the container at `/config/config.yaml`, `/config/config.yml`, or specified via the `BACKUP_CONFIG_FILE` environment variable.
|
|
||||||
|
|
||||||
---
|
---
|
||||||
|
|
||||||
## 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
|
```yaml
|
||||||
# Optional: Define a global cron expression for scheduled backups
|
# Optional: Define a global cron expression for scheduled backups.
|
||||||
# cronExpression: "@every 20m"
|
# Example: "@every 20m" (runs every 20 minutes). If omitted, backups run immediately.
|
||||||
cronExpression: ""
|
cronExpression: ""
|
||||||
|
|
||||||
databases:
|
databases:
|
||||||
- host: mysql1
|
- host: mysql1 # Optional: Overrides DB_HOST or uses DB_HOST_DATABASE1.
|
||||||
port: 3306
|
port: 3306 # Optional: Default is 5432. Overrides DB_PORT or uses DB_PORT_DATABASE1.
|
||||||
name: database1
|
name: database1 # Required: Database name.
|
||||||
user: database1
|
user: database1 # Optional: Overrides DB_USERNAME or uses DB_USERNAME_DATABASE1.
|
||||||
password: password
|
password: password # Optional: Overrides DB_PASSWORD or uses DB_PASSWORD_DATABASE1.
|
||||||
path: /s3-path/database1 # For SSH or FTP, define the full path (e.g., /home/toto/backup/)
|
path: /s3-path/database1 # Required: Backup path for SSH, FTP, or S3 (e.g., /home/toto/backup/).
|
||||||
|
|
||||||
- host: mysql2
|
- host: mysql2 # Optional: Overrides DB_HOST or uses DB_HOST_LLAP.
|
||||||
port: 3306
|
port: 3306 # Optional: Default is 5432. Overrides DB_PORT or uses DB_PORT_LLAP.
|
||||||
name: lldap
|
name: lldap # Required: Database name.
|
||||||
user: lldap
|
user: lldap # Optional: Overrides DB_USERNAME or uses DB_USERNAME_LLAP.
|
||||||
password: password
|
password: password # Optional: Overrides DB_PASSWORD or uses DB_PASSWORD_LLAP.
|
||||||
path: /s3-path/lldap # For SSH or FTP, define the full path (e.g., /home/toto/backup/)
|
path: /s3-path/lldap # Required: Backup path for SSH, FTP, or S3 (e.g., /home/toto/backup/).
|
||||||
|
|
||||||
- host: mysql3
|
- host: mysql3 # Optional: Overrides DB_HOST or uses DB_HOST_KEYCLOAK.
|
||||||
port: 3306
|
port: 3306 # Optional: Default is 5432. Overrides DB_PORT or uses DB_PORT_KEYCLOAK.
|
||||||
name: keycloak
|
name: keycloak # Required: Database name.
|
||||||
user: keycloak
|
user: keycloak # Optional: Overrides DB_USERNAME or uses DB_USERNAME_KEYCLOAK.
|
||||||
password: password
|
password: password # Optional: Overrides DB_PASSWORD or uses DB_PASSWORD_KEYCLOAK.
|
||||||
path: /s3-path/keycloak # For SSH or FTP, define the full path (e.g., /home/toto/backup/)
|
path: /s3-path/keycloak # Required: Backup path for SSH, FTP, or S3 (e.g., /home/toto/backup/).
|
||||||
|
|
||||||
- host: mysql4
|
- host: mysql4 # Optional: Overrides DB_HOST or uses DB_HOST_JOPLIN.
|
||||||
port: 3306
|
port: 3306 # Optional: Default is 5432. Overrides DB_PORT or uses DB_PORT_JOPLIN.
|
||||||
name: joplin
|
name: joplin # Required: Database name.
|
||||||
user: joplin
|
user: joplin # Optional: Overrides DB_USERNAME or uses DB_USERNAME_JOPLIN.
|
||||||
password: password
|
password: password # Optional: Overrides DB_PASSWORD or uses DB_PASSWORD_JOPLIN.
|
||||||
path: /s3-path/joplin # For SSH or FTP, define the full path (e.g., /home/toto/backup/)
|
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.
|
|
||||||
|
|||||||
@@ -30,6 +30,7 @@ import (
|
|||||||
"github.com/spf13/cobra"
|
"github.com/spf13/cobra"
|
||||||
"os"
|
"os"
|
||||||
"strconv"
|
"strconv"
|
||||||
|
"strings"
|
||||||
)
|
)
|
||||||
|
|
||||||
type Database struct {
|
type Database struct {
|
||||||
@@ -127,6 +128,11 @@ func initDbConfig(cmd *cobra.Command) *dbConfig {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func getDatabase(database Database) *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, "5432")
|
||||||
return &dbConfig{
|
return &dbConfig{
|
||||||
dbHost: database.Host,
|
dbHost: database.Host,
|
||||||
dbPort: database.Port,
|
dbPort: database.Port,
|
||||||
@@ -136,6 +142,20 @@ func getDatabase(database Database) *dbConfig {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Helper function to get environment variable or use a default value
|
||||||
|
func getEnvOrDefault(currentValue, envKey, suffix, defaultValue string) string {
|
||||||
|
if currentValue != "" {
|
||||||
|
return currentValue
|
||||||
|
}
|
||||||
|
if suffix != "" {
|
||||||
|
envSuffix := os.Getenv(fmt.Sprintf("%s_%s", envKey, strings.ToUpper(suffix)))
|
||||||
|
if envSuffix != "" {
|
||||||
|
return envSuffix
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return utils.EnvWithDefault(envKey, defaultValue)
|
||||||
|
}
|
||||||
|
|
||||||
// loadSSHConfig loads the SSH configuration from environment variables
|
// loadSSHConfig loads the SSH configuration from environment variables
|
||||||
func loadSSHConfig() (*SSHConfig, error) {
|
func loadSSHConfig() (*SSHConfig, error) {
|
||||||
utils.GetEnvVariable("SSH_HOST", "SSH_HOST_NAME")
|
utils.GetEnvVariable("SSH_HOST", "SSH_HOST_NAME")
|
||||||
|
|||||||
Reference in New Issue
Block a user