From 6974723efe5d30e939539c945fd80fc235e17aff Mon Sep 17 00:00:00 2001 From: jmqm Date: Wed, 26 May 2021 00:11:57 -0500 Subject: [PATCH] Switch from zip to tar.xz (#6) **README.md** - Minor text change. **Dockerfile** - Remove installation of `zip`. - Add installation if `xz`. **backup.sh** - Switch to `tar.xz` archive _(`zip` was messing up permissions)_. - `echo` when backups start and end. --- Dockerfile | 2 +- README.md | 45 +++++++++++++++++++++------------------------ backup.sh | 16 +++++++++++----- 3 files changed, 33 insertions(+), 30 deletions(-) diff --git a/Dockerfile b/Dockerfile index d28b7bc..b764543 100644 --- a/Dockerfile +++ b/Dockerfile @@ -6,7 +6,7 @@ RUN addgroup -S app && adduser -S -G app app RUN apk add --no-cache \ busybox-suid \ su-exec \ - zip \ + xz \ tzdata ENV CRON_TIME "* */12 * * *" diff --git a/README.md b/README.md index d0fa6aa..3ca3be8 100644 --- a/README.md +++ b/README.md @@ -1,4 +1,4 @@ -Backs up vaultwarden files using cron daemon. +Backs up vaultwarden files and folders to `tar.xz` archives. Can be set to run automatically. ## Usage @@ -18,43 +18,40 @@ services: image: jmqm/vaultwarden_backup container_name: vaultwarden_backup volumes: - - "/vaultwarden_data_directory:/data:ro" + - "/vaultwarden_data_directory:/data:ro" # Read-only - "/backup_directory:/backups" - "/etc/localtime:/etc/localtime:ro" # Container uses date from host. environment: - - DELETE_AFTER=30 #optional + - DELETE_AFTER=30 - CRON_TIME=* */24 * * * # Runs every 24 hours. - UID=1024 - GID=100 ``` +## Volumes +`/data` - Vaultwarden's `/data` folder. Recommend setting mount as read-only. + +`/backups` - Where to store backups to. + ## Environment Variables #### ⭐Required, 👍 Recommended -| Variable | Description | -| -------------- | ------------------------------------------------------------------------------------------------------------------------------------- | -| 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. _(unsupported at the moment)_ | +| Environment Variable | Info | +| -------------------- | ------------------------------------------------------------------------------------------------------------------------------------- | +| 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. _(unsupported at the moment)_ | #### Optional -| Variable | Description | -| -------------- | -------------------------------------------------------------------------------------------- | -| TZ ¹ | Timezone inside the container. Can mount `/etc/localtime` instead as well _(recommended)_. | -| LOGFILE | Log file path relative to inside the container. | -| CRONFILE | Cron file path relative to inside the container. | +| Environment Variable | Info | +| -------------------- | -------------------------------------------------------------------------------------------- | +| TZ ¹ | Timezone inside the container. Can mount `/etc/localtime` instead as well _(recommended)_. | +| LOGFILE | Log file path relative to inside the container. | +| CRONFILE | Cron file path relative to inside the container. | ¹ See for more information ## Errors -#### Wrong permissions -`Error: unable to open database file` is most likely caused by permission errors. -Note that sqlite3 creates a lock file in the source directory while running the backup. -So source *AND* destination have to be +rw for the user. You can set the user and group ID -via the `UID` and `GID` environment variables like described above. - -#### Date Time issues / Wrong timestamp -If you need timestamps in your local timezone you should mount `/etc/timezone:/etc/timezone:ro` and `/etc/localtime:/etc/localtime:ro` -like it's done in the [docker-compose.yml](docker-compose.yml). An other possible solution is to set the environment variable accordingly (like `TZ=Europe/Berlin`) -(see for more information). +#### Unexpected timestamp +Mount `etc/localtime` _(recommend mounting as read-only)_ or set `TZ` environment variable. diff --git a/backup.sh b/backup.sh index 75df936..f624e4e 100644 --- a/backup.sh +++ b/backup.sh @@ -1,14 +1,20 @@ #!/bin/sh +cd / -# Create variable for new backup zip. -BACKUP_ZIP=/backups/$(date "+%F_%H.%M.%S").zip +# Store current date in a variable. +TIMESTAMP=$(date "+%F_%H-%M-%S") -# Create variables for the files and directories to be zipped. +# Store new backup archive location in a variable. +BACKUP_LOCATION=/backups/$TIMESTAMP.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 a zip of the files and directories. -cd /data && zip -r $BACKUP_ZIP $BACKUP_DB $BACKUP_RSA $BACKUP_CONFIG $BACKUP_ATTACHMENTS $BACKUP_SENDS && cd .. +# Create an archive of the files and directories. +echo "Starting backup at ${TIMESTAMP}..." +cd /data && tar -Jcf $BACKUP_LOCATION $BACKUP_DB $BACKUP_RSA $BACKUP_CONFIG $BACKUP_ATTACHMENTS $BACKUP_SENDS 2>/dev/null && cd / +echo "Backup completed at ${TIMESTAMP}."