diff --git a/delete.sh b/delete.sh new file mode 100644 index 0000000..7975567 --- /dev/null +++ b/delete.sh @@ -0,0 +1,10 @@ +#!/bin/sh + +# 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 {} \; + +# Echo that script ran. +echo "Deleted files older than $DELETE_AFTER days." diff --git a/entrypoint.sh b/entrypoint.sh index ab62744..c815670 100755 --- a/entrypoint.sh +++ b/entrypoint.sh @@ -1,30 +1,37 @@ #!/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" -# Run backup script once ($1 = First argument passed). +# 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")." - $BACKUP_CMD - exit 0 + echo "[INFO] Running one-time, started at $(date +"%F %r")." + $BACKUP_CMD + exit 0 fi # Create cron jobs. if [ "$(id -u)" -eq 0 ] && [ "$(grep -c "$BACKUP_CMD" "$CRONFILE")" -eq 0 ]; then - echo "$CRON_TIME $BACKUP_CMD >> $LOGS_FILE 2>&1" | crontab - - # Delete after x days job here. + # Add backup script to cron job list. + echo "$CRON_TIME $BACKUP_CMD >> $LOGS_FILE 2>&1" | crontab - + + # Check if $DELETE_AFTER is not null and is greater than 0. + # If so, add it to the cron job list. + if [ -n "$DELETE_AFTER" ] && [[ "$DELETE_AFTER" -gt 0 ]]; then + echo "$CRON_TIME $DELETE_CMD >> $LOGS_FILE 2>&1" | crontab - + fi fi # Start crond if it's not running. pgrep crond > /dev/null 2>&1 if [ $? -ne 0 ]; then - /usr/sbin/crond -L /app/log/cron.log + /usr/sbin/crond -L /app/log/cron.log fi # Restart script as user "app:app". if [ "$(id -u)" -eq 0 ]; then - exec su-exec app:app "$0" "$@" + exec su-exec app:app "$0" "$@" fi echo "[INFO] Running automatically (${CRON_TIME}), started at $(date +"%F %r")." > "$LOGS_FILE"