[Helper] Some minor improvements

master
andryyy 2020-03-28 19:50:15 +01:00
parent 5e121eb4ff
commit ba246d34cb
1 changed files with 55 additions and 16 deletions

View File

@ -45,13 +45,28 @@ else
exit 1 exit 1
fi fi
fi fi
BACKUP_LOCATION=$(echo ${BACKUP_LOCATION} | sed 's#/$##') BACKUP_LOCATION=$(echo ${BACKUP_LOCATION} | sed 's#/$##')
SCRIPT_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )" SCRIPT_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
COMPOSE_FILE=${SCRIPT_DIR}/../docker-compose.yml COMPOSE_FILE=${SCRIPT_DIR}/../docker-compose.yml
if [ ! -f ${COMPOSE_FILE} ]; then
echo "Compose file not found"
exit 1
fi
echo "Using ${BACKUP_LOCATION} as backup/restore location." echo "Using ${BACKUP_LOCATION} as backup/restore location."
echo echo
source ${SCRIPT_DIR}/../mailcow.conf source ${SCRIPT_DIR}/../mailcow.conf
CMPS_PRJ=$(echo $COMPOSE_PROJECT_NAME | tr -cd "[A-Za-z-_]")
if [[ -z ${COMPOSE_PROJECT_NAME} ]]; then
echo "Could not determine compose project name"
exit 1
else
echo "Found project name ${COMPOSE_PROJECT_NAME}"
CMPS_PRJ=$(echo ${COMPOSE_PROJECT_NAME} | tr -cd "[A-Za-z-_]")
fi
function backup() { function backup() {
DATE=$(date +"%Y-%m-%d-%H-%M-%S") DATE=$(date +"%Y-%m-%d-%H-%M-%S")
@ -93,18 +108,24 @@ function backup() {
;;& ;;&
mysql|all) mysql|all)
SQLIMAGE=$(grep -iEo '(mysql|mariadb)\:.+' ${COMPOSE_FILE}) SQLIMAGE=$(grep -iEo '(mysql|mariadb)\:.+' ${COMPOSE_FILE})
if [[ -z "${SQLIMAGE}" ]]; then
echo "Could not determine SQL image version, skipping backup..."
shift
continue
else
echo "Using SQL image ${SQLIMAGE}, starting..."
docker run --rm \ docker run --rm \
--network $(docker network ls -qf name=${CMPS_PRJ}_) \ --network $(docker network ls -qf name=${CMPS_PRJ}_) \
-v $(docker volume ls -qf name=${CMPS_PRJ}_mysql-vol-1):/var/lib/mysql/:ro \ -v $(docker volume ls -qf name=${CMPS_PRJ}_mysql-vol-1):/var/lib/mysql/:ro \
--entrypoint= \ --entrypoint= \
-v ${BACKUP_LOCATION}/mailcow-${DATE}/mysql:/backup \ -v ${BACKUP_LOCATION}/mailcow-${DATE}/mysql:/backup \
${SQLIMAGE} /bin/sh -c "mariabackup --host mysql --user root --password ${DBROOT} --backup --rsync --target-dir=/backup" ${SQLIMAGE} /bin/sh -c "mariabackup --host mysql --user root --password ${DBROOT} --backup --rsync --target-dir=/backup"
docker run --rm \ docker run --rm \
--network $(docker network ls -qf name=${CMPS_PRJ}_) \ --network $(docker network ls -qf name=${CMPS_PRJ}_) \
--entrypoint= \ --entrypoint= \
-v ${BACKUP_LOCATION}/mailcow-${DATE}/mysql:/backup \ -v ${BACKUP_LOCATION}/mailcow-${DATE}/mysql:/backup \
${SQLIMAGE} /bin/sh -c "mariabackup --prepare --target-dir=/backup" ${SQLIMAGE} /bin/sh -c "mariabackup --prepare --target-dir=/backup"
fi
;;& ;;&
--delete-days) --delete-days)
shift shift
@ -120,7 +141,10 @@ function backup() {
} }
function restore() { function restore() {
echo
echo "Stopping watchdog-mailcow..."
docker stop $(docker ps -qf name=watchdog-mailcow) docker stop $(docker ps -qf name=watchdog-mailcow)
echo
RESTORE_LOCATION="${1}" RESTORE_LOCATION="${1}"
shift shift
while (( "$#" )); do while (( "$#" )); do
@ -178,10 +202,18 @@ function restore() {
;; ;;
mysql) mysql)
SQLIMAGE=$(grep -iEo '(mysql|mariadb)\:.+' ${COMPOSE_FILE}) SQLIMAGE=$(grep -iEo '(mysql|mariadb)\:.+' ${COMPOSE_FILE})
docker stop $(docker ps -qf name=mysql-mailcow) read -p "mailcow will be stopped and the corresponding mailcow.conf file of the backup will be restored, do you want to proceed? [y|N] " MYSQL_STOP_MAILCOW
if [[ ${MYSQL_STOP_MAILCOW,,} =~ ^(yes|y)$ ]]; then
echo "Stopping mailcow..."
docker-compose down
else
echo "OK, skipped."
shift
continue
fi
#docker stop $(docker ps -qf name=mysql-mailcow)
if [[ -d "${RESTORE_LOCATION}/mysql" ]]; then if [[ -d "${RESTORE_LOCATION}/mysql" ]]; then
docker run --rm \ docker run --rm \
--network $(docker network ls -qf name=${CMPS_PRJ}_) \
-v $(docker volume ls -qf name=${CMPS_PRJ}_mysql-vol-1):/var/lib/mysql/:rw \ -v $(docker volume ls -qf name=${CMPS_PRJ}_mysql-vol-1):/var/lib/mysql/:rw \
--entrypoint= \ --entrypoint= \
-v ${RESTORE_LOCATION}/mysql:/backup \ -v ${RESTORE_LOCATION}/mysql:/backup \
@ -199,11 +231,18 @@ function restore() {
gunzip < backup/backup_mysql.gz | mysql -uroot && \ gunzip < backup/backup_mysql.gz | mysql -uroot && \
mysql -uroot -e SHUTDOWN;" mysql -uroot -e SHUTDOWN;"
fi fi
docker start $(docker ps -aqf name=mysql-mailcow) echo "Copying mailcow.conf..."
mv mailcow.conf mailcow.conf_backup_$(date +"%Y-%m-%d-%H-%M-%S")
cp ${RESTORE_LOCATION}/mailcow.conf mailcow.conf
echo "Starting mailcow..."
docker-compose up -d
#docker start $(docker ps -aqf name=mysql-mailcow)
;; ;;
esac esac
shift shift
done done
echo
echo "Starting watchdog-mailcow..."
docker start $(docker ps -aqf name=watchdog-mailcow) docker start $(docker ps -aqf name=watchdog-mailcow)
} }