forked from redmatter/atlassian-bamboo-diy-backup
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbamboo.diy-backup.common.sh
106 lines (83 loc) · 3.55 KB
/
bamboo.diy-backup.common.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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
#!/bin/bash
check_command "curl"
check_command "jq"
BAMBOO_HTTP_AUTH="-u ${BAMBOO_BACKUP_USER}:${BAMBOO_BACKUP_PASS}"
: ${BAMBOO_PAUSE_WAIT_TIMEOUT:=1200}
# The name of the product
PRODUCT=Bamboo
function bamboo_is_paused {
local BAMBOO_STATUS_RESULT=$(curl ${CURL_OPTIONS} ${BAMBOO_HTTP_AUTH} -H "Accept: application/json" -H "Content-type: application/json" "${BAMBOO_URL}/rest/api/latest/server")
[ -n "${BAMBOO_STATUS_RESULT}" ] && [ "$(echo ${BAMBOO_STATUS_RESULT} | jq -r ".state" | tr -d '\r')" = 'PAUSED' ]
}
function bamboo_pause {
curl ${CURL_OPTIONS} ${BAMBOO_HTTP_AUTH} -X POST -H "Accept: application/json" -H "Content-type: application/json" "${BAMBOO_URL}/rest/api/latest/server/pause?os_authType=basic"
if [ $? != 0 ]; then
bail "Pausing this ${PRODUCT} instance failed"
fi
# Check the server status to see if its been paused
# Expected result {"state":"PAUSED","reindexInProgress":false}
local wait_till=$(( $(date +%s) + ${BAMBOO_PAUSE_WAIT_TIMEOUT} ))
while ! bamboo_is_paused; do
if [ "$(date +%s)" -gt "${wait_till}" ]; then
bail "Unable to pause ${PRODUCT} instance."
fi
sleep 2;
info "Waiting for ${PRODUCT} instance to pause..."
done
info "${PRODUCT} instance paused"
}
function bamboo_resume {
curl ${CURL_OPTIONS} ${BAMBOO_HTTP_AUTH} -X POST -H "Accept:application/json" -H "Content-type: application/json" "${BAMBOO_URL}/rest/api/latest/server/resume?os_authType=basic"
if [ $? != 0 ]; then
bail "Unable to resume ${PRODUCT} instance"
fi
info "${PRODUCT} instance resume"
}
function freeze_mount_point {
info "Freezing filesystem at mount point ${1}"
sudo fsfreeze -f ${1} > /dev/null
}
function unfreeze_mount_point {
info "Unfreezing filesystem at mount point ${1}"
sudo fsfreeze -u ${1} > /dev/null 2>&1
}
function mount_device {
local DEVICE_NAME="$1"
local MOUNT_POINT="$2"
sudo mount "${DEVICE_NAME}" "${MOUNT_POINT}" > /dev/null
success "Mounted device ${DEVICE_NAME} to ${MOUNT_POINT}"
}
function add_cleanup_routine() {
cleanup_queue+=($1)
trap run_cleanup EXIT
}
function run_cleanup() {
info "Cleaning up..."
for cleanup in ${cleanup_queue[@]}
do
${cleanup}
done
}
function check_mount_point {
local MOUNT_POINT="${1}"
# mountpoint check will return a non-zero exit code when mount point is free
mountpoint -q "${MOUNT_POINT}"
if [ $? == 0 ]; then
error "The directory mount point ${MOUNT_POINT} appears to be taken"
bail "Please stop ${PRODUCT}. Stop PostgreSQL if it is running. Unmount the device and detach the volume"
fi
}
# This removes config.lock, index.lock, gc.pid, and refs/heads/*.lock
function cleanup_locks {
local HOME_DIRECTORY="$1"
# From the shopt man page:
# globstar
# If set, the pattern ‘**’ used in a filename expansion context will match all files and zero or
# more directories and subdirectories. If the pattern is followed by a ‘/’, only directories and subdirectories match.
# shopt -s globstar
# Remove lock files in the repositories
# sudo -u ${BAMBOO_UID} rm -f ${HOME_DIRECTORY}/shared/data/repositories/*/{HEAD,config,index,gc,packed-refs,stash-packed-refs}.{pid,lock}
# sudo -u ${BAMBOO_UID} rm -f ${HOME_DIRECTORY}/shared/data/repositories/*/refs/**/*.lock
# sudo -u ${BAMBOO_UID} rm -f ${HOME_DIRECTORY}/shared/data/repositories/*/stash-refs/**/*.lock
# sudo -u ${BAMBOO_UID} rm -f ${HOME_DIRECTORY}/shared/data/repositories/*/logs/**/*.lock
}