How to logrotate daily backups

backuplogrotate

At my company we create daily backups (full, not incremental) of our various stuff. We'd like to rotate them in the following way:

  • keep daily backups for the past week
  • keep only weekly backups for the past month (or 3 months or so)
  • keep only monthly backups for the last year

So basically, recent backups should be fine-grained, not so recent – more coarse grained.

And we're lazy 🙂 Can this be done with logrotate? Or some other tool?

Regards,

Mike

Best Answer

You can schedule one or more scripts like the one below with cron.

#!/bin/bash

BULOG=/srv/backup/savelog
BULST="/etc /srv/www /var/lib/named"
EMAIL=me@example.com

today=`date +%d%m%y`
deldate=`date +%d%m%y --date '7 day ago'`
echo "Backup started: "`date`> $BULOG
echo "Backup "`date`         >>/srv/backup/ErrorLog

cd /srv/backup
rm -f backup$deldate.tar.gz

tar czf /srv/backup/backup$today.tar.gz $BULST 2>>/srv/backup/ErrorLog
if [ $? -eq 0 ];then
        echo "Backup $BULST success" >> $BULOG
else
        echo "Backup $BULST NOT processed" >> $BULOG
fi

echo "-------" >>/srv/backup/ErrorLog

echo "Backup finished: " `date` >> $BULOG

cat $BULOG | mail -s MyBacukp_$today $EMAIL

This needs just a little bit of tweaking to server your requests.