#!/usr/bin/env bash # ################################################## # My Generic sync script. # version="1.1.0" # Sets version variable # scriptTemplateVersion="1.0.0" # Version of Script Template # # This script will give you the option of using rsync # or Unison. Rsync is for one-way syncing, Unison is for # two-way syncing. # # Depending on what flags are passed to the script and # what information is written in the config file, this script # will perform different behavior. # # USAGE: # # 1) IMPORTANT: Copy this script and rename it for your purpose before running. # 2) Run the script. This will create a blank config file for you and then exit. # 3) Enter your information within the config file # 4) Run the script again. # # TO DO: # * Add SSH functionality # # DISCLAIMER: # I am a novice programmer and I bear no responsibility whatsoever # if this (or any other) script that I write wipes your computer, # destroys your data, crashes your car, or otherwise causes mayhem # and destruction. USE AT YOUR OWN RISK. # # # HISTORY: # * 2015-01-03 - v1.1.0 - Added support for using roots in Unison .prf # * 2015-01-02 - v1.0.0 - First Creation # # ################################################## # Source Scripting Utilities # ----------------------------------- # If these can't be found, update the path to the file # ----------------------------------- SCRIPTDIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )" if [ -f "${SCRIPTDIR}/../lib/utils.sh" ]; then source "${SCRIPTDIR}/../lib/utils.sh" else echo "Please find the file util.sh and add a reference to it in this script. Exiting." exit 1 fi # trapCleanup Function # ----------------------------------- # Any actions that should be taken if the script is prematurely # exited. Always call this function at the top of your script. # ----------------------------------- function trapCleanup() { echo "" if is_dir "${tmpDir}"; then rm -r "${tmpDir}" fi die "Exit trapped." # Edit this if you like. } # Set Flags # ----------------------------------- # Flags which can be overridden by user input. # Default values are below # ----------------------------------- quiet=0 printLog=0 verbose=0 force=0 strict=0 debug=0 # Set Local Variables # ----------------------------------- # A set of variables used by many scripts # ----------------------------------- # Set Script name and location variables scriptName=`basename ${0}` # Full name scriptBasename="$(basename ${scriptName} .sh)" # Strips '.sh' from name scriptPath="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" # Set time stamp now=$(date +"%m-%d-%Y %r") # Set hostname thisHost=$(hostname) # Create temp directory with three random numbers and the process ID # in the name. This directory is removed automatically at exit. tmpDir="/tmp/${scriptName}.$RANDOM.$RANDOM.$RANDOM.$$" (umask 077 && mkdir "${tmpDir}") || { die "Could not create temporary directory! Exiting." } # Configuration file # ----------------------------------- # This script calls for a configuration file. # This is its location. Default is the location # where it will be automatically created.` # ----------------------------------- CONFIG="../etc/${scriptName}.cfg" # Create new copy of the script if template is being executed function newCopy() { if [ "${scriptName}" = "SyncTemplate.sh" ]; then input "name your new script:" read newname cp "${scriptPath}"/"${scriptName}" "${scriptPath}"/"${newname}" && verbose "cp ${scriptPath}/${scriptName} ${scriptPath}/${newname}" success "${newname} created." exit 0 fi } function configFile() { # Here we source the Config file or create a new one if none exists. if is_file "${CONFIG}"; then source "${CONFIG}" verbose "source ${CONFIG}" else seek_confirmation "Config file does not exist. Would you like to create one?" if is_not_confirmed; then die "No config file. Exiting" else touch "${CONFIG}" && verbose "touch ${CONFIG}" cat >"${CONFIG}" <&2; safeExit ;; --version) echo "$(basename $0) $version"; safeExit ;; -v|--verbose) verbose=1 ;; -l|--log) printLog=1 ;; -d|--debug) debug=1 ;; -q|--quiet) quiet=1 ;; -s|--strict) strict=1;; -f|--force) force=1 ;; -n|--dryrun) DRYRUN=n ;; -z|--compress) COMPRESS=z ;; --endopts) shift; break ;; *) warning "invalid option: $1.\n"; usage >&2; safeExit ;; # *) die "invalid option: '$1'." ;; esac shift done ############## End Options and Usage ################### # ############# ############# ############# # ## TIME TO RUN THE SCRIPT ## # ## ## # ## You shouldn't need to edit anything ## # ## beneath this line ## # ## ## # ############# ############# ############# # Trap bad exits with your cleanup function trap trapCleanup EXIT INT TERM # Exit on error. Append ||true if you expect an error. set -o errexit # Exit on empty variable if [ "${strict}" == "1" ]; then set -o nounset fi # Bash will remember & return the highest exitcode in a chain of pipes. # This way you can catch the error in case mysqldump fails in `mysqldump |gzip` set -o pipefail # Set timer for script to start STARTTIME=$(date +"%s") header "${scriptName} Begun" newCopy configFile hostCheck MethodCheck moutDrives testSources runRsync runUnison unmountDrives # Time the script by logging the end time ENDTIME=$(date +"%s") TOTALTIME=$(($ENDTIME-$STARTTIME)) notifyPushover header "${scriptName} completed in $(convertsecs $TOTALTIME)" safeExit # Exit cleanly