#!/bin/bash
#the VOLUME_A is where you want your backup to be, on MACOSX you can plug a USB drive and it will always be mounted 
#on /Volumes/NAME_OF_DRIVE
#The example below is for a USB DRIVE named BACKUP which will automatically mounted on /VOLUMES
#You can change this to wherever your backup destination is, i.e.: a network drive.
TARGET_VOLUME_A="/Volumes/MYDRIVE/"

#This will be the directory where the files will be backed up to, on the backed up volume set above.
TARGET_DIR_A="mybackup/"

#This is the full path for the backup VOLUME + TARGET
TARGET_A=$TARGET_VOLUME_A$TARGET_DIR_A

#This is the path where the files and directory you want to be backed up are located
SOURCE_A="/Users/sylvain_user/"

DATE_ROOTNAME="Auto_Backup_Lastdone_"
BACKUP_FILE=$TARGET_VOLUME_A$DATE_ROOTNAME

#These FOLDERS below, are the one you want to specifically back up, up to 3.
#If you need more folder, add more variables below and in further in the code where FOLDER_A_1 appear
#Or if you don't want to specify specific folders, just use "*" in A_1 and leave the other ones empty.
FOLDER_A_1="Dropbox"
FOLDER_A_2="Movies"
FOLDER_A_3="Document"

FOLDERS_A=($FOLDER_A_1 $FOLDER_A_2 $FOLDER_A_3)
FOLDERS_A_NB=${FOLDERS_A[@]}

#These are the folder and files you don't want to be backed up
EXCLUDE_A1=".dropbox.cache"
EXCLUDE_A2="Home Videos"
EXCLUDE_A3=""

clear
echo "-----------------------------------"
echo "-----     BACKUP SYNC V1.2    -----"
echo "-----    01/05/2013  London   -----"
echo "-----------------------------------"
echo
echo "=== Init"
echo "[-] Target is:"
echo -e "\t$TARGET_A"
if [ ! -d "$TARGET_VOLUME_A" ]; then
	echo -e "[\033[0;31m!!\033[0m] The Target Drive is missing: $TARGET_VOLUME_A"
	echo -e "[\033[0;31m!!\033[0m] STOPPING."
	exit
fi
echo -e "[\033[0;32mOK\033[0m] Target Drive Detected"
if [ ! -d "$TARGET_A" ]; then
	echo -e "[\033[0;31m!!\033[0m] The following Target Folder is missing:"
	echo -e "\t$TARGET_A"
	mkdir -p $TARGET_A
	echo -e "[\033[0;32mOK\033[0m] $TARGET_A Created"
fi

echo "[-] Folders to backup:"
for i in "${FOLDERS_A[@]}"
do
echo -e "\t$SOURCE_A$i"
done

echo "[-] Target Volume Disk Utilisation is:"
df -h $TARGET_VOLUME_A | awk ' { print "\t" $2 "\t" $3 "\t"  $4 "\t" $5}'

if [ ! -f $BACKUP_FILE* ]; then
	echo -e "[\033[0;31m!!\033[0m] No Record of Last Backup Date"
else
	echo "[-] Last Backup done:"
	LAST_BACKUP=`cat $BACKUP_FILE*`
	echo -e "\t$LAST_BACKUP"
fi

echo -e "[\033[0;36m??\033[0m] Are you sure? [y/n] "
read -s -n 1
if [[ ! $REPLY =~ ^[Yy]$ ]]
then
	echo
	echo -e "[\033[0;31m!!\033[0m] STOPPING."
    exit 1
fi
echo
echo -e "[\033[0;32mOK\033[0m] User wants to continue"

start_time=$(date +%s)
echo "=== SYNCHRONISATION"
j=1
k=${#FOLDERS_A[@]}
d=$TARGET_A

for i in "${FOLDERS_A[@]}"
do
s=$SOURCE_A$i

echo "[$j/$k] Sync Folder Started of $s"
rsync -av --delete --exclude "$EXCLUDE_A1" --exclude "$EXCLUDE_A2" --exclude "$EXCLUDE_A3" --stats --progress --out-format="[$j/$k]%n%L" $s $d
echo -e "[\033[0;32mOK\033[0m] Sync Folder [$j] Finished"
((j++))
done

echo -e "[\033[0;32mOK\033[0m] FOLDERS SYNCHRONISATION COMPLETE"
finish_time=$(date +%s)
echo "[-] Nb of Seconds to complete: $((finish_time - start_time))"
echo "[-] Nb of Minutes to complete: $(( $((finish_time - start_time)) /60 ))"

rm -f $BACKUP_FILE*
DATE_T=`date +"%y%m%d"`
DATE_TOUCH=$BACKUP_FILE$DATE_T
echo $DATE_T > $DATE_TOUCH
