Last active
August 29, 2018 08:07
-
-
Save profiprog/448e783586aacf2cf54c89f1592cc51e to your computer and use it in GitHub Desktop.
Bash script to install, remove and purge data of docker wordpress image.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
DOMAIN="www.example.xy" # set your own domain | |
INSTANCE_NAME="appweb" # set your app name | |
INSTANCE_PORT="1090" # set dedicated port | |
DB_HOST="dbhost:3306" # set connection to database ... | |
DB_USER="db_user" | |
DB_PSWD="db_password" | |
DB_NAME="db_schema" | |
HTML_DIR="$(cd "$(dirname "$0")"; pwd)/html-content" # here is stoted html content | |
mysql-check() { mysql --protocol=TCP -u "$DB_USER" -p"$DB_PSWD" -h "${DB_HOST/:*/}" -P "$([ "${DB_HOST/*:/}" == "$DB_HOST" ] && echo 3306 || echo "${DB_HOST/*:/}")" "$@"; } | |
fail() { echo -e "\e[91mError: $@\e[0m" >&2; exit 1; } | |
info() { echo -e "\e[96m$@\e[0m"; } | |
mysql-do() { mysql -e "$2" || fail "unable $1\n try: \e[93mmysql \"$2\""; } | |
ok() { echo -e "\e[32m$@\e[0m"; } | |
case "$1" in | |
install) | |
[ -d "$HTML_DIR" ] || mkdir -v "$HTML_DIR" | |
info "Checking database connection ..." | |
mysql-check -BNe "select 'success';" && ok ok || { | |
info "Trying to create access" | |
mysql-do "create db user" "grant all privileges on $DB_NAME.* to '$DB_USER'@'%' identified by '$DB_PSWD';" | |
mysql-check -BNe "select 'success';" && ok ok || fail "cannot connect to database" | |
} | |
info "Checking database schema ..." | |
mysql-check -BNe "show tables;" "$DB_NAME" && ok ok || { | |
info "Trying to create database" | |
mysql-do "create schema" "create schema if not exists $DB_NAME charset utf8;" | |
mysql-check -BNe "select 'success';" "$DB_NAME" && ok ok || fail "cannot create database" | |
} | |
info "Installing docker ..." | |
docker run --detach \ | |
--hostname "$DOMAIN" \ | |
--name "$INSTANCE_NAME" \ | |
--env WORDPRESS_DB_HOST="$DB_HOST" \ | |
--env WORDPRESS_DB_USER="$DB_USER" \ | |
--env WORDPRESS_DB_PASSWORD="$DB_PSWD" \ | |
--env WORDPRESS_DB_NAME="$DB_NAME" \ | |
--publish "$INSTANCE_PORT":80 \ | |
--volume "$HTML_DIR":/var/www/html \ | |
--restart always \ | |
wordpress | |
;; | |
purge) | |
"$0" remove | |
mysql-check -BNe "drop schema if exists $DB_NAME;" | |
rm -vfr "$HTML_DIR" | |
;; | |
remove) | |
docker stop "$INSTANCE_NAME" | |
docker rm "$INSTANCE_NAME" | |
docker ps -a | |
;; | |
esac |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment