This repository has been archived by the owner on Jan 23, 2021. It is now read-only.
forked from travelaudience/docker-nexus-backup
-
Notifications
You must be signed in to change notification settings - Fork 0
/
docker-entrypoint.sh
executable file
·220 lines (177 loc) · 6.41 KB
/
docker-entrypoint.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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
#!/bin/bash
set -o pipefail
source /root/.bashrc
# Check for a stale lock file every 5 minutes.
LOCK_CHECK_INTERVAL=300
# Even if NEXUS_BACKUP_DIRECTORY is ill-defined, we define this up here and let validation below do the rest.
LOCK_FILE="${NEXUS_BACKUP_DIRECTORY}/.nexus-backup-lock"
# Set a timeout of 12 hours for the backup procedure.
LOCK_TIMEOUT=43200
# The name of the file used to trigger the backup procedure.
TRIGGER_FILE_NAME=".backup"
function backup {
# Store the current timestamp (in seconds) in the lock file for future reference...
echo "$(date +%s)" >! "${LOCK_FILE}"
local TIMESTAMP;
# The timestamp of the backup (we chose ISO-8601 for clarity).
TIMESTAMP="$(date -u +"%Y-%m-%dT%H:%M:%SZ")"
# Make sure every Groovy script necessary for backup is installed.
find /scripts -name "*.groovy" | while read -r FILE;
do
ensure_groovy_script "${FILE}";
done
echo "==> Attempting to stop repositories.";
manage_repos stop
echo "==> Sleeping for ${GRACE_PERIOD} seconds."
sleep "${GRACE_PERIOD}"
echo "==> Attempting to backup the 'default' blobstore."
tar c "${NEXUS_DATA_DIRECTORY}/blobs/default/" | gsutil cp - "${TARGET_BUCKET}/${TIMESTAMP}/blobstore.tar"
local EXIT_CODE_1=$?
if [ ${EXIT_CODE_1} -ne 0 ]; then
echo "(!) Couldn't backup the blobstore. Manual intervention is advised."
else
echo "(✓) Blobstore successfully backed-up."
fi
echo "==> Attempting to backup the Nexus databases."
tar c "${NEXUS_BACKUP_DIRECTORY}/" | gsutil cp - "${TARGET_BUCKET}/${TIMESTAMP}/databases.tar"
local EXIT_CODE_2=$?
if [ ${EXIT_CODE_2} -ne 0 ]; then
echo "(!) Couldn't backup the databases. Manual intervention is advised."
else
find "${NEXUS_BACKUP_DIRECTORY}" -name "*.bak" -exec rm {} \; # Cleanup leftovers so that they don't get picked up next time.
echo "(✓) Databases successfully backed-up."
fi
echo "==> Attempting to start repositories."
manage_repos start
# Remove the lock file...
rm -f "${LOCK_FILE}"
}
function ensure_groovy_script {
local BODY;
local NAME;
# Remove line breaks from the script.
BODY=$(tr -d '\n' < "${1}")
# Get the script's filename without extension.
NAME=$(basename "${1}" .groovy)
# Delete any previously existing script.
curl -H "Authorization: ${NEXUS_AUTHORIZATION}" \
-o /dev/null \
-s \
-w "${http_code}" \
-X DELETE \
"${NEXUS_LOCAL_HOST_PORT}${NEXUS_CONTEXT}/service/rest/v1/script/${NAME}/"
# Install the script.
curl -d "{\"name\":\"${NAME}\",\"type\":\"groovy\",\"content\":\"${BODY}\"}" \
-H "Authorization: ${NEXUS_AUTHORIZATION}" \
-H 'Content-Type: application/json' \
-s \
-S \
-X POST \
"${NEXUS_LOCAL_HOST_PORT}${NEXUS_CONTEXT}/service/rest/v1/script/"
}
function manage_repos { # Supported actions are 'start' and 'stop'.
local REPOS=($OFFLINE_REPOS)
for repo in "${REPOS[@]}";
do
curl -d "${repo}" \
-H "Authorization: ${NEXUS_AUTHORIZATION}" \
-H "Content-Type: text/plain" \
-s \
-X POST \
"${NEXUS_LOCAL_HOST_PORT}${NEXUS_CONTEXT}/service/rest/v1/script/${1}-repository/run" > /dev/null
done
}
function maybe_start_backup {
# Check for the presence of a lock file with size greater than zero. If it
# exists it means that a backup process is already in place, and we should
# not start a new one.
echo "==> Checking if a backup is currently in progress."
if [[ -s "${LOCK_FILE}" ]]; then
echo "(!) A lock file is present."
echo "(!) Maybe another backup process is already in place."
echo "(!) The timeout for the backup process is $((LOCK_TIMEOUT / 3600))h."
return 1;
fi
echo "==> Checking whether Nexus is reachable."
nc -z -w1 "${NEXUS_LOCAL_HOST_PORT}"
local EXIT_CODE=$?
if [ ${EXIT_CODE} -ne 0 ]; then
echo "(!) Nexus isn't responding. Maybe it's starting up or a backup procedure is in place."
return 1;
fi
echo "==> Starting the backup procedure @ $(date)."
backup & wait
echo "==> Finished the backup procedure @ $(date)."
}
function compare_and_sleep {
# Check for the presence of a lock file with size greater than zero.
if [[ -s "${LOCK_FILE}" ]]; then
# Read the timestamp it contains.
LOCK_TIMESTAMP="$(cat "${LOCK_FILE}")"
# Grab the current timestamp.
THIS_TIMESTAMP="$(date +%s)"
# Compare the difference between the timestamps with the value of
# LOCK_TIMEOUT. If this difference is greater than the timeout it
# means that the previous backup process did not complete.
if (( THIS_TIMESTAMP - LOCK_TIMESTAMP > LOCK_TIMEOUT )); then
echo "(!) Found a stale lock file."
echo "(!) The previous backup did not complete successfully."
echo "(!) The lock file will now be removed so that backups can proceed."
rm -f "${LOCK_FILE}"
fi
fi
sleep "${LOCK_CHECK_INTERVAL}"
}
function monitor_lock_file {
while true; do
compare_and_sleep
done
}
if [[ -z "${NEXUS_AUTHORIZATION}" ]];
then
echo "Nexus authorization token is not defined."
exit 1
fi
if [[ ! -d "${NEXUS_BACKUP_DIRECTORY}" ]];
then
echo "Backup directory not present. Is the volume mounted?"
exit 1
fi
if [[ ! -d "${NEXUS_DATA_DIRECTORY}" ]];
then
echo "Data directory not present. Is the volume mounted?"
exit 1
fi
if [[ -z "${NEXUS_LOCAL_HOST_PORT}" ]];
then
echo "Nexus pod-local host and port are not defined."
exit 1
fi
if [[ -z "${TARGET_BUCKET}" ]];
then
echo "Target GCS bucket is not defined."
exit 1
fi
if [[ -z "${GRACE_PERIOD}" ]];
then
echo "Grace period is not defined."
exit 1
fi
if [[ -f "${CLOUD_IAM_SERVICE_ACCOUNT_KEY_PATH}" ]];
then
echo "==> Setting up authentication with the specified service account..."
gcloud auth activate-service-account --key-file "${CLOUD_IAM_SERVICE_ACCOUNT_KEY_PATH}" || \
{
echo "(!) Couldn't activate the specified service account."
exit 1
}
fi
monitor_lock_file &
echo "==> Monitoring '${NEXUS_BACKUP_DIRECTORY}/${TRIGGER_FILE_NAME}'..."
inotifywait -e attrib,create --format "%f" -m -q "${NEXUS_BACKUP_DIRECTORY}" | while read -r FILE
do
if [[ "${FILE}" == "${TRIGGER_FILE_NAME}" ]];
then
maybe_start_backup
fi
done