Advertisement
Guest User

Backup_Rsync

a guest
Sep 26th, 2014
298
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 5.17 KB | None | 0 0
  1. #!/bin/bash
  2.  
  3. LOGFILE="$HOME/Documents/backup.log"
  4. DATE=`DATE +"%Y/%m/%d %H:%M:%S"`
  5. echo "$DATE : Starting rsync..." >> "$LOGFILE"
  6.  
  7. # The following function will ping the file server. If the server responds to ping, it will continue
  8. # If it does not respond to ping, it will exit
  9.  
  10. # Set the variable file_server to the appropriate destination
  11. file_server="file-03ny.HQ.Hview.com"; # Set for the correct local file server
  12.  
  13. function checkWorkNetwork() {
  14.   # sleep for a bit, wait for network to show up
  15.   sleep 10
  16.  
  17.   # ping file server and parse results
  18.   count=$(/sbin/ping -c 3 -o $file_server | grep "received" | awk -F',' '{ print $2 }' | awk '{ print $1 }')
  19.  
  20.   # if the file server does not reply, kill the script
  21.   if [[ $count -eq "0" ]]; then
  22.     echo "$DATE : ERROR: Network destination unreachable, exiting..." >> "$LOGFILE"
  23.       exit;
  24.      
  25.   # if the file server does reply, continue    
  26.   else
  27.     echo "$DATE : Network destination reachable, continuing..." >> "$LOGFILE"
  28.   fi
  29. }
  30.  
  31. # The following function will check to see if the user's network folder is already mounted
  32. # If it is already mounted, the script will continue without taking action
  33. # If it is not already mounted, it will mount it
  34.  
  35. function doMounts() {
  36.   # set variable to the current logged on user
  37.   vol=$USER
  38.  
  39.   # if the mount point does not already exist, mount it
  40.   if [ ! -d "/Volumes/RedirectFolders" ]; then
  41.     echo "$DATE : Server is not mounted. Mounting..." >> "$LOGFILE"
  42.             # There are multiple ways we can mount the share. This way here is commented out due to permissions issues
  43.             # mkdir $mountpt
  44.             # mount_smbfs "smb://$file_server/RedirectFolders/$vol" $mountpt
  45.         `/usr/bin/osascript <<EOT
  46.         tell application "Finder" to mount volume "smb://$file_server/RedirectFolders/$vol"
  47.             EOT`
  48.            
  49.   # if the mount point already exists, continue    
  50.     else
  51.         echo "$DATE : Server is already mounted, continuing..." >> "$LOGFILE"
  52.   fi
  53. }
  54.  
  55. # This function will check for existing rsync processes and kill them
  56.  
  57. function kill_rsync() {
  58.    # set a variable to the process ID for an existing rsync process
  59.         pid=`ps -A | grep rsync | grep -v grep | awk '{print $1}'` # check for existing rsync
  60.        
  61.         # this loop will run until the function is no longer able to find an rsync process
  62.         while [ ! -z "$pid" ]; do
  63.                 echo -n "."
  64.                 for pid in $pid
  65.                 do
  66.                         kill -9 $pid # kill existing rsync
  67.                 done
  68.                 sleep 3
  69.                 pid=`ps -A | grep rsync | grep -v grep | awk '{print $1}'`
  70.         done
  71.        
  72.         # continue on with the script
  73.         echo "$DATE : Finished checking existing rsync processes, continuing..." >> "$LOGFILE"
  74. }
  75.  
  76. # The following function will execute an rsync script between the source and destination folders
  77.  
  78. # set special variable for logging
  79. PROG=$0
  80.  
  81. # set source folder
  82. SRC="$HOME/Documents/"
  83.  
  84. # set destination folder
  85. DST="/Volumes/RedirectFolders/Documents/"
  86.  
  87. # set variable rsync command
  88. CMD="eval /usr/bin/rsync -auvixhzS --compare-dest='$DST' --stats --progress --log-file='$HOME/Documents/backup.log' '$SRC' '$DST'"
  89.     # RSYNC OPTIONS APPLIED:
  90.         # -a, --archive               archive mode; same as -rlptgoD (no -H)
  91.         # -u, --update                skip files that are newer on the receiver
  92.         # -v, --verbose               increase verbosity
  93.         # -i, --itemize-changes       output a change-summary for all upDATEs
  94.         # -x, --one-file-system       don't cross filesystem boundaries
  95.         # -h, --human-readable        output numbers in a human-readable format
  96.         # -z, --compress              compress file data during the transfer
  97.         # -S, --sparse                handle sparse files efficiently
  98.         # --compare-dest=DIR      also compare received files relative to DIR
  99.         # --stats                 give some file-transfer stats
  100.         # --progress              show progress during transfer
  101.         # --log-file=FILE         log what we're doing to the specified FILE
  102.  
  103. function rsync_backup() {
  104.     # if the destination folder does not exist, create it
  105.     if [ ! -d "$DST" ]; then
  106.         mkdir "$DST"
  107.         fi
  108.        
  109.     # if the source folder is not readable, kill the script
  110.     if [ ! -r "$SRC" ]; then
  111.         /usr/bin/logger -t $PROG "$DATE : ERROR: Source $SRC not readable - Cannot start the sync process"
  112.             echo "$DATE : ERROR: Source $SRC not readable - Cannot start the sync process" >> "$LOGFILE"
  113.             exit;
  114.         fi
  115.        
  116.     # if the destination folder is not writeable, kill the script
  117.     if [ ! -w "$DST" ]; then # check to see if destination folder is writeable
  118.         /usr/bin/logger -t $PROG "$DATE : ERROR: Destination $DST not writeable - Cannot start the sync process"
  119.             echo "$DATE : ERROR: Destination $DST not writeable - Cannot start the sync process" >> "$LOGFILE"
  120.                 exit;
  121.    
  122.     # execute rsync
  123.     else
  124.         /usr/bin/logger -t $PROG "Start rsync"
  125.             echo "$DATE : Starting rsync..." >> "$LOGFILE"
  126.                 $CMD
  127.         /usr/bin/logger -t $PROG "End rsync"
  128.             echo "$DATE : Rsync completed!" >> "$LOGFILE"
  129.     fi
  130.     echo "$DATE : DONE!"
  131. }
  132.  
  133. # execute the functions in order from top to bottom
  134. checkWorkNetwork
  135. doMounts
  136. kill_rsync
  137. rsync_backup
  138.  
  139. exit 0
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement