refcodes-batch.1.0.2.source-code.replicate-folder-mirror-to-folder-over-ssh-server-to-ssh-client.job Maven / Gradle / Ivy
#!/bin/bash
if [[ $HELP = y ]] ; then
echo "The switch \"-j $JOB\" rsyncs a remote site's folder structure " >&2
echo "via SSH locally. Remote files are copied into the local filder structure, any " >&2
echo "local files not exisiting remotely are removed from the local folder structure. " >&2
logSeparator
fi
. $SCRIPT_DIR/validate-folder-config.job
. $SCRIPT_DIR/validate-folder-mirror-config.job
. $SCRIPT_DIR/validate-ssh-minimal-config.job
if [[ $HELP = y ]] ; then
exit $EXIT_SUCCESS
fi
exitOnConfigOff "FOLDER"
exitOnConfigOff "FOLDER_MIRROR"
exitOnConfigOff "SSH_MINIMAL"
SSH_CLIENT_KEY_FILE=$SSH_CLIENT_KEY_DIR/$SSH_KEY_FILE_NAME
EXCLUDE=""
if [ ! -z "${FOLDER_MIRROR_DO_NOT_TOUCH}" ] ; then
EXCLUDE="--exclude ${FOLDER_MIRROR_DO_NOT_TOUCH}"
fi
INCLUDE=""
if [ ! -z "${FOLDER_INCLUDE_FILE}" ] ; then
exitOnMissingPropertyPath "${FOLDER_INCLUDE_FILE}"
INCLUDE="--include-from="${FOLDER_INCLUDE_FILE}""
fi
if [[ $VERBOSE = y ]] ; then
echo "Assuming the path \"$FOLDER_DIR\" to exist ..." >&2
echo "Assuming the path \"$SSH_CLIENT_KEY_FILE\" to exist ..." >&2
logSeparator
echo "Using exclude pattern \"$FOLDER_MIRROR_DO_NOT_TOUCH\" (when set) ..." >&2
echo "Using include file \"$FOLDER_INCLUDE_FILE\" (when set) ..." >&2
logSeparator
fi
exitOnMissingPropertyPath "FOLDER_DIR"
exitOnMissingVariablePath "SSH_CLIENT_KEY_FILE"
if [[ $VERBOSE = y ]] ; then
echo "Replicating folder mirror \"$FOLDER_MIRROR_DIR@$SSH_SERVER_HOSTNAME\" to folder \"$FOLDER_DIR\" ..." >&2
logSeparator
fi
rsync -avzP --delete ${EXCLUDE} ${INCLUDE} -e "ssh -i $SSH_CLIENT_KEY_FILE" $SSH_SERVER_USER_NAME@$SSH_SERVER_HOSTNAME:$FOLDER_MIRROR_DIR/ $FOLDER_DIR/
exitOnError "Unable replicate folder mirror \"$FOLDER_MIRROR_DIR@$SSH_SERVER_HOSTNAME\" to folder \"$FOLDER_DIR\" !!!"
logSeparator
# ----------------------------------------------
# Consider an exit code 23 still as scuccessful:
# ----------------------------------------------
# EXIT_CODE=$?
# if [[ $EXIT_CODE != 23 ]] && [[ $EXIT_CODE != 0 ]] ; then
# $(exit $EXIT_CODE)
# exitOnError "Unable replicate folder mirror \"$FOLDER_MIRROR_DIR@$SSH_SERVER_HOSTNAME\" to folder \"$FOLDER_DIR\" !!!"
# fi
showSuccess "Replicated folder mirror \"$SSH_SERVER_HOSTNAME@$FOLDER_MIRROR_DIR\" to folder \"$FOLDER_DIR\" !"