Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/bin/bash
- LOGFILE="$HOME/Documents/backup.log"
- DATE=`DATE +"%Y/%m/%d %H:%M:%S"`
- echo "$DATE : Starting rsync..." >> "$LOGFILE"
- # The following function will ping the file server. If the server responds to ping, it will continue
- # If it does not respond to ping, it will exit
- # Set the variable file_server to the appropriate destination
- file_server="file-03ny.HQ.Hview.com"; # Set for the correct local file server
- function checkWorkNetwork() {
- # sleep for a bit, wait for network to show up
- sleep 10
- # ping file server and parse results
- count=$(/sbin/ping -c 3 -o $file_server | grep "received" | awk -F',' '{ print $2 }' | awk '{ print $1 }')
- # if the file server does not reply, kill the script
- if [[ $count -eq "0" ]]; then
- echo "$DATE : ERROR: Network destination unreachable, exiting..." >> "$LOGFILE"
- exit;
- # if the file server does reply, continue
- else
- echo "$DATE : Network destination reachable, continuing..." >> "$LOGFILE"
- fi
- }
- # The following function will check to see if the user's network folder is already mounted
- # If it is already mounted, the script will continue without taking action
- # If it is not already mounted, it will mount it
- function doMounts() {
- # set variable to the current logged on user
- vol=$USER
- # if the mount point does not already exist, mount it
- if [ ! -d "/Volumes/RedirectFolders" ]; then
- echo "$DATE : Server is not mounted. Mounting..." >> "$LOGFILE"
- # There are multiple ways we can mount the share. This way here is commented out due to permissions issues
- # mkdir $mountpt
- # mount_smbfs "smb://$file_server/RedirectFolders/$vol" $mountpt
- `/usr/bin/osascript <<EOT
- tell application "Finder" to mount volume "smb://$file_server/RedirectFolders/$vol"
- EOT`
- # if the mount point already exists, continue
- else
- echo "$DATE : Server is already mounted, continuing..." >> "$LOGFILE"
- fi
- }
- # This function will check for existing rsync processes and kill them
- function kill_rsync() {
- # set a variable to the process ID for an existing rsync process
- pid=`ps -A | grep rsync | grep -v grep | awk '{print $1}'` # check for existing rsync
- # this loop will run until the function is no longer able to find an rsync process
- while [ ! -z "$pid" ]; do
- echo -n "."
- for pid in $pid
- do
- kill -9 $pid # kill existing rsync
- done
- sleep 3
- pid=`ps -A | grep rsync | grep -v grep | awk '{print $1}'`
- done
- # continue on with the script
- echo "$DATE : Finished checking existing rsync processes, continuing..." >> "$LOGFILE"
- }
- # The following function will execute an rsync script between the source and destination folders
- # set special variable for logging
- PROG=$0
- # set source folder
- SRC="$HOME/Documents/"
- # set destination folder
- DST="/Volumes/RedirectFolders/Documents/"
- # set variable rsync command
- CMD="eval /usr/bin/rsync -auvixhzS --compare-dest='$DST' --stats --progress --log-file='$HOME/Documents/backup.log' '$SRC' '$DST'"
- # RSYNC OPTIONS APPLIED:
- # -a, --archive archive mode; same as -rlptgoD (no -H)
- # -u, --update skip files that are newer on the receiver
- # -v, --verbose increase verbosity
- # -i, --itemize-changes output a change-summary for all upDATEs
- # -x, --one-file-system don't cross filesystem boundaries
- # -h, --human-readable output numbers in a human-readable format
- # -z, --compress compress file data during the transfer
- # -S, --sparse handle sparse files efficiently
- # --compare-dest=DIR also compare received files relative to DIR
- # --stats give some file-transfer stats
- # --progress show progress during transfer
- # --log-file=FILE log what we're doing to the specified FILE
- function rsync_backup() {
- # if the destination folder does not exist, create it
- if [ ! -d "$DST" ]; then
- mkdir "$DST"
- fi
- # if the source folder is not readable, kill the script
- if [ ! -r "$SRC" ]; then
- /usr/bin/logger -t $PROG "$DATE : ERROR: Source $SRC not readable - Cannot start the sync process"
- echo "$DATE : ERROR: Source $SRC not readable - Cannot start the sync process" >> "$LOGFILE"
- exit;
- fi
- # if the destination folder is not writeable, kill the script
- if [ ! -w "$DST" ]; then # check to see if destination folder is writeable
- /usr/bin/logger -t $PROG "$DATE : ERROR: Destination $DST not writeable - Cannot start the sync process"
- echo "$DATE : ERROR: Destination $DST not writeable - Cannot start the sync process" >> "$LOGFILE"
- exit;
- # execute rsync
- else
- /usr/bin/logger -t $PROG "Start rsync"
- echo "$DATE : Starting rsync..." >> "$LOGFILE"
- $CMD
- /usr/bin/logger -t $PROG "End rsync"
- echo "$DATE : Rsync completed!" >> "$LOGFILE"
- fi
- echo "$DATE : DONE!"
- }
- # execute the functions in order from top to bottom
- checkWorkNetwork
- doMounts
- kill_rsync
- rsync_backup
- exit 0
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement