Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/bin/bash
- PROG_NAME=$(basename $0)
- LOGFOLDER="$HOME/programming/scripts/sync/logs"
- function _show_time () {
- printf -v _new_time "%d days %d hours %02d minutes %02d seconds" $(($1 / 86400)) $((($1 / 3600) % 24)) $((($1 / 60) % 60)) $(($1 % 60))
- }
- function remote_status () {
- if ! ping -c 1 "$ip" &> /dev/null
- then
- printf "\n$ip is offline at $(date)\n"
- # printf "\n$ip is offline at $(date)\n" | /usr/bin/mail -s "ALERT!!!! $PROG_NAME" "marco.caeiro@gmail.com"
- exit 1
- else
- printf "\n$ip is online at $(date)\n"
- fi
- }
- function is_IP() {
- if [ `echo $1 | grep -o '\.' | wc -l` -ne 3 ]; then
- echo "Parameter '$1' does not look like an IP Address (does not contain 3 dots).";
- exit 1;
- elif [ `echo $1 | tr '.' ' ' | wc -w` -ne 4 ]; then
- echo "Parameter '$1' does not look like an IP Address (does not contain 4 octets).";
- exit 1;
- else
- for OCTET in `echo $1 | tr '.' ' '`; do
- if ! [[ $OCTET =~ ^[0-9]+$ ]]; then
- echo "Parameter '$1' does not look like in IP Address (octet '$OCTET' is not numeric).";
- exit 1;
- elif [[ $OCTET -lt 0 || $OCTET -gt 255 ]]; then
- echo "Parameter '$1' does not look like in IP Address (octet '$OCTET' in not in range 0-255).";
- exit 1;
- fi
- done
- fi
- return 0;
- }
- sync_direct_all () {
- clear
- printf "\nWARNING: Files and directories will be deleted on $dest\n"
- printf "Continue (Y/N)\n"
- read -n1 -r -s choice
- case "$choice" in
- y|Y)
- {
- rsync -acuvz --delete --stats --exclude=.sync --exclude=*.tar --exclude=*.gz --exclude=*.bz2 --exclude=*.xz --exclude=*.sha256sum --exclude=Software $source $dest
- rsync -acuvz --stats --exclude=.sync $source $dest
- printf "\n\nLast sync from $source to $dest at\n"
- date
- } 2>&1 | tee $LOGFOLDER/sync_direct_all_log.txt
- ;;
- *)
- exit
- ;;
- esac
- }
- sync_update_all () {
- clear
- printf "\nWARNING: Files and directories will NOT be deleted on $dest\n"
- printf "Continue (Y/N)\n"
- read -n1 -r -s choice
- case "$choice" in
- y|Y)
- {
- rsync -acuvz --stats --exclude=.sync $source $dest
- printf "\n\nLast sync from $source to $dest at\n"
- date
- } 2>&1 | tee $LOGFOLDER/sync_update_all_log.txt
- ;;
- *)
- exit
- ;;
- esac
- }
- sync_inverse_all () {
- tmp=$dest
- dest=$source
- source=$tmp
- clear
- printf "\nWARNING: Files and directories will be deleted on $dest\n"
- printf "Continue (Y/N)\n"
- read -n1 -r -s choice
- case "$choice" in
- y|Y)
- {
- rsync -acuvz --delete --stats --exclude=.sync --exclude=*.tar --exclude=*.gz --exclude=*.bz2 --exclude=*.xz --exclude=*.sha256sum --exclude=Software $source $dest
- rsync -acuvz --stats --exclude=.sync $source $dest
- printf "\n\nLast sync from $source to $dest at\n"
- date
- } 2>&1 | tee $LOGFOLDER/sync_inverse_all_log.txt
- ;;
- *)
- exit
- ;;
- esac
- }
- sync_inverse_update_all () {
- tmp=$dest
- dest=$source
- source=$tmp
- clear
- printf "\nWARNING: Files and directories will NOT be deleted on $dest\n"
- printf "Continue (Y/N)\n"
- read -n1 -r -s choice
- case "$choice" in
- y|Y)
- {
- rsync -acuvz --stats --exclude=.sync $source $dest
- printf "\n\nLast sync from $source to $dest at\n"
- date
- } 2>&1 | tee $LOGFOLDER/sync_inverse_update_all_log.txt
- ;;
- *)
- exit
- ;;
- esac
- }
- sync_direct_partial () {
- clear
- printf "\nWARNING: Files and directories will be deleted on $dest\n"
- printf "Continue (Y/N)\n"
- read -n1 -r -s choice
- case "$choice" in
- y|Y)
- {
- rsync -acuvz --delete --stats --exclude=.sync --exclude=*.tar --exclude=*.gz --exclude=*.bz2 --exclude=*.xz --exclude=*.sha256sum --exclude=Software $source $dest
- printf "\n\nLast sync from $source to $dest at\n"
- date
- } 2>&1 | tee $LOGFOLDER/sync_direct_partial_log.txt
- ;;
- *)
- exit
- ;;
- esac
- }
- sync_inverse_partial () {
- tmp=$dest
- dest=$source
- source=$tmp
- clear
- printf "\nWARNING: Files and directories will be deleted on $dest\n"
- printf "Continue (Y/N)\n"
- read -n1 -r -s choice
- case "$choice" in
- y|Y)
- {
- rsync -acuvz --delete --stats --exclude=.sync --exclude=*.tar --exclude=*.gz --exclude=*.bz2 --exclude=*.xz --exclude=*.sha256sum --exclude=Software $source $dest
- printf "\n\nLast sync from $source to $dest at\n"
- date
- } 2>&1 | tee $LOGFOLDER/sync_inverse_partial_log.txt
- ;;
- *)
- exit
- ;;
- esac
- }
- option=$1
- source=$2
- dest=$3
- dest_remote=FALSE
- source_remote=FALSE
- if [[ -z $source || -z $dest ]]
- then
- printf "\nUsage: sync_rsync da source dest : for full direct sync\n"
- printf "\nUsage: sync_rsync dp source dest : for partial direct sync\n"
- printf "\nUsage: sync_rsync du source dest : for update direct sync\n"
- printf "\nUsage: sync_rsync ia source dest : for full inverse sync\n"
- printf "\nUsage: sync_rsync ip source dest : for partial inverse sync\n"
- printf "\nUsage: sync_rsync iu source dest : for update inverse sync\n"
- exit
- fi
- if echo $dest | grep -q "@"
- then
- user1=$( echo ${dest//@*} )
- dest_remote=TRUE
- else
- dest_remote=FALSE
- if [[ ! -d $dest ]]
- then
- printf "Job failed at $(date)\n\n Directory destiny $dest does not exist\n" | /usr/bin/mail -s "ERROR!!!! $PROG_NAME" "marco.caeiro@gmail.com"
- exit 1
- fi
- fi
- if echo $source | grep -q "@"
- then
- user2=$( echo ${source//@*} )
- source_remote=TRUE
- else
- source_remote=FALSE
- if [[ ! -d $source ]]
- then
- printf "Job failed at $(date)\n\n Directory source $source does not exist\n" | /usr/bin/mail -s "ERROR!!!! $PROG_NAME" "marco.caeiro@gmail.com"
- exit 1
- fi
- fi
- if [[ "$source_remote" == "TRUE" && "$dest_remote" == "TRUE" ]]
- then
- printf "\nSource and destionation can't be both remote!!!!\n"
- exit 1
- fi
- if [[ ! -z $user1 && -z ${user1//[[:alpha:]]} && "$dest_remote" == "TRUE" ]]
- then
- ip1=$( echo ${dest} | sed -e 's/\(^.*@\)\(.*\)\(:.*$\)/\2/' )
- is_IP $ip1
- rem_dir=${dest#*:}
- ip=$ip1
- remote_status
- printf "\nUser is $user1\n"
- printf "\nIp is $ip1\n"
- if ssh $user1@$ip1 "[ -d $rem_dir ]"
- then
- printf "$rem_dir exists \n"
- # printf "$rem_dir exists \n" | /usr/bin/mail -s "ALERT!!!! $PROG_NAME" "marco.caeiro@gmail.com"
- else
- printf "\nRemote directory $rem_dir do not exists \n"
- printf "\nRemote directory $rem_dir do not exists \n" | /usr/bin/mail -s "ERROR!!!! $PROG_NAME" "marco.caeiro@gmail.com"
- exit 1
- fi
- fi
- if [[ ! -z $user2 && -z ${user2//[[:alpha:]]} && "$source_remote" == "TRUE" ]]
- then
- ip2=$( echo ${source} | sed -e 's/\(^.*@\)\(.*\)\(:.*$\)/\2/' )
- is_IP $ip2
- rem_dir=${source#*:}
- ip=$ip2
- remote_status
- printf "\nUser is $user2\n"
- printf "\nIp is $ip2\n"
- if ssh $user2@$ip2 "[ -d $rem_dir ]"
- then
- printf "\n$rem_dir exists \n"
- # printf "\n$rem_dir exists \n" | /usr/bin/mail -s "ALERT!!!! $PROG_NAME" "marco.caeiro@gmail.com"
- else
- printf "\nRemote directory $rem_dir do not exists \n"
- printf "\nRemote directory $rem_dir do not exists \n" | /usr/bin/mail -s "ERROR!!!! $PROG_NAME" "marco.caeiro@gmail.com"
- exit 1
- fi
- fi
- case "$option" in
- da)
- time1=$(date +"%s")
- sync_direct_all
- if [[ "$?" == "0" ]]
- then
- time2=$(date +"%s")
- dif=$(($time2-$time1))
- _show_time $dif
- printf "\n\n$(date) ::::: All Done! $_new_time elapsed\n" > $LOGFOLDER/time_to_sync_direct_all.txt
- printf "Last sync of $source to $dest was at $(date)\n" > $LOGFOLDER/source_to_dest.txt
- printf "Last sync of $source to $dest was at $(date)\n" | /usr/bin/mail -a $LOGFOLDER/sync_direct_all_log.txt -a $LOGFOLDER/time_to_sync_direct_all.txt -s "ALERT!!!! Sync direct all by $PROG_NAME Done!!!" "marco.caeiro@gmail.com"
- else
- printf "Job failed at $(date)\n" | /usr/bin/mail -s "ERROR!!!! $PROG_NAME" "marco.caeiro@gmail.com"
- fi
- ;;
- dp)
- time1=$(date +"%s")
- sync_direct_partial
- if [[ "$?" == "0" ]]
- then
- time2=$(date +"%s")
- dif=$(($time2-$time1))
- _show_time $dif
- printf "\n\n$(date) ::::: All Done! $_new_time elapsed\n" > $LOGFOLDER/time_to_sync_direct_partial.txt
- printf "Last sync of $source to $dest was at $(date)\n" > $LOGFOLDER/source_to_dest.txt
- printf "Last sync of $source to $dest was at $(date)\n" | /usr/bin/mail -a $LOGFOLDER/sync_direct_partial_log.txt -a $LOGFOLDER/time_to_sync_direct_partial.txt -s "ALERT!!!! Sync direct partial by $PROG_NAME" "marco.caeiro@gmail.com"
- else
- printf "Job failed at $(date)\n" | /usr/bin/mail -s "ERROR!!!! $PROG_NAME" "marco.caeiro@gmail.com"
- fi
- ;;
- ia)
- time1=$(date +"%s")
- sync_inverse_all
- if [[ "$?" == "0" ]]
- then
- time2=$(date +"%s")
- dif=$(($time2-$time1))
- _show_time $dif
- printf "\n\n$(date) ::::: All Done! $_new_time elapsed\n" > $LOGFOLDER/time_to_sync_inverse_all.txt
- printf "Last sync of $source to $dest was at $(date)\n" > $LOGFOLDER/source_to_dest.txt
- printf "Last sync of $source to $dest was at $(date)\n" | /usr/bin/mail -a $LOGFOLDER/sync_inverse_all_log.txt -a $LOGFOLDER/time_to_sync_inverse_all.txt -s "ALERT!!!! Sync inverse all by $PROG_NAME" "marco.caeiro@gmail.com"
- else
- printf "Job failed at $(date)\n" | /usr/bin/mail -s "ERROR!!!! $PROG_NAME" "marco.caeiro@gmail.com"
- fi
- ;;
- ip)
- time1=$(date +"%s")
- sync_inverse_partial
- if [[ "$?" == "0" ]]
- then
- time2=$(date +"%s")
- dif=$(($time2-$time1))
- _show_time $dif
- printf "\n\n$(date) ::::: All Done! $_new_time elapsed\n" > $LOGFOLDER/time_to_sync_inverse_partial.txt
- printf "Last sync of $source to $dest was at $(date)\n" > $LOGFOLDER/source_to_dest.txt
- printf "Last sync of $source to $dest was at $(date)\n" | /usr/bin/mail -a $LOGFOLDER/sync_inverse_partial_log.txt -a $LOGFOLDER/time_to_sync_inverse_partial.txt -s "ALERT!!!! Sync inverse partial by $PROG_NAME" "marco.caeiro@gmail.com"
- else
- printf "Job failed at $(date)\n" | /usr/bin/mail -s "ERROR!!!! $PROG_NAME" "marco.caeiro@gmail.com"
- fi
- ;;
- du)
- time1=$(date +"%s")
- sync_update_all
- if [[ "$?" == "0" ]]
- then
- time2=$(date +"%s")
- dif=$(($time2-$time1))
- _show_time $dif
- printf "\n\n$(date) ::::: All Done! $_new_time elapsed\n" > $LOGFOLDER/time_to_sync_update_all.txt
- printf "Last sync of $source to $dest was at $(date)\n" > $LOGFOLDER/source_to_dest.txt
- printf "Last sync of $source to $dest was at $(date)\n" | /usr/bin/mail -a $LOGFOLDER/sync_update_all_log.txt -a $LOGFOLDER/time_to_sync_update_all.txt -s "ALERT!!!! Sync update all by $PROG_NAME Done!!!" "marco.caeiro@gmail.com"
- else
- printf "Job failed at $(date)\n" | /usr/bin/mail -s "ERROR!!!! $PROG_NAME" "marco.caeiro@gmail.com"
- fi
- ;;
- iu)
- time1=$(date +"%s")
- sync_inverse_update_all
- if [[ "$?" == "0" ]]
- then
- time2=$(date +"%s")
- dif=$(($time2-$time1))
- _show_time $dif
- printf "\n\n$(date) ::::: All Done! $_new_time elapsed\n" > $LOGFOLDER/time_to_sync_inverse_update_all.txt
- printf "Last sync of $source to $dest was at $(date)\n" > $LOGFOLDER/source_to_dest.txt
- printf "Last sync of $source to $dest was at $(date)\n" | /usr/bin/mail -a $LOGFOLDER/sync_inverse_update_all_log.txt -a $LOGFOLDER/time_to_sync_inverse_update_all.txt -s "ALERT!!!! Sync update all by $PROG_NAME Done!!!" "marco.caeiro@gmail.com"
- else
- printf "Job failed at $(date)\n" | /usr/bin/mail -s "ERROR!!!! $PROG_NAME" "marco.caeiro@gmail.com"
- fi
- ;;
- *)
- printf "\nOption not available\n"
- exit
- ;;
- esac
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement