A simple, robust bash script for backing up MySQL/MariaDB databases on Linux systems.
- Dumps each non-system database to compressed
.sql.gz
files - Creates timestamped backup folders
- Packs all database dumps into a single tar.gz archive
- Configurable retention policy (auto-delete old backups)
- Safe authentication using external config file
- Intelligent compression (uses pigz for multi-threaded compression if available)
- Comprehensive logging
- Lock mechanism to prevent concurrent runs
- Bash shell
- MySQL/MariaDB client tools
- gzip (or pigz for faster compression)
-
Download the script:
wget https://raw.githubusercontent.com/wnstify/db-backup/refs/heads/main/db_backup.sh chmod +x db_backup.sh
-
(Optional) Create a credentials file to avoid password prompts:
echo -e "[client]\nuser=root\npassword=YOUR_PASSWORD" > .db.cnf chmod 600 .db.cnf
Simply run the script:
./mysql_backup.sh
Or set up a cron job for automated backups:
# Run daily at 2:30 AM
30 2 * * * /path/to/mysql_backup.sh
Edit these variables at the top of the script:
LOCAL_BACKUP_DIR="${PWD}/db_backups" # where backups are stored
KEEP_DAYS=14 # retention period (0 = keep forever)
REMOVE_FOLDER_AFTER_ARCHIVE=true # remove individual dumps after archiving
- Creates a timestamped directory for the current backup
- Connects to MySQL/MariaDB and gets a list of non-system databases
- Dumps each database with optimal settings for reliable restores
- Compresses each dump with gzip/pigz
- Archives all dumps into a single tar.gz file
- Cleans up old backups according to retention policy
- Logs all operations
For more advanced features, check out our Premium Backup Solution:
- Enhanced Security: AES-256 encryption for all backups
- Multi-destination Support: Upload to S3, Google Drive, FTP, SFTP
- Notification System: NTFY push notifications
- Integrity Verification: Automatic backup validation
- Cron Wizard: Easy scheduling interface
- Remote Upload Wizard: Simple configuration for cloud storage
The premium version is available exclusively for Webnestify YouTube channel members.
Join our channel: https://www.youtube.com/channel/UCqkKrB0YsnooQsRmwkBEV3g/join
This script is provided "AS IS", without any warranty, express or implied. The author is not responsible for any damages or data loss. By using this script, you accept full responsibility.
Simon Gajdosik (Webnestify)
MIT