Skip to content

Instantly share code, notes, and snippets.

@ground-creative
Created April 1, 2012 14:05
Show Gist options
  • Save ground-creative/2275492 to your computer and use it in GitHub Desktop.
Save ground-creative/2275492 to your computer and use it in GitHub Desktop.
RSYNC AUOBACKUP TO USB DEVICE
#!/bin/bash
####### RSYNC AUOBACKUP TO USB DEVICE V0.5 ##########
####### Developed by Carlo Pietrobattista ###########
### CONFIG VARS #####################################
LOCK_FILE="/tmp/rsync-usb.pid" # the lock file name
MOUNT_DEV="/dev/sdh1" # mount device
MOUNT_PATH="/mnt/autobackup-usb" # mount point
BACKUP_DIR="/home/*" # backup directory
RSYNC_LOG_FILE="/var/log/rsync/usb_backup.log" # rsync log file
#####################################################
#####################################################
echo "Rsync usb backup task started"
logger -t rsync[usb-backup][$$] "Rsync usb backup task started" -p daemon.notice
sleep 1
### CHECK IF THE SCRIPT IS ALREADY RUNNING ##################
if [[ -f $LOCK_FILE ]] ;
then
echo "The script is already running, previous backup might be still executing, will exit!"
logger -t rsync[usb-backup][$$] "The script is already running, previous backup might be still executing, will exit!" -p daemon.notice
exit 0
fi
touch $LOCK_FILE
### BACKUP FUNCTION ##########################################
function rsync-backup
{
echo "Found device $MOUNT_DEV on mount point $MOUNT_PATH, starting to backup data!"
logger -t rsync[usb-backup][$$] "Found device $MOUNT_DEV on mount point $MOUNT_PATH, starting to backup data!" -p daemon.notice
rsync -razdv $BACKUP_DIR $MOUNT_PATH 2>&1 >> $RSYNC_LOG_FILE
echo "Backup job is finished, unmounting $MOUNT_DEV"
logger -t rsync[usb-backup][$$] "Backup job is finished, unmounting $MOUNT_DEV and exiting" -p daemon.notice
umount $MOUNT_DEV
rm -f $LOCK_FILE
exit 0
}
### THE ACTUAL SCRIPT ########################################
if df |grep -q $MOUNT_PATH
then
rsync-backup
else
echo "$MOUNT_DEV is not mounted, trying to mount on $MOUNT_PATH!"
logger -t rsync[usb-backup][$$] "$MOUNT_DEV is not mounted, trying to mount on $MOUNT_PATH!" -p daemon.notice
mount $MOUNT_DEV $MOUNT_PATH
sleep 30
if df |grep -q $MOUNT_PATH
then
#do the backup
rsync-backup
else
echo "Something went wrong, could not mount hdd. Exiting!"
logger -t rsync[usb-backup][$$]"Something went wrong, could not mount hdd, exiting!" -p daemon.err
exit -1
fi
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment