Rsync Backup Script

From Notepedia
Jump to: navigation, search

I've created a script to do daily backups of my RAID using rsync. It checks if a disk is mounted, if it's mounted it'll do the rsync, if it's not mounted it'll mount it then rsync, if the mount fails it'll exit the script.

This hasn't been recently tested, I used this for a while but ended up writing a Python backup script instead hosted at https://bitbucket.org/mandreacchio/backup-script/overview
#!/bin/bash
MOUNTDIR='/mnt/backupLV'
SOURCEDIR='/dev/mapper/BackupVol-backupLV'
OSBACKUP='/mnt/backupLV/OS'
DATE=`date +'%F %R'`
USER="dude"
if [ $(mount | grep -c $MOUNTDIR) != 1 ]; then
   `mount $SOURCEDIR $MOUNTDIR`
   if [ $(mount | grep -c $MOUNTDIR) == 1 ]; then
       echo "$DATE $MOUNTDIR is now mounted"
   else
       echo "$DATE Some problem happened when mounting, abort"
       exit 0
   fi
else
  echo "$DATE $MOUNTDIR already mounted"
fi
echo "$DATE Rsync RAID Array"
/usr/bin/rsync -aumvv --delete --progress /home/$USER/Video /home/$USER/Music /home/$USER/Pictures $MOUNTDIR
echo "$DATE TAR Home DIR"
find /home/$USER/ -maxdepth 1 -iname ".*" | xargs tar -czvf $OSBACKUP/`date +%F`-home.tar.gz
echo "$DATE TAR ETC DIR"
tar -czvf $OSBACKUP/`date +%F`-etc.tar.gz /etc
echo "$DATE TAR SRV DIR"
tar -czvf $OSBACKUP/`date +%F`-srv.tar.gz /srv/http
if  -n $(find /mnt/backupLV/OS -mtime +60 -type f) 
then
  echo "$DATE Found old backup files to delete"
  find /mnt/backupLV/OS -mtime +60 -type f | xargs rm
else
  echo "$DATE No old backup files to delete"
fi
The crontab for the script is the following, note it makes a log file of the backup events
 5 0 * * * $HOME/bin/backup.sh > $HOME/tmp/out 2>&1