DevOps / Scripts bash Intermediate

MySQL Nightly Backup

Dump all MySQL databases to timestamped files and rotate old backups.

bash
#!/bin/bash
DATESTAMP=$(date +%F)
DUMPDIR=/var/backups/mysql/$DATESTAMP
mkdir -p $DUMPDIR
mysqldump --all-databases | gzip > $DUMPDIR/all.sql.gz
find /var/backups/mysql/ -maxdepth 1 -mtime +14 -exec rm -rf {} +

Prerequisites

  • mysqldump
  • gzip

Pro Tips

  • → Run via cron nightly at 2am.

Was this module useful?

Share your experience — it helps the team improve.

Leave Feedback

Commercial Licensing

Acquire a production access token or template license for enterprise deployment.

Module Info

Language
bash
Level
Intermediate
Read time
3 min
Published
Apr 2024
License
Commercial License

Newsletter

New modules every week, directly to your inbox.