Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

deleting old files using crontab

Tags:

linux

crontab

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?

like image 535
Ran Avatar asked Mar 21 '11 08:03

Ran


People also ask

What does cron 0 * * * * * mean?

*/5 * * * * Execute a cron job every 5 minutes. 0 * * * * Execute a cron job every hour.


2 Answers

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.

like image 59
dogbane Avatar answered Oct 02 '22 02:10

dogbane


find /db_backups/ -mtime +30 -delete

This command would delete DB backups older than 30 days.

like image 23
Shamit Verma Avatar answered Oct 02 '22 03:10

Shamit Verma