forked from BretFisher/docker-vackup
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvackup
More file actions
executable file
·296 lines (230 loc) · 7.51 KB
/
vackup
File metadata and controls
executable file
·296 lines (230 loc) · 7.51 KB
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
#!/usr/bin/env bash
# Docker Volume File Backup and Restore Tool
# Easily tar up a volume on a local (or remote) engine
# Inspired by CLIP from Lukasz Lach
set -Eeo pipefail
handle_error() {
case $# in
1) LINE_NUMBER=$1; EXIT_CODE=$? ;;
2) LINE_NUMBER=$1; EXIT_CODE=$2 ;;
*) LINE_NUMBER=$LINENO; EXIT_CODE=1 ;;
esac
if [ -n "${VACKUP_FAILURE_SCRIPT}" ]; then
/bin/bash "${VACKUP_FAILURE_SCRIPT}" "$LINE_NUMBER" "$EXIT_CODE"
fi
exit "$EXIT_CODE"
}
trap 'handle_error $LINENO' ERR
usage() {
cat <<EOF
"Docker Volume Backup". Replicates container image management commands for container volumes.
export/import copies files between a host tarball and a volume.
For making volume backups and restores on your local file system.
save/load copies files between a container image and a container volume.
For storing container volumes in images and push/pulling to registries.
Usage:
vackup export [OPTIONS] VOLUME FILE
Creates a gzip'ed tarball in current directory from a container volume
Options:
--owner=UID[:GID] Change the ownership of the exported archive on Linux (won't have any effect on macOS)
vackup import FILE VOLUME
Extracts a gzip'ed tarball into a container volume
vackup save VOLUME IMAGE
Copies a container volume to a busybox container image in the /volume-data directory
vackup load IMAGE VOLUME
Copies /volume-data from a container image to a container volume
EOF
}
error() {
if [ "$1" == 'u' ] || [ "$1" == 'usage' ]; then
USAGE=1
MESSAGE=$2
CODE=$3
else
USAGE=0
MESSAGE=$1
CODE=$2
fi
if [ -z "$MESSAGE" ]; then
echo 1>&2 'Error'
else
echo 1>&2 "Error: $MESSAGE"
fi
if [ $USAGE -eq 1 ]; then
usage 1>&2
fi
if [ -z "$CODE" ]; then
CODE=1
fi
LINE_NUMBER=$(caller | awk '{ print $1 }')
handle_error "$LINE_NUMBER" "$CODE"
}
fulldirname() {
DIRECTORY=$(dirname "$1")
case "$DIRECTORY" in
/*) ;;
*) DIRECTORY="$(pwd)/$DIRECTORY" ;;
esac
# Use realpath if available, else fallback to cd/pwd for macOS compatibility
if command -v realpath >/dev/null 2>&1; then
DIRECTORY=$(realpath "$DIRECTORY")
else
DIRECTORY=$(cd "$DIRECTORY" && pwd)
fi
echo "$DIRECTORY"
}
is_digits() {
case "$1" in
*[!0-9]*) return 1 ;;
*) return 0 ;;
esac
}
if [ -z "$1" ] || [ "$1" == "-h" ] || [ "$1" == "--help" ]; then
usage
exit 0
fi
cmd_export() {
OWNER=''
while [ $# -ne 0 ]; do
case "$1" in
-*)
OPTION="$1"
shift
case "$OPTION" in
--owner) error usage "'$OPTION' option requires a value" ;;
--owner=*)
if [ -n "$OWNER" ]; then
error usage "'--owner' option may only be used once"
fi
OWNER=${OPTION#--owner=}
if [ -z "$OWNER" ]; then
error usage "'$OPTION' option requires a non-empty value"
fi
USER=$(echo "$OWNER" | cut -d':' -f1)
GROUP=$(echo "$OWNER" | cut -d':' -f2)
if [ -z "$USER" ]; then
error usage "The UID in the '$OPTION' option can not be empty"
fi
if [ -z "$GROUP" ]; then
GROUP="$USER"
fi
if ! is_digits "$USER" || ! is_digits "$GROUP"; then
error usage "UID:GID in the '$OPTION' option may only contain digits"
fi
OWNER="$USER:$GROUP"
;;
*) error usage "'$OPTION' is not a recognized export option" ;;
esac
;;
*) break ;;
esac
done
VOLUME_NAME="$1"
FILE_NAME="$2"
if [ -z "$VOLUME_NAME" ] || [ -z "$FILE_NAME" ]; then
error usage 'Not enough arguments'
fi
if ! docker volume inspect --format '{{.Name}}' "$VOLUME_NAME";
then
error "Volume $VOLUME_NAME does not exist"
fi
# TODO: check if file exists on host, if it does, create overwrite option and check if set
DIRECTORY=$(fulldirname "$FILE_NAME")
FILE_NAME=$(basename "$FILE_NAME")
if ! docker run --rm \
-v "$VOLUME_NAME":/volume-data \
-v "$DIRECTORY":/mount-volume \
busybox \
/bin/sh -c 'tar -cvzf /mount-volume/'"'$FILE_NAME'"' -C /volume-data . && chown '"'${OWNER:-"$(id -u):$(id -g)"}'"' /mount-volume/'"'$FILE_NAME'";
then
error 'Failed to start busybox backup container'
fi
echo "Successfully tar'ed volume $VOLUME_NAME into file $FILE_NAME"
}
cmd_import() {
FILE_NAME="$1"
VOLUME_NAME="$2"
if [ -z "$VOLUME_NAME" ] || [ -z "$FILE_NAME" ]; then
error usage 'Not enough arguments'
fi
if ! docker volume inspect --format '{{.Name}}' "$VOLUME_NAME";
then
echo "Warning: Volume $VOLUME_NAME does not exist, creating..."
docker volume create "$VOLUME_NAME"
fi
if [ ! -r "$FILE_NAME" ]; then
echo "Error: Could not find or open tar file $FILE_NAME"
exit 1
fi
if [ -d "$FILE_NAME" ]; then
echo "Error: $FILE_NAME is a directory"
exit 1
fi
DIRECTORY=$(fulldirname "$FILE_NAME")
FILE_NAME=$(basename "$FILE_NAME")
if ! docker run --rm \
-v "$VOLUME_NAME":/volume-data \
-v "$DIRECTORY":/mount-volume \
busybox \
tar -xvzf /mount-volume/"$FILE_NAME" -C /volume-data;
then
error 'Failed to start busybox container'
fi
echo "Successfully unpacked $FILE_NAME into volume $VOLUME_NAME"
}
cmd_save() {
VOLUME_NAME="$1"
IMAGE_NAME="$2"
if [ -z "$VOLUME_NAME" ] || [ -z "$IMAGE_NAME" ]; then
error usage 'Not enough arguments'
fi
if ! docker volume inspect --format '{{.Name}}' "$VOLUME_NAME";
then
error "Volume $VOLUME_NAME does not exist"
fi
if ! docker run \
--pull missing \
-v "$VOLUME_NAME":/mount-volume \
busybox \
cp -Rp /mount-volume/. /volume-data/;
then
error 'Failed to start busybox container'
fi
# FIXME: this command assumes that no containers started between the cp and container rm commands.
# It would be safer to capture ID of the busybox container on start, then delete that ID
CONTAINER_ID=$(docker ps -lq)
docker commit -c "LABEL com.docker.desktop.volume-contents.action=true" -m "saving volume $VOLUME_NAME to /volume-data" "$CONTAINER_ID" "$IMAGE_NAME"
docker container rm "$CONTAINER_ID"
echo "Successfully copied volume $VOLUME_NAME into image $IMAGE_NAME, under /volume-data"
}
cmd_load() {
IMAGE_NAME="$1"
VOLUME_NAME="$2"
if [ -z "$VOLUME_NAME" ] || [ -z "$IMAGE_NAME" ]; then
error usage 'Not enough arguments'
fi
if ! docker volume inspect --format '{{.Name}}' "$VOLUME_NAME";
then
echo "Warning: Volume $VOLUME_NAME does not exist, creating..."
docker volume create "$VOLUME_NAME"
fi
# FIXME: this command assumes the image we're copying from has the cp command available
if ! docker run --rm \
-v "$VOLUME_NAME":/mount-volume \
"$IMAGE_NAME" \
cp -Rp /volume-data/. /mount-volume/;
then
error "Failed to start container from $IMAGE_NAME"
fi
echo "Successfully copied /volume-data from $IMAGE_NAME into volume $VOLUME_NAME"
}
COMMAND="$1"
shift
case "$COMMAND" in
export) cmd_export "$@" ;;
import) cmd_import "$@" ;;
save) cmd_save "$@" ;;
load) cmd_load "$@" ;;
*) echo "Error: '$COMMAND' is not a recognized command" ; usage ;;
esac
exit 0