1
0
mirror of https://github.com/xzeldon/vwdump.git synced 2025-06-27 23:08:14 +03:00

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.
This commit is contained in:
jmqm 2022-01-02 19:37:08 -06:00 committed by GitHub
parent 23d0115222
commit 479d78b9e3
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 61 additions and 62 deletions

View File

@ -15,8 +15,7 @@ ENV GID 100
ENV DELETE_AFTER 0 ENV DELETE_AFTER 0
COPY entrypoint.sh /usr/local/bin/entrypoint.sh COPY entrypoint.sh /usr/local/bin/entrypoint.sh
COPY backup.sh /app/ COPY script.sh /app/
COPY delete.sh /app/
RUN mkdir /app/log/ \ RUN mkdir /app/log/ \
&& chown -R app:app /app/ \ && chown -R app:app /app/ \

View File

@ -25,6 +25,7 @@ services:
backup: backup:
image: jmqm/vaultwarden_backup:latest image: jmqm/vaultwarden_backup:latest
container_name: vaultwarden_backup container_name: vaultwarden_backup
network_mode: none
volumes: volumes:
- /vaultwarden_data_directory:/data:ro # Read-only - /vaultwarden_data_directory:/data:ro # Read-only
- /backup_directory:/backups - /backup_directory:/backups
@ -37,10 +38,10 @@ services:
- GID=100 - GID=100
``` ```
## Volumes ## Volumes _(permissions required)_
`/data` - Vaultwarden's `/data` directory. Recommend setting mount as read-only. `/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 ## Environment Variables
#### ⭐Required, 👍 Recommended #### ⭐Required, 👍 Recommended
@ -48,10 +49,8 @@ services:
| -------------------- | ------------------------------------------------------------------------------------------------------------------------------------- | | -------------------- | ------------------------------------------------------------------------------------------------------------------------------------- |
| UID ⭐| User ID to run the cron job as. | | UID ⭐| User ID to run the cron job as. |
| GID ⭐| Group 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/). | | CRON_TIME 👍| When to run _(default is every 12 hours)_. Info [here][cron-format-wiki] and editor [here][cron-editor]. |
| DELETE_AFTER 👍| Delete backups _X_ days old. _(exclusive to automatic mode)_ | | DELETE_AFTER 👍| _(exclusive to automatic mode)_ Delete backups _X_ days old. Requires `read` and `write` permissions. |
❗ User must have read permission for `/data` directory and read, write and delete permissions for `/backups`.
#### Optional #### Optional
| Environment Variable | Info | | Environment Variable | Info |
@ -62,4 +61,7 @@ services:
## Errors ## Errors
#### Unexpected timestamp #### 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/

View File

@ -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."

View File

@ -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

View File

@ -1,13 +1,12 @@
#!/bin/sh #!/bin/sh
BACKUP_CMD="/sbin/su-exec ${UID}:${GID} /app/backup.sh" SCRIPT_CMD="/sbin/su-exec ${UID}:${GID} /app/script.sh"
DELETE_CMD="/sbin/su-exec ${UID}:${GID} /app/delete.sh" LOGS_FILE="/app/log/log.log"
LOGS_FILE="/app/log/backup.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 if [ "$1" = "manual" ]; then
echo "[$(date +"%F %r")] Running one-time." echo "[$(date +"%F %r")] Running one-time."
$BACKUP_CMD $SCRIPT_CMD
exit 0 exit 0
fi fi
@ -17,15 +16,9 @@ if [ "$(id -u)" -eq 0 ]; then
echo "" | crontab - echo "" | crontab -
echo "[$(date +"%F %r")] Cron jobs cleared." echo "[$(date +"%F %r")] Cron jobs cleared."
# Add backup script to cron jobs. # Add script to cron jobs.
(crontab -l 2>/dev/null; echo "$CRON_TIME $BACKUP_CMD >> $LOGS_FILE 2>&1") | crontab - (crontab -l 2>/dev/null; echo "$CRON_TIME $SCRIPT_CMD >> $LOGS_FILE 2>&1") | crontab -
echo "[$(date +"%F %r")] Added backup script to cron jobs." echo "[$(date +"%F %r")] Added 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
fi fi
# Start crond if it's not running. # Start crond if it's not running.

43
script.sh Normal file
View File

@ -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}."