-
Notifications
You must be signed in to change notification settings - Fork 3
/
clone-docker-volumes.sh
executable file
·59 lines (56 loc) · 2 KB
/
clone-docker-volumes.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
#!/usr/bin/env bash
#########################################################################
#Name: clone-docker-volumes.sh
#Subscription: that sctipt clones docker volumes - Source to Destination.
#by A. Laub
#andreas[-at-]laub-home.de
#
#License:
#This program is free software: you can redistribute it and/or modify it
#under the terms of the GNU General Public License as published by the
#Free Software Foundation, either version 3 of the License, or (at your option)
#any later version.
#This program is distributed in the hope that it will be useful,
#but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
#or FITNESS FOR A PARTICULAR PURPOSE.
#########################################################################
#Set the language
export LANG="en_US.UTF-8"
#Load the Pathes
export PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
#do the stuff
if [ "$1" = "" -o "$2" = "" ]; then
echo -e "Usage: ${0} { SourceVolumeName } { DestinationVolumeName }\n"
echo -e ' use: "docker volume ls"\n for a list of all available docker volume names\n'
exit
fi
# check if Volumes existing
docker volume inspect $1 > /dev/null 2>&1
if [ "$?" != "0" ]
then
echo "The source volume \"$1\" does not exist"
exit
fi
docker volume inspect $2 > /dev/null 2>&1
if [ "$?" != "0" ]
then
echo "The destination volume \"$2\" does not exist"
while true; do
read -p "Would you like to create it? " yn
case $yn in
[Yy]* ) echo "Creating destination volume \"$2\"...";docker volume create --name $2;break;;
[Nn]* ) exit;;
* ) echo "Please answer yes or no.";;
esac
done
fi
# copy the stuff
echo "Copying data from source volume \"$1\" to destination volume \"$2\"..."
docker run --rm \
--name clonevolume \
-i \
-t \
-v $1:/from:ro \
-v $2:/to \
alpine ash -c "cd /from ; cp -av . /to"
echo "Done copying data from source volume \"$1\" to destination volume \"$2\""