-
Notifications
You must be signed in to change notification settings - Fork 4
/
entrypoint
executable file
·625 lines (483 loc) · 16.8 KB
/
entrypoint
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
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
#!/bin/bash
DEBUG=${DEBUG:-}
if [ x"$DEBUG" = x"ECHO" ] ; then
set -x
fi
set -e
#
# Backup Mode. Possible values:
# - SIMPLE: Create ${TARGET_PREFIX}/${DATE}
# - FULL: Create ${TARGET_PREFIX}/full-${DATE}
# - INCREMENTAL: Create ${TARGET_PREFIX}/full-{LASTDATE}-inc-${DATE} based on the last full-${DATE}
#
BACKUP_MODE="${BACKUP_MODE:-SIMPLE}"
RUN_EVERY="${RUN_EVERY:-}"
RESTORE_STACK="${RESTORE_STACK:-}"
RANCHER_URL="${RANCHER_URL:-}"
RANCHER_ACCESS_KEY="${RANCHER_ACCESS_KEY}"
RANCHER_SECRET_KEY="${RANCHER_SECRET_KEY}"
BACKUP_VOLUME="${BACKUP_VOLUME:-}"
BACKUP_DRIVER="${BACKUP_DRIVER:-}"
STORAGEBOX_URL="${STORAGEBOX_URL:-}"
STORAGEBOX_USER="${STORAGEBOX_USER:-}"
STORAGEBOX_PASSWORD="${STORAGEBOX_PASSWORD:-}"
MYSQL_PASSWORD="${MYSQL_PASSWORD:-}"
MYSQL_HOST="${MYSQL_HOST:-}"
MYSQL_PORT="${MYSQL_PORT}"
RANCHER_CLI_VERSION="${RANCHER_CLI_VERSION:-v0.4.1}"
KEEP_BACKUPS="${KEEP_BACKUPS:-4}"
BACKUP_DIRECTORY="/target"
DATA_DIRECTORY="/var/lib/mysql"
#
# Find the directory name of the latest full backup
#
find_last_full_backup() {
FILES=( $(ls -d "${BACKUP_DIRECTORY}/"full-????-??-??-??????) )
basename ${FILES[-1]}
return
}
download_rancher_compose() {
RANCHER_COMPOSE_VERSION="$1"
if [ -z "${RANCHER_COMPOSE_VERSION}" ] ; then
RANCHER_COMPOSE_VERSION="0.12.2"
fi
RANCHER_COMPOSE="rancher-compose-${RANCHER_COMPOSE_VERSION}"
TARGET_RANCHER_COMPOSE="/usr/local/bin/$RANCHER_COMPOSE"
if [ ! -f "$TARGET_RANCHER_COMPOSE" ] ; then
echo "rancher-compose version $RANCHER_COMPOSE_VERSION not found. Downloading."
curl "https://releases.rancher.com/compose/v${RANCHER_COMPOSE_VERSION}/rancher-compose-linux-amd64-v${RANCHER_COMPOSE_VERSION}.tar.gz" \
| gunzip | tar -C /tmp -x
cp /tmp/rancher-compose-v${RANCHER_COMPOSE_VERSION}/rancher-compose "$TARGET_RANCHER_COMPOSE"
chmod +x "$TARGET_RANCHER_COMPOSE"
echo "Installed Rancher compose $RANCHER_COMPOSE_VERSION to $TARGET_RANCHER_COMPOSE"
fi
return
}
retrieveDockerCompose() {
BACKUP_NAME="$1"
cd /tmp/
curl -o /tmp/docker-compose.yml.tpl http://rancher-metadata/2015-12-19/self/service/metadata/docker-compose
ALPHANUM_TARGET_BACKUP=$(echo ${BACKUP_NAME} | sed -e 's/[^a-zA-Z0-9\-]//g')
sed \
-e "s~%%BACKUP%%~$BACKUP_NAME~" \
-e "s~%%BACKUP_ALPHANUM%%~$ALPHANUM_TARGET_BACKUP~" \
-e "s~%%RANCHER_URL%%~${RANCHER_URL}~" \
-e "s~%%RANCHER_KEY%%~${RANCHER_ACCESS_KEY}~" \
-e "s~%%RANCHER_SECRET%%~${RANCHER_SECRET_KEY}~" \
-e "s~%%BACKUP_VOLUME%%~${BACKUP_VOLUME}~" \
-e "s~%%BACKUP_DRIVER%%~${BACKUP_DRIVER}~" \
-e "s~%%STORAGEBOX_URL%%~${STORAGEBOX_URL}~" \
-e "s~%%STORAGEBOX_USER%%~${STORAGEBOX_USER}~" \
-e "s~%%STORAGEBOX_PASSWORD%%~${STORAGEBOX_PASSWORD}~" \
/tmp/docker-compose.yml.tpl > /tmp/docker-compose.yml
return
}
createRestoreService() {
BACKUP_NAME="$1"
if [ -z "${RESTORE_STACK}" ] ; then
echo "RESTORE_STACK environment variable not set, not creating a restore service"
return
fi
echo "======================================================================"
echo "= Creating restore service for $BACKUP_NAME"
echo "======================================================================"
echo ""
OLDDIR=$(pwd)
retrieveDockerCompose "${BACKUP_NAME}"
download_rancher_compose ${RANCHER_COMPOSE_VERSION}
echo "Using ${RANCHER_COMPOSE} as rancher-compose executable"
${RANCHER_COMPOSE} -p "${RESTORE_STACK}" create
cd "$OLDDIR"
return
}
removeRestoreService() {
BACKUP_NAME="$1"
if [ -z "${RESTORE_STACK}" ] ; then
echo "RESTORE_STACK environment variable not set, not removing a restore service"
return
fi
echo "======================================================================"
echo "= Removing restore service for ${BACKUP_NAME}"
echo "======================================================================"
echo ""
OLDDIR=$(pwd)
cd /tmp/
retrieveDockerCompose "${BACKUP_NAME}"
download_rancher_compose ${RANCHER_COMPOSE_VERSION}
echo "Using ${RANCHER_COMPOSE} as rancher-compose executable"
#
# This will not remove the whole stack as a custom docker-compose.yml is written with only this service in it
#
${RANCHER_COMPOSE} -p "${RESTORE_STACK}" rm
cd "$OLDDIR"
return
}
setDefaults() {
if [ -z "${MYSQL_USER}" ] ; then
echo "WARNING: Running with default user."
else
echo "Connecting as ${MYSQL_USER}"
USER=(--user "${MYSQL_USER}")
fi
if [ -z "${MYSQL_PASSWORD}" ] ; then
echo "WARNING: Running without password."
else
echo "Connecting with password"
PASSWORD=(--password "${MYSQL_PASSWORD}")
fi
if [ -z "${MYSQL_HOST}" ] ; then
echo "WARNING: Connecting to default host 'source', make sure to set links"
MYSQL_HOST="target"
else
echo "Connecting to host $MYSQL_HOST"
fi
if [ -z "$MYSQL_PORT" ] ; then
MYSQL_PORT="3306"
echo "WARNING: Connecting to default port '3306'"
else
echo "Connecting to port $MYSQL_PORT"
fi
return
}
backupRancher() {
echo "Backing up rancher stack"
local METADATA_URL="http://rancher-metadata/2015-07-25"
local STACK=$(curl -s $METADATA_URL/self/service/links | sed 's/%2F.*//')
local SERVICE=$(curl -s $METADATA_URL/self/service/links | sed 's/[^%]*%2F//')
local TARGET_DIRECTORY="$1"
if [ -z "$RANCHER_URL" -o -z "$RANCHER_ACCESS_KEY" -o -z "$RANCHER_SECRET_KEY" ] ; then
echo "No rancher access data set, not backing up {docker,rancher}-compose.yml files"
return
fi
RANCHER_CLI_DIRECTORY="/opt/rancher/${RANCHER_CLI_VERSION}"
if [ ! -d "$RANCHER_CLI_DIRECTORY" ] ; then
mkdir -p "$RANCHER_CLI_DIRECTORY"
fi
if [ ! -f "$RANCHER_CLI_DIRECTORY/rancher" ] ; then
echo "Downloading rancher cli $RANCHER_CLI_VERSION"
curl -s "https://releases.rancher.com/cli/${RANCHER_CLI_VERSION}/rancher-linux-amd64-${RANCHER_CLI_VERSION}.tar.gz" \
| tar xvz -C "$RANCHER_CLI_DIRECTORY" --strip-components=2
fi
$RANCHER_CLI_DIRECTORY/rancher export "${STACK}"
cp ${STACK}/{docker,rancher}-compose.yml "${TARGET_DIRECTORY}/"
echo "{\"stack\":\"${STACK}\",\"service\":\"${SERVICE}\"}" > "${TARGET_DIRECTORY}/backup.json"
return
}
usage() {
echo "======================================================================"
echo "= Usage"
echo "======================================================================"
echo "docker run ipunktbs/xtrabackup -v "name-backup-directory:${BACKUP_DIRECTORY}" [COMMAND]"
echo ""
echo "The ipunktbs/xtrabackup docker image tries to provide percona xtrabackup for use in rancher environments"
echo ""
echo "======================================================================"
echo "= Commands"
echo "======================================================================"
echo "- backup"
echo " Does a backup, prepare, prepare cycle from /var/lib/mysql to ${BACKUP_DIRECTORY}/YY-mm-dd-HH_ii"
echo " If the environment variable RUN_EVERY is set then backups will continue to be made, with"
echo " 'sleep \$RUN_EVERY' in between"
echo ""
echo "- restore YY-mm-dd-HH_ii"
echo " Does a copy-back ${BACKUP_DIRECTORY}/YY-mm-dd-HH_ii to /var/lib/mysql"
echo ""
echo "- run COMMAND"
echo " Runs the given command within the container."
exit 0
}
backup() {
RUNNING="true"
while [ x"$RUNNING" = x"true" ] ; do
DATE=$(date '+%Y-%m-%d-%H%M%S')
BACKUP_NAME="${DATE}"
if [ x"${BACKUP_MODE}" = x"FULL" ] ; then
echo "======================================================================"
echo "= Full backup process"
echo "======================================================================"
echo ""
BACKUP_NAME="full-${DATE}"
fi
if [ x"${BACKUP_MODE}" = x"INCREMENTAL" ] ; then
echo "======================================================================"
echo "= Incremental backup process"
echo "======================================================================"
echo ""
BASE_BACKUP_NAME="$(find_last_full_backup)"
BASE_BACKUP_PATH="${BACKUP_DIRECTORY}/${BASE_BACKUP_NAME}"
BACKUP_NAME="${BASE_BACKUP_NAME}-inc-$DATE"
echo "Base backup: ${BASE_BACKUP_NAME}"
echo ""
fi
BACKUP_PATH="${BACKUP_DIRECTORY}/$BACKUP_NAME"
echo "======================================================================"
echo "= Starting backup process to $BACKUP_PATH"
echo "======================================================================"
echo ""
setDefaults
if [ ! -d "$BACKUP_PATH" ] ; then
echo "$BACKUP_PATH does not exist yet, creating"
mkdir -p "$BACKUP_PATH"
fi
echo "Creating backup in $BACKUP_PATH"
backupRancher $BACKUP_PATH
if [ x"${BACKUP_MODE}" = x"INCREMENTAL" ] ; then
# Backup - copies innodb files and reads binary log while doing so
xtrabackup --backup --datadir="$DATA_DIRECTORY" \
--incremental-basedir="${BASE_BACKUP_PATH}" \
--target-dir="$BACKUP_PATH" ${USER[@]} ${PASSWORD[@]} \
--host "${MYSQL_HOST}" --port ${MYSQL_PORT}
else
# Backup - copies innodb files and reads binary log while doing so
xtrabackup --backup --datadir="$DATA_DIRECTORY" \
--target-dir="$BACKUP_PATH" ${USER[@]} ${PASSWORD[@]} \
--host "${MYSQL_HOST}" --port ${MYSQL_PORT}
fi
if [ x"${BACKUP_MODE}" = x"SIMPLE" ] ; then
echo "Mode is SIMPLE, preparing backups"
# First prepare - selfheal innodb and apply binary log to make it consistent
xtrabackup --prepare --target-dir="$BACKUP_PATH"
# Second prepare - write logfiles for faster server startup
xtrabackup --prepare --target-dir="$BACKUP_PATH"
fi
echo "$BACKUP_PATH Done"
createRestoreService "$BACKUP_NAME"
if [ ! -z "${RUN_EVERY}" ] ; then
echo "Sleeping for ${RUN_EVERY} before starting again"
sleep "${RUN_EVERY}"
else
echo "Environment variable RUN_EVERY was not set, not looping"
RUNNING="false"
fi
done
exit 0
}
#
# Input:
# - $1 Incremental Backup directory
# Output:
# - Full backup which is the base of the incremental backup
#
parse_full_backup_base() {
sed 's~^\(full-[0-9][0-9][0-9][0-9]-[0-9][0-9]-[0-9][0-9]-[0-9][0-9][0-9][0-9][0-9][0-9]\).*~\1~' <<< "$1"
return
}
#
# Parameter
# $1 - TARGET_PREFIX - The prefix(directory) in which the NAME directory lies
# $2 - NAME - The name of the backup to be restored
# $3 - RESTORE_DIRECTORY - the temporary directory in which the backup should be restored
#
#
restore_incremental() {
local BACKUP_DIRECTORY="$1"
local BACKUP_NAME="$2"
local RESTORE_DIRECTORY="$3"
local BACKUP_PATH="${BACKUP_DIRECTORY}/${BACKUP_NAME}"
local BACKUP_INCREMENT_PATH="/tmp/${BACKUP_NAME}-inc1"
echo "======================================================================"
echo "= Incremental full backup ${BACKUP_PATH}"
echo "======================================================================"
echo ""
BASE_BACKUP_NAME="$(parse_full_backup_base ${BACKUP_NAME})"
BASE_BACKUP_PATH="${BACKUP_DIRECTORY}/${BASE_BACKUP_NAME}"
# Copy the full backup - preparing incremental backups would otherwise overwrite the full backup
if [ ! -d "${BASE_BACKUP_PATH}" ] ; then
echo "E> Base backup ${BASE_BACKUP_PATH} does not exist. Aborting."
exit 1
fi
echo "Copying full backup"
cp -Rfv "${BASE_BACKUP_PATH}" "${RESTORE_DIRECTORY}"
#
# Copying the incremental backup. Xtrabackup accesses a lot of files at the same time. Backups are usually on a slow
# medium which might not allow such access
#
echo "Copying incremental Backup"
cp -Rfv "${BACKUP_PATH}" "${BACKUP_INCREMENT_PATH}"
# Prepare the full backup
echo "Preparing ${BASE_BACKUP_NAME}"
xtrabackup --prepare --apply-log-only --target-dir="$RESTORE_DIRECTORY"
# apply the incremental backup
echo "Preparing ${BACKUP_NAME}"
xtrabackup --prepare --target-dir="$RESTORE_DIRECTORY" --incremental-dir="${BACKUP_INCREMENT_PATH}"
echo "Done preparing"
# copying back the prepared directory to the data directory is done in restore() as it is a shared step
return
}
#
# Restore a full backup
#
restore_full() {
local BACKUP_DIRECTORY="$1"
local BACKUP_NAME="$2"
local RESTORE_DIRECTORY="$3"
local BACKUP_PATH="${BACKUP_DIRECTORY}/${BACKUP_NAME}"
echo "======================================================================"
echo "= Restoring full backup ${BACKUP_PATH}"
echo "======================================================================"
echo ""
# Copy the backup to a temporary restore directory. Otherwise the the backup would be prepared and it would no longer
# be possible to use it as base to restore incremental backups
cp -Rfv "${BACKUP_PATH}" "${RESTORE_DIRECTORY}"
# Prepare the backup
xtrabackup --prepare --target-dir="$RESTORE_DIRECTORY"
# copying back the prepared directory to the data directory is done in restore() as it is a shared step
return
}
#
# Restore $1
#
# Parameter
# $1 - BACKUP_DIRECTORY
# $2 - BACKUP_NAME
#
#
restore() {
local BACKUP_DIRECTORY="$1"
local BACKUP_NAME="$2"
local BACKUP_PATH="${BACKUP_DIRECTORY}/${BACKUP_NAME}"
echo "======================================================================"
echo "= Starting restore process from ${BACKUP_PATH}"
echo "======================================================================"
echo ""
if [ -z "$BACKUP_NAME" ] ; then
usage
echo "E> Error: no target backup given to"
exit 1
fi
if [ ! -d "${BACKUP_PATH}" ] ; then
echo "E> Directory '${BACKUP_PATH}' does not exist. Backup restore failed."
exit 2
fi
# Create the TEMPORARY_RESTORE_PATH and make sure it is empty
TEMPORARY_RESTORE_PATH="/tmp/${BACKUP_NAME}"
if [ -d "${TEMPORARY_RESTORE_PATH}" ] ; then
echo "Restore directory ${TEMPORARY_RESTORE_PATH} already exists. Deleting"
rm -Rf "${TEMPORARY_RESTORE_PATH}"
fi
local REGEX='^full-.*-inc-.*'
if [[ "${BACKUP_NAME}" =~ $REGEX ]] ; then
restore_incremental "$BACKUP_DIRECTORY" "$BACKUP_NAME" "${TEMPORARY_RESTORE_PATH}"
else
restore_full "$BACKUP_DIRECTORY" "${BACKUP_NAME}" "${TEMPORARY_RESTORE_PATH}"
fi
# Copy back the prepared data
xtrabackup --copy-back --datadir="$DATA_DIRECTORY" --target-dir="${TEMPORARY_RESTORE_PATH}"
echo "Done"
exit 0
}
get_backup_date() {
local BACKUP_NAME="$1"
sed 's~full-\([0-9][0-9][0-9][0-9]-[0-9][0-9]-[0-9][0-9]\)-[0-9][0-9][0-9][0-9][0-9][0-9]~\1~' <<< "${BACKUP_NAME}"
return
}
cleanup() {
echo "======================================================================"
echo "= Starting cleanup process"
echo "======================================================================"
echo ""
IFS='
'
local FULL_BACKUPS=( $(find "${BACKUP_DIRECTORY}" -mindepth 1 -maxdepth 1 -name 'full-????-??-??-??????' -printf '%f\n' | sort -d -r) )
local NUM_FULL_BACKUPS="${#FULL_BACKUPS[@]}"
echo "Found ${NUM_FULL_BACKUPS} full backups"
echo "Keeping ${KEEP_BACKUPS} backups"
if [ "${NUM_FULL_BACKUPS}" -le "${KEEP_BACKUPS}" ] ; then
echo "No more than ${KEEP_BACKUPS} full backups found."
echo "Exiting."
exit 0
fi
index="${KEEP_BACKUPS}"
while [ "$index" -lt "${NUM_FULL_BACKUPS}" ] ; do
local BACKUP_NAME="${FULL_BACKUPS[$index]}"
echo "Removing $index: ${BACKUP_NAME}"
remove ${BACKUP_NAME}
index=$(( ++index ))
done
return
}
#
# Remove the full backup given as $1, all its incremental backups and their respective services.
#
remove() {
local FULL_BACKUP="$1"
echo "Removing:"
ls -d "${BACKUP_DIRECTORY}/${FULL_BACKUP}"*
for BACKUP_PATH in "${BACKUP_DIRECTORY}/${FULL_BACKUP}"* ; do
local BACKUP_NAME="$(basename $BACKUP_PATH)"
echo "Removing ${BACKUP_NAME} in ${BACKUP_PATH}"
removeRestoreService "${BACKUP_NAME}"
rm -Rf "${BACKUP_PATH}"
done
}
case $1 in
backup)
backup
;;
restore)
shift
restore "${BACKUP_DIRECTORY}" $1
;;
create)
shift
BACKUP_PATH="$1"
echo "======================================================================"
echo "= Creating restore service from $BACKUP_PATH"
echo "======================================================================"
echo ""
createRestoreService "$BACKUP_PATH"
;;
clear)
echo "======================================================================"
echo "= Starting clear process for /var/lib/mysql"
echo "======================================================================"
echo ""
if [ "$2" != "yes" ] ; then
echo "Clearing not confirmed. Please add 'yes' as parameter"
echo "Example: docker run ipunktbs/xtrabackup clear yes"
exit 1
fi
echo "Clearing confirmed, starting"
rm -Rfv /var/lib/mysql/*
echo "Done"
;;
cleanup)
echo "======================================================================"
echo "= Clean up expired backups from ${BACKUP_PATH}"
echo "======================================================================"
echo ""
cleanup
exit 0
;;
run)
shift
echo "======================================================================"
echo "= Starting command $*"
echo "======================================================================"
echo ""
$*
echo "Done"
exit 0
;;
remove)
shift
echo "======================================================================"
echo "= Removing backup $1"
echo "======================================================================"
echo ""
if [ -z "$1" ] ; then
echo "Missing argument: Backup to remove"
exit 1
fi
remove $1
;;
help)
shift
usage
exit 0
;;
*)
usage
exit 0
;;
esac