Tweaks to syncTemplate.sh

This commit is contained in:
Nathaniel Landau
2015-01-02 22:27:03 -05:00
parent cebcd99cff
commit 41e524870d
3 changed files with 189 additions and 143 deletions

View File

@@ -8,6 +8,17 @@
# #
# ################################################## # ##################################################
# scriptPath
# ------------------------------------------------------
# This function will populate the variable SOURCEPATH with the
# full path of the script being run.
# Note: The function must be run within the script before using
# the variable
# ------------------------------------------------------
function scriptPath() {
SCRIPTPATH=$( cd "$( dirname "$0" )" && pwd )
}
# readFile # readFile
# ------------------------------------------------------ # ------------------------------------------------------
# Function to read a line from a file. # Function to read a line from a file.

View File

@@ -14,7 +14,7 @@ while [ -h "$SOURCE" ]; do # resolve $SOURCE until the file is no longer a symli
SOURCE="$(readlink "$SOURCE")" SOURCE="$(readlink "$SOURCE")"
[[ $SOURCE != /* ]] && SOURCE="$DIR/$SOURCE" # if $SOURCE was a relative symlink, we need to resolve it relative to the path where the symlink file was located [[ $SOURCE != /* ]] && SOURCE="$DIR/$SOURCE" # if $SOURCE was a relative symlink, we need to resolve it relative to the path where the symlink file was located
done done
SCRIPTPATH="$( cd -P "$( dirname "$SOURCE" )" && pwd )" SOURCEPATH="$( cd -P "$( dirname "$SOURCE" )" && pwd )"
# Write the list of utility files to be sourced # Write the list of utility files to be sourced
FILES=" FILES="
@@ -26,8 +26,8 @@ FILES="
# Source the Utility Files # Source the Utility Files
for file in $FILES for file in $FILES
do do
if [ -f "$SCRIPTPATH/$file" ]; then if [ -f "$SOURCEPATH/$file" ]; then
source "$SCRIPTPATH/$file" source "$SOURCEPATH/$file"
else else
e_error "$file does not exist. Exiting" e_error "$file does not exist. Exiting"
Exit 1 Exit 1

View File

@@ -46,7 +46,7 @@ CONFIG="../etc/$SCRIPTNAME.cfg"
# HELP # HELP
# When -h is passed to the script, this will display inline help # When -h is passed to the script, this will display inline help
function HELP () { function HELP () {
echo -e "\nHelp for $SCRIPTNAME" e_bold "\nHelp for $SCRIPTNAME"
echo -e "This script will give you the option of using rsync" echo -e "This script will give you the option of using rsync"
echo -e "or Unison. Rsync is for one-way syncing, Unison is for" echo -e "or Unison. Rsync is for one-way syncing, Unison is for"
echo -e "two-way syncing.\n" echo -e "two-way syncing.\n"
@@ -54,8 +54,9 @@ function HELP () {
echo -e "what information is written in the config file, this script" echo -e "what information is written in the config file, this script"
echo -e "will perform different behavior.\n" echo -e "will perform different behavior.\n"
echo -e "USAGE:" echo -e "USAGE:"
echo -e " 1) IMPORTANT: Copy this script and rename it for your purpose before running." echo -e " 1) Copy this script and rename it for your purpose before running."
echo -e " 2) Run the script. This will create a blank config file for you and then exit." echo -e " The script will do this for you when run."
echo -e " 2) Run the new script. This will create a blank config file for you and then exit."
echo -e " 3) Enter your information within the config file" echo -e " 3) Enter your information within the config file"
echo -e " 4) Run the script again.\n" echo -e " 4) Run the script again.\n"
echo -e "This script requires a config file located at: \"$CONFIG\"" echo -e "This script requires a config file located at: \"$CONFIG\""
@@ -68,16 +69,50 @@ function HELP () {
exit 0 exit 0
} }
# Here we source the Config file or create a new one if none exists. # READ OPTIONS
if is_file "$CONFIG"; then # Reads the options passed to the script
source "$CONFIG" # from the command line
else # ------------------------
seek_confirmation "Config file does not exist. Would you like to create one?" while getopts "hnz" opt; do
if is_not_confirmed; then case $opt in
die "No config file. Exiting" h) # show help
HELP
;;
n) # Show progress in terminal
DRYRUN="n"
;;
z) # Compress Data
COMPRESS="z"
;;
\?)
HELP
;;
esac
done
# Create new copy of the script if template is being executed
function newCopy() {
scriptPath
if [ "$SCRIPTNAME" = "SyncTemplate.sh" ]; then
e_bold "name your new script:"
read newname
cp "$SCRIPTPATH"/"$SCRIPTNAME" "$SCRIPTPATH"/"$newname"
e_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"
else else
touch "$CONFIG" seek_confirmation "Config file does not exist. Would you like to create one?"
cat >"$CONFIG" <<EOL if is_not_confirmed; then
die "No config file. Exiting"
else
touch "$CONFIG"
cat >"$CONFIG" <<EOL
# ################################################## # ##################################################
# CONFIG FILE FOR $SCRIPTNAME # CONFIG FILE FOR $SCRIPTNAME
# CREATED ON $NOW # CREATED ON $NOW
@@ -93,12 +128,6 @@ cat >"$CONFIG" <<EOL
# Set the METHOD variable to either 'unison' or 'rsync' # Set the METHOD variable to either 'unison' or 'rsync'
METHOD="" METHOD=""
# Directories To Sync
# ---------------------------
# These are the COMPLETE paths two directories that will be synced.
# Be sure to include trailing slashes on directories.
SOURCEDIRECTORY=""
TARGETDIRECTORY=""
# --------------------------- # ---------------------------
# Network Volume Mounting # Network Volume Mounting
@@ -113,11 +142,18 @@ MOUNTPOINT=""
# REMOTEVOLUME is the directory that the drive should be mounted # REMOTEVOLUME is the directory that the drive should be mounted
# into on the local computer. Typically this is in the /Volumes/ dir. # into on the local computer. Typically this is in the /Volumes/ dir.
# and should be named the same as the mount name in the MOUNTPOINT. # and should be named the same as the mountname in the MOUNTPOINT.
# Use the complete path, not a relative path # Use a complete path, not a relative path without a trailing slash.
REMOTEVOLUME="" REMOTEVOLUME=""
# Directories To Sync
# ---------------------------
# These are the COMPLETE paths two directories that will be synced.
# Be sure to include trailing slashes on directories.
SOURCEDIRECTORY=""
TARGETDIRECTORY=""
# --------------------------- # ---------------------------
# UNISON PREFERENCES # UNISON PREFERENCES
# --------------------------- # ---------------------------
@@ -142,6 +178,8 @@ UNISONPROFILE=""
# Anything listed within this file will be ignored during sync. # Anything listed within this file will be ignored during sync.
EXCLUDE="" EXCLUDE=""
# ---------------------------
# ADDITIONAL OPTIONS # ADDITIONAL OPTIONS
# --------------------------- # ---------------------------
@@ -159,149 +197,146 @@ PUSHOVERNOTIFY="false"
# Leave blank if not needed. # Leave blank if not needed.
CANONICALHOST="" CANONICALHOST=""
EOL EOL
e_success "Config file created. Edit the values before running this script again. Exiting." e_success "Config file created. Edit the values before running this script again."
e_arrow "The file is located at: $CONFIG"
echo "Exiting."
exit 0 exit 0
fi fi
fi fi
}
# READ OPTIONS
# Reads the options passed to the script
# from the command line
# ------------------------
while getopts "hnz" opt; do
case $opt in
h) # show help
HELP
;;
n) # Show progress in terminal
DRYRUN="n"
;;
z) # Compress Data
COMPRESS="z"
;;
\?)
HELP
;;
esac
done
# HostCheck # HostCheck
# Confirm we can run this script. If a canonical host is set in # Confirm we can run this script. If a canonical host is set in
# the config file we check it here. # the config file we check it here.
if [ "$THISHOST" = "$CANONICALHOST" ]; then function hostCheck() {
echo "$NOW - Script was not run since we were on the wrong host" >> "$LOGFILE" if [ "$THISHOST" = "$CANONICALHOST" ]; then
die "We are currently on $THISHOST and can not proceed. Be sure to run this script on the non-canonical host." echo "$NOW - Script was not run since we were on the wrong host" >> "$LOGFILE"
fi die "We are currently on $THISHOST and can not proceed. Be sure to run this script on the non-canonical host."
fi
}
# MethodCheck # MethodCheck
# Confirm we have either Unison or Rsync specified # Confirm we have either Unison or Rsync specified
# in the config file. Exit if not # in the config file. Exit if not
if [ "$METHOD" != "rsync" ] && [ "$METHOD" != "unison" ]; then function MethodCheck() {
echo "$NOW - Script aborted without a method specified in the config file." >> "$LOGFILE" if [ "$METHOD" != "rsync" ] && [ "$METHOD" != "unison" ]; then
die "We can not continue. Please specify a sync method in the config file." echo "$NOW - Script aborted without a method specified in the config file." >> "$LOGFILE"
fi die "We can not continue. Please specify a sync method in the config file."
# Time the script by logging the start time
STARTTIME=$(date +"%s")
# Log Script Start to $LOGFILE
echo -e "-----------------------------------------------------" >> "$LOGFILE"
echo -e "$NOW - $SCRIPTNAME Begun" >> "$LOGFILE"
echo -e "-----------------------------------------------------\n" >> "$LOGFILE"
if [ "$NEEDMOUNT" = "true" ] || [ "$NEEDMOUNT" = "TRUE" ]; then
# Mount AFP volume
if is_not_dir "$REMOTEVOLUME"; then
e_arrow "Mounting drive"
mkdir "$REMOTEVOLUME"
mount_afp "$MOUNTPOINT" "$REMOTEVOLUME"
sleep 10
echo "$NOW - $REMOTEVOLUME Mounted" >> "$LOGFILE"
e_success "$REMOTEVOLUME Mounted"
else
e_success "$REMOTEVOLUME already mounted"
fi fi
fi }
function mainScript() {
# Time the script by logging the start time
STARTTIME=$(date +"%s")
# Log Script Start to $LOGFILE
echo -e "-----------------------------------------------------" >> "$LOGFILE"
echo -e "$NOW - $SCRIPTNAME Begun" >> "$LOGFILE"
echo -e "-----------------------------------------------------\n" >> "$LOGFILE"
# Test for source directories.
# If the don't exist we can't continue
if is_dir "$TARGETDIRECTORY"; then
e_success "$TARGETDIRECTORY exists"
else
if [ "$NEEDMOUNT" = "true" ] || [ "$NEEDMOUNT" = "TRUE" ]; then if [ "$NEEDMOUNT" = "true" ] || [ "$NEEDMOUNT" = "TRUE" ]; then
unmountDrive "$REMOTEVOLUME" # Mount AFP volume
if is_dir "$REMOTEVOLUME"; then if is_not_dir "$REMOTEVOLUME"; then
rm -r "$REMOTEVOLUME" e_arrow "Mounting drive"
fi mkdir "$REMOTEVOLUME"
fi mount_afp "$MOUNTPOINT" "$REMOTEVOLUME"
echo -e "$NOW - Script aborted since $TARGETDIRECTORY was not found. Exited.\n" >> "$LOGFILE" sleep 10
die "$TARGETDIRECTORY does not exist. Exiting." echo "$NOW - $REMOTEVOLUME Mounted" >> "$LOGFILE"
fi e_success "$REMOTEVOLUME Mounted"
# Test for local directory
if is_dir "$SOURCEDIRECTORY"; then
e_success "$SOURCEDIRECTORY exists"
else
if [ "$NEEDMOUNT" = "true" ] || [ "$NEEDMOUNT" = "TRUE" ]; then
unmountDrive "$REMOTEVOLUME"
if is_dir "$REMOTEVOLUME"; then
rm -r "$REMOTEVOLUME"
fi
fi
echo -e "$NOW - Script aborted since $SOURCEDIRECTORY was not found. Exited.\n" >> "$LOGFILE"
die "$SOURCEDIRECTORY does not exist. Exiting."
fi
# Time to sync
if [ "$METHOD" = "rsync" ]; then
/usr/bin/rsync -vahh"$DRYRUN""$COMPRESS" --progress --force --delete --exclude-from="$EXCLUDE" "$SOURCEDIRECTORY" "$TARGETDIRECTORY" --log-file="$LOGFILE"
fi
if [ "$METHOD" = "unison" ]; then
# Check if Unison is installed. It is not a standard package
if type_not_exists 'unison'; then
seek_confirmation "Unison not installed, install it?"
if is_confirmed; then
hasHomebrew
brewMaintenance
brew install unison
else else
if [ "$NEEDMOUNT" = "true" ] || [ "$NEEDMOUNT" = "TRUE" ]; then e_success "$REMOTEVOLUME already mounted"
unmountDrive "$REMOTEVOLUME"
if is_dir "$REMOTEVOLUME"; then
rm -r "$REMOTEVOLUME"
fi
fi
die "Can not continue without Unison."
fi fi
fi fi
if [ "$USEPROFILE" != "true"]; then # Test for source directories.
# Run Unison without a profile # If they don't exist we can't continue
unison "$SOURCEDIRECTORY" "$TARGETDIRECTORY"
# test for target
if is_dir "$TARGETDIRECTORY"; then
e_success "$TARGETDIRECTORY exists"
else else
# Run unison with a profile if [ "$NEEDMOUNT" = "true" ] || [ "$NEEDMOUNT" = "TRUE" ]; then
unison "$UNISONPROFILE" unmountDrive "$REMOTEVOLUME"
if is_dir "$REMOTEVOLUME"; then
rm -r "$REMOTEVOLUME"
fi
fi
echo -e "$NOW - Script aborted since target dir: $TARGETDIRECTORY was not found. Exited.\n" >> "$LOGFILE"
die "target directory: $TARGETDIRECTORY does not exist. Exiting."
fi fi
fi
# Unmount the drive (if mounted) # Test for source directory
if [ "$NEEDMOUNT" = "true" ] || [ "$NEEDMOUNT" = "TRUE" ]; then if is_dir "$SOURCEDIRECTORY"; then
unmountDrive "$REMOTEVOLUME" e_success "$SOURCEDIRECTORY exists"
fi else
if [ "$NEEDMOUNT" = "true" ] || [ "$NEEDMOUNT" = "TRUE" ]; then
unmountDrive "$REMOTEVOLUME"
if is_dir "$REMOTEVOLUME"; then
rm -r "$REMOTEVOLUME"
fi
fi
echo -e "$NOW - Script aborted since source dir: $SOURCEDIRECTORY was not found. Exited.\n" >> "$LOGFILE"
die "source directory: $SOURCEDIRECTORY does not exist. Exiting."
fi
# Time the script by logging the end time # Time to sync
ENDTIME=$(date +"%s") if [ "$METHOD" = "rsync" ]; then
TOTALTIME=$(($ENDTIME-$STARTTIME-20)) /usr/bin/rsync -vahh"$DRYRUN""$COMPRESS" --progress --force --delete --exclude-from="$EXCLUDE" "$SOURCEDIRECTORY" "$TARGETDIRECTORY" --log-file="$LOGFILE"
fi
if [ "$METHOD" = "unison" ]; then
# notify with pushover if requested # Check if Unison is installed. It is not a standard package
if [ "$PUSHOVERNOTIFY" = "true" ]; then if type_not_exists 'unison'; then
pushover "$SCRIPTNAME Completed" "$SCRIPTNAME was run in $(convertsecs $TOTALTIME)" seek_confirmation "Unison not installed, install it?"
fi if is_confirmed; then
hasHomebrew
brewMaintenance
brew install unison
else
if [ "$NEEDMOUNT" = "true" ] || [ "$NEEDMOUNT" = "TRUE" ]; then
unmountDrive "$REMOTEVOLUME"
if is_dir "$REMOTEVOLUME"; then
rm -r "$REMOTEVOLUME"
fi
fi
die "Can not continue without Unison."
fi
fi
echo -e "\n-----------------------------------------------------" >> "$LOGFILE" if [ "$USEPROFILE" != "true"]; then
echo "$NOW - $SCRIPTNAME completed in $(convertsecs $TOTALTIME)" >> "$LOGFILE" # Run Unison without a profile
echo -e "-----------------------------------------------------\n" >> "$LOGFILE" unison "$SOURCEDIRECTORY" "$TARGETDIRECTORY"
else
# Run unison with a profile
unison "$UNISONPROFILE"
fi
fi
e_success "$NOW - $SCRIPTNAME completed in $(convertsecs $TOTALTIME)" # Unmount the drive (if mounted)
if [ "$NEEDMOUNT" = "true" ] || [ "$NEEDMOUNT" = "TRUE" ]; then
unmountDrive "$REMOTEVOLUME"
fi
# Time the script by logging the end time
ENDTIME=$(date +"%s")
TOTALTIME=$(($ENDTIME-$STARTTIME-20))
# notify with pushover if requested
if [ "$PUSHOVERNOTIFY" = "true" ]; then
pushover "$SCRIPTNAME Completed" "$SCRIPTNAME was run in $(convertsecs $TOTALTIME)"
fi
echo -e "\n-----------------------------------------------------" >> "$LOGFILE"
echo "$NOW - $SCRIPTNAME completed in $(convertsecs $TOTALTIME)" >> "$LOGFILE"
echo -e "-----------------------------------------------------\n" >> "$LOGFILE"
e_success "$NOW - $SCRIPTNAME completed in $(convertsecs $TOTALTIME)"
}
newCopy
configFile
hostCheck
MethodCheck
mainScript