#!/bin/bash # # SYNCRONISATION SCRIPT TO GOOGLE DRIVE # # Usage: gdrive_sync.sh [-n] # # Fri Aug 22 14:21:56 CEST 2014 ### Config RSYNCPARAMS="-aHvh --delete" TMPFILE="/tmp/gdrive_sync.sh.tmp" # Complete path with filename for mail content EMAIL="hanr" # Address where content or alert shoult be sent, semicolon sep. FROM="gdrive-sync" GDFSTOOL=/usr/local/bin/gdfstool CREDFILE=/var/cache/gdfs/credcache SRCFS="/mybook" SOURCE="$SRCFS/backup" MNTPT="/mnt" DEST="$MNTPT/backup/`hostname`" TIMESTART=`date +%s` DRYRUN= ### Functions f_sendmail() { if [ "$DRYRUN" ]; then cat $TMPFILE else cat $TMPFILE |mailx -a "From:$FROM" -s "GDrive Syncronization" $EMAIL # Send mail with content of file fi exit 0 } f_checkfs() { if [[ -z `mount |grep "$1 "` ]]; then echo "!!! Skipped GDrive Syncronization!! $1 not mounted" >>$TMPFILE f_sendmail fi } ### SCRIPT [ "`whoami`" != "root" ] && { echo "Must run as root"; exit 2; } echo "++++++++++++++++++++++ GDrive sync start at `date +"%H:%M:%S"` +++++++++++++++++++++++" >$TMPFILE if [ "$1" == "-n" ]; then RSYNCPARAMS="$RSYNCPARAMS -n"; DRYRUN=1 echo "RUNNING IN DRY MODE" fi # Check if relevent FS are mounted and dirs exist [ -x $GDFSTOOL ] || { echo "$GDFSTOOL not found or executable"; exit 2; } [ -r $CREDFILE ] || { echo "$CREDFILE not found or readable"; exit 2; } $GDFSTOOL mount $CREDFILE $MNTPT f_checkfs $MNTPT f_checkfs $SRCFS [ -d $DEST ] || mkdir -p $DEST # Do the sync rsync $RSYNCPARAMS $SOURCE $DEST >> $TMPFILE printf "\n\n++++++++++++++++++++++ GDrive sync end at `date +"%H:%M:%S"` +++++++++++++++++++++++" >>$TMPFILE TIMESTOP=`date +%s` printf "\n++++++++++++++++++++++++++ Duration `expr $TIMESTOP - $TIMESTART`sec +++++++++++++++++++++++++++" >>$TMPFILE umount $MNTPT f_sendmail