#!/bin/bash
#                      /usr/local/bin/syncup
# http://crystalfaeries.net/posix/bin/syncup
# celeste:crystalfaery 2015-12-27 12:06:51+00:00

# We are lazily coding only to pass a single argument, presumably "--dry-run"

# This bash script is used to invoke the synchronize family of sync scripts.
# Active sync scripts are symlinked in the folder /usr/local/bin/synchronize/
# lrwxrwxrwx 1 kahealani staff     10 Jan 25 11:33 sync000		-> ../sync000*
# lrwxrwxrwx 1 kahealani staff     16 Feb 17 09:47 syncbookmarks	-> ../syncbookmarks*
# lrwxrwxrwx 1 kahealani staff     16 Feb 19 08:53 syncdocuments	-> ../syncdocuments*
# lrwxrwxrwx 1 kahealani staff     12 Mar  6 23:29 syncemail		-> ../syncemail*
# lrwxrwxrwx 1 kahealani staff     11 Jan 25 11:34 synchome		-> ../synchome*
# lrwxrwxrwx 1 kahealani staff     12 Jan 25 11:32 synclocal		-> ../synclocal*
# lrwxrwxrwx 1 kahealani staff     15 Feb 18 15:40 synclocaldoc		-> ../synclocaldoc*
# lrwxrwxrwx 1 kahealani staff     10 Jan 25 11:34 synczzz		-> ../synczzz*

# Invoking user must have necessary access rights to user mounted (cifs and/or sshfs) partitions.
# (note that root may be unable to access a partition mounted by a user)
# Invoking user must have in place an active ssh-agent to manage key-based login to other hosts.
# (see bottom of script for further info about recoding this script to fix this problem).
# /usr/local/bin/syncup	is the bash script used to invoke synchronize family of sync scripts:
# /usr/bin/sudo /usr/bin/touch					/var/log/syncup.log		&& \
# /usr/bin/sudo /bin/chmod	640				/var/log/syncup.log		&& \
# whowasi=`/usr/bin/whoami`									&& \
# /usr/bin/sudo /bin/chown "$whowasi":				/var/log/syncup.log		&& \
# eval	`/usr/bin/ssh-agent`									&& \
#	 /usr/bin/ssh-add									&& \
# /usr/bin/nice /usr/bin/ionice -c 3								   \
# /bin/run-parts --report /usr/local/bin/synchronize/ >>	/var/log/syncup.log 2>&1	&& \

if [[ $# -ne 0 ]]
then
    case $1 in
--dry-run)
	# we are doing a dry-run not a real sync
# 	/usr/bin/nice /usr/bin/ionice -c 3 /bin/run-parts --report --arg=$1	/usr/local/bin/synchronize/ \
	/usr/bin/nice /bin/run-parts --report --arg=$1	/usr/local/bin/synchronize/ \
	>> /var/log/syncup.log 2>&1
	;;
*)
	# we are doing a real sync
#	/usr/bin/nice /usr/bin/ionice -c 3 /bin/run-parts --report		/usr/local/bin/synchronize/ \
	/usr/bin/nice /bin/run-parts --report		/usr/local/bin/synchronize/ \
	>> /var/log/syncup.log 2>&1
	;;
    esac
else
	# we are doing a real sync
#	/usr/bin/nice /usr/bin/ionice -c 3 /bin/run-parts --report		/usr/local/bin/synchronize/ \
	/usr/bin/nice /bin/run-parts --report		/usr/local/bin/synchronize/ \
	>> /var/log/syncup.log 2>&1
fi
let	result=$?	# did the (dry-run) sync work?
if  [[ $result -ne 0 ]]
then			# if not then report the failure
	/usr/bin/tail -n 1024 /var/log/syncup.log						|  \
	/usr/bin/mail -s "`/bin/hostname`: `/usr/bin/basename $0`: $result" `/usr/bin/whoami`	&& \
	/bin/echo "`/usr/local/bin/now` e-tailed errorlog ======="	>> /var/log/syncup.log	|| \
	let result=$?					# error code for failure to e-mail log
fi
exit	$result

# Tech Tip
# If you have a lot of scripts, shell functions or aliases that depend on SSH,
# you’re probably already using ssh-agent and ssh-add to store your identity
# so you don’t have to retype your passphrase every time.
# But what happens when you forget to run ssh-add,
# and you run one of your scripts that uses SSH?
# You get a passphrase prompt for every command that uses SSH transport,
# whether it’s rsync, cvs, scp or unison.
# To stop the prompts and give yourself a chance to enter the passphrase once,
# here’s how a script can check to make sure you have an identity loaded
# before doing an ssh.
# Just use the return value of ssh-add -L,
# which is true if you have an identity loaded,
# false if you don’t.
# One line, as in a script:
# ssh-add -L && rsync -a Images xenu.example.com:Images
# To control a longer block of commands, as in a shell function:
# sync_images()
# {
# ssh-add -L || return 1
# rsync -a Images xenu.example.com:Images
# # more commands here if you like
# }
# Run one of those examples without an identity loaded,
# and you get a nice
# “The agent has no identities.” error message
# instead of an annoying passphrase prompt.
# —DON MARTI


syntax highlighted by Code2HTML, v. 0.9.1