A simple bash script to automate MySQL database backups with options for compression and upload.
I created this script with ChatGPT because I quickly needed a database backup script for my weather station.
I do not plan to maintain it, but if you find it useful, feel free to use it.
- 💾 Local MySQL database backups using
mysqldump - 📦 Optional compression using
gzip - ☁️ Optional remote upload via rclone
- 🧹 Optional cleanup of old local backups
- Install dependencies:
mysqldump(part of MySQL client tools)gzip(optional for compression, usually pre-installed on most Linux distributions)rclone(optional for remote uploads, see rclone installation guide)
- Clone the repository.
git clone https://github.com/bartekl1/mysql-backup.git
cd mysql-backup
cp backup.example.conf backup.confInstead of cloning the repository, you can also download the backup.sh script directly.
wget https://raw.githubusercontent.com/bartekl1/mysql-backup/refs/heads/main/backup.sh
wget https://raw.githubusercontent.com/bartekl1/mysql-backup/refs/heads/main/backup.example.conf -O backup.conf-
Edit
backup.confconfiguration file. See the Configuration section for more details. -
Make the script executable.
chmod +x backup.sh- Manually run the script to test it.
./backup.sh- (Optional) Set up a cron job to automatically run the script at the desired time.
crontab -eExample (backup every day at 2 AM):
0 2 * * * bash /path/to/backup.sh
The script can be configured via the backup.conf file.
Example configuration file is available in backup.example.conf.
See comments in the file for explanation of each option.
This project is licensed under Zero-Clause BSD License.
This means you can freely use, modify, and redistribute the code for commercial and non-commercial purposes without attribution.