Tuesday, October 4, 2011

Backup Deletion X no. of days

vi rmbackup.sh

#!/bin/bash
set -x
###Script for cron to delete backups
DIRECTORY=/home/BackupLocation/
DELETEOLDERTHAN="3"
find $DIRECTORY -type f -ctime +$DELETEOLDERTHAN
ls -la | mail -s "backup delete $HOSTNAME -'date'" abc@yourdoamin.com
exit 0

save and exit and chmod +x rmbackup.sh

Put the above script in cron
#crontab -e

*  3  * * * /home/rmbackup.sh

==================================================
Rsync Remote host backup script : Put this script on the machine where you want to take/pull the backup. generate the rsync key
#!/bin/sh
RSYNC=/usr/bin/rsync
SSH=/usr/bin/ssh
KEY=/root/rsync/mirror-rsync-key
RUSER=ursync                                              : create a user for rsync
RHOST=hostname of the remote machine
RPATH=/var/www/html/                                : remote data backup path
LPATH=/var/www/html/                                : Local Path
$RSYNC -azq --delete -e "$SSH -i $KEY" $RUSER@$RHOST:$RPATH $LPATH



Another way

Delete directory based on the time stamp. The below command will delete all the directories with time stamp 2012.

** you need to run this command in the same directory

rm -vrf `ls -lh | sed -n '/2012/p' | awk '{print $8}'`



No comments:

Post a Comment