Skip to content

Commit

Permalink
Merge pull request #21 from mattock/fix_jira_backup
Browse files Browse the repository at this point in the history
Fix JIRA backups
  • Loading branch information
mattock authored Dec 12, 2018
2 parents 9d1754c + 5fd3536 commit cf67b3c
Show file tree
Hide file tree
Showing 2 changed files with 93 additions and 2 deletions.
85 changes: 85 additions & 0 deletions backup-jira-api-token.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
#!/bin/bash


###--- CONFIGURATION SECTION STARTS HERE ---###
# MAKE SURE ALL THE VALUES IN THIS SECTION ARE CORRECT BEFORE RUNNIG THE SCRIPT

CONFIG="$HOME/.backup.sh.vars"

if [ -r "$CONFIG" ]; then
. $CONFIG
else
echo "Usable to load $CONFIG! Please create one based on backup.sh.vars.example"
exit 1
fi


####- START SCRIPT -#####

TODAY=$(TZ=$TIMEZONE date +%d-%m-%Y)
echo "starting the script: $TODAY"


## The $BKPMSG variable is used to save and print the response
BKPMSG=$(curl -s -u ${EMAIL}:${API_TOKEN} -H "Accept: application/json" -H "Content-Type: application/json" --data-binary '{"cbAttachments":"true", "exportToCloud":"true"}' -X POST https://${HOSTNAME}/rest/backup/1/export/runbackup )

## Uncomment below line to print the response message also in case of no errors ##
# echo "Response: $BKPMSG"

# If the backup did not start print the error messaget returned and exits the script
if [ "$(echo "$BKPMSG" | grep -ic error)" -ne 0 ]; then
echo "BACKUP FAILED!! Message returned: $BKPMSG"
exit
fi

# If the backup started correctly it extracts the taskId value from the response
# As an alternative you can call the endpoint /rest/backup/1/export/lastTaskId to get the last task-id
TASK_ID=$(echo "$BKPMSG" | sed -n 's/.*"taskId"[ ]*:[ ]*"\([^"]*\).*/\1/p')


# Checks if the backup process completed for the number of times specified in PROGRESS_CHECKS variable
for (( c=1; c<=${PROGRESS_CHECKS}; c++ ))
do
PROGRESS_JSON=$(curl -s -u ${EMAIL}:${API_TOKEN} -X GET https://${HOSTNAME}/rest/backup/1/export/getProgress?taskId=${TASK_ID})
FILE_NAME=$(echo "$PROGRESS_JSON" | sed -n 's/.*"result"[ ]*:[ ]*"\([^"]*\).*/\1/p')

# Print progress message
echo "$PROGRESS_JSON"

if [[ $PROGRESS_JSON == *"error"* ]]; then
break
fi

if [ ! -z "$FILE_NAME" ]; then
break
fi

# Waits for the amount of seconds specified in SLEEP_SECONDS variable between a check and the other
sleep ${SLEEP_SECONDS}
done

# If the backup is not ready after the configured amount of PROGRESS_CHECKS, it ends the script.
if [ -z "$FILE_NAME" ];
then
exit
else


## PRINT THE FILE TO DOWNLOAD ##
echo "File to download: https://${HOSTNAME}/plugins/servlet/${FILE_NAME}"

#Check if we should overwrite the previous backup or append a timestamp to
#prevent just that. The former is useful when an external backup program handles
#backup rotation.
if [ $TIMESTAMP = "true" ]; then
OUTFILE="${DOWNLOAD_FOLDER}/JIRA-backup-${TODAY}.zip"
elif [ $TIMESTAMP = "false" ]; then
OUTFILE="${DOWNLOAD_FOLDER}/JIRA-backup.zip"
else
echo "ERROR: invalid value for TIMESTAMP: should be either \"true\" or \"false\""
exit 1
fi

curl -s -L -u ${EMAIL}:${API_TOKEN} -X GET "https://${HOSTNAME}/plugins/servlet/${FILE_NAME}" -o "$OUTFILE"

fi
10 changes: 8 additions & 2 deletions backup.sh.vars.example
Original file line number Diff line number Diff line change
@@ -1,8 +1,14 @@
USERNAME=youruser
PASSWORD=yourpassword
USERNAME=confluence_username
PASSWORD=confluence_password
EMAIL=email_for_jira_authentication
API_TOKEN=api_token_for_jira_authentication
INSTANCE=example.atlassian.net
LOCATION=/where/to/store/the/file

# The new JIRA backup script reinvented these variable names
HOSTNAME=$INSTANCE
DOWNLOAD_FOLDER=$LOCATION

# Append a timestamp to the backed up file (true/false). Set to false if an
# external backup applications handles backup rotatoin.
TIMESTAMP=true
Expand Down

0 comments on commit cf67b3c

Please sign in to comment.