2016-12-10 03:39:02 +08:00
|
|
|
#!/bin/bash
|
|
|
|
|
|
|
|
. mailcow.conf
|
|
|
|
|
2016-12-11 21:13:17 +08:00
|
|
|
NAME="mariadb-mailcow"
|
2016-12-10 03:39:02 +08:00
|
|
|
|
|
|
|
reconf() {
|
|
|
|
echo "Installing database schema (this will not overwrite existing data)"
|
2016-12-11 21:13:17 +08:00
|
|
|
echo "It may take a while for MariaDB to warm up, please wait..."
|
2016-12-12 16:26:27 +08:00
|
|
|
echo docker exec ${NAME} mysql -u${DBUSER} -p${DBPASS} ${DBNAME}
|
2016-12-10 03:39:02 +08:00
|
|
|
until docker exec ${NAME} /bin/bash -c "mysql -u'${DBUSER}' -p'${DBPASS}' ${DBNAME} < /assets/init.sql"; do
|
|
|
|
echo "Trying again in 2 seconds..."
|
|
|
|
sleep 2
|
|
|
|
done
|
|
|
|
echo "Done."
|
|
|
|
}
|
|
|
|
|
2016-12-11 21:13:17 +08:00
|
|
|
dump() {
|
|
|
|
DATE=$(date +"%Y%m%d_%H%M%S")
|
|
|
|
echo "Creating dump file ./backup_${DBNAME}_${DATE}.sql"
|
|
|
|
docker exec -it ${NAME} /bin/bash mysqldump --default-character-set=utf8mb4 -u${DBUSER} -p${DBPASS} ${DBNAME} > backup_${DBNAME}_${DATE}.sql
|
|
|
|
}
|
|
|
|
|
|
|
|
restore() {
|
|
|
|
echo "Restoring dump file ${2}..."
|
|
|
|
docker exec -i ${NAME} mysql -u${DBUSER} -p${DBPASS} ${DBNAME} < ${1}
|
|
|
|
}
|
|
|
|
|
2016-12-10 03:39:02 +08:00
|
|
|
insert_admin() {
|
|
|
|
echo 'Setting mailcow UI admin login to "admin:moohoo"...'
|
2016-12-11 21:13:17 +08:00
|
|
|
echo "It may take a while for MariaDB to warm up, please wait..."
|
2016-12-10 03:39:02 +08:00
|
|
|
until docker exec ${NAME} /bin/bash -c "mysql -u'${DBUSER}' -p'${DBPASS}' ${DBNAME} < /assets/pw.sql"; do
|
|
|
|
echo "Trying again in 2 seconds..."
|
|
|
|
sleep 2
|
|
|
|
done
|
|
|
|
echo "Done."
|
|
|
|
}
|
|
|
|
|
|
|
|
client() {
|
|
|
|
echo "==============================="
|
|
|
|
echo "DB: ${DBNAME} - USER: ${DBUSER}"
|
|
|
|
echo "==============================="
|
2016-12-11 21:13:17 +08:00
|
|
|
docker exec -it ${NAME} mysql -u${DBUSER} -p${DBPASS} ${DBNAME}
|
2016-12-10 03:39:02 +08:00
|
|
|
}
|
|
|
|
|
2016-12-11 21:13:17 +08:00
|
|
|
if [[ ${1} == "--init-schema" ]]; then
|
2016-12-10 03:39:02 +08:00
|
|
|
reconf
|
|
|
|
exit 0
|
2016-12-11 21:13:17 +08:00
|
|
|
elif [[ ${1} == "--dump" ]]; then
|
|
|
|
dump
|
|
|
|
exit 0
|
|
|
|
elif [[ ${1} == "--restore" ]]; then
|
|
|
|
if [[ -z ${2} || ! -f ${2} ]]; then
|
|
|
|
echo "Invalid input file"
|
|
|
|
exit 1
|
|
|
|
fi
|
|
|
|
restore ${2}
|
|
|
|
exit 0
|
|
|
|
elif [[ ${1} == "--client" ]]; then
|
2016-12-10 03:39:02 +08:00
|
|
|
client
|
|
|
|
exit 0
|
2016-12-11 21:13:17 +08:00
|
|
|
elif [[ ${1} == "--reset-admin" ]]; then
|
2016-12-10 03:39:02 +08:00
|
|
|
insert_admin
|
|
|
|
exit 0
|
2016-12-11 21:13:17 +08:00
|
|
|
elif [[ ! -z ${1} ]]; then
|
|
|
|
echo "Unknown parameter"
|
|
|
|
exit 1
|
2016-12-10 03:39:02 +08:00
|
|
|
fi
|
|
|
|
|
|
|
|
echo "Stopping and removing containers with name tag ${NAME}..."
|
|
|
|
if [[ ! -z $(docker ps -af "name=${NAME}" -q) ]]; then
|
|
|
|
docker stop $(docker ps -af "name=${NAME}" -q)
|
|
|
|
docker rm $(docker ps -af "name=${NAME}" -q)
|
|
|
|
fi
|
|
|
|
|
2016-12-11 21:13:17 +08:00
|
|
|
if [[ ! -z "$(docker images -q mariadb:${DBVERS})" ]]; then
|
2016-12-10 03:52:19 +08:00
|
|
|
read -r -p "Found image locally. Delete local image and repull? [y/N] " response
|
2016-12-10 03:39:02 +08:00
|
|
|
response=${response,,}
|
|
|
|
if [[ $response =~ ^(yes|y)$ ]]; then
|
2016-12-11 21:13:17 +08:00
|
|
|
docker rmi mariadb:${DBVERS}
|
2016-12-10 03:39:02 +08:00
|
|
|
fi
|
|
|
|
fi
|
|
|
|
|
|
|
|
docker run \
|
|
|
|
-v ${PWD}/data/db/mysql/:/var/lib/mysql/ \
|
2016-12-13 04:53:58 +08:00
|
|
|
-v ${PWD}/data/conf/mysql/:/etc/mysql/conf.d/:ro \
|
|
|
|
-v ${PWD}/data/assets/mysql:/assets:ro \
|
2016-12-10 03:39:02 +08:00
|
|
|
--name=${NAME} \
|
|
|
|
--network=${DOCKER_NETWORK} \
|
|
|
|
-h mysql \
|
2016-12-14 05:30:45 +08:00
|
|
|
--network-alias=mysql \
|
2016-12-10 03:39:02 +08:00
|
|
|
-e MYSQL_ROOT_PASSWORD=${DBROOT} \
|
|
|
|
-e MYSQL_DATABASE=${DBNAME} \
|
|
|
|
-e MYSQL_USER=${DBUSER} \
|
|
|
|
-e MYSQL_PASSWORD=${DBPASS} \
|
2016-12-11 21:13:17 +08:00
|
|
|
-d mariadb:${DBVERS}
|
2016-12-10 03:39:02 +08:00
|
|
|
|
|
|
|
reconf
|
|
|
|
|
|
|
|
read -r -p "Do you want to reset mailcow admin to admin:moohoo? [y/N] " response
|
|
|
|
response=${response,,}
|
|
|
|
if [[ $response =~ ^(yes|y)$ ]]; then
|
|
|
|
insert_admin
|
|
|
|
fi
|