From 6878ac844dacf4488ecccc3a7c766dc63ae195f2 Mon Sep 17 00:00:00 2001 From: Bo Wang Date: Sun, 24 Apr 2022 16:33:25 +0800 Subject: [PATCH 1/4] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=E5=A4=87=E4=BB=BD?= =?UTF-8?q?=E5=92=8C=E6=81=A2=E5=A4=8D=E8=84=9A=E6=9C=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- scripts/backup.sh | 31 +++++++++++++++++++++++++++++ scripts/restore.sh | 49 ++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 80 insertions(+) create mode 100755 scripts/backup.sh create mode 100755 scripts/restore.sh diff --git a/scripts/backup.sh b/scripts/backup.sh new file mode 100755 index 00000000..e78014ba --- /dev/null +++ b/scripts/backup.sh @@ -0,0 +1,31 @@ +#!/bin/bash -e + +function parse_conf_value() { + echo $(grep -e "^$1=" $2 | awk '{split($0,array,"="); print array[2]}' | tr -d '"') +} + +ROOT_DIR=$(cd "$(dirname "$0")";pwd) +CONF_FILE=${ROOT_DIR}/conf/mm-wiki.conf +echo "Config file: ${CONF_FILE}" + +BACKUP_NAME=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 100755 index 00000000..f222b88c --- /dev/null +++ b/scripts/restore.sh @@ -0,0 +1,49 @@ +#!/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" +} + +if [[ "$1" == "" ]]; then + help + exit 1 +fi + +ROOT_DIR=$(cd "$(dirname "$0")";pwd) +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 ${ROOT_DIR}/conf/mm-wiki.conf ]]; then + mv ${ROOT_DIR}/conf/mm-wiki.conf ${ROOT_DIR}/conf/mm-wiki.conf.save +fi +cp ${BACKUP_DIR}/mm-wiki.conf ${ROOT_DIR}/conf/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." From cc67525c5dd8c2dd5c6c6b48875063d736eb986b Mon Sep 17 00:00:00 2001 From: Bo Wang Date: Sun, 24 Apr 2022 16:41:14 +0800 Subject: [PATCH 2/4] =?UTF-8?q?=E5=88=A0=E9=99=A4=E5=8F=AF=E6=89=A7?= =?UTF-8?q?=E8=A1=8Cflag?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- scripts/backup.sh | 0 scripts/restore.sh | 0 2 files changed, 0 insertions(+), 0 deletions(-) mode change 100755 => 100644 scripts/backup.sh mode change 100755 => 100644 scripts/restore.sh diff --git a/scripts/backup.sh b/scripts/backup.sh old mode 100755 new mode 100644 diff --git a/scripts/restore.sh b/scripts/restore.sh old mode 100755 new mode 100644 From ae2ef9d1b19b9f249301f9cfda2e5c04204420bc Mon Sep 17 00:00:00 2001 From: Bo Wang Date: Sun, 24 Apr 2022 17:22:55 +0800 Subject: [PATCH 3/4] =?UTF-8?q?=E8=A6=81=E6=B1=82=E6=98=BE=E5=BC=8F?= =?UTF-8?q?=E4=BC=A0=E9=80=92conf=E6=96=87=E4=BB=B6=E8=B7=AF=E5=BE=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- scripts/backup.sh | 13 +++++++++++-- scripts/restore.sh | 15 ++++++++------- 2 files changed, 19 insertions(+), 9 deletions(-) diff --git a/scripts/backup.sh b/scripts/backup.sh index e78014ba..c7161958 100644 --- a/scripts/backup.sh +++ b/scripts/backup.sh @@ -4,8 +4,17 @@ function parse_conf_value() { echo $(grep -e "^$1=" $2 | awk '{split($0,array,"="); print array[2]}' | tr -d '"') } -ROOT_DIR=$(cd "$(dirname "$0")";pwd) -CONF_FILE=${ROOT_DIR}/conf/mm-wiki.conf +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=backup-`date '+%Y%m%d-%H%M%S'` diff --git a/scripts/restore.sh b/scripts/restore.sh index f222b88c..feffa7e1 100644 --- a/scripts/restore.sh +++ b/scripts/restore.sh @@ -5,15 +5,16 @@ function parse_conf_value() { } function help() { - echo "$0 backup-date-time.tgz" + echo "$0 backup-date-time.tgz /path/to/conf/" } -if [[ "$1" == "" ]]; then +if [[ "$#" -ne 2 ]]; then help exit 1 fi -ROOT_DIR=$(cd "$(dirname "$0")";pwd) +CONF_DIR=$2 +[[ ! -d ${CONF_DIR} ]] && mkdir -p ${CONF_DIR} BACKUP_FILEPATH=$1 BACKUP_FILENAME=$(basename ${BACKUP_FILEPATH}) BACKUP_NAME=${BACKUP_FILENAME%.*} @@ -25,10 +26,10 @@ tar xf ${BACKUP_FILEPATH} -C ${BACKUP_DIR} echo "Restore config..." CONF_FILE=${BACKUP_DIR}/mm-wiki.conf -if [[ -f ${ROOT_DIR}/conf/mm-wiki.conf ]]; then - mv ${ROOT_DIR}/conf/mm-wiki.conf ${ROOT_DIR}/conf/mm-wiki.conf.save +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 ${ROOT_DIR}/conf/mm-wiki.conf +cp ${BACKUP_DIR}/mm-wiki.conf ${CONF_DIR}/mm-wiki.conf echo "Restore database..." DB_DATABASE=$(parse_conf_value name ${CONF_FILE}) @@ -46,4 +47,4 @@ mkdir -p ${DOC_DIR} tar xf ${BACKUP_DIR}/documents.tgz -C ${DOC_DIR} rm -rf ${BACKUP_DIR} -echo "Restore completed." +echo "Restore completed: ${CONF_DIR}/mm-wiki.conf" From ac04c3d0eae7238ed1828f2bdce0ee6a1f223d91 Mon Sep 17 00:00:00 2001 From: Bo Wang Date: Sun, 24 Apr 2022 19:32:55 +0800 Subject: [PATCH 4/4] =?UTF-8?q?=E5=A4=87=E4=BB=BD=E6=96=87=E4=BB=B6?= =?UTF-8?q?=E6=B7=BB=E5=8A=A0mm-wiki-=E5=89=8D=E7=BC=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- scripts/backup.sh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/scripts/backup.sh b/scripts/backup.sh index c7161958..bca7ecd0 100644 --- a/scripts/backup.sh +++ b/scripts/backup.sh @@ -17,7 +17,7 @@ CONF_FILE=$1 [[ ! -f ${CONF_FILE} ]] && echo "Failed to open ${CONF_FILE}" && exit 1 echo "Config file: ${CONF_FILE}" -BACKUP_NAME=backup-`date '+%Y%m%d-%H%M%S'` +BACKUP_NAME=mm-wiki-backup-`date '+%Y%m%d-%H%M%S'` BACKUP_DIR=/tmp/${BACKUP_NAME} mkdir -p ${BACKUP_DIR}