-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbackupdate.sh
542 lines (455 loc) · 15.3 KB
/
backupdate.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
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
#!/bin/bash
version="1.2.0"
# Copyright (c) 2024 hazzuk
# This Source Code Form is subject to the terms of the Mozilla Public
# License, v. 2.0. If a copy of the MPL was not distributed with this
# file, You can obtain one at https://mozilla.org/MPL/2.0/.
# _ _ _ _
# ___ ___ _____ ___ ___ ___ ___ ___| |_ ___ ___| |_ _ _ ___ _| |___| |_ ___
# | _| . | | . | . |_ -| -_|___| . | .'| _| '_| | | . | . | .'| _| -_|
# |___|___|_|_|_| _|___|___|___| |___|__,|___|_,_|___| _|___|__,|_| |___|
# |_| |_|
#
# Bash script for creating scheduled backups, and performing (backed-up) guided updates on Docker compose stacks
# https://github.com/hazzuk/compose-backupdate
# exit on any error
set -e
# check if running as root
if [ "$EUID" -ne 0 ]; then
echo "Error, please run as root!"
exit 1
fi
# variables
# ---
# required
backup_dir=${BACKUP_DIR:-"null"} # -b "/opt/backup"
docker_dir=${DOCKER_DIR:-"null"} # -d "/opt/docker"
stack_name=${STACK_NAME:-"null"} # -s "nginx"
# optional
backup_blocklist=${BACKUP_BLOCKLIST:-"null"} # -l "media_vol,/media-bind"
update_requested=false # -u
version_requested=false # -v
# internal
timestamp=$(date +"%Y%m%d-%H%M%S")
stack_running=false
working_dir="null"
volume_blocklist=()
path_blocklist=()
running_container_ids=""
running_container_names=""
# script
# ---
main() {
# script version check
if [ "$version_requested" = true ]; then
script_update_check
exit 0
fi
# check current directory for compose file
docker_stack_dir
# check script variables before continuing
verify_config
# create backup directory
mkdir -p "$backup_dir/$stack_name" || { echo "Error, failed to create backup directory $backup_dir!"; exit 1; }
# stop stack before backup
echo "(stop)"
docker_stack_stop
# backup compose stack working directory
echo "(backups)"
backup_working_dir
# backup docker volumes
backup_stack_volumes
# update if requested
if [ "$update_requested" = true ]; then
echo "(updates)"
# print stack changelog url
# print_changelog_url
# update compose stack
docker_stack_update
fi
# restart stack again if previously running
echo "(restart)"
docker_stack_start
# prune unused docker images
if [ "$update_requested" = true ]; then
echo "(prune)"
# new images must be associated with a running stack
if [ "$stack_running" = true ]; then
docker_image_prune
else
echo "- Docker stack was not recreated, skipping image prune"
echo
fi
fi
echo -e "backupdate complete!\n\n"
exit 0
}
# utilities
# ---
usage() {
echo "Usage: $0 [-b backup_dir] [-d docker_dir] [-s stack_name] [-l backup_blocklist] [-u] [-v]"
echo " --backup-dir --docker-dir --stack-name --backup-blocklist --update --version"
exit 1
}
parse_args() {
local OPTIONS=b:d:s:l:uv
local LONGOPTS=backup-dir:,docker-dir:,stack-name:,backup-blocklist:,update,version
# parse options
if ! PARSED=$(getopt --options=$OPTIONS --longoptions=$LONGOPTS --name "$0" -- "$@"); then
exit 2
fi
# evaluate parsed options
eval set -- "$PARSED"
# Now handle the options
while true; do
case "$1" in
-b|--backup-dir)
backup_dir="$2"
shift 2
;;
-d|--docker-dir)
docker_dir="$2"
shift 2
;;
-s|--stack-name)
stack_name="$2"
shift 2
;;
-l|--backup-blocklist)
backup_blocklist="$2"
shift 2
;;
-u|--update)
update_requested=true
shift
;;
-v|--version)
version_requested=true
shift
;;
--)
shift
break
;;
*)
echo "Unknown option: $1"
exit 3
;;
esac
done
}
verify_config() {
# check required inputs
if [ "$backup_dir" = "null" ]; then
echo "Error, backup_dir not provided!"
usage
fi
if [ "$working_dir" = "null/$stack_name" ]; then
echo "Error, docker_dir not provided!"
usage
fi
if [ "$stack_name" = "null" ]; then
echo "Error, stack_name not provided!"
usage
fi
if [ "$working_dir" = "null" ]; then
echo "Error, working_dir not set!"
exit 1
fi
# echo script config
echo "backupdate <$stack_name> $timestamp"
echo "- backup_dir: $backup_dir"
echo "- working_dir: $working_dir"
# check backup blocklist
if [ "$backup_blocklist" != "null" ]; then
# convert string to array
IFS=',' read -r -a blockarray <<< "$backup_blocklist"
# process items in array
for item in "${blockarray[@]}"; do
if [[ $item == /* ]]; then
# item starts with slash
item="${item#/}"
path_blocklist+=("$item")
else
volume_blocklist+=("$item")
fi
done
# echo volume blocklist
if [ ${#volume_blocklist[@]} -gt 0 ]; then
echo "- volume_blocklist:"
for vol in "${volume_blocklist[@]}"; do
echo -e "\t- $vol"
done
fi
# echo path blocklist
if [ ${#path_blocklist[@]} -gt 0 ]; then
echo "- path_blocklist:"
for path in "${path_blocklist[@]}"; do
echo -e "\t- $path"
done
fi
fi
echo
}
confirm() {
local prompt="$1"
read -r -p "${prompt} (y/N): " confirm
if [[ "$confirm" == "y" || "$confirm" == "Y" ]]; then
return 0 # true
else
return 1 # false
fi
}
script_update_check() {
local repo="hazzuk/compose-backupdate"
local raw_url="https://raw.githubusercontent.com/$repo/refs/heads/release/backupdate.sh"
local latest_version_line
local latest_version
# fetch second line (version="X.Y.Z") from the script hosted on github
latest_version_line=$(curl -s "$raw_url" | sed -n '2p')
# extract version from the fetched line
latest_version=$(echo "$latest_version_line" | grep -oP '(?<=version=")[^"]+')
if [[ $latest_version == "" ]]; then
echo "Warn, could not check for updates"
return 0
fi
# compare local version with the latest version
if [[ "$version" != "$latest_version" ]]; then
echo "A new version (v$latest_version) is available! You are using backupdate-v$version"
else
echo "Running backupdate-v$version"
fi
}
# docker
# ---
docker_stack_stop() {
local container_ids
local container_names
echo "Stopping <$stack_name> containers"
cd "$working_dir" || exit
# for updates, require stack to be removed
if [ "$update_requested" = true ]; then
stack_running=false
# stop stack
echo "- Update requested, removing all stack containers"
docker compose down
else
# check stack running, with at least one container running
if docker compose ls --quiet --filter "name=$stack_name" | grep -q "$stack_name"; then
stack_running=true
# get running containers ids
container_ids=$(docker compose ps --quiet --filter "status=running")
# get running containers names
if [ -n "$container_ids" ]; then
# shellcheck disable=SC2086
container_names=$(docker inspect --format '{{.Name}}' $container_ids | sed 's|^/||' | tr -d '\r')
# print container names
for name in $container_names; do
echo "- $name"
done
# set global variables
running_container_ids=$container_ids
running_container_names=$container_names
else
echo "Error, stack running but no container IDs found!"
exit 1
fi
# stop stack
docker compose --progress "quiet" stop
else
stack_running=false
echo "- Docker stack <$stack_name> not running, skipping docker stop"
fi
fi
echo
}
docker_stack_start() {
# check stack was previously running
if [ "$stack_running" = true ]; then
cd "$working_dir" || exit
echo "Resuming <$stack_name> containers"
# restart only previously running containers
if [ -n "$running_container_ids" ]; then
# print container names
for name in $running_container_names; do
echo "- $name"
done
# restart containers
echo -e "\nInfo, restarted container IDs"
# shellcheck disable=SC2086
docker start $running_container_ids
fi
else
# for updates, stack should be recreated with updated images
if [ "$update_requested" = true ]; then
if confirm "Do you want to recreate <$stack_name>'s containers now?"; then
echo "- Recreating Docker stack..."
docker compose up -d
# new stack running, can prune unused images
stack_running=true
else
echo "- Stack recreation canceled"
fi
# stack was not previously running
else
echo "- Docker stack <$stack_name> not previously running, skipping docker start"
fi
fi
echo
}
docker_stack_dir() {
# possible compose file names
local compose_files=("compose.yaml" "compose.yml" "docker-compose.yaml" "docker-compose.yml")
# current directory name
local current_dir
current_dir=$(basename "$PWD") # "nginx"?
# check neither $docker_dir or $stack_name were provided
if [[ "$docker_dir" == "null" && "$stack_name" == "null" ]]; then
echo "Info, neither docker_dir or stack_name were provided, using current directory"
# but if $docker_dir was provided alone (likely as an environment variable), and is correct
elif [[ "$docker_dir/$current_dir" = "$(pwd)" && "$stack_name" == "null" ]]; then
echo "Info, stack_name was not provided, using current directory"
# otherwise something was provided, do not use current directory
else
# update working_dir with provided options
working_dir="$docker_dir/$stack_name"
return 0
fi
# search current directory for compose file
for file in "${compose_files[@]}"; do
if [[ -f "$file" ]]; then
# update working_dir and stack_name to current directory
working_dir="$(pwd)"
stack_name=$current_dir
echo -e "Found <$stack_name> $file in current directory\n "
return 0
fi
done
echo "Error, compose file not found in current directory!"
usage
}
docker_stack_update() {
if confirm "Are you sure you want to update <$stack_name>?"; then
echo "- Updating Docker stack..."
docker compose pull
else
echo "- Update canceled"
fi
echo
}
docker_image_prune() {
local docker_images
local docker_images_unused=()
echo "Searching for unused docker images..."
# collect docker images output
docker_images=$(
docker images --format "table {{.ID}}\t{{.Repository}}\t{{.Tag}}\t{{.Size}}" | \
tail -n +2
)
# process images output
while read -r image_id repository tag size; do
# skip unused busybox image
if [[ "$repository" == "busybox" ]]; then
continue
fi
# check the image is not being used by a running/stopped container
if [[ -z $(docker ps -a --filter "ancestor=$image_id" --format '{{.ID}}') ]]; then
# append unused image_id to array
docker_images_unused+=("$image_id")
# print unused image details
printf "%-16s %-45s %-10s\n" "- $image_id" "$repository:$tag" "$size"
fi
done <<< "$docker_images" # avoid subshell
# check no unused images found
if [[ ${#docker_images_unused[@]} -eq 0 ]]; then
echo -e "- No unused images found\n "
return 0
else
# prompt user for confirmation before proceeding
if confirm "Do you want to prune unused images?"; then
# prune unused images
for image_id in "${docker_images_unused[@]}"; do
echo "- Removing $image_id"
docker rmi "$image_id" -f
done
else
echo "- Prune cancelled"
fi
fi
echo
}
# backups
# ---
backup_working_dir() {
local exclude_opts=""
local exclude_info=""
echo "Backup <$stack_name> directory: $working_dir"
# set blocklist options
if [ ${#path_blocklist[@]} -gt 0 ]; then
for path in "${path_blocklist[@]}"; do
exclude_opts+="--exclude=$path "
exclude_info+="$path "
done
echo "- Skipping blocklisted paths: $exclude_info"
fi
# create archive with exclude options
eval tar -czf "$backup_dir/$stack_name/d-$stack_name-$timestamp.tar.gz" "$exclude_opts" -C "$working_dir" .
echo "- Directory backup complete"
}
backup_stack_volumes() {
# get all stack volumes
local stack_volumes
stack_volumes=$(
docker volume ls --filter "label=com.docker.compose.project=$stack_name" --format "{{.Name}}"
)
# check volumes found
if [ -z "$stack_volumes" ]; then
echo -e "Info, no related volumes found for <$stack_name>\n "
return 0
fi
# backup each volume
for volume_name in $stack_volumes; do
echo "Backup volume: <$volume_name>"
# skip blocklisted volumes
if [[ " ${volume_blocklist[*]} " == *" $volume_name "* ]]; then
echo "- Skipping blocklisted volume"
continue
fi
# create backup
backup_volume "$volume_name"
done
echo
}
backup_volume() {
local volume_name=$1
# backup volume data with temporary container
docker run --rm \
-v "$volume_name":/volume_data \
-v "$backup_dir":/backup \
busybox tar czf "/backup/$stack_name/v-$volume_name-$timestamp.tar.gz" -C /volume_data . || \
{ echo "Error, failed to create busybox backup container!"; exit 1; }
echo "- Volume backup complete"
}
# print_changelog_url() {
# local changelog_file="$working_dir/changelog.url"
# # check changelog.url exists
# if [[ -f "$changelog_file" ]]; then
# echo "Link to read the <$stack_name> changelog: "
# cat "$changelog_file"
# else
# # ask user to create changelog.url
# echo "File $changelog_file does not exist"
# read -r -p "Please provide a URL (or press Enter to continue without): " user_input
# if [[ $user_input == http* ]]; then
# # create changelog.url with user input
# echo "$user_input" > "$changelog_file"
# echo "- $changelog_file created"
# else
# echo "- No valid URL provided. Continuing without the <$stack_name> changelog"
# fi
# fi
# }
# run script
# ---
parse_args "$@"
main