Skip to content

Add debugging flag and logic #20

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
13 changes: 12 additions & 1 deletion backup.sh
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ else
exit 1
fi

while [[ $# -gt 1 ]]
while [[ $# -ge 1 ]]
do
key="$1"

Expand All @@ -38,6 +38,9 @@ do
fi
shift # past argument
;;
-d|--debug)
DEBUG=true
;;

esac
shift # past argument or value
Expand Down Expand Up @@ -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' \
Expand All @@ -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)
Expand All @@ -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
Expand All @@ -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

Expand All @@ -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