mirror of
https://github.com/xzeldon/vwdump.git
synced 2025-06-27 19:48:15 +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:
parent
23d0115222
commit
479d78b9e3
@ -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/ \
|
||||
|
18
README.md
18
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/
|
||||
|
16
backup.sh
16
backup.sh
@ -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."
|
22
delete.sh
22
delete.sh
@ -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
|
@ -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.
|
||||
|
43
script.sh
Normal file
43
script.sh
Normal 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}."
|
Loading…
x
Reference in New Issue
Block a user