1
0
mirror of https://github.com/mailcow/mailcow-dockerized.git synced 2024-12-27 02:43:38 +02:00
mailcow-dockerized/build-sql.sh

104 lines
2.7 KiB
Bash
Raw Normal View History

2016-12-09 21:39:02 +02:00
#!/bin/bash
. mailcow.conf
2016-12-11 15:13:17 +02:00
NAME="mariadb-mailcow"
2016-12-09 21:39:02 +02:00
reconf() {
echo "Installing database schema (this will not overwrite existing data)"
2016-12-11 15:13:17 +02:00
echo "It may take a while for MariaDB to warm up, please wait..."
2016-12-09 21:39:02 +02: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 15:13:17 +02: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-09 21:39:02 +02:00
insert_admin() {
echo 'Setting mailcow UI admin login to "admin:moohoo"...'
2016-12-11 15:13:17 +02:00
echo "It may take a while for MariaDB to warm up, please wait..."
2016-12-09 21:39:02 +02: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 15:13:17 +02:00
docker exec -it ${NAME} mysql -u${DBUSER} -p${DBPASS} ${DBNAME}
2016-12-09 21:39:02 +02:00
}
2016-12-11 15:13:17 +02:00
if [[ ${1} == "--init-schema" ]]; then
2016-12-09 21:39:02 +02:00
reconf
exit 0
2016-12-11 15:13:17 +02: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-09 21:39:02 +02:00
client
exit 0
2016-12-11 15:13:17 +02:00
elif [[ ${1} == "--reset-admin" ]]; then
2016-12-09 21:39:02 +02:00
insert_admin
exit 0
2016-12-11 15:13:17 +02:00
elif [[ ! -z ${1} ]]; then
echo "Unknown parameter"
exit 1
2016-12-09 21:39:02 +02: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 15:13:17 +02:00
if [[ ! -z "$(docker images -q mariadb:${DBVERS})" ]]; then
2016-12-09 21:52:19 +02:00
read -r -p "Found image locally. Delete local image and repull? [y/N] " response
2016-12-09 21:39:02 +02:00
response=${response,,}
if [[ $response =~ ^(yes|y)$ ]]; then
2016-12-11 15:13:17 +02:00
docker rmi mariadb:${DBVERS}
2016-12-09 21:39:02 +02:00
fi
fi
docker run \
-v ${PWD}/data/db/mysql/:/var/lib/mysql/ \
-v ${PWD}/data/conf/mysql/:/etc/mysql/conf.d/ \
-v ${PWD}/data/assets/mysql:/assets \
--name=${NAME} \
--network=${DOCKER_NETWORK} \
-h mysql \
--network-alias mysql \
-e MYSQL_ROOT_PASSWORD=${DBROOT} \
-e MYSQL_DATABASE=${DBNAME} \
-e MYSQL_USER=${DBUSER} \
-e MYSQL_PASSWORD=${DBPASS} \
2016-12-11 15:13:17 +02:00
-d mariadb:${DBVERS}
2016-12-09 21:39:02 +02: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