Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

添加备份和恢复脚本 #351

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 40 additions & 0 deletions scripts/backup.sh
Original file line number Diff line number Diff line change
@@ -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"
50 changes: 50 additions & 0 deletions scripts/restore.sh
Original file line number Diff line number Diff line change
@@ -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"