Wednesday, 8 June 2011

Shell script trick to distribute a backup on different folders based on day of the month

The attached backup script checks uses the day of months to determine which backup folder to use. The equation is day of month modulus 5, so basically backups are taken in five different folders before it starts overriding the oldest folder once again.

The script will generate the following folders:
/storage/backup/0/storage/backup/1
/storage/backup/2
/storage/backup/3

/storage/backup/4

So if for example the day of month is 12, then backup is taken in folder /storage/backup/2 and so on.

 backup.sh
#!/bin/ksh
BackupParentDir=/storage/backup
backupid=`date | awk '{print$3%5}' `
echo backupid=$backupid
BackupDir=$BackupParentDir/$backupid

echo Backup directory = $BackupDir

rm -rf $BackupDir
mkdir $BackupDir
#BackupDir is ready for use now, and you can copy what ever needed to this folder
#Add the rest of your backup logic

No comments:

Post a Comment