This is a simple backup script for MySQL servers, designed to help you easily back up your databases and manage your backup files. You can specify your database connection details and customize the backup settings using environment variables. This tool also provides the option to clean up old backups to save space.
Before you can use this tool, make sure you have the following prerequisites installed on your system:
- MySQL Client: Ensure that you have the MySQL client installed to interact with your MySQL databases.
- GNU Parallel: Required to run backup jobs concurrently.
-
Clone this repository to your local machine or server.
git clone https://github.com/obrunsmann/mysql-server-backup.git cd mysql-server-backup -
Create a
.envfile based on the.env.exampletemplate provided. This file will hold your database connection details and backup configuration.cp .env.example .env
-
Open the
.envfile in a text editor and provide the required information:db_host: The hostname or IP address of your MySQL server.db_user: The MySQL user with backup privileges.db_pass: The password for the MySQL user.max_parallel: The maximum number of parallel database backups, orauto.max_parallel_cap: Upper bound whenmax_parallel=auto.mysql_connection_reserve: Number of MySQL connections reserved for non-backup traffic whenmax_parallel=auto.max_backups: The maximum number of backup sets to retain.
-
Make sure the script file is executable.
chmod +x dump.sh
To create database backups using this tool, simply run the dump.sh script:
./dump.shThe script will perform the following actions:
-
Load Environment Variables: It reads the configuration from the
.envfile. -
Create Backup Directory: It creates a directory in the
backup/folder with a timestamp to store the backups. -
Backup Databases in Parallel: It retrieves a list of databases from the MySQL server (excluding system databases) and backs them up in parallel based on the
max_parallelsetting. -
Cleanup Old Backups: It checks the number of backup sets and removes old backups if the number exceeds the
max_backupssetting.
For large MySQL servers (for example 400+ databases), set:
max_parallel=auto
max_parallel_cap=16
mysql_connection_reserve=20With auto, the script computes parallel jobs from:
- detected CPU core count
- MySQL
max_connectionsminusmysql_connection_reserve max_parallel_cap
The minimum of these values is used as job count.
Guidelines:
- Many small databases: increase
max_parallel_capcarefully and observe DB load. - Few large databases: lower cap can reduce I/O contention and stabilize runtime.
- Busy production DB: increase
mysql_connection_reserveto protect foreground traffic.
Here's an example of how to run the backup script:
./dump.shThis backup tool is released under the MIT License. See the LICENSE file for more details.
- Oliver Brunsmann: @obrunsmann
That's it! You now have a simple backup tool to help you manage your MySQL database backups. If you encounter any issues or have suggestions for improvements, feel free to open an issue on GitHub or contribute to the project.