#!/bin/sh
date=`date -Iminutes`
find /var/sqlbackup/ -name 'sqlbackup-*' -mtime +2 -delete
mysqldump --all-databases -pmypassword | gzip > /var/sqlbackup/sqlbackup-$date.sql.gz
You can pipe to gzip
to compress.
I moved the find to before the mysqldump. I'm not sure how big your databases are, but it'll help to keep disk usage down and avoid any space issues.
It's usually better to use -mtime +2
instead of -mtime 3
to affect anything older than two days rather than files exactly 3 days old. This way if your cron misses a day, you don't get older files missed by the delete.
Personally I prefer to restrict find
to a name pattern, too, to make sure you aren't deleting anything unexpectedly.
The -delete
option in find is helpful if you don't want to use xargs
. Both ways will work, but I prefer to use fewer commands when possible.