diff --git a/README.md b/README.md index cc1808d..65255a1 100644 --- a/README.md +++ b/README.md @@ -26,10 +26,10 @@ services: image: jmqm/vaultwarden_backup:latest container_name: vaultwarden_backup volumes: - - "/vaultwarden_data_directory:/data:ro" # Read-only - - "/backup_directory:/backups" + - /vaultwarden_data_directory:/data:ro # Read-only + - /backup_directory:/backups - - "/etc/localtime:/etc/localtime:ro" # Container uses date from host. + - /etc/localtime:/etc/localtime:ro # Container uses date from host. environment: - DELETE_AFTER=30 - CRON_TIME=* */24 * * * # Runs at 12:00 AM. diff --git a/backup.sh b/backup.sh index db65d29..96128f7 100644 --- a/backup.sh +++ b/backup.sh @@ -13,4 +13,4 @@ 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 "[INFO] Created a new backup on $(date +"%F %r")." +echo "[$(date +"%F %r")] Created a new backup." diff --git a/delete.sh b/delete.sh index 1a14961..d42b55e 100644 --- a/delete.sh +++ b/delete.sh @@ -6,8 +6,17 @@ sleep 1m # Go to the backups directory. cd /backups -# Delete tar.xz archives older than x days. -find . -iname "*.tar.xz" -type f -mtime +$DELETE_AFTER -exec rm -f {} \; +# 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) -# Echo that script ran. -echo "[INFO] Deleted files older than $DELETE_AFTER days." +# 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 72e9a7d..72293c7 100755 --- a/entrypoint.sh +++ b/entrypoint.sh @@ -6,7 +6,7 @@ LOGS_FILE="/app/log/backup.log" # If passed "manual", run backup script once ($1 = First argument passed). if [ "$1" = "manual" ]; then - echo "[INFO] Running one-time, started at $(date +"%F %r")." + echo "[$(date +"%F %r")] Running one-time." $BACKUP_CMD exit 0 fi @@ -15,16 +15,16 @@ fi if [ "$(id -u)" -eq 0 ]; then # Clear cron jobs. echo "" | crontab - - echo "[INFO] Cron jobs cleared." + 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 "[INFO] Added backup script to cron jobs." + 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 "[INFO] Added delete script to cron jobs." + echo "[$(date +"%F %r")] Added delete script to cron jobs." fi fi @@ -39,5 +39,5 @@ if [ "$(id -u)" -eq 0 ]; then exec su-exec app:app "$0" "$@" fi -echo "[INFO] Running automatically (${CRON_TIME}), started at $(date +"%F %r")." > "$LOGS_FILE" +echo "[$(date +"%F %r")] Running automatically (${CRON_TIME})." > "$LOGS_FILE" tail -F "$LOGS_FILE" # Keeps terminal open and writes logs.