From df32ec66f1310ee46269383d2e67759f33340c6a Mon Sep 17 00:00:00 2001 From: jmqm Date: Tue, 25 May 2021 16:55:22 -0500 Subject: [PATCH] Add support for sends folder (#1) * Add sends backup * Add send * Add docs for send --- README.md | 9 ++++++++- backup.sh | 22 ++++++++++++++++++++++ entrypoint.sh | 7 +++++++ 3 files changed, 37 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index ea88bdc..2cbc6ca 100644 --- a/README.md +++ b/README.md @@ -23,6 +23,11 @@ Example for backup including attachment folder (see [Environment variables secti docker run -d --restart=always --name bitwarden_backup --volumes-from=bitwarden -e ATTACHMENT_BACKUP_FILE=/data/attachments_backup/attachments bruceforce/bw_backup ``` +Example for backup including send folder (see [Environment variables section](#environment-variables) for more information) +```sh +docker run -d --restart=always --name bitwarden_backup --volumes-from=bitwarden -e SEND_BACKUP_FILE=/data/sends_backup/sends bruceforce/bw_backup +``` + Example for hourly backups ```sh docker run -d --restart=always --name bitwarden_backup --volumes-from=bitwarden -e CRON_TIME="0 * * * *" bruceforce/bw_backup @@ -65,7 +70,9 @@ docker run --rm --volumes-from=bitwarden -e UID=0 -e BACKUP_FILE=/myBackup/backu | DELETE_AFTER | Delete old backups after X many days | | TZ | Set the timezone inside the container [^2] | | ATTACHMENT_BACKUP_FILE | If present, the directory `ATTACHMENT_DIR` are backup in path `ATTACHMENT_BACKUP_FILE` | -| ATTACHMENT_DIR | Path to the Bitwarden attachement file *inside* the container | +| ATTACHMENT_DIR | Path to the Bitwarden attachment folder *inside* the container | +| SEND_BACKUP_FILE | If present, the directory `SEND_DIR` are backup in path `SEND_BACKUP_FILE` | +| SEND_DIR | Path to the Bitwarden send folder *inside* the container | [^1]: The permissions should at least be 700 since the backup folder itself gets the same permissions and with 600 it would not be accessible. [^2]: see for more information diff --git a/backup.sh b/backup.sh index 6e7a083..78e63df 100644 --- a/backup.sh +++ b/backup.sh @@ -17,13 +17,24 @@ else LOCALVAR_ATTACHMENT_BACKUP_FILE="" fi +# Check if SEND_BACKUP_FILE exist. If it's true, attechment are backup. We define var with or without TIMESTAMP +# In anycase, we define var LOCALVAR_SEND_BACKUP_FILE to limit the complexity of code (the number of if-else) +if [ ! -z $SEND_BACKUP_FILE ] +then + LOCALVAR_SEND_BACKUP_FILE="$SEND_BACKUP_FILE" +else + LOCALVAR_SEND_BACKUP_FILE="" +fi + if [ $TIMESTAMP = true ] then FINAL_BACKUP_FILE="$(echo "$BACKUP_FILE")_$(date "+%F-%H%M%S")" FINAL_BACKUP_ATTACHMENT="$(echo "$LOCALVAR_ATTACHMENT_BACKUP_FILE")_$(date "+%F-%H%M%S")" + FINAL_BACKUP_SEND="$(echo "$LOCALVAR_SEND_BACKUP_FILE")_$(date "+%F-%H%M%S")" else FINAL_BACKUP_FILE=$BACKUP_FILE FINAL_BACKUP_ATTACHMENT=$LOCALVAR_ATTACHMENT_BACKUP_FILE + FINAL_BACKUP_SEND=$LOCALVAR_SEND_BACKUP_FILE fi @@ -42,6 +53,12 @@ then /bin/tar -czf ${FINAL_BACKUP_ATTACHMENT}.tgz ${ATTACHMENT_DIR} fi +if [ ! -z $SEND_BACKUP_FILE ] +then + echo "Create tar ${FINAL_BACKUP_SEND}.tgz\n" + /bin/tar -czf ${FINAL_BACKUP_SEND}.tgz ${SEND_DIR} +fi + if [ ! -z $DELETE_AFTER ] && [ $DELETE_AFTER -gt 0 ] then find $(dirname "$BACKUP_FILE") -name "$(basename "$BACKUP_FILE")*" -type f -mtime +$DELETE_AFTER -exec rm -f {} \; -exec echo "Deleted {} after $DELETE_AFTER days" \; @@ -50,4 +67,9 @@ then then find $(dirname "$FINAL_BACKUP_ATTACHMENT") -name "$(basename "$FINAL_BACKUP_ATTACHMENT")*" -type f -mtime +$DELETE_AFTER -exec rm -f {} \; -exec echo "Deleted {} after $DELETE_AFTER days" \; fi + + if [ ! -z $SEND_BACKUP_FILE ] + then + find $(dirname "$FINAL_BACKUP_SEND") -name "$(basename "$FINAL_BACKUP_SEND")*" -type f -mtime +$DELETE_AFTER -exec rm -f {} \; -exec echo "Deleted {} after $DELETE_AFTER days" \; + fi fi diff --git a/entrypoint.sh b/entrypoint.sh index 4fe0238..87b2051 100755 --- a/entrypoint.sh +++ b/entrypoint.sh @@ -22,6 +22,13 @@ then install -o $UID -g $GID -m $BACKUP_FILE_PERMISSIONS -d $ATTACHMENT_BACKUP_DIR fi +SEND_BACKUP_DIR=$(dirname "$SEND_BACKUP_FILE") +if [ ! -d "$SEND_BACKUP_DIR" ] +then + echo "$SEND_BACKUP_DIR not exists. Creating it with owner $UID:$GID and permissions $BACKUP_FILE_PERMISSIONS." + install -o $UID -g $GID -m $BACKUP_FILE_PERMISSIONS -d $SEND_BACKUP_DIR +fi + # For compatibility reasons if [ "$1" = "/backup.sh" ]; then >&2 echo "Using /backup.sh is deprecated and will be removed in future versions! Please use \`manual\` as argument instead"