-
Notifications
You must be signed in to change notification settings - Fork 0
/
DockerManger.sh
301 lines (247 loc) Β· 9.3 KB
/
DockerManger.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
#!/bin/bash
# Project: Docker Bash Manger
# Name: Mohammed Alsayegh
# The script uses whiptail to navigate into docker images and volumes lists for import, export, and deletion
clear
retval=()
store=()
tempFlag=()
function DockerImageNameSelect() {
STATIONS=()
number=()
s2=()
FILE=img_names.txt
if test -f "$FILE"; then
echo "$FILE exists."
rm $FILE
else
echo "$FILE does not exist."
fi
docker image list --format "table {{.Repository}}" > "$FILE"
FILE=img_names.txt
mapfile -t STATIONS < <( cat $FILE)
cnt=${#STATIONS[@]}
j=1
for (( i=1 ; i<cnt ; i++ )); do
s2[$j]="$(($i))"
j=$(expr $j + 1)
s2[j]=${STATIONS[i]}
j=$(expr $j + 1)
done
choice=$(whiptail --title "Choose radio station:" --menu "select your choice" 0 0 0 "${s2[@]}" 3>&1 1>&2 2>&3)
echo 'whiptail choice is: ' $choice
number=$(expr $choice)
echo 'number is: ' $number
echo 'string is: ' ${STATIONS[$number]}
retval="${STATIONS[$number]}"
}
function DockerVolumeNameSelect() {
STATIONS3=()
number3=()
s2=()
FILE=vol_names.txt
if test -f "$FILE"; then
echo "$FILE exists."
rm $FILE
else
echo "$FILE does not exist."
fi
docker volume list --format "table {{.Name}}" > "$FILE"
FILE=vol_names.txt
mapfile -t STATIONS3 < <( cat $FILE)
cnt=${#STATIONS3[@]}
j=1
for (( i=1 ; i<cnt ; i++ )); do
s2[$j]="$(($i))"
j=$(expr $j + 1)
s2[j]=${STATIONS3[i]}
j=$(expr $j + 1)
done
choice=$(whiptail --title "Choose radio station:" --menu "select your choice" 0 0 0 "${s2[@]}" 3>&1 1>&2 2>&3)
echo 'whiptail choice is: ' $choice
number3=$(expr $choice)
echo 'number is: ' $number3
echo 'string is: ' ${STATIONS3[$number3]}
retval="${STATIONS3[$number3]}"
}
function tarSearshSelect() {
STATIONS2=()
number2=()
s2=()
FILE=tar_files_list.txt
if test -f "$FILE"; then
echo "$FILE exists."
rm $FILE
else
echo "$FILE does not exist."
fi
find -type f -name "*.tar" > "$FILE"
mapfile -t STATIONS2 < <( cat $FILE)
cnt=${#STATIONS2[@]}
j=0
for (( i=0 ; i<cnt ; i++ )); do
s2[$j]="$(($i+1))"
j=$(expr $j + 1)
s2[j]=${STATIONS2[i]}
j=$(expr $j + 1)
done
choice=$(whiptail --title "tar List:" --menu "Choose .tar from the list" 0 0 0 "${s2[@]}" 3>&1 1>&2 2>&3)
echo 'whiptail choice is: ' $choice
number2=$(expr $choice)
echo 'number is: ' $number2
echo 'string is: ' ${STATIONS2[$number2 -1]}
retval="${STATIONS2[$number2 -1]}"
}
function tgzSearshSelect() {
STATIONS2=()
number2=()
s2=()
FILE=tgz_files_list.txt
if test -f "$FILE"; then
echo "$FILE exists."
rm $FILE
else
echo "$FILE does not exist."
fi
find -type f -name "*.tgz" > "$FILE"
mapfile -t STATIONS2 < <( cat $FILE)
cnt=${#STATIONS2[@]}
j=0
for (( i=0 ; i<cnt ; i++ )); do
s2[$j]="$(($i+1))"
j=$(expr $j + 1)
s2[j]=${STATIONS2[i]}
j=$(expr $j + 1)
done
choice=$(whiptail --title "tgz List:" --menu "Choose .tgz from the list" 0 0 0 "${s2[@]}" 3>&1 1>&2 2>&3)
echo 'whiptail choice is: ' $choice
number2=$(expr $choice)
echo 'number is: ' $number2
echo 'string is: ' ${STATIONS2[$number2 -1]}
retval="${STATIONS2[$number2 -1]}"
}
function DockerManagerMenu() {
SELECTED=$(whiptail --title "Image Volume Docker Manager" --fb --menu "Choose an option" 15 60 4 \
"1" "image export into image.tar πΏ" \
"2" "image import from image.tar" \
"3" "backup export into .tgz file ππ" \
"4" "Volume import from .tgz π¦" \
"5" "Remove image π₯" \
"6" "Remove volume π₯" \
"7" "Exit" \
3>&1 1>&2 2>&3)
case $SELECTED in
1)
echo "image saved gone start in few second"
DockerImageNameSelect
if [[ $retval =~ ['!@#$%^&*()_\+/'] ]]; then
#echo yes
tempFlag=$(echo $retval | tr '<>:\\//#%|?*' ' ')
if (whiptail --title "Image Name β οΈ" --yesno "$retval containe characters that Windows doesn't accept in a filename, do you want to change the name of .tar ?" 8 78); then
#save file to $tempFlag
store=$(echo $retval | tr '<>:\\//#%|?*' ' ')
fi
else
echo "no, special characters that Windows would't accept as a filename"
fi
if (whiptail --title "Export Image" --yesno "Are you sure you want to export $retval ?" 8 78); then
#time docker save -o "$retval"".tar" "$retval"
time docker save -o "$store"".tar" "$retval"
whiptail --title "Image save" --msgbox ""$retval" save successfully completed." 8 45
else
echo "User selected No, exit status was $?."
fi
DockerManagerMenu
;;
2)
echo "image load gone start in few second"
tarSearshSelect
if (whiptail --title "Import Image" --yesno "Are you sure you want to import $retval ?" 8 78); then
time docker load -i "$retval"
whiptail --title "Image load" --msgbox ""$retval" load successfully completed." 8 45
else
echo "User selected No, exit status was $?."
fi
DockerManagerMenu
;;
3)
echo "The backup gone start in few second"
DockerVolumeNameSelect
echo \ && read -p "Press key to continue.. " -n1 -s
if (whiptail --title "Export Volume" --yesno "Are you sure you want to export $retval ?" 8 78); then
time docker run --rm -v "$retval":/volume busybox sh -c 'tar -cOzf - volume' > "$retval".tgz
whiptail --title "Option 1" --msgbox ""$retval" were fully backed up." 8 45
else
echo "User selected No, exit status was $?."
fi
DockerManagerMenu
;;
4)
echo "In a few seconds, the volume restore will start"
docker volume ls
tgzSearshSelect
echo \ && read -p "Press key to continue.. " -n1 -s
if (whiptail --title "Import Volume" --yesno "Are you sure you want to import $retval ?" 8 78); then
docker volume create "${retval:2:-4}"
docker run -v "${retval:2:-4}":/dbdata --name dbstore2 ubuntu /bin/bash
time docker run --rm --volumes-from dbstore2 -v $(pwd):/backup ubuntu bash -c "cd /dbdata && tar xf /backup/"${retval:2}" --strip 1 -C /dbdata"
docker stop dbstore2 && docker rm dbstore2
whiptail --title "Volume restore" --msgbox ""${retval:2:-4}" restoration have been completed successfully." 8 45
else
echo "User selected No, exit status was $?."
fi
DockerManagerMenu
;;
5)
echo "Remove image π₯"
docker image ls
DockerImageNameSelect
echo \ && read -p "Press key to continue.. " -n1 -s
if [[ $retval =~ ['!@#$%^&*()_\+/'] ]]; then
#echo yes
tempFlag=$(docker image ls $retval --format "{{.ID}}")
if (whiptail --title "Image Name β οΈ" --yesno "$retval containe characters that ""Docker rm"" required Docker ID, Do you want to delete Docker Image ID $tempFlag?" 8 78); then
store="$tempFlag"
fi
else
echo "There is no special characters in ""Docker rm"" need the ""Docker ID"" "
fi
echo "$store"
echo \ && read -p "Press key to continue.. " -n1 -s
if (whiptail --title "Remove Image" --yesno "Are you sure you want to remove $retval ?" 8 78); then
#time docker image rm "$retval"
#time docker image rm f07977d2b0e0
time docker image rm "$store"
whiptail --title "Remove image" --msgbox "$retval have been removed" 8 45
else
echo "User selected No, exit status was $?."
fi
DockerManagerMenu
;;
6)
echo "Remove volume π₯"
docker volume ls
DockerVolumeNameSelect
echo \ && read -p "Press key to continue.. " -n1 -s
if (whiptail --title "Remove volume" --yesno "Are you sure you want to remove $retval ?" 8 78); then
time docker volume rm "$retval"
whiptail --title "Remove volume" --msgbox "$retval have been removed" 8 45
else
echo "User selected No, exit status was $?."
fi
DockerManagerMenu
;;
7)
echo "Exit π"
;;
esac
}
function warning()
{
if (whiptail --title "β οΈ WARNING" --yesno "There is still a lot of work to be done on this script or it has not been fully debugged. Would you like to use it?" 8 78); then
DockerManagerMenu
else
echo "Docker Manager is closing!!"
fi
}
warning