diff --git a/scripts/entrypoint.sh b/scripts/entrypoint.sh index 57be212..eb1c35f 100644 --- a/scripts/entrypoint.sh +++ b/scripts/entrypoint.sh @@ -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 diff --git a/scripts/includes.sh b/scripts/includes.sh index 88989a5..b0dc9ca 100644 --- a/scripts/includes.sh +++ b/scripts/includes.sh @@ -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. diff --git a/scripts/restore.sh b/scripts/restore.sh new file mode 100644 index 0000000..bf103d4 --- /dev/null +++ b/scripts/restore.sh @@ -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 +}