-
Notifications
You must be signed in to change notification settings - Fork 69
/
vackup
executable file
·233 lines (179 loc) · 5.28 KB
/
vackup
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
#!/bin/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 image management commands for volumes.
export/import copies files between a host tarball and a volume. For making
volume backups and restores.
save/load copies files between an image and a volume. For when you want to use
image registries as a way to push/pull volume data.
Usage:
vackup export VOLUME FILE
Creates a gzip'ed tarball in current directory from a volume
vackup import FILE VOLUME
Extracts a gzip'ed tarball into a volume
vackup save VOLUME IMAGE
Copies the volume contents to a busybox image in the /volume-data directory
vackup load IMAGE VOLUME
Copies /volume-data contents from an image to a 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
/*) ;;
.*) ;& # fallthrough
*) DIRECTORY="$(pwd)/$DIRECTORY" ;;
esac
DIRECTORY=$(readlink -m "$DIRECTORY")
echo "$DIRECTORY"
}
if [ -z "$1" ] || [ "$1" == "-h" ] || [ "$1" == "--help" ]; then
usage
exit 0
fi
cmd_export() {
VOLUME_NAME="$2"
FILE_NAME="$3"
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 a option for overwrite and check if that's set
DIRECTORY=$(fulldirname "$FILE_NAME")
FILE_NAME=$(basename "$FILE_NAME")
if ! docker run --rm \
-v "$VOLUME_NAME":/vackup-volume \
-v "$DIRECTORY":/vackup \
busybox \
tar -zcvf /vackup/"$FILE_NAME" /vackup-volume;
then
error 'Failed to start busybox backup container'
fi
echo "Successfully tar'ed volume $VOLUME_NAME into file $FILE_NAME"
}
cmd_import() {
FILE_NAME="$2"
VOLUME_NAME="$3"
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":/vackup-volume \
-v "$DIRECTORY":/vackup \
busybox \
tar -xvzf /vackup/"$FILE_NAME" -C /;
then
error 'Failed to start busybox container'
fi
echo "Successfully unpacked $FILE_NAME into volume $VOLUME_NAME"
}
cmd_save() {
VOLUME_NAME="$2"
IMAGE_NAME="$3"
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 \
-v "$VOLUME_NAME":/mount-volume \
busybox \
cp -Rp /mount-volume/. /volume-data/;
then
error 'Failed to start busybox container'
fi
CONTAINER_ID=$(docker ps -lq)
docker commit -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="$2"
VOLUME_NAME="$3"
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
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"
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