12

I use the following crontab record in order to daily backup my DB:

0 2 * * * MYSQL_PWD=password mysqldump -u user db_name > $HOME/db_backups/db_name-$(date +\%Y-\%m-\%d-\%H-\%M).sql 2>> $HOME/db_backups/cron.log

I want to add another crontab record that will delete the DB dumps that are older then one month.

Any thoughts?

Ran
  • 3,455
  • 12
  • 47
  • 60
  • @knittle logrotate is for logs (?) – AlxVallejo Aug 30 '12 at 22:37
  • 1
    While logrotate can delete files of a certain age, it only operates on files that it rotates; not a typical scenario for a db backup. Of course you can add the `find ... -exec rm {} \;` command (documented in other answers) as a script within the logrotate configuration. This may be useful in some cases; [example in last entry of a conversation thread on another forum](http://www.directadmin.com/forum/archive/index.php/t-3654.html) – Tom Harrison Oct 11 '12 at 16:20

3 Answers3

27
find /db_backups/ -mtime +30 -delete

This command would delete DB backups older than 30 days.

Shamit Verma
  • 3,839
  • 23
  • 22
22

Just create another cron:

0 3 * * * find $HOME/db_backups -name "db_name*.sql" -mtime +30 -exec rm {} \; >> $HOME/db_backups/purge.log 2>&1

It will find all backups older than 30 days and delete them.

dogbane
  • 266,786
  • 75
  • 396
  • 414
3

There is a tool called tmpreaper that securely deletes files matching certain criteria, such as an access or modification date n days in the past.

Simon Richter
  • 28,572
  • 1
  • 42
  • 64