feat: support restore from backup file

This commit is contained in:
ttionya
2020-11-08 13:43:47 +08:00
parent 0b6cb8f16e
commit 1710c92e73
3 changed files with 147 additions and 0 deletions
+10
View File
@@ -25,6 +25,16 @@ if [[ "$1" == "mail" ]]; then
exit 0
fi
# restore
if [[ "$1" == "restore" ]]; then
. /app/restore.sh
shift
restore $*
exit 0
fi
function configure_timezone() {
if [[ ! -f /etc/localtime || ! -f /etc/timezone ]]; then
cp -f /usr/share/zoneinfo/${TIMEZONE} /etc/localtime
+2
View File
@@ -5,6 +5,8 @@ DATA_DB="${DATA_DIR}/db.sqlite3"
DATA_CONFIG="${DATA_DIR}/config.json"
DATA_ATTACHMENTS="${DATA_DIR}/attachments"
BACKUP_DIR="/bitwarden/backup/"
RESTORE_DIR="/bitwarden/restore/"
#################### Function ####################
########################################
# Print colorful message.
+135
View File
@@ -0,0 +1,135 @@
#!/bin/sh
. /app/includes.sh
RESTORE_FILE_DB=""
RESTORE_FILE_CONFIG=""
RESTORE_FILE_ATTACHMENTS=""
RESTORE_FILE_ZIP=""
ZIP_PASSWORD=""
function restore_zip() {
color blue "restore bitwarden_rs backup zip file"
}
function restore_db() {
color blue "restore bitwarden_rs sqlite database"
cp -f ${RESTORE_FILE_DB} ${DATA_DB}
if [[ $? == 0 ]]; then
color green "restore bitwarden_rs sqlite database successful"
else
color red "restore bitwarden_rs sqlite database failed"
fi
}
function restore_config() {
color blue "restore bitwarden_rs config"
cp -f ${RESTORE_FILE_CONFIG} ${DATA_CONFIG}
if [[ $? == 0 ]]; then
color green "restore bitwarden_rs config successful"
else
color red "restore bitwarden_rs config failed"
fi
}
function restore_attachments() {
color blue "restore bitwarden_rs attachments"
rm -rf ${DATA_ATTACHMENTS}
tar -x -C ${DATA_DIR} -f ${RESTORE_FILE_ATTACHMENTS}
if [[ $? == 0 ]]; then
color green "restore bitwarden_rs attachments successful"
else
color red "restore bitwarden_rs attachments failed"
fi
}
function check_restore_file_exist() {
if [[ ! -f "${RESTORE_DIR}/$1" ]]; then
color red "$2: cannot access $1: No such file"
exit 1
fi
}
function restore_file() {
if [[ -n "${RESTORE_FILE_ZIP}" ]]; then
check_restore_file_exist ${RESTORE_FILE_ZIP} "--zip-file"
RESTORE_FILE_ZIP="${RESTORE_DIR}/${RESTORE_FILE_ZIP}"
restore_zip
else
if [[ -n "${RESTORE_FILE_DB}" ]]; then
check_restore_file_exist ${RESTORE_FILE_DB} "--db-file"
RESTORE_FILE_DB="${RESTORE_DIR}/${RESTORE_FILE_DB}"
fi
if [[ -n "${RESTORE_FILE_CONFIG}" ]]; then
check_restore_file_exist ${RESTORE_FILE_CONFIG} "--config-file"
RESTORE_FILE_CONFIG="${RESTORE_DIR}/${RESTORE_FILE_CONFIG}"
fi
if [[ -n "${RESTORE_FILE_ATTACHMENTS}" ]]; then
check_restore_file_exist ${RESTORE_FILE_ATTACHMENTS} "--attachments-file"
RESTORE_FILE_ATTACHMENTS="${RESTORE_DIR}/${RESTORE_FILE_ATTACHMENTS}"
fi
if [[ -n "${RESTORE_FILE_DB}" ]]; then
restore_db
fi
if [[ -n "${RESTORE_FILE_CONFIG}" ]]; then
restore_config
fi
if [[ -n "${RESTORE_FILE_ATTACHMENTS}" ]]; then
restore_attachments
fi
fi
}
function restore() {
while [[ $# -gt 0 ]]; do
case "$1" in
-p|--password)
shift
ZIP_PASSWORD="$1"
shift
;;
--zip-file)
shift
RESTORE_FILE_ZIP=$(basename "$1")
shift
;;
--db-file)
shift
RESTORE_FILE_DB=$(basename "$1")
shift
;;
--config-file)
shift
RESTORE_FILE_CONFIG=$(basename "$1")
shift
;;
--attachments-file)
shift
RESTORE_FILE_ATTACHMENTS=$(basename "$1")
shift
;;
*)
color red "Illegal input"
exit 1
;;
esac
done
mkdir -p ${DATA_DIR}
restore_file
}