Weblog entry #8 for blackm
#8
rotating backup files
Posted by blackm on Sun 2 Oct 2005 at 20:31
If you have a directory in which are all you backups (one file per day) you can use the following bash code to rotate these files.
This will delete the oldest file if there are more then 30 file but always keep 30 files (if the backup process fail, old backups are not going to be deleted).
if [ `ls -1 $DIR | wc -l` -ge 30 ]
then
OLD=`expr \`ls -t1 $DIR | wc -l\` - 30`
for FILE in `ls -t1 $DIR | tail -n $OLD`
do
rm $FILE
done
fi
This will delete the oldest file if there are more then 30 file but always keep 30 files (if the backup process fail, old backups are not going to be deleted).