0

I have a CentOS server running with backup made to an external HDD.

I run a full backup everyday at 4am and incremental backups every 2 hour. I am keeping the last 30 days backups which is achieved by running a cron job every day at 6am which clears all files older than 30 days:

0  6  *  *  *  root /bin/find /mnt/hp/backups -mtime +30 -exec rm -f {} \;

Recently my HDD is getting out of space so I am changing my backup strategy to only keep 4 full backups for last 4 weeks. Eg, full backup of the every Monday.

How do I write a script to keep last 4 full backup for the past 4 weeks? I am using dump to perform the backups

Kong Jin Jie
  • 535
  • 5
  • 11

1 Answers1

1

Do like this:

  • Take full weekly backup with a unique name prefix like weeklybkp_
  • Then put a script like following in cron after every weekly backup.
DELETEMORETHAN=$(ls -1 weeklybkp* | wc -l)
if [ "$DELETEMORETHAN" -gt 4 ] ; then
   COUNT=$(echo "$DELETEMORETHAN - 4" | bc -l)
   rm -rvf $(ls -1t weeklybkp* | tail -${COUNT})
fi
Suku
  • 3,820
  • 1
  • 21
  • 23