Advertisement
Guest User

Untitled

a guest
Mar 14th, 2016
105
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 13.45 KB | None | 0 0
  1. #!/bin/bash  
  2.  
  3. PROG_NAME=$(basename $0)
  4.  
  5. LOGFOLDER="$HOME/programming/scripts/sync/logs"
  6.  
  7.  
  8. function _show_time () {
  9.  
  10.     printf -v _new_time "%d days %d hours %02d minutes %02d seconds" $(($1 / 86400)) $((($1 / 3600) % 24)) $((($1 / 60) % 60)) $(($1 % 60))                                                                                                                                                                                    
  11. }
  12.  
  13.  
  14. function remote_status () {
  15.  
  16.     if ! ping -c 1 "$ip"  &> /dev/null
  17.     then
  18.       printf "\n$ip is offline at $(date)\n"
  19. #     printf "\n$ip is offline at $(date)\n" | /usr/bin/mail -s "ALERT!!!! $PROG_NAME" "marco.caeiro@gmail.com"
  20.       exit 1
  21.     else
  22.       printf "\n$ip is online at $(date)\n"
  23.     fi
  24.   }
  25.  
  26.  
  27.  
  28. function is_IP() {  
  29.  
  30.     if [ `echo $1 | grep -o '\.' | wc -l` -ne 3 ]; then
  31.       echo "Parameter '$1' does not look like an IP Address (does not contain 3 dots).";
  32.       exit 1;
  33.     elif [ `echo $1 | tr '.' ' ' | wc -w` -ne 4 ]; then
  34.       echo "Parameter '$1' does not look like an IP Address (does not contain 4 octets).";
  35.       exit 1;
  36.     else
  37.       for OCTET in `echo $1 | tr '.' ' '`; do
  38.         if ! [[ $OCTET =~ ^[0-9]+$ ]]; then
  39.           echo "Parameter '$1' does not look like in IP Address (octet '$OCTET' is not numeric).";
  40.           exit 1;
  41.         elif [[ $OCTET -lt 0 || $OCTET -gt 255 ]]; then
  42.           echo "Parameter '$1' does not look like in IP Address (octet '$OCTET' in not in range 0-255).";
  43.           exit 1;
  44.         fi
  45.       done
  46.     fi
  47.     return 0;
  48.  
  49.   }  
  50.  
  51.  
  52.  
  53. sync_direct_all () {                                                                                                                                                
  54.     clear
  55.     printf "\nWARNING: Files and directories will be deleted on $dest\n"
  56.     printf "Continue (Y/N)\n"
  57.     read -n1 -r -s choice
  58.  
  59.     case "$choice" in
  60.       y|Y)
  61.         {
  62.           rsync -acuvz --delete --stats --exclude=.sync --exclude=*.tar --exclude=*.gz --exclude=*.bz2 --exclude=*.xz --exclude=*.sha256sum --exclude=Software $source $dest
  63.           rsync -acuvz --stats --exclude=.sync $source $dest
  64.           printf "\n\nLast sync from $source to $dest at\n"
  65.           date
  66.         }  2>&1 | tee $LOGFOLDER/sync_direct_all_log.txt
  67.         ;;
  68.       *)
  69.         exit
  70.         ;;
  71.     esac
  72.  
  73.   }
  74.  
  75.  
  76. sync_update_all () {                                                                                                                                                
  77.     clear
  78.     printf "\nWARNING: Files and directories will NOT be deleted on $dest\n"
  79.     printf "Continue (Y/N)\n"
  80.     read -n1 -r -s choice
  81.  
  82.     case "$choice" in
  83.       y|Y)
  84.         {
  85.           rsync -acuvz --stats --exclude=.sync $source $dest
  86.           printf "\n\nLast sync from $source to $dest at\n"
  87.           date
  88.         }  2>&1 | tee $LOGFOLDER/sync_update_all_log.txt
  89.         ;;
  90.       *)
  91.         exit
  92.         ;;
  93.     esac
  94.  
  95.   }
  96.  
  97.                                                                                                                                                                      
  98. sync_inverse_all () {
  99.  
  100.     tmp=$dest
  101.     dest=$source
  102.     source=$tmp
  103.    
  104.     clear
  105.     printf "\nWARNING: Files and directories will be deleted on $dest\n"
  106.     printf "Continue (Y/N)\n"
  107.     read -n1 -r -s choice
  108.    
  109.     case "$choice" in
  110.       y|Y)
  111.         {
  112.           rsync -acuvz --delete --stats --exclude=.sync --exclude=*.tar --exclude=*.gz --exclude=*.bz2 --exclude=*.xz --exclude=*.sha256sum --exclude=Software $source $dest
  113.           rsync -acuvz --stats --exclude=.sync $source $dest
  114.           printf "\n\nLast sync from $source to $dest at\n"
  115.           date
  116.        
  117.         } 2>&1 | tee $LOGFOLDER/sync_inverse_all_log.txt
  118.         ;;
  119.       *)
  120.         exit
  121.         ;;
  122.     esac
  123.  
  124.   }
  125.  
  126.  
  127. sync_inverse_update_all () {
  128.  
  129.     tmp=$dest
  130.     dest=$source
  131.     source=$tmp
  132.  
  133.     clear
  134.     printf "\nWARNING: Files and directories will NOT be deleted on $dest\n"
  135.     printf "Continue (Y/N)\n"
  136.     read -n1 -r -s choice
  137.  
  138.     case "$choice" in
  139.       y|Y)
  140.         {
  141.           rsync -acuvz --stats --exclude=.sync $source $dest
  142.           printf "\n\nLast sync from $source to $dest at\n"
  143.           date
  144.  
  145.         } 2>&1 | tee $LOGFOLDER/sync_inverse_update_all_log.txt
  146.         ;;
  147.       *)
  148.         exit
  149.         ;;
  150.     esac
  151.  
  152.   }
  153.  
  154.  
  155. sync_direct_partial () {
  156.  
  157.     clear
  158.     printf "\nWARNING: Files and directories will be deleted on $dest\n"
  159.     printf "Continue (Y/N)\n"
  160.     read -n1 -r -s choice
  161.     case "$choice" in
  162.       y|Y)
  163.         {
  164.           rsync -acuvz --delete --stats --exclude=.sync --exclude=*.tar --exclude=*.gz --exclude=*.bz2 --exclude=*.xz --exclude=*.sha256sum --exclude=Software $source $dest
  165.           printf "\n\nLast sync from $source to $dest at\n"                                                                                            
  166.           date
  167.         } 2>&1 | tee $LOGFOLDER/sync_direct_partial_log.txt
  168.         ;;
  169.       *)
  170.         exit
  171.         ;;
  172.     esac
  173.  
  174.   }    
  175.  
  176.                                                                                                                                                                      
  177. sync_inverse_partial () {
  178.    
  179.     tmp=$dest
  180.     dest=$source
  181.     source=$tmp
  182.    
  183.     clear
  184.     printf "\nWARNING: Files and directories will be deleted on $dest\n"                                                                            
  185.     printf "Continue (Y/N)\n"
  186.     read -n1 -r -s choice
  187.     case "$choice" in
  188.       y|Y)
  189.         {
  190.           rsync -acuvz --delete --stats --exclude=.sync --exclude=*.tar --exclude=*.gz --exclude=*.bz2 --exclude=*.xz --exclude=*.sha256sum --exclude=Software $source $dest
  191.           printf "\n\nLast sync from $source to $dest at\n"
  192.           date
  193.         } 2>&1 | tee $LOGFOLDER/sync_inverse_partial_log.txt
  194.         ;;
  195.       *)
  196.         exit
  197.         ;;
  198.     esac
  199.  
  200.   }                                                                                                                                                        
  201.  
  202. option=$1
  203. source=$2
  204. dest=$3
  205. dest_remote=FALSE
  206. source_remote=FALSE
  207.  
  208. if [[ -z $source || -z $dest ]]
  209. then
  210.  printf "\nUsage: sync_rsync da source dest : for full direct sync\n"
  211.  printf "\nUsage: sync_rsync dp source dest : for partial direct sync\n"
  212.  printf "\nUsage: sync_rsync du source dest : for update direct sync\n"
  213.  printf "\nUsage: sync_rsync ia source dest : for full inverse sync\n"
  214.  printf "\nUsage: sync_rsync ip source dest : for partial inverse sync\n"
  215.  printf "\nUsage: sync_rsync iu source dest : for update inverse sync\n"
  216.  exit
  217. fi
  218.  
  219. if echo $dest | grep -q "@"
  220. then
  221.   user1=$( echo ${dest//@*} )
  222.   dest_remote=TRUE
  223. else
  224.   dest_remote=FALSE
  225.   if [[ ! -d $dest ]]
  226.   then
  227.     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"
  228.     exit 1
  229.   fi
  230. fi
  231.  
  232. if echo $source | grep -q "@"
  233. then
  234.   user2=$( echo ${source//@*} )
  235.   source_remote=TRUE
  236. else
  237.   source_remote=FALSE
  238.   if [[ ! -d $source ]]
  239.   then
  240.     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"
  241.     exit 1
  242.   fi
  243. fi
  244.  
  245. if [[ "$source_remote" == "TRUE" && "$dest_remote" == "TRUE" ]]
  246. then
  247.   printf "\nSource and destionation can't be both remote!!!!\n"
  248.   exit 1
  249. fi
  250.  
  251.  
  252. if [[ ! -z $user1  &&  -z ${user1//[[:alpha:]]} && "$dest_remote" == "TRUE"  ]]
  253. then
  254.   ip1=$( echo ${dest} | sed -e 's/\(^.*@\)\(.*\)\(:.*$\)/\2/' )
  255.   is_IP $ip1
  256.   rem_dir=${dest#*:}
  257.   ip=$ip1
  258.   remote_status
  259.   printf "\nUser is $user1\n"
  260.   printf "\nIp is $ip1\n"
  261.   if  ssh $user1@$ip1 "[ -d $rem_dir ]"
  262.   then
  263.     printf "$rem_dir exists \n"
  264. #    printf "$rem_dir exists \n" | /usr/bin/mail -s "ALERT!!!! $PROG_NAME" "marco.caeiro@gmail.com"
  265.   else
  266.     printf "\nRemote directory $rem_dir do not exists \n"
  267.     printf "\nRemote directory $rem_dir do not exists \n" | /usr/bin/mail -s "ERROR!!!! $PROG_NAME" "marco.caeiro@gmail.com"
  268.     exit 1
  269.   fi
  270. fi
  271.  
  272. if [[ ! -z $user2  &&  -z ${user2//[[:alpha:]]} && "$source_remote" == "TRUE" ]]
  273. then
  274.   ip2=$( echo ${source} | sed -e 's/\(^.*@\)\(.*\)\(:.*$\)/\2/' )
  275.   is_IP $ip2
  276.   rem_dir=${source#*:}
  277.   ip=$ip2
  278.   remote_status
  279.   printf "\nUser is $user2\n"
  280.   printf "\nIp is $ip2\n"
  281.   if  ssh $user2@$ip2 "[ -d $rem_dir ]"
  282.   then
  283.     printf "\n$rem_dir exists \n"
  284. #    printf "\n$rem_dir exists \n" | /usr/bin/mail -s "ALERT!!!! $PROG_NAME" "marco.caeiro@gmail.com"
  285.   else
  286.     printf "\nRemote directory $rem_dir do not exists \n"
  287.     printf "\nRemote directory $rem_dir do not exists \n" | /usr/bin/mail -s "ERROR!!!! $PROG_NAME" "marco.caeiro@gmail.com"
  288.     exit 1
  289.   fi
  290. fi
  291.  
  292.  
  293. case "$option" in
  294.  
  295.   da)
  296.     time1=$(date +"%s")
  297.    
  298.     sync_direct_all
  299.    
  300.     if [[ "$?" == "0" ]]
  301.     then
  302.       time2=$(date +"%s")
  303.       dif=$(($time2-$time1))
  304.       _show_time $dif
  305.       printf "\n\n$(date) ::::: All Done! $_new_time elapsed\n" > $LOGFOLDER/time_to_sync_direct_all.txt
  306.       printf "Last sync of $source to $dest was at $(date)\n" > $LOGFOLDER/source_to_dest.txt
  307.       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"
  308.     else
  309.       printf "Job failed at $(date)\n" | /usr/bin/mail -s "ERROR!!!! $PROG_NAME" "marco.caeiro@gmail.com"
  310.     fi                            
  311.    
  312.     ;;
  313.  
  314.   dp)
  315.     time1=$(date +"%s")
  316.    
  317.     sync_direct_partial
  318.    
  319.     if [[ "$?" == "0" ]]
  320.     then
  321.       time2=$(date +"%s")
  322.       dif=$(($time2-$time1))
  323.       _show_time $dif
  324.       printf "\n\n$(date) ::::: All Done! $_new_time elapsed\n" > $LOGFOLDER/time_to_sync_direct_partial.txt
  325.       printf "Last sync of $source to $dest was at $(date)\n" > $LOGFOLDER/source_to_dest.txt
  326.       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"
  327.     else
  328.       printf "Job failed at $(date)\n" | /usr/bin/mail -s "ERROR!!!! $PROG_NAME" "marco.caeiro@gmail.com"
  329.     fi                            
  330.    
  331.     ;;
  332.    
  333.   ia)
  334.     time1=$(date +"%s")
  335.    
  336.     sync_inverse_all
  337.    
  338.     if [[ "$?" == "0" ]]
  339.     then
  340.       time2=$(date +"%s")
  341.       dif=$(($time2-$time1))
  342.       _show_time $dif
  343.       printf "\n\n$(date) ::::: All Done! $_new_time elapsed\n" > $LOGFOLDER/time_to_sync_inverse_all.txt
  344.       printf "Last sync of $source to $dest was at $(date)\n" > $LOGFOLDER/source_to_dest.txt
  345.       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"
  346.     else
  347.       printf "Job failed at $(date)\n" | /usr/bin/mail -s "ERROR!!!! $PROG_NAME" "marco.caeiro@gmail.com"
  348.     fi                            
  349.  
  350.     ;;
  351.  
  352.   ip)
  353.     time1=$(date +"%s")
  354.    
  355.     sync_inverse_partial
  356.    
  357.     if [[ "$?" == "0" ]]
  358.     then
  359.       time2=$(date +"%s")
  360.       dif=$(($time2-$time1))
  361.       _show_time $dif
  362.       printf "\n\n$(date) ::::: All Done! $_new_time elapsed\n" > $LOGFOLDER/time_to_sync_inverse_partial.txt
  363.       printf "Last sync of $source to $dest was at $(date)\n" > $LOGFOLDER/source_to_dest.txt
  364.       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"
  365.     else
  366.       printf "Job failed at $(date)\n" | /usr/bin/mail -s "ERROR!!!! $PROG_NAME" "marco.caeiro@gmail.com"
  367.     fi                            
  368.  
  369.     ;;
  370.  
  371.   du)
  372.     time1=$(date +"%s")
  373.    
  374.     sync_update_all
  375.  
  376.     if [[ "$?" == "0" ]]
  377.     then
  378.       time2=$(date +"%s")
  379.       dif=$(($time2-$time1))
  380.       _show_time $dif
  381.       printf "\n\n$(date) ::::: All Done! $_new_time elapsed\n" > $LOGFOLDER/time_to_sync_update_all.txt
  382.       printf "Last sync of $source to $dest was at $(date)\n" > $LOGFOLDER/source_to_dest.txt
  383.       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"
  384.     else
  385.       printf "Job failed at $(date)\n" | /usr/bin/mail -s "ERROR!!!! $PROG_NAME" "marco.caeiro@gmail.com"
  386.     fi
  387.  
  388.     ;;
  389.  
  390.   iu)
  391.     time1=$(date +"%s")
  392.  
  393.     sync_inverse_update_all
  394.  
  395.     if [[ "$?" == "0" ]]
  396.     then
  397.       time2=$(date +"%s")
  398.       dif=$(($time2-$time1))
  399.       _show_time $dif
  400.       printf "\n\n$(date) ::::: All Done! $_new_time elapsed\n" > $LOGFOLDER/time_to_sync_inverse_update_all.txt
  401.       printf "Last sync of $source to $dest was at $(date)\n" > $LOGFOLDER/source_to_dest.txt
  402.       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"
  403.     else
  404.       printf "Job failed at $(date)\n" | /usr/bin/mail -s "ERROR!!!! $PROG_NAME" "marco.caeiro@gmail.com"
  405.     fi
  406.  
  407.     ;;
  408.  
  409.   *)
  410.     printf "\nOption not available\n"
  411.     exit
  412.     ;;
  413.    
  414. esac
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement