This document provides comprehensive documentation for the MariaDB Backup Manager script, a powerful Bash utility designed to manage backups, restoration, and point-in-time recovery for MariaDB database servers.
Version: 1.1.0
-
Multiple Backup Types:
- Full backups
- Incremental backups
- Binary log backups
-
Advanced Recovery Options:
- Full backup restoration
- Incremental Backup restoration
- Point-in-time recovery (PITR)
-
Security:
- Encryption support (AES-256)
- Secure handling of credentials
-
Performance:
- Parallel processing
- Compression support
- I/O throttling
-
Automation:
- Cronjob setup
- Configurable retention policies
-
Monitoring:
- Email notifications
- Webhook alerts
- Slack integration
- Backup reports
-
Validation:
- Backup verification
- Test recovery
Before using this script, ensure you have the following:
- MariaDB server installed
mariabackuputility installed- Proper database credentials with backup permissions
- Sufficient disk space for backups
- (Optional) Mail utility for email notifications
sudo apt-get update
sudo apt-get install mariadb-backup# For MariaDB from the official repositories
sudo yum install MariaDB-backup
# Or if using MariaDB from MariaDB repositories
sudo yum install mariadb-backupsudo dnf install mariadb-backupsudo zypper install mariadb-backupmariabackup --version-
Copy the script to a suitable location:
cp mariadb-backup.sh /usr/local/bin/
-
Make it executable:
chmod +x /usr/local/bin/mariadb-backup.sh
-
(Optional) Create a configuration file (see Configuration section)
# Basic full backup
./mariadb-backup.sh --type full --mysql-user root --backup-dir /var/backup/mariadb
# Full backup with password
./mariadb-backup.sh --type full --mysql-user root --mysql-password "your_password" --backup-dir /var/backup/mariadb
# Full backup using MySQL defaults file
./mariadb-backup.sh --type full --defaults-file /root/.my.cnf --backup-dir /var/backup/mariadb# Basic incremental backup
./mariadb-backup.sh --type incremental --mysql-user root --backup-dir /var/backup/mariadb
# Incremental backup with password
./mariadb-backup.sh --type incremental --mysql-user root --mysql-password "your_password" --backup-dir /var/backup/mariadb
# Incremental backup using MySQL defaults file
./mariadb-backup.sh --type incremental --defaults-file /root/.my.cnf --backup-dir /var/backup/mariadb# Basic binary log backup
./mariadb-backup.sh --type binlog --mysql-user root --backup-dir /var/backup/mariadb
# Binary log backup with password
./mariadb-backup.sh --type binlog --mysql-user root --mysql-password "your_password" --backup-dir /var/backup/mariadb
# Binary log backup using MySQL defaults file
./mariadb-backup.sh --type binlog --defaults-file /root/.my.cnf --backup-dir /var/backup/mariadb# Restore from a full backup
./mariadb-backup.sh --restore /var/backup/mariadb/full_20250304120000
# Restore from an incremental backup (automatically applies the full backup and all necessary incremental backups)
./mariadb-backup.sh --restore /var/backup/mariadb/incr_20250305120000
# Restore using MySQL defaults file
./mariadb-backup.sh --restore /var/backup/mariadb/full_20250304120000 --defaults-file /root/.my.cnf# PITR from a full backup
./mariadb-backup.sh --restore /var/backup/mariadb/full_20250304120000 --pitr "2025-03-04 15:30:00"
# PITR from an incremental backup
./mariadb-backup.sh --restore /var/backup/mariadb/incr_20250305120000 --pitr "2025-03-05 15:30:00"| Option | Description | Default |
|---|---|---|
--backup-dir DIR |
Backup directory | /var/backup/mariadb |
--type TYPE |
Backup type: full, incremental, binlog | full |
--retention DAYS |
Overall retention period in days | 7 |
--retention-full N |
Number of full backups to keep | 4 |
--retention-incr N |
Number of incremental backups to keep | 14 |
--compress |
Compress backup using mbstream | No |
--compress-threads N |
Number of compression threads | 4 |
--encrypt |
Encrypt backup with AES-256 | No |
--encrypt-key-file FILE |
Path to encryption key file | |
--parallel N |
Number of parallel threads | 4 |
--dry-run |
Show what would be done without making changes | |
--temp-dir DIR |
Temporary directory for operations | /tmp |
--throttle-io N |
Limit I/O with ionice/nice settings (1-10) |
| Option | Description | Default |
|---|---|---|
--mysql-user USER |
MySQL user | root |
--mysql-password PASS |
MySQL password | empty |
--mysql-host HOST |
MySQL host | 127.0.0.1 |
--mysql-port PORT |
MySQL port | 3306 |
--mysql-datadir DIR |
MySQL data directory | /var/lib/mysql |
--defaults-file FILE |
Use MySQL defaults file instead of command line credentials |
| Option | Description |
|---|---|
--restore PATH |
Restore from a full or incremental backup path |
--pitr DATETIME |
Point-in-time recovery to specified datetime (YYYY-MM-DD HH:MM:SS) |
--pitr-only DATETIME |
PITR without restoring backup sequences |
--test-recovery |
Test restore to validate backup (requires additional disk space) |
--force |
Force operations without confirmation |
--ignore-errors |
Continue even if non-critical errors occur |
| Option | Description |
|---|---|
--webhook URL |
Webhook URL for notifications |
--send-webhook |
Enable webhook notifications |
--slack-webhook URL |
Slack webhook URL for notifications |
--email EMAIL |
Email address for notifications |
| Option | Description | Default |
|---|---|---|
--config FILE |
Load settings from configuration file | |
--setup-cron |
Setup cronjob for regular backups | |
--log-file FILE |
Log file path | stdout |
--log-level LEVEL |
Log level: DEBUG, INFO, WARN, ERROR | INFO |
| Option | Description |
|---|---|
--version |
Display version information |
--help |
Display help message |
Instead of specifying all options on the command line, you can create a configuration file:
# MariaDB Backup configuration
BACKUP_DIR="/var/backup/mariadb"
MYSQL_USER="backup"
MYSQL_PASSWORD="securepassword"
MYSQL_HOST="127.0.0.1"
MYSQL_PORT="3306"
MYSQL_DATADIR="/var/lib/mysql"
RETENTION_DAYS=7
RETENTION_FULL=4
RETENTION_INCR=14
COMPRESS=1
COMPRESS_THREADS=4
PARALLEL_THREADS=8
EMAIL_RECIPIENT="admin@example.com"
LOG_FILE="/var/log/mariadb-backup.log"
LOG_LEVEL="INFO"Use the configuration file:
./mariadb-backup.sh --config /etc/mariadb-backup.conf --type fullAs an alternative to providing credentials on the command line or in the backup script's configuration, you can use a MySQL defaults file. Create a file /root/.my.cnf with the following content:
[client]
user=root
password=your_password
host=localhost
port=3306
Secure the file:
chmod 600 /root/.my.cnfThen use it with the backup script:
./mariadb-backup.sh --defaults-file /root/.my.cnf --type fullThis is more secure than passing the password on the command line, as the password won't appear in the process list or command history.
A full backup contains a complete copy of all database data at a specific point in time. It serves as the foundation for incremental backups and is required for any restoration process.
An incremental backup only contains the changes made since the last backup (either full or incremental). This significantly reduces backup size and time. However, restoration requires having both the full backup and all subsequent incremental backups in sequence.
Binary logs record all changes to the database. Backing up binary logs enables point-in-time recovery, allowing restoration to any moment between backups.
The script automatically verifies backups after creation. You can also use the --test-recovery option for more thorough verification, which tests the backup by performing a recovery simulation.
To use encryption:
-
Generate an encryption key:
openssl rand -base64 24 > /root/backup_encryption.key chmod 400 /root/backup_encryption.key -
Enable encryption in your backups:
./mariadb-backup.sh --type full --encrypt --encrypt-key-file /root/backup_encryption.key
Set up automated backups using the built-in cronjob setup:
./mariadb-backup.sh --setup-cron --config /etc/mariadb-backup.confThis creates the following schedule:
- Weekly full backup (Sunday 1 AM)
- Daily incremental backup (weekdays 1 AM)
- Hourly binary log backup
Restoring a full backup:
# Basic restoration
./mariadb-backup.sh --restore /var/backup/mariadb/full_20250304120000
# Restoration with credentials
./mariadb-backup.sh --restore /var/backup/mariadb/full_20250304120000 --mysql-user root --mysql-password "your_password"
# Restoration using defaults file
./mariadb-backup.sh --restore /var/backup/mariadb/full_20250304120000 --defaults-file /root/.my.cnfRestoring an incremental backup (this automatically applies all necessary incremental backups on top of the full backup):
# Basic incremental restoration
./mariadb-backup.sh --restore /var/backup/mariadb/incr_20250305120000
# Incremental restoration with credentials
./mariadb-backup.sh --restore /var/backup/mariadb/incr_20250305120000 --mysql-user root --mysql-password "your_password"
# Incremental restoration using defaults file
./mariadb-backup.sh --restore /var/backup/mariadb/incr_20250305120000 --defaults-file /root/.my.cnf
# Force restoration (without asking for confirmation)
./mariadb-backup.sh --restore /var/backup/mariadb/incr_20250305120000 --forceRestore to a specific point in time:
./mariadb-backup.sh --restore /var/backup/mariadb/full_20250304120000 --pitr "2025-03-04 15:30:00"
# Incremental restoration with pitr
./mariadb-backup.sh --restore /var/backup/mariadb/incr_20250304130000 --pitr "2025-03-04 15:30:00"Apply binary logs without restoring a backup:
./mariadb-backup.sh --pitr-only "2025-03-04 15:30:00"The script can send notifications through multiple channels:
./mariadb-backup.sh --type full --email admin@example.com./mariadb-backup.sh --type full --send-webhook --webhook https://example.com/webhook./mariadb-backup.sh --type full --slack-webhook https://hooks.slack.com/services/T00000000/B00000000/XXXXXXXXXXXXXXXXXXXXXXXX./mariadb-backup.sh --type full --parallel 8./mariadb-backup.sh --type full --compress --compress-threads 4./mariadb-backup.sh --type full --throttle-io 5The script generates logs and can create backup reports:
- Logs can be directed to a file using
--log-file - Log verbosity can be adjusted with
--log-level - A backup report is automatically generated in the backup directory
- Regular Testing: Periodically test backup restoration to ensure reliability
- Diversify Storage: Store backups on different physical servers or cloud storage
- Monitor Backup Jobs: Set up alerts for backup failures
- Secure Encryption Keys: Store encryption keys securely and separately from backups
- Maintain Backup Logs: Keep backup logs for auditing and troubleshooting
- Adjust Retention: Configure retention based on your recovery point objective (RPO)
- Check Disk Space: Ensure sufficient disk space for backups and temporary operations
-
Backup Fails with "Permission Denied"
- Ensure the user running the script has appropriate permissions
- Check directory permissions for backup location
-
"mariabackup: command not found"
- Install the mariabackup package:
apt-get install mariadb-backuporyum install MariaDB-backup
- Install the mariabackup package:
-
Connection Errors
- Verify database credentials
- Check if the MariaDB server is running
- Ensure network connectivity to the database server
-
Out of Disk Space
- Increase available disk space
- Adjust retention policies
- Enable compression to reduce backup size
-
Recovery Fails
- Check MariaDB error logs for detailed information
- Ensure all required incremental backups are available
- Verify permissions on the data directory
# Morning full backup
./mariadb-backup.sh --type full --backup-dir /var/backup/mariadb --mysql-user backup --compress
# Hourly incremental backups throughout the day
./mariadb-backup.sh --type incremental --backup-dir /var/backup/mariadb --mysql-user backup --compress
# Binary log backups every 15 minutes
./mariadb-backup.sh --type binlog --backup-dir /var/backup/mariadb --mysql-user backup# Restore the most recent full backup
./mariadb-backup.sh --restore /var/backup/mariadb/full_20250304120000
# Apply all changes up to the point of failure
./mariadb-backup.sh --pitr-only "2025-03-04 15:28:30"- Store MySQL passwords in a defaults file with restricted permissions
- Encrypt backups containing sensitive data
- Secure encryption keys with proper permissions (chmod 400)
- Use a dedicated backup user with minimal privileges
- Protect backup storage with appropriate access controls
The MariaDB Backup Manager script provides a comprehensive solution for database backup and recovery. By following the guidelines in this documentation, you can implement a robust backup strategy for your MariaDB databases.