diff --git a/README.md b/README.md index 39e53d9..602c164 100644 --- a/README.md +++ b/README.md @@ -52,6 +52,10 @@ Arguments overview: The former is useful when an external backup program handles backup rotation. Set the argument to "false" if there should be no timestamp in the filename. +* -d, --debug + + Set to enable additional debugging information. + # Implementation The script generates an authorization cookie using your Atlassian credentials if diff --git a/backup.sh b/backup.sh index 775ecec..789e43b 100755 --- a/backup.sh +++ b/backup.sh @@ -13,7 +13,7 @@ else exit 1 fi -while [[ $# -gt 1 ]] +while [[ $# -ge 1 ]] do key="$1" @@ -38,6 +38,9 @@ do fi shift # past argument ;; + -d|--debug) + DEBUG=true + ;; esac shift # past argument or value @@ -73,13 +76,16 @@ COOKIE_FILE_LOCATION="$HOME/.backup.sh-cookie" # Only generate a new cookie if one does not exist, or if it is more than 24 # hours old. This is to allow reuse of the same cookie until a new backup can be # triggered. +if [[ $DEBUG == true ]]; then echo "Checking for cookie"; fi find $COOKIE_FILE_LOCATION -mtime -1 2> /dev/null |grep $COOKIE_FILE_LOCATION 2>&1 > /dev/null if [ $? -ne 0 ]; then + if [[ $DEBUG == true ]]; then echo "Generating cookie"; fi curl --silent --cookie-jar $COOKIE_FILE_LOCATION -X POST "https://${INSTANCE}/rest/auth/1/session" -d "{\"username\": \"$USERNAME\", \"password\": \"$PASSWORD\"}" -H 'Content-Type: application/json' --output /dev/null chmod 600 $COOKIE_FILE_LOCATION fi # The $BKPMSG variable will print the error message, you can use it if you're planning on sending an email +if [[ $DEBUG == true ]]; then echo "Triggering backup"; fi BKPMSG=$(curl -s --cookie $COOKIE_FILE_LOCATION $RUNBACKUP_URL \ -X POST \ -H 'DNT: 1' \ @@ -88,6 +94,8 @@ BKPMSG=$(curl -s --cookie $COOKIE_FILE_LOCATION $RUNBACKUP_URL \ -H 'X-Requested-With: XMLHttpRequest' \ --data-binary "{\"cbAttachments\":\"${ATTACHMENTS}\", \"exportToCloud\":\"true\" }" ) +if [[ $DEBUG == true ]]; then echo $BKPMSG; fi + # Checks if we were authorized to create a new backup if [ $FILEPREFIX = "JIRA" ]; then STATUS_CODE=$(echo "$BKPMSG" | jq '."status-code"' -r) @@ -104,6 +112,7 @@ else fi # Checks if the backup exists every $SLEEP_SECONDS seconds, $PROGRESS_CHECKS times. +if [[ $DEBUG == true ]]; then echo "Polling for backup"; fi for (( c=1; c<=$PROGRESS_CHECKS; c++ )) do if [ $FILEPREFIX = "JIRA" ]; then @@ -125,6 +134,7 @@ for (( c=1; c<=$PROGRESS_CHECKS; c++ )) do break fi fi + if [[ $DEBUG == true ]]; then echo $PROGRESS_JSON; fi sleep $SLEEP_SECONDS done @@ -133,5 +143,6 @@ if [ -z "$FILE_NAME" ]; then exit else # Download the new way, starting Nov 2016 + if [[ $DEBUG == true ]]; then echo "curl -s -S -L --cookie $COOKIE_FILE_LOCATION "$DOWNLOAD_URL/$FILE_NAME" -o "$OUTFILE""; fi curl -s -S -L --cookie $COOKIE_FILE_LOCATION "$DOWNLOAD_URL/$FILE_NAME" -o "$OUTFILE" fi