Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/bin/bash
- set -e
- systemctl stop mirrors
- systemctl restart docker
- systemctl stop nginx 2>/dev/null || true
- docker stop $(docker ps -aq) > /dev/null || true
- pkill mysqld || true
- pkill php-fpm7 || true
- pkill nginx || true
- sleep 5
- pidof mysqld && echo "Can't kill mysqld process, kill it by hands and restart script!" && exit 1
- pidof php-fpm7 && echo "Can't kill php-fpm7 process, kill it by hands and restart script!" && exit 1
- pidof nginx && echo "Can't kill nginx process, kill it by hands and restart script!" && exit 1
- docker rm -f "clone-wp-db" > /dev/null 2>&1 || true
- insp=( $(docker inspect wp-db -f '{{.Image}} {{(index .Mounts 0).Name}}') )
- db_image=${insp[0]}
- db_volume=${insp[1]}
- printf "\n---\nClone db container: wb-db\n"
- docker create \
- --name "clone-wp-db" \
- --label "wp-role=db" \
- --volume "$db_volume:/var/lib/mysql" \
- "$db_image"
- cids=$(docker ps -a --filter 'name=^/wp-domain-[0-9]+$' --format '{{.Names}}')
- for cid in $cids; do
- printf "\n---\nClone domain container: $cid\n"
- insp=( $(docker inspect $cid -f '{{index .Config.Labels "wp-domain-id"}} {{index .Config.Labels "wp-domain-name"}} {{.Image}} {{(index .Mounts 0).Name}}') )
- domain_id=${insp[0]}
- domain_name=${insp[1]}
- image=${insp[2]}
- volume=${insp[3]}
- nw_name="wp-nw-domain-$domain_id"
- ct_name="clone-wp-domain-$domain_id"
- echo "Domain_name: $domain_name"
- if ! docker network inspect "$nw_name" > /dev/null 2>&1; then
- echo "Create network $nw_name"
- docker network create \
- --label "wp-role=domain-db-bridge" \
- --label "wp-domain-id=$domain_id" \
- --label "wp-domain-name=$domain_name" \
- "$nw_name"
- fi
- echo "Connect network $nw_name to db container"
- docker network connect --alias "db" "$nw_name" "clone-wp-db"
- echo "Create domain cointainer: $ct_name"
- docker rm -f "$ct_name" > /dev/null 2>&1 || true
- docker create \
- --name "$ct_name" \
- -P \
- --network "$nw_name" \
- --volume "$volume:/var/www/html" \
- --label "wp-role=wp" \
- --label "wp-domain-id=$domain_id" \
- --label "wp-domain-name=$domain_name" \
- "$image"
- done
- printf "\n---\nReplace db container: clone-wp-db -> wb-db\n"
- docker rm -f "wp-db"
- docker rename "clone-wp-db" "wp-db"
- docker start "wp-db"
- sleep 2
- for cid in $cids; do
- ct_name="clone-$cid"
- printf "\n---\nReplace domain container: $ct_name -> $cid\n"
- docker rm -f "$cid"
- docker rename "$ct_name" "$cid"
- docker start "$cid"
- done
- systemctl start mirrors
- if [ -f /etc/nginx/metrics.htpasswd ]; then
- systemctl start nginx
- else
- true # do nothing
- fi
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement