diff --git a/scripts/backup.sh b/scripts/backup.sh new file mode 100644 index 00000000..bca7ecd0 --- /dev/null +++ b/scripts/backup.sh @@ -0,0 +1,40 @@ +#!/bin/bash -e + +function parse_conf_value() { + echo $(grep -e "^$1=" $2 | awk '{split($0,array,"="); print array[2]}' | tr -d '"') +} + +function help() { + echo "$0 /path/to/mm-wiki.conf" +} + +if [[ "$1" == "" ]]; then + help + exit 1 +fi + +CONF_FILE=$1 +[[ ! -f ${CONF_FILE} ]] && echo "Failed to open ${CONF_FILE}" && exit 1 +echo "Config file: ${CONF_FILE}" + +BACKUP_NAME=mm-wiki-backup-`date '+%Y%m%d-%H%M%S'` +BACKUP_DIR=/tmp/${BACKUP_NAME} +mkdir -p ${BACKUP_DIR} + +echo "Backup config..." +cp ${CONF_FILE} ${BACKUP_DIR}/ + +echo "Backup database..." +DB_DATABASE=$(parse_conf_value name ${CONF_FILE}) +DB_USERNAME=$(parse_conf_value user ${CONF_FILE}) +DB_PASSWORD=$(parse_conf_value pass ${CONF_FILE}) +MYSQL_PWD=${DB_PASSWORD} mysqldump -u${DB_USERNAME} --no-tablespaces ${DB_DATABASE} > ${BACKUP_DIR}/${DB_DATABASE}.sql + +echo "Backup documents..." +DOC_DIR=$(parse_conf_value root_dir ${CONF_FILE}) +tar czf ${BACKUP_DIR}/documents.tgz -C ${DOC_DIR} . + +echo "Pack backup..." +tar czf ${BACKUP_NAME}.tgz -C /tmp/${BACKUP_NAME} . +rm -rf /tmp/${BACKUP_NAME} +echo "Backup completed: ${BACKUP_NAME}.tgz" diff --git a/scripts/restore.sh b/scripts/restore.sh new file mode 100644 index 00000000..feffa7e1 --- /dev/null +++ b/scripts/restore.sh @@ -0,0 +1,50 @@ +#!/bin/bash -e + +function parse_conf_value() { + echo $(grep -e "^$1=" $2 | awk '{split($0,array,"="); print array[2]}' | tr -d '"') +} + +function help() { + echo "$0 backup-date-time.tgz /path/to/conf/" +} + +if [[ "$#" -ne 2 ]]; then + help + exit 1 +fi + +CONF_DIR=$2 +[[ ! -d ${CONF_DIR} ]] && mkdir -p ${CONF_DIR} +BACKUP_FILEPATH=$1 +BACKUP_FILENAME=$(basename ${BACKUP_FILEPATH}) +BACKUP_NAME=${BACKUP_FILENAME%.*} +BACKUP_DIR=/tmp/${BACKUP_NAME} +mkdir -p ${BACKUP_DIR} + +echo "Restore backup: ${BACKUP_FILEPATH}" +tar xf ${BACKUP_FILEPATH} -C ${BACKUP_DIR} + +echo "Restore config..." +CONF_FILE=${BACKUP_DIR}/mm-wiki.conf +if [[ -f ${CONF_DIR}/mm-wiki.conf ]]; then + mv ${CONF_DIR}/mm-wiki.conf ${CONF_DIR}/mm-wiki.conf.save +fi +cp ${BACKUP_DIR}/mm-wiki.conf ${CONF_DIR}/mm-wiki.conf + +echo "Restore database..." +DB_DATABASE=$(parse_conf_value name ${CONF_FILE}) +DB_USERNAME=$(parse_conf_value user ${CONF_FILE}) +DB_PASSWORD=$(parse_conf_value pass ${CONF_FILE}) +MYSQL_PWD=${DB_PASSWORD} mysql -u${DB_USERNAME} ${DB_DATABASE} < ${BACKUP_DIR}/${DB_DATABASE}.sql + +echo "Restore documents..." +DOC_DIR=$(parse_conf_value root_dir ${CONF_FILE}) +if [[ -d ${DOC_DIR} ]]; then + [[ -d ${DOC_DIR}.save ]] && rm -rf ${DOC_DIR}.save + mv ${DOC_DIR} ${DOC_DIR}.save +fi +mkdir -p ${DOC_DIR} +tar xf ${BACKUP_DIR}/documents.tgz -C ${DOC_DIR} + +rm -rf ${BACKUP_DIR} +echo "Restore completed: ${CONF_DIR}/mm-wiki.conf"