Mysqldump rotate backup

I have a very simple script that uses a cron job to take a daily backup of our orders database.

echo "Dumping ORDERS database";
mysqldump -u root --password='mypassword' -h '1.1.1.1' --opt --compress ORDERS $tbl_names > /Volumes/Files_Backup_1/db_backups/orders.sql
echo "Copied database to external disk";

This will make a single backup on the external hard disk. My worry is if somehow the orders database was TRUNCATED, DROPPED then that evening the database backup would overwrite the empty version of the database to the backup and i would be in big trouble. What i need to know how to do it understand how i can use mysqldump to make a weeks worth of daily backup. Maybe a mon, tue, wed, thur, fri, sat, sun backup or something similar.

I could do

mysqldump ... > database_$(date +%Y_%m_%d).sql

Or something but then my backup disk would fill up. I need some way of deleting the older backups. Any ideas

You may use the day of the week:

mysqldump ... > database_$(date +%w).sql
1 Like

Thanks that seems to work :smiley: