This Bash script provides a robust and flexible solution for backing up MySQL databases. It is designed to run automatically (e.g., via cron) or manually, featuring validation, detailed email notifications, and advanced retention management.
- Flexible Backup: Automatically backs up all databases (with exclusions) or specific databases.
- Configurable mysqldump Command: Allows specifying the exact
mysqldumpexecutable to use, useful for environments with multiple MySQL versions. - Flexible Compression: Option to enable or disable compression entirely (
COMPRESSION_ENABLED). - Configurable Compression Strategy: If compression is enabled, choose between:
per_database: Each database is compressed into its own archive (DBNAME_UNIQUEID.tar.ext).per_job: All successful database dumps from a single run are combined into one archive (DD_UNIQUEID.tar.ext).
- Unique File Naming: Option to append a unique ID (timestamp
HHMMSS) to backup filenames (UNIQUE_ID_ENABLED="yes") to support multiple backups within a day. IfUNIQUE_ID_ENABLED="no", backup files for the same day will be overwritten. - Retention Management: Automatically deletes old backups based on a configured count, sorted by modification time. It also cleans up any empty date directories left behind after cleanup. Set
RETENTION_COUNT=0to disable cleanup entirely. Note: IfUNIQUE_ID_ENABLED="no",RETENTION_COUNTis ignored, and no old backups will be cleaned up. - Detailed Email Notifications: Sends comprehensive backup status reports (start, success, failure) with file sizes, locations, lists of old backups, and a list of deleted old backups (conditional on
UNIQUE_ID_ENABLED). - Execution Mode Detection: Adjusts console output when run interactively or via cron (INFO messages only in interactive console, ERROR/FATAL always).
- Lock File: Prevents simultaneous script execution.
- Secure Temporary Directory: Temporary directories for SQL dumps are created within
BACKUP_PATHand automatically cleaned up. - Consistent Backups: Uses
--single-transactionfor InnoDB tables to ensure data consistency without locking.
- Bash shell
- Accessible MySQL server
mysqldumpandmysqlCLI tools installed and available in PATH (the specificmysqldumpcommand can be configured viaMYSQLDUMP_CMD)sendmailconfigured for email delivery- Your chosen compression tool (
gzip,bzip2,xz) - Standard Linux/BSD utilities:
du,df,find,rm,tar,sort,cut,stat,basename,mktemp,base64
- Clone Repository: (If the script is in a repository)
git clone https://github.com/alifgufron/bashforge.git cd bashforge/BackupMysql - Create Configuration File: Copy the sample configuration file:
cp mysql-backup.conf.sample mysql-backup.conf
- Edit Configuration: Open
mysql-backup.confwith your favorite text editor and adjust parameters according to your MySQL environment. EnsureBACKUP_PATHis a valid and writable directory. - Set Permissions: Make sure the script is executable:
chmod +x BackupMysql
Some important parameters you need to adjust:
MYSQL_HOST: MySQL host address.MYSQL_PORT: MySQL port. Leave empty to use the default port (3306).MYSQLDUMP_CMD: The command or absolute path to themysqldumpexecutable to use (e.g.,mysqldump,mysqldump57,/usr/local/bin/mysqldump).MYSQLDUMP_EXTRA_OPTS: Additional options passed directly tomysqldump. Useful for compatibility with different MySQL versions. Refer to the comments inmysql-backup.conf.samplefor detailed examples.BACKUP_ALL_DATABASES:yesto back up all,nofor specific databases.DATABASES: Array of specific databases ifBACKUP_ALL_DATABASES="no".EXCLUDE_DATABASES: Array of databases to exclude ifBACKUP_ALL_DATABASES="yes".BACKUP_PATH: Absolute directory for storing backup files.COMPRESSION_ENABLED:yesto enable compression,noto store raw.sqlfiles.COMPRESSION_STRATEGY:per_databaseorper_job. Only relevant ifCOMPRESSION_ENABLED="yes".COMPRESSION_TYPE:gzip,bzip2, orxz. Only relevant ifCOMPRESSION_ENABLED="yes".UNIQUE_ID_ENABLED:yesto append unique IDs (timestampHHMMSS) to filenames (e.g.,db_HHMMSS.sqlordb_HHMMSS.tar.gz),nootherwise. Ifno, backup files for the same day will be overwritten.RETENTION_COUNT: Number of backups to keep (0 to disable cleanup). Note: IfUNIQUE_ID_ENABLED="no",RETENTION_COUNTis ignored, and no old backups will be cleaned up.MIN_DISK_SPACE_GB: Minimum required free disk space.MAIL_TO,MAIL_FROM,NOTIFY_ON_START: Email notification settings.
Run the script by providing your configuration file:
./BackupMysql mysql-backup.confIf you do not provide a configuration file name, the script will look for mysql-backup.conf by default.
All script activities are logged to /var/log/custom/mysql_dump_HOST.log.
The script will send email notifications to the configured MAIL_TO for the initial status and a final report (success/failure) of the backup process.