-
Notifications
You must be signed in to change notification settings - Fork 19
/
backup-jira-api-token.sh
executable file
·85 lines (61 loc) · 2.69 KB
/
backup-jira-api-token.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
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