remove old backups automatically with cron

your first problem is solved. you already have a little crontab entry that generates nightly backups. here’s what your backup directory might look like:

$ cd ~/backup
$ ls -F
2008-06-17/  2008-06-22/  2008-06-27/  2008-07-02/  2008-07-07/
2008-06-18/  2008-06-23/  2008-06-28/  2008-07-03/  2008-07-08/
2008-06-19/  2008-06-24/  2008-06-29/  2008-07-04/  2008-07-09/
2008-06-20/  2008-06-25/  2008-06-30/  2008-07-05/  2008-07-10/
2008-06-21/  2008-06-26/  2008-07-01/  2008-07-06/  2008-07-11/

neato. you have backups. but (gross) it takes up a buncha disk space. thusly:

$ du -sh .
671M	.

so you need a little bash script jobbie that removes everything older than 30 days! here’s such a script:

#!/bin/sh
MONTH_AGO=`date -d "30 days ago" +%Y-%m-%d` # linux
#MONTH_AGO=`date -r $(echo $(date +%s) - 30*24*60*60 | bc) +%Y-%m-%d` # osx
for d in $(ls -r); do
  if [[ -d $d && $d < $MONTH_AGO ]]; then
    #echo removing $d
    rm -rf $d
  fi
done

cool. now just make a crontab entry to run after your backups or something. no more quota issues. hooray for you!!


About this entry