diff --git a/scripts/backup.sh b/scripts/backup.sh index 28b7795..f974a53 100644 --- a/scripts/backup.sh +++ b/scripts/backup.sh @@ -32,7 +32,7 @@ function backup_config() { color blue "backup bitwarden_rs config" if [[ -f "${DATA_CONFIG}" ]]; then - cp -f "${DATA_DIR}/config.json" "${BACKUP_FILE_CONFIG}" + cp -f "${DATA_CONFIG}" "${BACKUP_FILE_CONFIG}" else color yellow "not found bitwarden_rs config, skipping" fi @@ -41,10 +41,8 @@ function backup_config() { function backup_attachments() { color blue "backup bitwarden_rs attachments" - local DATA_ATTACHMENTS="attachments" - - if [[ -d "${DATA_DIR}/${DATA_ATTACHMENTS}" ]]; then - tar -c -C "${DATA_DIR}" -f "${BACKUP_FILE_ATTACHMENTS}" "${DATA_ATTACHMENTS}" + if [[ -d "${DATA_ATTACHMENTS}" ]]; then + tar -c -C "${DATA_ATTACHMENTS_DIRNAME}" -f "${BACKUP_FILE_ATTACHMENTS}" "${DATA_ATTACHMENTS_BASENAME}" color blue "display attachments tar file list" diff --git a/scripts/includes.sh b/scripts/includes.sh index 09eab5f..8932079 100644 --- a/scripts/includes.sh +++ b/scripts/includes.sh @@ -1,10 +1,6 @@ #!/bin/bash ENV_FILE="/.env" -DATA_DIR="/bitwarden/data" -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" RESTORE_EXTRACT_DIR="/bitwarden/extract" @@ -24,7 +20,7 @@ function color() { green) echo -e "\033[32m$2\033[0m" ;; yellow) echo -e "\033[33m$2\033[0m" ;; blue) echo -e "\033[34m$2\033[0m" ;; - none) echo $2 ;; + none) echo "$2" ;; esac } @@ -41,6 +37,30 @@ function check_rclone_connection() { fi } +######################################## +# Check file is exist. +# Arguments: +# file +######################################## +function check_file_exist() { + if [[ ! -f "$1" ]]; then + color red "cannot access $2: No such file" + exit 1 + fi +} + +######################################## +# Check directory is exist. +# Arguments: +# directory +######################################## +function check_dir_exist() { + if [[ ! -d "$1" ]]; then + color red "cannot access $2: No such directory" + exit 1 + fi +} + ######################################## # Send mail by mailx. # Arguments: @@ -142,7 +162,8 @@ function get_env() { ######################################## function init_env() { # export - export_env_file + + init_env_dir # CRON get_env CRON @@ -225,6 +246,11 @@ function init_env() { TIMEZONE="UTC" fi + color yellow "========================================" + color yellow "DATA_DIR: ${DATA_DIR}" + color yellow "DATA_DB: ${DATA_DB}" + color yellow "DATA_CONFIG: ${DATA_CONFIG}" + color yellow "DATA_ATTACHMENTS: ${DATA_ATTACHMENTS}" color yellow "========================================" color yellow "CRON: ${CRON}" color yellow "RCLONE_REMOTE_NAME: ${RCLONE_REMOTE_NAME}" @@ -244,3 +270,23 @@ function init_env() { color yellow "TIMEZONE: ${TIMEZONE}" color yellow "========================================" } + +function init_env_dir() { + # DATA_DIR + get_env DATA_DIR + DATA_DIR="${DATA_DIR:-"/bitwarden/data"}" + check_dir_exist "${DATA_DIR}" + + # DATA_DB + get_env DATA_DB + DATA_DB="${DATA_DB:-"${DATA_DIR}/db.sqlite3"}" + + # DATA_CONFIG + DATA_CONFIG="${DATA_DIR}/config.json" + + # DATA_ATTACHMENTS + get_env DATA_ATTACHMENTS + DATA_ATTACHMENTS="$(realpath "${DATA_ATTACHMENTS:-"${DATA_DIR}/attachments"}")" + DATA_ATTACHMENTS_DIRNAME="$(dirname "${DATA_ATTACHMENTS}")" + DATA_ATTACHMENTS_BASENAME="$(basename "${DATA_ATTACHMENTS}")" +} diff --git a/scripts/restore.sh b/scripts/restore.sh index 98f5277..8f27f0d 100644 --- a/scripts/restore.sh +++ b/scripts/restore.sh @@ -77,8 +77,16 @@ function restore_config() { function restore_attachments() { color blue "restore bitwarden_rs attachments" + # When customizing the attachments folder, the root directory of the tar file + # is the directory name at the time of packing + local RESTORE_FILE_ATTACHMENTS_DIRNAME=$(tar -tf "${DATA_ATTACHMENTS}" | head -n 1 | xargs basename) + local DATA_ATTACHMENTS_EXTRACT="${DATA_ATTACHMENTS}.extract" + rm -rf "${DATA_ATTACHMENTS}" - tar -x -C "${DATA_DIR}" -f "${RESTORE_FILE_ATTACHMENTS}" + mkdir "${DATA_ATTACHMENTS_EXTRACT}" + tar -x -C "${DATA_ATTACHMENTS_EXTRACT}" -f "${RESTORE_FILE_ATTACHMENTS}" + mv "${DATA_ATTACHMENTS_EXTRACT}/${RESTORE_FILE_ATTACHMENTS_DIRNAME}" "${DATA_ATTACHMENTS}" + rf -rf "${DATA_ATTACHMENTS_EXTRACT}" if [[ $? == 0 ]]; then color green "restore bitwarden_rs attachments successful" @@ -187,6 +195,7 @@ function restore() { esac done + init_env_dir check_empty_input check_data_dir_exist