forked from gchudnov/docker-tools
-
Notifications
You must be signed in to change notification settings - Fork 0
/
dcp.sh
executable file
·191 lines (160 loc) · 5.13 KB
/
dcp.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
#!/bin/sh
set -e
# v1.0.1
# The MIT License (MIT)
#
# Copyright (c) 2015 Grigoriy Chudnov
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in all
# copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.
self=$0
# print an error
fatal () {
echo "$0:" "$@" >&2
exit 1
}
# test whether a string contains the given substring
string_contain() { [ -n "$2" ] && [ -z "${2##*$1*}" ]; }
# test for container path
is_container_path() { string_contain ':' "$1"; }
# make directory inside the container
container_mkdir() {
docker exec "$1" sh -c "[ -d $2 ] || mkdir -p $2"
}
# copy host file to container
container_cp() {
cmd="docker exec -i $2 sh -c 'cat > $3' < $1"
eval "$cmd";
}
# transfer file or directory from the host to a container
transfer_host_container() {
item_base_dir=$1
item_path=$2
container_id=$3
container_dir=$4
it_dir=''
if [ -d "$item_path" ]; then
it_dir=$item_path
elif [ -f "$item_path" ]; then
it_dir=$(dirname "$item_path")
it_file=$(basename "$item_path")
fi
if [ "$it_dir" ]; then
dst_dir="$container_dir"${it_dir##*$item_base_dir}
container_mkdir "$container_id" "$dst_dir"
if [ -f "$item_path" ]; then
dst_path="$dst_dir/$it_file"
container_cp "$item_path" "$container_id" "$dst_path"
fi
fi
}
# copy CONTAINER:PATH -> HOSTPATH
copy_container_host() {
docker cp "$1" "$2"
}
# copy HOSTPATH -> CONTAINER:PATH22
copy_host_container() {
src_path=$1
dst_container=${2%%:*}
dst_path=${2##*:}
if [ -f "$src_path" ]; then
transfer_host_container $(dirname "$src_path") "$src_path" "$dst_container" "$dst_path"
elif [ -d "$src_path" ]; then
dst_path=$dst_path'/'$(basename "$src_path")
find "$src_path" -print0 | xargs -0 bash -c '. $0 --source-only; for it; do transfer_host_container '"$src_path"' $it '"$dst_container"' '"$dst_path"'; done;' "$self"
else
fatal "path $src_path not found"
fi
}
# copy CONTAINER1:PATH -> CONTAINER2:PATH
copy_container_container() {
tmp_dir=$(mktemp -d)
copy_container_host "$1" "$tmp_dir"
copy_host_container "$tmp_dir"'/'$(basename "$1") "$2"
rm -r "$tmp_dir"
}
usage() {
script=$(basename "$0")
echo "Usage: $script [OPTIONS] SOURCE DESTINATION
Copies files and directories between running containers and the host
Patterns:
$script CONTAINER:PATH HOSTPATH
$script HOSTPATH CONTAINER:PATH
$script CONTAINER1:PATH CONTAINER2:PATH
Examples:
* CONTAINER:PATH -> HOSTPATH
$script kickass_yonath:/home/data.txt .
- Copies '/home/data.txt' from 'kickass_yonath' container to the current host directory
* HOSTPATH -> CONTAINER:PATH
$script ./test.txt kickass_yonath:/home/e1/e2
- Copies 'test.txt' from the current host directory to '/home/e1/e2' in the 'kickass_yonath' container
$script /home/user/Downloads/test.txt kickass_yonath:/home/e1/e2
- Copies 'test.txt' from the host directory to '/home/e1/e2' in the 'kickass_yonath' container
$script /home/user/Downloads kickass_yonath:/home/d1/d2
- Copies the entire 'Downloads' directory to '/home/d1/d2' in the 'kickass_yonath' container
* CONTAINER1:PATH -> CONTAINER2:PATH
$script kickass_yonath:/home ecstatic_colden:/
- Copies the entire 'home' directory from 'kickass_yonath' to 'ecstatic_colden'
$script kickass_yonath:/home/data.txt ecstatic_colden:/
- Copies 'data.txt' file from 'kickass_yonath' conrainer to the 'ecstatic_colden' container
Options:
-h Display this help message
"
}
main() {
# parse options
while getopts ':h:' option; do
case "${option}" in
h) usage
exit
;;
\?) fatal "Invalid option: -$OPTARG"
exit
;;
esac
done
shift $((OPTIND - 1))
# check arguments
if [ $# -eq 0 ]; then
usage
exit
fi
# run
src=$1
dst=$2
if [ -n "$src" ] && [ -n "$dst" ]; then
if is_container_path "$src"; then
if is_container_path "$dst"; then
copy_container_container "$src" "$dst"
else
copy_container_host "$src" "$dst"
fi
else
if is_container_path "$dst"; then
copy_host_container "$src" "$dst"
else
fatal "cannot copy files/folders from a host path to a host path"
fi
fi
else
usage
fi
}
if [ "$1" != "--source-only" ]; then
main "${@}"
fi