From 479d78b9e305edb1f4e086081bd6032d3c1f2f0b Mon Sep 17 00:00:00 2001 From: jmqm Date: Sun, 2 Jan 2022 19:37:08 -0600 Subject: [PATCH] Combine scripts, refactor, minor README improvements (#26) - Combined backup and delete scripts. - Consequently, deleted `backup.sh` and `delete.sh`. - Changed Dockerfile and `entrypoint.sh`. - Refactored script. - Docker compose example in README now includes network property _(is it called a property?)_. - README volume permission requirements are placed at better locations. --- Dockerfile | 3 +-- README.md | 18 ++++++++++-------- backup.sh | 16 ---------------- delete.sh | 22 ---------------------- entrypoint.sh | 21 +++++++-------------- script.sh | 43 +++++++++++++++++++++++++++++++++++++++++++ 6 files changed, 61 insertions(+), 62 deletions(-) delete mode 100644 backup.sh delete mode 100644 delete.sh create mode 100644 script.sh diff --git a/Dockerfile b/Dockerfile index f4161fa..553e340 100644 --- a/Dockerfile +++ b/Dockerfile @@ -15,8 +15,7 @@ ENV GID 100 ENV DELETE_AFTER 0 COPY entrypoint.sh /usr/local/bin/entrypoint.sh -COPY backup.sh /app/ -COPY delete.sh /app/ +COPY script.sh /app/ RUN mkdir /app/log/ \ && chown -R app:app /app/ \ diff --git a/README.md b/README.md index 292befe..d77efba 100644 --- a/README.md +++ b/README.md @@ -25,6 +25,7 @@ services: backup: image: jmqm/vaultwarden_backup:latest container_name: vaultwarden_backup + network_mode: none volumes: - /vaultwarden_data_directory:/data:ro # Read-only - /backup_directory:/backups @@ -37,10 +38,10 @@ services: - GID=100 ``` -## Volumes -`/data` - Vaultwarden's `/data` directory. Recommend setting mount as read-only. +## Volumes _(permissions required)_ +`/data` _(read)_- Vaultwarden's `/data` directory. Recommend setting mount as read-only. -`/backups` - Where to store backups to. +`/backups` _(write)_ - Where to store backups to. ## Environment Variables #### ⭐Required, πŸ‘ Recommended @@ -48,10 +49,8 @@ services: | -------------------- | ------------------------------------------------------------------------------------------------------------------------------------- | | UID ⭐| User ID to run the cron job as. | | GID ⭐| Group ID to run the cron job as. | -| CRON_TIME πŸ‘| When to run (default is every 12 hours). Info [here](https://www.ibm.com/docs/en/db2oc?topic=task-unix-cron-format) and editor [here](https://crontab.guru/). | -| DELETE_AFTER πŸ‘| Delete backups _X_ days old. _(exclusive to automatic mode)_ | - -❗ User must have read permission for `/data` directory and read, write and delete permissions for `/backups`. +| CRON_TIME πŸ‘| When to run _(default is every 12 hours)_. Info [here][cron-format-wiki] and editor [here][cron-editor]. | +| DELETE_AFTER πŸ‘| _(exclusive to automatic mode)_ Delete backups _X_ days old. Requires `read` and `write` permissions. | #### Optional | Environment Variable | Info | @@ -62,4 +61,7 @@ services: ## Errors #### Unexpected timestamp -Mount `etc/localtime` _(recommend mounting as read-only)_ or set `TZ` environment variable. +Mount `/etc/localtime` _(recommend mounting as read-only)_ or set `TZ` environment variable. + +[cron-format-wiki]: https://www.ibm.com/docs/en/db2oc?topic=task-unix-cron-format +[cron-editor]: https://crontab.guru/ diff --git a/backup.sh b/backup.sh deleted file mode 100644 index 96128f7..0000000 --- a/backup.sh +++ /dev/null @@ -1,16 +0,0 @@ -#!/bin/sh -cd / - -# Store new backup archive location in a variable. -BACKUP_LOCATION=/backups/$(date +"%F_%H-%M-%S").tar.xz - -# Create variables for the files and directories to be archived. -BACKUP_DB=db.sqlite3 # file -BACKUP_RSA=rsa_key* # files -BACKUP_CONFIG=config.json # file -BACKUP_ATTACHMENTS=attachments # directory -BACKUP_SENDS=sends # directory - -# Create an archive of the files and directories. -cd /data && tar -Jcf $BACKUP_LOCATION $BACKUP_DB $BACKUP_RSA $BACKUP_CONFIG $BACKUP_ATTACHMENTS $BACKUP_SENDS 2>/dev/null && cd / -echo "[$(date +"%F %r")] Created a new backup." diff --git a/delete.sh b/delete.sh deleted file mode 100644 index d42b55e..0000000 --- a/delete.sh +++ /dev/null @@ -1,22 +0,0 @@ -#!/bin/sh - -# Sleep for one minute to ensure a backup is made first. -sleep 1m - -# Go to the backups directory. -cd /backups - -# Find all tar.xz archives older than x days and store them in a variable. -TO_DELETE=$(find . -iname "*.tar.xz" -type f -mtime +$DELETE_AFTER) - -# Check if TO_DELETE is empty. -if [ ! -z "$TO_DELETE" ]; then - # Delete tar.xz archives older than x days. - find . -iname "*.tar.xz" -type f -mtime +$DELETE_AFTER -exec rm -f {} \; - - # Echo that archives were deleted. - echo "[$(date +"%F %r")] Deleted archives older than $DELETE_AFTER days." -else - # Echo that there are no archives to delete. - echo "[$(date +"%F %r")] No archives older than $DELETE_AFTER days to delete." -fi diff --git a/entrypoint.sh b/entrypoint.sh index 72293c7..4935fc3 100755 --- a/entrypoint.sh +++ b/entrypoint.sh @@ -1,13 +1,12 @@ #!/bin/sh -BACKUP_CMD="/sbin/su-exec ${UID}:${GID} /app/backup.sh" -DELETE_CMD="/sbin/su-exec ${UID}:${GID} /app/delete.sh" -LOGS_FILE="/app/log/backup.log" +SCRIPT_CMD="/sbin/su-exec ${UID}:${GID} /app/script.sh" +LOGS_FILE="/app/log/log.log" -# If passed "manual", run backup script once ($1 = First argument passed). +# If passed "manual", run script once ($1 = First argument passed). if [ "$1" = "manual" ]; then echo "[$(date +"%F %r")] Running one-time." - $BACKUP_CMD + $SCRIPT_CMD exit 0 fi @@ -17,15 +16,9 @@ if [ "$(id -u)" -eq 0 ]; then echo "" | crontab - echo "[$(date +"%F %r")] Cron jobs cleared." - # Add backup script to cron jobs. - (crontab -l 2>/dev/null; echo "$CRON_TIME $BACKUP_CMD >> $LOGS_FILE 2>&1") | crontab - - echo "[$(date +"%F %r")] Added backup script to cron jobs." - - # Add delete script to cron jobs if DELETE_AFTER is not null and is greater than 0. - if [ -n "$DELETE_AFTER" ] && [ "$DELETE_AFTER" -gt 0 ]; then - (crontab -l 2>/dev/null; echo "$CRON_TIME $DELETE_CMD >> $LOGS_FILE 2>&1") | crontab - - echo "[$(date +"%F %r")] Added delete script to cron jobs." - fi + # Add script to cron jobs. + (crontab -l 2>/dev/null; echo "$CRON_TIME $SCRIPT_CMD >> $LOGS_FILE 2>&1") | crontab - + echo "[$(date +"%F %r")] Added script to cron jobs." fi # Start crond if it's not running. diff --git a/script.sh b/script.sh new file mode 100644 index 0000000..6c4118f --- /dev/null +++ b/script.sh @@ -0,0 +1,43 @@ +#!/bin/sh + +# --------------- [ PREREQUISITES ] --------------- + +EXTENSION="tar.xz" + + +# ------------------ [ BACKUP ] ------------------ + +cd /data + +BACKUP_LOCATION="/backups/$(date +"%F_%H-%M-%S").${EXTENSION}" + +BACKUP_DB="db.sqlite3" # file +BACKUP_RSA="rsa_key*" # files +BACKUP_CONFIG="config.json" # file +BACKUP_ATTACHMENTS="attachments" # directory +BACKUP_SENDS="sends" # directory + +# Back up files and folders. +tar -Jcf $BACKUP_LOCATION $BACKUP_DB $BACKUP_RSA $BACKUP_CONFIG $BACKUP_ATTACHMENTS $BACKUP_SENDS 2>/dev/null + +OUTPUT="${OUTPUT}New backup created" + + +# ------------------ [ DELETE ] ------------------ + +if [ -n "$DELETE_AFTER" ] && [ "$DELETE_AFTER" -gt 0 ]; then + cd /backups + + # Find all archives older than x days, store them in a variable, delete them. + TO_DELETE=$(find . -iname "*.${EXTENSION}" -type f -mtime +$DELETE_AFTER) + find . -iname "*.${EXTENSION}" -type f -mtime +$DELETE_AFTER -exec rm -f {} \; + + OUTPUT="${OUTPUT}, $([ ! -z "$TO_DELETE" ] \ + && echo "deleted $(echo "$TO_DELETE" | wc -l) archives older than ${DELETE_AFTER} days" \ + || echo "no archives older than ${DELETE_AFTER} days to delete")" +fi + + +# ------------------ [ EXIT ] ------------------ + +echo "[$(date +"%F %r")] ${OUTPUT}." \ No newline at end of file