-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathJira-backup.sh
87 lines (62 loc) · 2.8 KB
/
Jira-backup.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
86
87
#!/bin/bash
###--- CONFIGURATION SECTION STARTS HERE ---###
# MAKE SURE ALL THE VALUES IN THIS SECTION ARE CORRECT BEFORE RUNNIG THE SCRIPT
source api-token.sh
mkdir $folder
EMAIL=$email
API_TOKEN=$token
HOSTNAME=$instance
DOWNLOAD_FOLDER=$result
# Set to false if you don't want to backup attachments
INCLUDE_ATTACHMENTS=true
# Set to false if you want to create a backup for Jira Server
EXPORT_TO_CLOUD=true
### Checks for progress max 3000 times, waiting 20 seconds between one check and the other ###
# If your instance is big you may want to increase the below values #
PROGRESS_CHECKS=3000
SLEEP_SECONDS=20
# Set this to your Atlassian instance's timezone.
# See this for a list of possible values:
# https://en.wikipedia.org/wiki/List_of_tz_database_time_zones
TIMEZONE=Europe/Belgrade
###--- END OF CONFIGURATION SECTION ---####
####- 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\":\"$INCLUDE_ATTACHMENTS\", \"exportToCloud\":\"$EXPORT_TO_CLOUD\"}" -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 "Downloading https://${HOSTNAME}/plugins/servlet/${FILE_NAME}"
curl -L -u ${EMAIL}:${API_TOKEN} -X GET "https://${HOSTNAME}/plugins/servlet/${FILE_NAME}" -o "$DOWNLOAD_FOLDER/JIRA-backup-${TODAY}.zip"
fi