massive cleaning with shellcheck: from 5 errors and 200 warnings to zero

This commit is contained in:
zertrin 2016-03-25 19:47:29 +01:00
parent 8c0f58d8f0
commit 3797960809

View file

@ -44,7 +44,7 @@ CONFIG="duplicity-backup.conf"
usage(){ usage(){
echo "USAGE: echo "USAGE:
`basename $0` [options] $(basename "$0") [options]
Options: Options:
-c, --config CONFIG_FILE specify the config file to use -c, --config CONFIG_FILE specify the config file to use
@ -73,8 +73,8 @@ echo "USAGE:
CURRENT SCRIPT VARIABLES: CURRENT SCRIPT VARIABLES:
======================== ========================
DEST (backup destination) = ${DEST} DEST (backup destination) = ${DEST}
INCLIST (directories included) = ${INCLIST[@]:0} INCLIST (directories included) = ${INCLIST[*]:0}
EXCLIST (directories excluded) = ${EXCLIST[@]:0} EXCLIST (directories excluded) = ${EXCLIST[*]:0}
ROOT (root directory of backup) = ${ROOT} ROOT (root directory of backup) = ${ROOT}
LOGFILE (log file path) = ${LOGFILE} LOGFILE (log file path) = ${LOGFILE}
" "
@ -88,45 +88,45 @@ while getopts ":c:t:bfvlsnd-:" opt; do
# parse long options (a bit tricky because builtin getopts does not # parse long options (a bit tricky because builtin getopts does not
# manage long options and I don't want to impose GNU getopt dependancy) # manage long options and I don't want to impose GNU getopt dependancy)
-) -)
case "$OPTARG" in case "${OPTARG}" in
# --restore [restore dest] # --restore [restore dest]
restore) restore)
COMMAND=$OPTARG COMMAND=${OPTARG}
# We try to find the optional value [restore dest] # We try to find the optional value [restore dest]
if [ ! -z "${!OPTIND:0:1}" -a ! "${!OPTIND:0:1}" = "-" ]; then if [ ! -z "${!OPTIND:0:1}" -a ! "${!OPTIND:0:1}" = "-" ]; then
RESTORE_DEST=${!OPTIND} RESTORE_DEST=${!OPTIND}
OPTIND=$(( $OPTIND + 1 )) # we found it, move forward in arg parsing OPTIND=$(( OPTIND + 1 )) # we found it, move forward in arg parsing
fi fi
;; ;;
# --restore-file [file to restore] [restore dest] # --restore-file [file to restore] [restore dest]
# --restore-dir [path to restore] [restore dest] # --restore-dir [path to restore] [restore dest]
restore-file|restore-dir) restore-file|restore-dir)
COMMAND=$OPTARG COMMAND=${OPTARG}
# We try to find the first optional value [file to restore] # We try to find the first optional value [file to restore]
if [ ! -z "${!OPTIND:0:1}" -a ! "${!OPTIND:0:1}" = "-" ]; then if [ ! -z "${!OPTIND:0:1}" -a ! "${!OPTIND:0:1}" = "-" ]; then
FILE_TO_RESTORE=${!OPTIND} FILE_TO_RESTORE=${!OPTIND}
OPTIND=$(( $OPTIND + 1 )) # we found it, move forward in arg parsing OPTIND=$(( OPTIND + 1 )) # we found it, move forward in arg parsing
else else
continue # no value for the restore-file option, skip the rest continue # no value for the restore-file option, skip the rest
fi fi
# We try to find the second optional value [restore dest] # We try to find the second optional value [restore dest]
if [ ! -z "${!OPTIND:0:1}" -a ! "${!OPTIND:0:1}" = "-" ]; then if [ ! -z "${!OPTIND:0:1}" -a ! "${!OPTIND:0:1}" = "-" ]; then
RESTORE_DEST=${!OPTIND} RESTORE_DEST=${!OPTIND}
OPTIND=$(( $OPTIND + 1 )) # we found it, move forward in arg parsing OPTIND=$(( OPTIND + 1 )) # we found it, move forward in arg parsing
fi fi
;; ;;
config) # set the config file from the command line config) # set the config file from the command line
# We try to find the config file # We try to find the config file
if [ ! -z "${!OPTIND:0:1}" -a ! "${!OPTIND:0:1}" = "-" ]; then if [ ! -z "${!OPTIND:0:1}" -a ! "${!OPTIND:0:1}" = "-" ]; then
CONFIG=${!OPTIND} CONFIG=${!OPTIND}
OPTIND=$(( $OPTIND + 1 )) # we found it, move forward in arg parsing OPTIND=$(( OPTIND + 1 )) # we found it, move forward in arg parsing
fi fi
;; ;;
time) # set the restore time from the command line time) # set the restore time from the command line
# We try to find the restore time # We try to find the restore time
if [ ! -z "${!OPTIND:0:1}" -a ! "${!OPTIND:0:1}" = "-" ]; then if [ ! -z "${!OPTIND:0:1}" -a ! "${!OPTIND:0:1}" = "-" ]; then
TIME=${!OPTIND} TIME=${!OPTIND}
OPTIND=$(( $OPTIND + 1 )) # we found it, move forward in arg parsing OPTIND=$(( OPTIND + 1 )) # we found it, move forward in arg parsing
fi fi
;; ;;
dry-run) dry-run)
@ -136,13 +136,13 @@ while getopts ":c:t:bfvlsnd-:" opt; do
ECHO=$(which echo) ECHO=$(which echo)
;; ;;
*) *)
COMMAND=$OPTARG COMMAND=${OPTARG}
;; ;;
esac esac
;; ;;
# here are parsed the short options # here are parsed the short options
c) CONFIG=$OPTARG;; # set the config file from the command line c) CONFIG=${OPTARG};; # set the config file from the command line
t) TIME=$OPTARG;; # set the restore time from the command line t) TIME=${OPTARG};; # set the restore time from the command line
b) COMMAND="backup";; b) COMMAND="backup";;
f) COMMAND="full";; f) COMMAND="full";;
v) COMMAND="verify";; v) COMMAND="verify";;
@ -151,29 +151,29 @@ while getopts ":c:t:bfvlsnd-:" opt; do
n) DRY_RUN="--dry-run ";; # dry run n) DRY_RUN="--dry-run ";; # dry run
d) ECHO=$(which echo);; # debug d) ECHO=$(which echo);; # debug
:) :)
echo "Option -$OPTARG requires an argument." >&2 echo "Option -${OPTARG} requires an argument." >&2
COMMAND="" COMMAND=""
;; ;;
\?) \?)
echo "Invalid option: -$OPTARG" >&2 echo "Invalid option: -${OPTARG}" >&2
COMMAND="" COMMAND=""
;; ;;
esac esac
done done
# Read config file if specified # Read config file if specified
if [ ! -z "$CONFIG" -a -f "$CONFIG" ]; if [ ! -z "${CONFIG}" -a -f "${CONFIG}" ];
then then
. $CONFIG . "${CONFIG}"
else else
echo "ERROR: can't find config file! (${CONFIG})" >&2 echo "ERROR: can't find config file! (${CONFIG})" >&2
usage usage
exit 1 exit 1
fi fi
STATIC_OPTIONS="$DRY_RUN$STATIC_OPTIONS" STATIC_OPTIONS="${DRY_RUN}${STATIC_OPTIONS}"
SIGN_PASSPHRASE=$PASSPHRASE SIGN_PASSPHRASE=${PASSPHRASE}
export AWS_ACCESS_KEY_ID export AWS_ACCESS_KEY_ID
export AWS_SECRET_ACCESS_KEY export AWS_SECRET_ACCESS_KEY
@ -186,11 +186,11 @@ export SWIFT_AUTHVERSION
export PASSPHRASE export PASSPHRASE
export SIGN_PASSPHRASE export SIGN_PASSPHRASE
if [[ -n "$FTP_PASSWORD" ]]; then if [[ -n "${FTP_PASSWORD}" ]]; then
export FTP_PASSWORD export FTP_PASSWORD
fi fi
if [[ -n "$TMPDIR" ]]; then if [[ -n "${TMPDIR}" ]]; then
export TMPDIR export TMPDIR
fi fi
@ -204,24 +204,24 @@ DUPLICITY="$(which duplicity)"
# the script is running at a time. # the script is running at a time.
LOCKFILE=${LOGDIR}backup.lock LOCKFILE=${LOGDIR}backup.lock
if [ "$ENCRYPTION" = "yes" ]; then if [ "${ENCRYPTION}" = "yes" ]; then
if [ ! -z "$GPG_ENC_KEY" ] && [ ! -z "$GPG_SIGN_KEY" ]; then if [ ! -z "${GPG_ENC_KEY}" ] && [ ! -z "${GPG_SIGN_KEY}" ]; then
if [ "$HIDE_KEY_ID" = "yes" ]; then if [ "${HIDE_KEY_ID}" = "yes" ]; then
ENCRYPT="--hidden-encrypt-key=${GPG_ENC_KEY}" ENCRYPT="--hidden-encrypt-key=${GPG_ENC_KEY}"
if [ "$COMMAND" != "restore" -a "$COMMAND" != "restore-file" -a "$COMMAND" != "restore-dir" ]; then if [ "${COMMAND}" != "restore" -a "${COMMAND}" != "restore-file" -a "${COMMAND}" != "restore-dir" ]; then
ENCRYPT="$ENCRYPT --sign-key=${GPG_SIGN_KEY}" ENCRYPT="${ENCRYPT} --sign-key=${GPG_SIGN_KEY}"
fi fi
else else
ENCRYPT="--encrypt-key=${GPG_ENC_KEY} --sign-key=${GPG_SIGN_KEY}" ENCRYPT="--encrypt-key=${GPG_ENC_KEY} --sign-key=${GPG_SIGN_KEY}"
fi fi
if [ ! -z "$SECRET_KEYRING" ]; then if [ ! -z "${SECRET_KEYRING}" ]; then
KEYRING="--secret-keyring ${SECRET_KEYRING}" KEYRING="--secret-keyring ${SECRET_KEYRING}"
ENCRYPT="${ENCRYPT} --encrypt-secret-keyring=${SECRET_KEYRING}" ENCRYPT="${ENCRYPT} --encrypt-secret-keyring=${SECRET_KEYRING}"
fi fi
elif [ ! -z "$PASSPHRASE" ]; then elif [ ! -z "${PASSPHRASE}" ]; then
ENCRYPT="" ENCRYPT=""
fi fi
elif [ "$ENCRYPTION" = "no" ]; then elif [ "${ENCRYPTION}" = "no" ]; then
ENCRYPT="--no-encryption" ENCRYPT="--no-encryption"
fi fi
@ -239,18 +239,18 @@ size information unavailable."
README_TXT="In case you've long forgotten, this is a backup script that you used to backup some files (most likely remotely at Amazon S3). In order to restore these files, you first need to import your GPG private(s) key(s) (if you haven't already). The key(s) is/are in this directory and the following command(s) should do the trick:\n\nIf you were using the same key for encryption and signature:\n gpg --allow-secret-key-import --import duplicity-backup-encryption-and-sign-secret.key.txt\nOr if you were using two separate keys for encryption and signature:\n gpg --allow-secret-key-import --import duplicity-backup-encryption-secret.key.txt\n gpg --allow-secret-key-import --import duplicity-backup-sign-secret.key.txt\n\nAfter your key(s) has/have been succesfully imported, you should be able to restore your files.\n\nGood luck!" README_TXT="In case you've long forgotten, this is a backup script that you used to backup some files (most likely remotely at Amazon S3). In order to restore these files, you first need to import your GPG private(s) key(s) (if you haven't already). The key(s) is/are in this directory and the following command(s) should do the trick:\n\nIf you were using the same key for encryption and signature:\n gpg --allow-secret-key-import --import duplicity-backup-encryption-and-sign-secret.key.txt\nOr if you were using two separate keys for encryption and signature:\n gpg --allow-secret-key-import --import duplicity-backup-encryption-secret.key.txt\n gpg --allow-secret-key-import --import duplicity-backup-sign-secret.key.txt\n\nAfter your key(s) has/have been succesfully imported, you should be able to restore your files.\n\nGood luck!"
if [ ! -x "$DUPLICITY" ]; then if [ ! -x "${DUPLICITY}" ]; then
echo "ERROR: duplicity not installed, that's gotta happen first!" >&2 echo "ERROR: duplicity not installed, that's gotta happen first!" >&2
exit 1 exit 1
fi fi
if [ "`echo ${DEST} | cut -c 1,2`" = "gs" ]; then if [ "$(echo "${DEST}" | cut -c 1,2)" = "gs" ]; then
DEST_IS_GS=true DEST_IS_GS=true
GSCMD="$(which gsutil)" GSCMD="$(which gsutil)"
if [ ! -x "$GSCMD" ]; then if [ ! -x "${GSCMD}" ]; then
echo $NO_GSCMD; GSCMD_AVAIL=false echo "${NO_GSCMD}"; GSCMD_AVAIL=false
elif [ ! -f "${HOME}/.boto" ]; then elif [ ! -f "${HOME}/.boto" ]; then
echo $NO_GSCMD_CFG; GSCMD_AVAIL=false echo "${NO_GSCMD_CFG}"; GSCMD_AVAIL=false
else else
GSCMD_AVAIL=true GSCMD_AVAIL=true
GSCMD="${GSCMD}" GSCMD="${GSCMD}"
@ -259,23 +259,23 @@ else
DEST_IS_GS=false DEST_IS_GS=false
fi fi
if [ "`echo ${DEST} | cut -c 1,2`" = "s3" ]; then if [ "$(echo "${DEST}" | cut -c 1,2)" = "s3" ]; then
DEST_IS_S3=true DEST_IS_S3=true
S3CMD="$(which s3cmd)" S3CMD="$(which s3cmd)"
if [ ! -x "$S3CMD" ]; then if [ ! -x "${S3CMD}" ]; then
echo $NO_S3CMD; S3CMD_AVAIL=false echo "${NO_S3CMD}"; S3CMD_AVAIL=false
elif [ -z "$S3CMD_CONF_FILE" -a ! -f "${HOME}/.s3cfg" ]; then elif [ -z "${S3CMD_CONF_FILE}" -a ! -f "${HOME}/.s3cfg" ]; then
S3CMD_CONF_FOUND=false S3CMD_CONF_FOUND=false
echo $NO_S3CMD_CFG; S3CMD_AVAIL=false echo "${NO_S3CMD_CFG}"; S3CMD_AVAIL=false
elif [ ! -z "$S3CMD_CONF_FILE" -a ! -f "$S3CMD_CONF_FILE" ]; then elif [ ! -z "${S3CMD_CONF_FILE}" -a ! -f "${S3CMD_CONF_FILE}" ]; then
S3CMD_CONF_FOUND=false S3CMD_CONF_FOUND=false
echo "${S3CMD_CONF_FILE} not found, check S3CMD_CONF_FILE variable in duplicity-backup's configuration!"; echo "${S3CMD_CONF_FILE} not found, check S3CMD_CONF_FILE variable in duplicity-backup's configuration!";
echo $NO_S3CMD_CFG; echo "${NO_S3CMD_CFG}";
S3CMD_AVAIL=false S3CMD_AVAIL=false
else else
S3CMD_AVAIL=true S3CMD_AVAIL=true
S3CMD_CONF_FOUND=true S3CMD_CONF_FOUND=true
if [ ! -z "$S3CMD_CONF_FILE" -a -f "$S3CMD_CONF_FILE" ]; then if [ ! -z "${S3CMD_CONF_FILE}" -a -f "${S3CMD_CONF_FILE}" ]; then
# if conf file specified and it exists then add it to the command line for s3cmd # if conf file specified and it exists then add it to the command line for s3cmd
S3CMD="${S3CMD} -c ${S3CMD_CONF_FILE}" S3CMD="${S3CMD} -c ${S3CMD_CONF_FILE}"
fi fi
@ -307,14 +307,14 @@ check_variables ()
config_sanity_fail "An s3 DEST has been specified, but AWS_ACCESS_KEY_ID or AWS_SECRET_ACCESS_KEY have not been configured" config_sanity_fail "An s3 DEST has been specified, but AWS_ACCESS_KEY_ID or AWS_SECRET_ACCESS_KEY have not been configured"
[[ ( ${DEST_IS_GS} = true && (${GS_ACCESS_KEY_ID} = "foobar_gcs_key_id" || ${GS_SECRET_ACCESS_KEY} = "foobar_gcs_secret_id" )) ]] && \ [[ ( ${DEST_IS_GS} = true && (${GS_ACCESS_KEY_ID} = "foobar_gcs_key_id" || ${GS_SECRET_ACCESS_KEY} = "foobar_gcs_secret_id" )) ]] && \
config_sanity_fail "A Google Cloud Storage DEST has been specified, but GS_ACCESS_KEY_ID or GS_SECRET_ACCESS_KEY have not been configured" config_sanity_fail "A Google Cloud Storage DEST has been specified, but GS_ACCESS_KEY_ID or GS_SECRET_ACCESS_KEY have not been configured"
[[ ! -z "$INCEXCFILE" && ! -f $INCEXCFILE ]] && config_sanity_fail "The specified INCEXCFILE $INCEXCFILE does not exists" [[ ! -z "${INCEXCFILE}" && ! -f ${INCEXCFILE} ]] && config_sanity_fail "The specified INCEXCFILE ${INCEXCFILE} does not exists"
} }
check_logdir() check_logdir()
{ {
if [ ! -d ${LOGDIR} ]; then if [ ! -d "${LOGDIR}" ]; then
echo "Attempting to create log directory ${LOGDIR} ..." echo "Attempting to create log directory ${LOGDIR} ..."
if ! mkdir -p ${LOGDIR}; then if ! mkdir -p "${LOGDIR}"; then
echo "Log directory ${LOGDIR} could not be created by this user: ${USER}" echo "Log directory ${LOGDIR} could not be created by this user: ${USER}"
echo "Aborting..." echo "Aborting..."
exit 1 exit 1
@ -322,14 +322,14 @@ check_logdir()
echo "Directory ${LOGDIR} successfully created." echo "Directory ${LOGDIR} successfully created."
fi fi
echo "Attempting to change owner:group of ${LOGDIR} to ${LOG_FILE_OWNER} ..." echo "Attempting to change owner:group of ${LOGDIR} to ${LOG_FILE_OWNER} ..."
if ! chown ${LOG_FILE_OWNER} ${LOGDIR}; then if ! chown "${LOG_FILE_OWNER}" "${LOGDIR}"; then
echo "User ${USER} could not change the owner:group of ${LOGDIR} to $LOG_FILE_OWNER" echo "User ${USER} could not change the owner:group of ${LOGDIR} to ${LOG_FILE_OWNER}"
echo "Aborting..." echo "Aborting..."
exit 1 exit 1
else else
echo "Directory ${LOGDIR} successfully changed to owner:group of ${LOG_FILE_OWNER}" echo "Directory ${LOGDIR} successfully changed to owner:group of ${LOG_FILE_OWNER}"
fi fi
elif [ ! -w ${LOGDIR} ]; then elif [ ! -w "${LOGDIR}" ]; then
echo "Log directory ${LOGDIR} is not writeable by this user: ${USER}" echo "Log directory ${LOGDIR} is not writeable by this user: ${USER}"
echo "Aborting..." echo "Aborting..."
exit 1 exit 1
@ -338,60 +338,60 @@ check_logdir()
email_logfile() email_logfile()
{ {
if [ ! -z "$EMAIL_TO" ]; then if [ ! -z "${EMAIL_TO}" ]; then
MAILCMD=$(which $MAIL) MAILCMD=$(which "${MAIL}")
if [ ! -x "$MAILCMD" ]; then if [ ! -x "${MAILCMD}" ]; then
echo -e "Email couldn't be sent. ${MAIL} not available." >> ${LOGFILE} echo -e "Email couldn't be sent. ${MAIL} not available." >> "${LOGFILE}"
else else
EMAIL_SUBJECT=${EMAIL_SUBJECT:="duplicity-backup alert ${LOG_FILE}"} EMAIL_SUBJECT=${EMAIL_SUBJECT:="duplicity-backup alert ${LOG_FILE}"}
if [ "$MAIL" = "ssmtp" ]; then if [ "${MAIL}" = "ssmtp" ]; then
echo """Subject: ${EMAIL_SUBJECT}""" | cat - ${LOGFILE} | ${MAILCMD} -s ${EMAIL_TO} echo """Subject: ${EMAIL_SUBJECT}""" | cat - "${LOGFILE}" | ${MAILCMD} -s "${EMAIL_TO}"
elif [ "$MAIL" = "msmtp" ]; then elif [ "${MAIL}" = "msmtp" ]; then
echo """Subject: ${EMAIL_SUBJECT}""" | cat - ${LOGFILE} | ${MAILCMD} ${EMAIL_TO} echo """Subject: ${EMAIL_SUBJECT}""" | cat - "${LOGFILE}" | ${MAILCMD} "${EMAIL_TO}"
elif [ "$MAIL" = "mailx" ]; then elif [ "${MAIL}" = "mailx" ]; then
EMAIL_FROM=${EMAIL_FROM:+"-r ${EMAIL_FROM}"} EMAIL_FROM=${EMAIL_FROM:+"-r ${EMAIL_FROM}"}
cat ${LOGFILE} | ${MAILCMD} -s """${EMAIL_SUBJECT}""" $EMAIL_FROM ${EMAIL_TO} ${MAILCMD} -s """${EMAIL_SUBJECT}""" "${EMAIL_FROM}" "${EMAIL_TO}" < "${LOGFILE}"
elif [ "$MAIL" = "mail" ]; then elif [ "${MAIL}" = "mail" ]; then
case `uname` in case $(uname) in
FreeBSD|Darwin|DragonFly|OpenBSD) FreeBSD|Darwin|DragonFly|OpenBSD)
cat ${LOGFILE} | ${MAILCMD} -s """${EMAIL_SUBJECT}""" ${EMAIL_TO} -- ${MAILCMD} -s """${EMAIL_SUBJECT}""" "${EMAIL_TO}" -- < "${LOGFILE}"
;; ;;
*) *)
cat ${LOGFILE} | ${MAILCMD} -s """${EMAIL_SUBJECT}""" $EMAIL_FROM ${EMAIL_TO} -- -f ${EMAIL_FROM} ${MAILCMD} -s """${EMAIL_SUBJECT}""" "${EMAIL_FROM}" "${EMAIL_TO}" -- -f "${EMAIL_FROM}" < "${LOGFILE}"
;; ;;
esac esac
elif [[ "$MAIL" = "sendmail" ]]; then elif [[ "${MAIL}" = "sendmail" ]]; then
(echo """Subject: ${EMAIL_SUBJECT}""" ; cat ${LOGFILE}) | ${MAILCMD} -f ${EMAIL_FROM} ${EMAIL_TO} (echo """Subject: ${EMAIL_SUBJECT}""" ; cat "${LOGFILE}") | ${MAILCMD} -f "${EMAIL_FROM}" "${EMAIL_TO}"
elif [ "$MAIL" = "nail" ]; then elif [ "${MAIL}" = "nail" ]; then
cat ${LOGFILE} | ${MAILCMD} -s """${EMAIL_SUBJECT}""" $EMAIL_FROM ${EMAIL_TO} ${MAILCMD} -s """${EMAIL_SUBJECT}""" "${EMAIL_FROM}" "${EMAIL_TO}" < "${LOGFILE}"
else else
cat ${LOGFILE} | ${MAILCMD} """${EMAIL_SUBJECT}""" ${EMAIL_FROM} ${EMAIL_TO} ${MAILCMD} """${EMAIL_SUBJECT}""" "${EMAIL_FROM}" "${EMAIL_TO}" < "${LOGFILE}"
fi fi
echo -e "Email alert sent to ${EMAIL_TO} using ${MAIL}" >> ${LOGFILE} echo -e "Email alert sent to ${EMAIL_TO} using ${MAIL}" >> "${LOGFILE}"
fi fi
fi fi
} }
send_notification() send_notification()
{ {
if [ ! -z "$NOTIFICATION_SERVICE" ]; then if [ ! -z "${NOTIFICATION_SERVICE}" ]; then
if [ "$NOTIFICATION_SERVICE" = "slack" ]; then if [ "${NOTIFICATION_SERVICE}" = "slack" ]; then
curl -X POST -H 'Content-type: application/json' --data "{\"text\": \"$NOTIFICATION_CONTENT\", \"channel\": \"$SLACK_CHANNEL\", \"username\": \"$SLACK_USERNAME\", \"icon_emoji\": \":$SLACK_EMOJI:\"}" ${SLACK_HOOK_URL} curl -X POST -H 'Content-type: application/json' --data "{\"text\": \"${NOTIFICATION_CONTENT}\", \"channel\": \"${SLACK_CHANNEL}\", \"username\": \"${SLACK_USERNAME}\", \"icon_emoji\": \":${SLACK_EMOJI}:\"}" "${SLACK_HOOK_URL}"
fi fi
fi fi
} }
get_lock() get_lock()
{ {
echo "Attempting to acquire lock ${LOCKFILE}" >> ${LOGFILE} echo "Attempting to acquire lock ${LOCKFILE}" >> "${LOGFILE}"
if ( set -o noclobber; echo "$$" > "${LOCKFILE}" ) 2> /dev/null; then if ( set -o noclobber; echo "$$" > "${LOCKFILE}" ) 2> /dev/null; then
# The lock succeeded. Create a signal handler to remove the lock file when the process terminates. # The lock succeeded. Create a signal handler to remove the lock file when the process terminates.
trap 'EXITCODE=$?; echo "Removing lock. Exit code: ${EXITCODE}" >>${LOGFILE}; rm -f "${LOCKFILE}"' 0 trap 'EXITCODE=$?; echo "Removing lock. Exit code: ${EXITCODE}" >>${LOGFILE}; rm -f "${LOCKFILE}"' 0
echo "successfully acquired lock." >> ${LOGFILE} echo "successfully acquired lock." >> "${LOGFILE}"
else else
# Write lock acquisition errors to log file and stderr # Write lock acquisition errors to log file and stderr
echo "lock failed, could not acquire ${LOCKFILE}" | tee -a ${LOGFILE} >&2 echo "lock failed, could not acquire ${LOCKFILE}" | tee -a "${LOGFILE}" >&2
echo "lock held by $(cat ${LOCKFILE})" | tee -a ${LOGFILE} >&2 echo "lock held by $(cat "${LOCKFILE}")" | tee -a "${LOGFILE}" >&2
email_logfile email_logfile
exit 2 exit 2
fi fi
@ -399,19 +399,19 @@ get_lock()
get_source_file_size() get_source_file_size()
{ {
echo "-----------[ Source Disk Use Information ]-----------" >> ${LOGFILE} echo "-----------[ Source Disk Use Information ]-----------" >> "${LOGFILE}"
# Patches to support spaces in paths- # Patches to support spaces in paths-
# Remove space as a field separator temporarily # Remove space as a field separator temporarily
OLDIFS=$IFS OLDIFS=$IFS
IFS=$(echo -en "\t\n") IFS=$(echo -en "\t\n")
case `uname` in case $(uname) in
FreeBSD|Darwin|DragonFly) FreeBSD|Darwin|DragonFly)
DUEXCFLAG="-I -" DUEXCFLAG="-I -"
;; ;;
OpenBSD) OpenBSD)
echo "WARNING: OpenBSD du does not support exclusion, sizes may be off" >> ${LOGFILE} echo "WARNING: OpenBSD du does not support exclusion, sizes may be off" >> "${LOGFILE}"
DUEXCFLAG="" DUEXCFLAG=""
;; ;;
*) *)
@ -419,25 +419,25 @@ get_source_file_size()
;; ;;
esac esac
for exclude in ${EXCLIST[@]}; do for exclude in "${EXCLIST[@]}"; do
DUEXCLIST="${DUEXCLIST}${exclude}\n" DUEXCLIST="${DUEXCLIST}${exclude}\n"
done done
# if INCLIST is not set or empty, add ROOT to it to be able to calculate disk usage # if INCLIST is not set or empty, add ROOT to it to be able to calculate disk usage
if [ -z "$INCLIST" ]; then if [ -z "${INCLIST}" ]; then
DUINCLIST=($ROOT) DUINCLIST=(${ROOT})
else else
DUINCLIST=("${INCLIST[@]}") DUINCLIST=("${INCLIST[@]}")
fi fi
for include in ${DUINCLIST[@]}; do for include in "${DUINCLIST[@]}"; do
echo -e "$DUEXCLIST" | \ echo -e "${DUEXCLIST}" | \
du -hs ${DUEXCFLAG} ${include} | \ du -hs ${DUEXCFLAG} ${include} | \
awk '{ FS="\t"; $0=$0; print $1"\t"$2 }' \ awk '{ FS="\t"; $0=$0; print $1"\t"$2 }' \
>> ${LOGFILE} >> "${LOGFILE}"
done done
echo >> ${LOGFILE} echo >> "${LOGFILE}"
# Restore IFS # Restore IFS
IFS=$OLDIFS IFS=$OLDIFS
@ -445,44 +445,45 @@ get_source_file_size()
get_remote_file_size() get_remote_file_size()
{ {
echo "---------[ Destination Disk Use Information ]--------" >> ${LOGFILE} echo "---------[ Destination Disk Use Information ]--------" >> "${LOGFILE}"
FRIENDLY_TYPE_NAME="" FRIENDLY_TYPE_NAME=""
dest_type=`echo ${DEST} | cut -c 1,2` dest_type=$(echo "${DEST}" | cut -c 1,2)
case $dest_type in case $dest_type in
"ss") "ss")
FRIENDLY_TYPE_NAME="SSH" FRIENDLY_TYPE_NAME="SSH"
TMPDEST="${DEST#*://*/}" TMPDEST="${DEST#*://*/}"
TMPDEST="${DEST%/${TMPDEST}}" TMPDEST="${DEST%/${TMPDEST}}"
ssh_opt=`echo $STATIC_OPTIONS |awk -vo="--ssh-options=" '{s=index($0,o); if (s) {s=substr($0,s+length(o)); m=substr(s,0,1); for (i=2; i < length(s); i++) { if (substr(s,i,1) == m && substr(s,i-1,1) != "\\\\") break; } print substr(s,2,i-2)}}'` ssh_opt=$(echo "${STATIC_OPTIONS}" |awk -vo="--ssh-options=" '{s=index($0,o); if (s) {s=substr($0,s+length(o)); m=substr(s,0,1); for (i=2; i < length(s); i++) { if (substr(s,i,1) == m && substr(s,i-1,1) != "\\\\") break; } print substr(s,2,i-2)}}')
SIZE=`${TMPDEST%://*} ${ssh_opt} ${TMPDEST#*//} du -hs ${DEST#${TMPDEST}/} | awk '{print $1}'` 2>> ${LOGFILE} SIZE=$(${TMPDEST%://*} "${ssh_opt}" "${TMPDEST#*//}" du -hs "${DEST#${TMPDEST}/}" | awk '{print $1}') 2>> "${LOGFILE}"
EMAIL_SUBJECT="$EMAIL_SUBJECT $SIZE `${TMPDEST%://*} ${ssh_opt} ${TMPDEST#*//} df -hP ${DEST#${TMPDEST}/} | awk '{tmp=$5 " used"}END{print tmp}'`" 2>> ${LOGFILE} EMAIL_SUBJECT="${EMAIL_SUBJECT} ${SIZE} $(${TMPDEST%://*} "${ssh_opt}" "${TMPDEST#*//}" df -hP "${DEST#${TMPDEST}/}" | awk '{tmp=$5 " used"}END{print tmp}')" 2>> "${LOGFILE}"
;; ;;
"fi") "fi")
FRIENDLY_TYPE_NAME="File" FRIENDLY_TYPE_NAME="File"
TMPDEST=`echo ${DEST} | cut -c 6-` TMPDEST=$(echo "${DEST}" | cut -c 6-)
SIZE=`du -hs ${TMPDEST} | awk '{print $1}'` SIZE=$(du -hs "${TMPDEST}" | awk '{print $1}')
;; ;;
"gs") "gs")
FRIENDLY_TYPE_NAME="Google Cloud Storage" FRIENDLY_TYPE_NAME="Google Cloud Storage"
if $GSCMD_AVAIL ; then if ${GSCMD_AVAIL} ; then
TMPDEST=`echo $DEST | sed -e "s/\/*$//" ` #TMPDEST=$(echo "${DEST}" | sed -e "s/\/*$//" )
SIZE=`gsutil du -hs ${TMPDEST} | awk '{print $1$2}'` TMPDEST=${DEST//\/*$/}
SIZE=$(gsutil du -hs "${TMPDEST}" | awk '{print $1$2}')
fi fi
;; ;;
"s3") "s3")
FRIENDLY_TYPE_NAME="Amazon S3" FRIENDLY_TYPE_NAME="Amazon S3"
if $S3CMD_AVAIL ; then if ${S3CMD_AVAIL} ; then
TMPDEST=$(echo ${DEST} | cut -c 11-) TMPDEST=$(echo "${DEST}" | cut -c 11-)
dest_scheme=$(echo ${DEST} | cut -f -1 -d :) dest_scheme=$(echo "${DEST}" | cut -f -1 -d :)
if [ "$dest_scheme" = "s3" ]; then if [ "$dest_scheme" = "s3" ]; then
# Strip off the host name, too. # Strip off the host name, too.
TMPDEST=`echo $TMPDEST | cut -f 2- -d /` TMPDEST=$(echo "${TMPDEST}" | cut -f 2- -d /)
fi fi
SIZE=`${S3CMD} du -H s3://${TMPDEST} | awk '{print $1}'` SIZE=$(${S3CMD} du -H s3://"${TMPDEST}" | awk '{print $1}')
else else
if ! $S3CMD_CONF_FOUND ; then if ! ${S3CMD_CONF_FOUND} ; then
SIZE="-s3cmd config not found-" SIZE="-s3cmd config not found-"
else else
SIZE="-s3cmd not found in PATH-" SIZE="-s3cmd not found in PATH-"
@ -495,13 +496,13 @@ get_remote_file_size()
;; ;;
esac esac
if [[ $FRIENDLY_TYPE_NAME ]] ; then if [[ ${FRIENDLY_TYPE_NAME} ]] ; then
echo -e ""$SIZE"\t"$FRIENDLY_TYPE_NAME" type backend" >> ${LOGFILE} echo -e "${SIZE}\t${FRIENDLY_TYPE_NAME} type backend" >> "${LOGFILE}"
else else
echo "Destination disk use information is currently only available for the following storage backends:" >> ${LOGFILE} echo "Destination disk use information is currently only available for the following storage backends:" >> "${LOGFILE}"
echo "File, SSH, Amazon S3 and Google Cloud" >> ${LOGFILE} echo "File, SSH, Amazon S3 and Google Cloud" >> "${LOGFILE}"
fi fi
echo >> ${LOGFILE} echo >> "${LOGFILE}"
} }
include_exclude() include_exclude()
@ -512,31 +513,31 @@ include_exclude()
IFS=$(echo -en "\t\n") IFS=$(echo -en "\t\n")
# Exlcude device files? # Exlcude device files?
if [ ! -z $EXDEVICEFILES ] && [ $EXDEVICEFILES -ne 0 ]; then if [ ! -z "${EXDEVICEFILES}" ] && [ "${EXDEVICEFILES}" -ne 0 ]; then
TMP=" --exclude-device-files" TMP=" --exclude-device-files"
EXCLUDE=$EXCLUDE$TMP EXCLUDE=${EXCLUDE}${TMP}
fi fi
for include in ${INCLIST[@]} for include in "${INCLIST[@]}"
do do
TMP=" --include=""'"$include"'" TMP=" --include='$include'"
INCLUDE=$INCLUDE$TMP INCLUDE=${INCLUDE}${TMP}
done done
for exclude in ${EXCLIST[@]} for exclude in "${EXCLIST[@]}"
do do
TMP=" --exclude ""'"$exclude"'" TMP=" --exclude '$exclude'"
EXCLUDE=$EXCLUDE$TMP EXCLUDE=${EXCLUDE}${TMP}
done done
# Include/Exclude globbing filelist # Include/Exclude globbing filelist
if [ "$INCEXCFILE" != '' ]; then if [ "${INCEXCFILE}" != '' ]; then
TMP=" --include-globbing-filelist ""'"$INCEXCFILE"'" TMP=" --include-globbing-filelist '${INCEXCFILE}'"
INCLUDE=$INCLUDE$TMP INCLUDE=${INCLUDE}${TMP}
fi fi
# INCLIST and globbing filelist is empty so every file needs to be saved # INCLIST and globbing filelist is empty so every file needs to be saved
if [ "$INCLIST" == '' ] && [ "$INCEXCFILE" == '' ]; then if [ "${INCLIST}" == '' ] && [ "${INCEXCFILE}" == '' ]; then
EXCLUDEROOT='' EXCLUDEROOT=''
else else
EXCLUDEROOT="--exclude=**" EXCLUDEROOT="--exclude=**"
@ -549,41 +550,41 @@ include_exclude()
duplicity_cleanup() duplicity_cleanup()
{ {
echo "----------------[ Duplicity Cleanup ]----------------" >> ${LOGFILE} echo "----------------[ Duplicity Cleanup ]----------------" >> "${LOGFILE}"
if [[ "${CLEAN_UP_TYPE}" != "none" && ! -z ${CLEAN_UP_TYPE} && ! -z ${CLEAN_UP_VARIABLE} ]]; then if [[ "${CLEAN_UP_TYPE}" != "none" && ! -z ${CLEAN_UP_TYPE} && ! -z ${CLEAN_UP_VARIABLE} ]]; then
{ {
eval ${ECHO} ${DUPLICITY} ${CLEAN_UP_TYPE} ${CLEAN_UP_VARIABLE} ${STATIC_OPTIONS} --force \ eval "${ECHO}" "${DUPLICITY}" "${CLEAN_UP_TYPE}" "${CLEAN_UP_VARIABLE}" "${STATIC_OPTIONS}" --force \
${ENCRYPT} \ "${ENCRYPT}" \
${DEST} >> ${LOGFILE} "${DEST}" >> "${LOGFILE}"
} || { } || {
BACKUP_ERROR=1 BACKUP_ERROR=1
} }
echo >> ${LOGFILE} echo >> "${LOGFILE}"
fi fi
if [ ! -z ${REMOVE_INCREMENTALS_OLDER_THAN} ] && [[ ${REMOVE_INCREMENTALS_OLDER_THAN} =~ ^[0-9]+$ ]]; then if [ ! -z "${REMOVE_INCREMENTALS_OLDER_THAN}" ] && [[ ${REMOVE_INCREMENTALS_OLDER_THAN} =~ ^[0-9]+$ ]]; then
{ {
eval ${ECHO} ${DUPLICITY} remove-all-inc-of-but-n-full ${REMOVE_INCREMENTALS_OLDER_THAN} \ eval "${ECHO}" "${DUPLICITY}" remove-all-inc-of-but-n-full "${REMOVE_INCREMENTALS_OLDER_THAN}" \
${STATIC_OPTIONS} --force \ "${STATIC_OPTIONS}" --force \
${ENCRYPT} \ "${ENCRYPT}" \
${DEST} >> ${LOGFILE} "${DEST}" >> "${LOGFILE}"
} || { } || {
BACKUP_ERROR=1 BACKUP_ERROR=1
} }
echo >> ${LOGFILE} echo >> "${LOGFILE}"
fi fi
} }
duplicity_backup() duplicity_backup()
{ {
{ {
eval ${ECHO} ${DUPLICITY} ${OPTION} ${VERBOSITY} ${STATIC_OPTIONS} \ eval "${ECHO}" "${DUPLICITY}" "${OPTION}" "${VERBOSITY}" "${STATIC_OPTIONS}" \
${STORAGECLASS} \ "${STORAGECLASS}" \
${ENCRYPT} \ "${ENCRYPT}" \
${EXCLUDE} \ "${EXCLUDE}" \
${INCLUDE} \ "${INCLUDE}" \
${EXCLUDEROOT} \ "${EXCLUDEROOT}" \
${ROOT} ${DEST} \ "${ROOT}" "${DEST}" \
>> ${LOGFILE} >> "${LOGFILE}"
} || { } || {
BACKUP_ERROR=1 BACKUP_ERROR=1
} }
@ -591,11 +592,11 @@ duplicity_backup()
setup_passphrase() setup_passphrase()
{ {
if [ ! -z "$GPG_ENC_KEY" -a ! -z "$GPG_SIGN_KEY" -a "$GPG_ENC_KEY" != "$GPG_SIGN_KEY" ]; then if [ ! -z "${GPG_ENC_KEY}" -a ! -z "${GPG_SIGN_KEY}" -a "${GPG_ENC_KEY}" != "${GPG_SIGN_KEY}" ]; then
echo -n "Please provide the passphrase for decryption (GPG key 0x${GPG_ENC_KEY}): " echo -n "Please provide the passphrase for decryption (GPG key 0x${GPG_ENC_KEY}): "
builtin read -s ENCPASSPHRASE builtin read -s ENCPASSPHRASE
echo -ne "\n" echo -ne "\n"
PASSPHRASE=$ENCPASSPHRASE PASSPHRASE=${ENCPASSPHRASE}
export PASSPHRASE export PASSPHRASE
fi fi
} }
@ -605,42 +606,42 @@ get_file_sizes()
get_source_file_size get_source_file_size
get_remote_file_size get_remote_file_size
case `uname` in case $(uname) in
FreeBSD|Darwin|DragonFly) FreeBSD|Darwin|DragonFly)
sed -i '' -e '/^--*$/d' ${LOGFILE} sed -i '' -e '/^--*$/d' "${LOGFILE}"
;; ;;
OpenBSD) OpenBSD)
ed -s ${LOGFILE} <<-"EOF" ed -s "${LOGFILE}" <<-"EOF"
g/^--*$/d g/^--*$/d
w w
q q
EOF EOF
;; ;;
*) *)
sed -i -e '/^--*$/d' ${LOGFILE} sed -i -e '/^--*$/d' "${LOGFILE}"
;; ;;
esac esac
[[ -n "${LOG_FILE_OWNER}" ]] && chown ${LOG_FILE_OWNER} ${LOGFILE} [[ -n "${LOG_FILE_OWNER}" ]] && chown "${LOG_FILE_OWNER}" "${LOGFILE}"
} }
backup_this_script() backup_this_script()
{ {
if [ `echo ${0} | cut -c 1` = "." ]; then if [ "$(echo "${0}" | cut -c 1)" = "." ]; then
SCRIPTFILE=$(echo ${0} | cut -c 2-) SCRIPTFILE=$(echo "${0}" | cut -c 2-)
SCRIPTPATH=$(pwd)${SCRIPTFILE} SCRIPTPATH=$(pwd)${SCRIPTFILE}
else else
SCRIPTPATH=$(which ${0}) SCRIPTPATH=$(which "${0}")
fi fi
TMPDIR=duplicity-backup-`date +%Y-%m-%d` TMPDIR=duplicity-backup-$(date +%Y-%m-%d)
TMPFILENAME=${TMPDIR}.tar.gpg TMPFILENAME=${TMPDIR}.tar.gpg
README=${TMPDIR}/README README=${TMPDIR}/README
echo "You are backing up: " echo "You are backing up: "
echo " 1. ${SCRIPTPATH}" echo " 1. ${SCRIPTPATH}"
if [ ! -z "$GPG_ENC_KEY" -a ! -z "$GPG_SIGN_KEY" ]; then if [ ! -z "${GPG_ENC_KEY}" -a ! -z "${GPG_SIGN_KEY}" ]; then
if [ "$GPG_ENC_KEY" = "$GPG_SIGN_KEY" ]; then if [ "${GPG_ENC_KEY}" = "${GPG_SIGN_KEY}" ]; then
echo " 2. GPG Secret encryption and sign key: ${GPG_ENC_KEY}" echo " 2. GPG Secret encryption and sign key: ${GPG_ENC_KEY}"
else else
echo " 2. GPG Secret encryption key: ${GPG_ENC_KEY} and GPG secret sign key: ${GPG_SIGN_KEY}" echo " 2. GPG Secret encryption key: ${GPG_ENC_KEY} and GPG secret sign key: ${GPG_SIGN_KEY}"
@ -649,52 +650,52 @@ backup_this_script()
echo " 2. GPG Secret encryption and sign key: none (symmetric encryption)" echo " 2. GPG Secret encryption and sign key: none (symmetric encryption)"
fi fi
if [ ! -z "$CONFIG" -a -f "$CONFIG" ]; if [ ! -z "${CONFIG}" -a -f "${CONFIG}" ];
then then
echo " 3. Config file: ${CONFIG}" echo " 3. Config file: ${CONFIG}"
fi fi
if [ ! -z "$INCEXCFILE" -a -f "$INCEXCFILE" ]; if [ ! -z "${INCEXCFILE}" -a -f "${INCEXCFILE}" ];
then then
echo " 4. Include/Exclude globbing file: ${INCEXCFILE}" echo " 4. Include/Exclude globbing file: ${INCEXCFILE}"
fi fi
echo "Backup tarball will be encrypted and saved to: `pwd`/${TMPFILENAME}" echo "Backup tarball will be encrypted and saved to: $(pwd)/${TMPFILENAME}"
echo echo
echo ">> Are you sure you want to do that ('yes' to continue)?" echo ">> Are you sure you want to do that ('yes' to continue)?"
read ANSWER read ANSWER
if [ "$ANSWER" != "yes" ]; then if [ "${ANSWER}" != "yes" ]; then
echo "You said << ${ANSWER} >> so I am exiting now." echo "You said << ${ANSWER} >> so I am exiting now."
exit 1 exit 1
fi fi
mkdir -p ${TMPDIR} mkdir -p "${TMPDIR}"
cp $SCRIPTPATH ${TMPDIR}/ cp "${SCRIPTPATH}" "${TMPDIR}"/
if [ ! -z "$CONFIG" -a -f "$CONFIG" ]; if [ ! -z "${CONFIG}" -a -f "${CONFIG}" ];
then then
cp $CONFIG ${TMPDIR}/ cp "${CONFIG}" "${TMPDIR}"/
fi fi
if [ ! -z "$INCEXCFILE" -a -f "$INCEXCFILE" ]; if [ ! -z "${INCEXCFILE}" -a -f "${INCEXCFILE}" ];
then then
cp $INCEXCFILE ${TMPDIR}/ cp "${INCEXCFILE}" "${TMPDIR}"/
fi fi
if [ ! -z "$GPG_ENC_KEY" -a ! -z "$GPG_SIGN_KEY" ]; then if [ ! -z "${GPG_ENC_KEY}" -a ! -z "${GPG_SIGN_KEY}" ]; then
export GPG_TTY=`tty` export GPG_TTY=$(tty)
if [ "$GPG_ENC_KEY" = "$GPG_SIGN_KEY" ]; then if [ "${GPG_ENC_KEY}" = "${GPG_SIGN_KEY}" ]; then
gpg -a --export-secret-keys ${KEYRING} ${GPG_ENC_KEY} > ${TMPDIR}/duplicity-backup-encryption-and-sign-secret.key.txt gpg -a --export-secret-keys "${KEYRING}" "${GPG_ENC_KEY}" > "${TMPDIR}"/duplicity-backup-encryption-and-sign-secret.key.txt
else else
gpg -a --export-secret-keys ${KEYRING} ${GPG_ENC_KEY} > ${TMPDIR}/duplicity-backup-encryption-secret.key.txt gpg -a --export-secret-keys "${KEYRING}" "${GPG_ENC_KEY}" > "${TMPDIR}"/duplicity-backup-encryption-secret.key.txt
gpg -a --export-secret-keys ${KEYRING} ${GPG_SIGN_KEY} > ${TMPDIR}/duplicity-backup-sign-secret.key.txt gpg -a --export-secret-keys "${KEYRING}" "${GPG_SIGN_KEY}" > "${TMPDIR}"/duplicity-backup-sign-secret.key.txt
fi fi
fi fi
echo -e ${README_TXT} > ${README} echo -e "${README_TXT}" > "${README}"
echo "Encrypting tarball, choose a password you'll remember..." echo "Encrypting tarball, choose a password you'll remember..."
tar -cf - ${TMPDIR} | gpg -aco ${TMPFILENAME} tar -cf - "${TMPDIR}" | gpg -aco "${TMPFILENAME}"
rm -Rf ${TMPDIR} rm -Rf "${TMPDIR}"
echo -e "\nIMPORTANT!!" echo -e "\nIMPORTANT!!"
echo ">> To restore these files, run the following (remember your password):" echo ">> To restore these files, run the following (remember your password):"
echo "gpg -d ${TMPFILENAME} | tar -xf -" echo "gpg -d ${TMPFILENAME} | tar -xf -"
@ -704,7 +705,7 @@ backup_this_script()
check_variables check_variables
check_logdir check_logdir
echo -e "-------- START DUPLICITY-BACKUP SCRIPT for ${HOSTNAME} --------\n" >> ${LOGFILE} echo -e "-------- START DUPLICITY-BACKUP SCRIPT for ${HOSTNAME} --------\n" >> "${LOGFILE}"
get_lock get_lock
@ -712,7 +713,7 @@ INCLUDE=
EXCLUDE= EXCLUDE=
EXCLUDEROOT= EXCLUDEROOT=
case "$COMMAND" in case "${COMMAND}" in
"backup-script") "backup-script")
backup_this_script backup_this_script
exit exit
@ -732,7 +733,7 @@ case "$COMMAND" in
DEST=${OLDROOT} DEST=${OLDROOT}
OPTION="verify" OPTION="verify"
echo -e "-------[ Verifying Source & Destination ]-------\n" >> ${LOGFILE} echo -e "-------[ Verifying Source & Destination ]-------\n" >> "${LOGFILE}"
include_exclude include_exclude
setup_passphrase setup_passphrase
echo -e "Attempting to verify now ..." echo -e "Attempting to verify now ..."
@ -748,26 +749,26 @@ case "$COMMAND" in
;; ;;
"restore") "restore")
ROOT=$DEST ROOT=${DEST}
OPTION="restore" OPTION="restore"
if [ ! -z "$TIME" ]; then if [ ! -z "${TIME}" ]; then
STATIC_OPTIONS="$STATIC_OPTIONS --time $TIME" STATIC_OPTIONS="${STATIC_OPTIONS} --time ${TIME}"
fi fi
if [[ ! "$RESTORE_DEST" ]]; then if [[ ! "${RESTORE_DEST}" ]]; then
echo "Please provide a destination path (eg, /home/user/dir):" echo "Please provide a destination path (eg, /home/user/dir):"
read -e NEWDESTINATION read -e NEWDESTINATION
DEST=$NEWDESTINATION DEST=${NEWDESTINATION}
echo ">> You will restore from ${ROOT} to ${DEST}" echo ">> You will restore from ${ROOT} to ${DEST}"
echo "Are you sure you want to do that ('yes' to continue)?" echo "Are you sure you want to do that ('yes' to continue)?"
read ANSWER read ANSWER
if [[ "$ANSWER" != "yes" ]]; then if [[ "${ANSWER}" != "yes" ]]; then
echo "You said << ${ANSWER} >> so I am exiting now." echo "You said << ${ANSWER} >> so I am exiting now."
echo -e "User aborted restore process ...\n" >> ${LOGFILE} echo -e "User aborted restore process ...\n" >> "${LOGFILE}"
exit 1 exit 1
fi fi
else else
DEST=$RESTORE_DEST DEST=${RESTORE_DEST}
fi fi
setup_passphrase setup_passphrase
@ -776,39 +777,39 @@ case "$COMMAND" in
;; ;;
"restore-file"|"restore-dir") "restore-file"|"restore-dir")
ROOT=$DEST ROOT=${DEST}
OPTION="restore" OPTION="restore"
if [ ! -z "$TIME" ]; then if [ ! -z "${TIME}" ]; then
STATIC_OPTIONS="$STATIC_OPTIONS --time $TIME" STATIC_OPTIONS="${STATIC_OPTIONS} --time ${TIME}"
fi fi
if [[ ! "$FILE_TO_RESTORE" ]]; then if [[ ! "${FILE_TO_RESTORE}" ]]; then
echo "Which file or directory do you want to restore?" echo "Which file or directory do you want to restore?"
echo "(give the path relative to the root of the backup eg, mail/letter.txt):" echo "(give the path relative to the root of the backup eg, mail/letter.txt):"
read -e FILE_TO_RESTORE read -e FILE_TO_RESTORE
echo echo
fi fi
if [[ "$RESTORE_DEST" ]]; then if [[ "${RESTORE_DEST}" ]]; then
DEST=$RESTORE_DEST DEST=${RESTORE_DEST}
else else
DEST=$(basename $FILE_TO_RESTORE) DEST=$(basename "${FILE_TO_RESTORE}")
fi fi
echo -e "YOU ARE ABOUT TO..." echo -e "YOU ARE ABOUT TO..."
echo -e ">> RESTORE: $FILE_TO_RESTORE" echo -e ">> RESTORE: ${FILE_TO_RESTORE}"
echo -e ">> TO: ${DEST}" echo -e ">> TO: ${DEST}"
echo -e "\nAre you sure you want to do that ('yes' to continue)?" echo -e "\nAre you sure you want to do that ('yes' to continue)?"
read ANSWER read ANSWER
if [ "$ANSWER" != "yes" ]; then if [ "${ANSWER}" != "yes" ]; then
echo "You said << ${ANSWER} >> so I am exiting now." echo "You said << ${ANSWER} >> so I am exiting now."
echo -e "--------------------- END ---------------------\n" >> ${LOGFILE} echo -e "--------------------- END ---------------------\n" >> "${LOGFILE}"
exit 1 exit 1
fi fi
FILE_TO_RESTORE="'"$FILE_TO_RESTORE"'" FILE_TO_RESTORE="'${FILE_TO_RESTORE}'"
DEST="'"$DEST"'" DEST="'${DEST}'"
setup_passphrase setup_passphrase
echo "Restoring now ..." echo "Restoring now ..."
@ -820,25 +821,25 @@ case "$COMMAND" in
"list-current-files") "list-current-files")
OPTION="list-current-files" OPTION="list-current-files"
if [ ! -z "$TIME" ]; then if [ ! -z "${TIME}" ]; then
STATIC_OPTIONS="$STATIC_OPTIONS --time $TIME" STATIC_OPTIONS="${STATIC_OPTIONS} --time ${TIME}"
fi fi
eval \ eval \
${DUPLICITY} ${OPTION} ${VERBOSITY} ${STATIC_OPTIONS} \ "${DUPLICITY}" "${OPTION}" "${VERBOSITY}" "${STATIC_OPTIONS}" \
$ENCRYPT \ ${ENCRYPT} \
${DEST} | tee -a ${LOGFILE} "${DEST}" | tee -a "${LOGFILE}"
echo -e "--------------------- END ---------------------\n" >> ${LOGFILE} echo -e "--------------------- END ---------------------\n" >> "${LOGFILE}"
;; ;;
"collection-status") "collection-status")
OPTION="collection-status" OPTION="collection-status"
eval \ eval \
${DUPLICITY} ${OPTION} ${VERBOSITY} ${STATIC_OPTIONS} \ "${DUPLICITY}" "${OPTION}" "${VERBOSITY}" "${STATIC_OPTIONS}" \
$ENCRYPT \ ${ENCRYPT} \
${DEST} | tee -a ${LOGFILE} "${DEST}" | tee -a "${LOGFILE}"
echo -e "--------------------- END ---------------------\n" >> ${LOGFILE} echo -e "--------------------- END ---------------------\n" >> "${LOGFILE}"
;; ;;
"backup") "backup")
@ -849,14 +850,14 @@ case "$COMMAND" in
;; ;;
*) *)
echo -e "[Only show `basename $0` usage options]\n" >> ${LOGFILE} echo -e "[Only show $(basename "$0") usage options]\n" >> "${LOGFILE}"
usage usage
;; ;;
esac esac
echo -e "--------- END DUPLICITY-BACKUP SCRIPT ---------\n" >> ${LOGFILE} echo -e "--------- END DUPLICITY-BACKUP SCRIPT ---------\n" >> "${LOGFILE}"
if [ "$EMAIL_FAILURE_ONLY" = "yes" ]; then if [ "${EMAIL_FAILURE_ONLY}" = "yes" ]; then
if [ ${BACKUP_ERROR} ]; then if [ ${BACKUP_ERROR} ]; then
EMAIL_SUBJECT="BACKUP ERROR: ${EMAIL_SUBJECT}" EMAIL_SUBJECT="BACKUP ERROR: ${EMAIL_SUBJECT}"
email_logfile email_logfile
@ -870,25 +871,25 @@ else
email_logfile email_logfile
fi fi
if [ "$NOTIFICATION_FAILURE_ONLY" = "yes" ]; then if [ "${NOTIFICATION_FAILURE_ONLY}" = "yes" ]; then
if [ ${BACKUP_ERROR} ]; then if [ ${BACKUP_ERROR} ]; then
NOTIFICATION_CONTENT="BACKUP ERROR: ${HOSTNAME} - \`$LOGFILE\`" NOTIFICATION_CONTENT="BACKUP ERROR: ${HOSTNAME} - \`${LOGFILE}\`"
send_notification send_notification
fi fi
else else
if [ ${BACKUP_ERROR} ]; then if [ ${BACKUP_ERROR} ]; then
NOTIFICATION_CONTENT="BACKUP ERROR: ${HOSTNAME} - \`$LOGFILE\`" NOTIFICATION_CONTENT="BACKUP ERROR: ${HOSTNAME} - \`${LOGFILE}\`"
else else
NOTIFICATION_CONTENT="BACKUP OK: ${HOSTNAME} - \`$LOGFILE\`" NOTIFICATION_CONTENT="BACKUP OK: ${HOSTNAME} - \`${LOGFILE}\`"
fi fi
send_notification send_notification
fi fi
# remove old logfiles # remove old logfiles
# stops them from piling up infinitely # stops them from piling up infinitely
[[ -n "${REMOVE_LOGS_OLDER_THAN}" ]] && find ${LOGDIR} -type f -mtime +"${REMOVE_LOGS_OLDER_THAN}" -delete [[ -n "${REMOVE_LOGS_OLDER_THAN}" ]] && find "${LOGDIR}" -type f -mtime +"${REMOVE_LOGS_OLDER_THAN}" -delete
if [ ${ECHO} ]; then if [ "${ECHO}" ]; then
echo "TEST RUN ONLY: Check the logfile for command output." echo "TEST RUN ONLY: Check the logfile for command output."
fi fi