-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdmove
66 lines (51 loc) · 2.78 KB
/
dmove
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
#!/bin/bash
echo "Hello, this utility will help you to transfer containers with volumes"
. ~/.dmove
CONTAINER=$1
if [ -z $CONTAINER ]; then
echo -e "List of all running docker containers, sorted by name: "
docker ps --format "{{.Names}}" | sort | column
read -p "Type the name of container which you want to backup, default is $CONTAINER : " input
CONTAINER=${input:-$CONTAINER}
fi
move-container "$CONTAINER"
# Auto transfer volumes
if docker inspect -f '{{ range .Mounts }}{{println .Source .Destination}}{{ end }}' $CONTAINER ; then
echo "Container $CONTAINER has following volumes : "
docker inspect -f '{{ range .Mounts }}{{println .Source .Destination}}{{ end }}' $CONTAINER
read -p "Would you like to move volumes now? " -n 1 -r
echo
if [[ $REPLY =~ ^[Yy]$ ]]; then
move-volumes "$CONTAINER" "$TAG" nginx-proxy
fi
else
echo "Container $CONTAINER does not have any volumes so no data to transfer"
fi
# Auto transfer mysql data
if docker inspect --format='{{.Config.Env}}' $CONTAINER | grep -q "MYSQL_HOST" ; then
echo "Seems like you also need to backup database! ENV variables related to SQL containers founded: "
docker inspect --format='{{json .Config.Env}}' $CONTAINER | jq . | tr -d '[]", ' | grep 'MYSQL_*'
read -p "Would you like to backup SQL database now ? " -n 1 -r
echo
# TO-DO: warning about unique hostname and creation of separate docker network
if [[ $REPLY =~ ^[Yy]$ ]]; then
MYSQL_HOST=$(docker inspect --format='{{json .Config.Env}}' $CONTAINER | jq . | tr -d '[]", ' | grep 'MYSQL_HOST' | cut -d= -f2)
db_suffix="_db" # convention
DB_CONTAINER_NAME="$CONTAINER$db_suffix"
read -p "Type the name of database container, default is $CONTAINER$db_suffix: " input
DB_CONTAINER_NAME=${input:-$DB_CONTAINER_NAME}
move-container "$DB_CONTAINER_NAME" # to save ENV # TO-DO: return image tag to fill insted $HOSTNAME/$DB_CONTAINER_NAME
move-db "$CONTAINER $HOSTNAME/$DB_CONTAINER_NAME nginx-proxy $MYSQL_HOST 1" # make deploy
fi
fi
# Warning about DNS records
if docker inspect --format='{{.Config.Env}}' $CONTAINER | grep -q "VIRTUAL_HOST" ; then
VIRTUAL_HOST=$(docker inspect --format='{{json .Config.Env}}' $CONTAINER | jq . | tr -d '[]", ' | grep 'VIRTUAL_HOST' | cut -d= -f2)
echo "Do not forget to update A DNS records for $VIRTUAL_HOST points to $REMOTE_HOST"
nslookup -type=a $VIRTUAL_HOST
# TO-DO: automatic adding via popular APIs: CloudFlare, Digitalocean, Reg.ru, Yandex DNS
fi
# Warning about Docker Hub
echo "If ENV variables of your container are not contain sensitive data like API keys you can push image to Docker Hub. It is free"
# TO-DO: push if yes
# TO-DO: Warning about bad image versionning in case of transfering docker-compose file