From f678ec65b96c10ae98393edfc780c2da7d72c9cf Mon Sep 17 00:00:00 2001 From: ViViDboarder Date: Mon, 29 Nov 2021 10:39:52 -0800 Subject: [PATCH] Initial addition of timegaps pruning Might want to move from two cron tasks to a single one where rclone and prune are executed sequentially --- rclone/DOCS.md | 12 ++++++++++++ rclone/Dockerfile | 5 +++++ rclone/config.yml | 8 ++++++++ rclone/rootfs/etc/cont-init.d/rclone.sh | 12 ++++++------ rclone/rootfs/etc/cont-init.d/timegaps.sh | 16 ++++++++++++++++ 5 files changed, 47 insertions(+), 6 deletions(-) create mode 100755 rclone/rootfs/etc/cont-init.d/timegaps.sh diff --git a/rclone/DOCS.md b/rclone/DOCS.md index 32cabf7..4530282 100755 --- a/rclone/DOCS.md +++ b/rclone/DOCS.md @@ -27,6 +27,18 @@ The rclone destination in the form `:`. See note above on how to se Rclone command to execute (`sync`, `copy`, or `move`) +### Option: `prune.rules` + +Rules for pruning backups based on time gap configuration. This is done with a tool caled [timegaps](https://gehrcke.de/timegaps/) and is optional. + +### Option: `prune.cron` + +Cron schedule to run the prune job. Need help? https://crontab.guru/ + +### Option: `prune.delete` + +Rather than logging, if set to true, this will delete backups based on the rules set in `prune.rules`. + ### Option: `credentials.username` Username to view web UI diff --git a/rclone/Dockerfile b/rclone/Dockerfile index 3e8d7d4..abd8d0a 100755 --- a/rclone/Dockerfile +++ b/rclone/Dockerfile @@ -6,9 +6,14 @@ ENV LANG C.UTF-8 ARG BUILD_ARCH=aarch64 ARG VERSION=v1.55.1 +# Install rclone COPY install_rclone.sh / RUN /install_rclone.sh "${VERSION}" "${BUILD_ARCH}" +# Add timegaps for pruning backups +RUN apk add python3=3.9.5-r2 py3-pip=20.3.4-r1 +RUN pip install timegaps==0.1.1 + ENV XDG_CONFIG_HOME=/data COPY rootfs / diff --git a/rclone/config.yml b/rclone/config.yml index de483eb..5fa71bf 100755 --- a/rclone/config.yml +++ b/rclone/config.yml @@ -23,6 +23,10 @@ options: cron: "0 * * * *" destination: ":" sync_command: sync + prune: + rules: "" + cron: "" + delete: false credentials: username: homeassistant password: homeassistant @@ -34,6 +38,10 @@ schema: cron: str destination: str sync_command: list(sync|copy|move) + prune: + rules: str + cron: str + delete: bool credentials: username: str password: str diff --git a/rclone/rootfs/etc/cont-init.d/rclone.sh b/rclone/rootfs/etc/cont-init.d/rclone.sh index 9a67d7c..666916b 100755 --- a/rclone/rootfs/etc/cont-init.d/rclone.sh +++ b/rclone/rootfs/etc/cont-init.d/rclone.sh @@ -8,13 +8,13 @@ bashio::config.require.password 'credentials.password' bashio::config.suggest.true 'tls.ssl' bashio::config.require.ssl 'tls' 'tls.certfile' 'tls.keyfile' -sync_command=$(bashio::config 'sync_command') -destination=$(bashio::config 'destination') +SYNC_COMMAND=$(bashio::config 'sync_command') +DESTINATION=$(bashio::config 'destination') -username=$(bashio::config 'credentials.username') -password=$(bashio::config 'credentials.password') +USERNAME=$(bashio::config 'credentials.username') +PASSWORD=$(bashio::config 'credentials.password') -command="rclone rc --user \"$username\" --pass \"$password\" sync/$sync_command srcFs=/backup dstFs=$destination _async=true" +command="rclone rc --user \"$USERNAME\" --pass \"$PASSWORD\" sync/$SYNC_COMMAND srcFs=/backup dstFs=$DESTINATION _async=true" -echo "$(bashio::config 'cron') $command" > /etc/crontabs/root +echo "$(bashio::config 'cron') $command" >> /etc/crontabs/root crontab -l diff --git a/rclone/rootfs/etc/cont-init.d/timegaps.sh b/rclone/rootfs/etc/cont-init.d/timegaps.sh new file mode 100755 index 0000000..2fa2698 --- /dev/null +++ b/rclone/rootfs/etc/cont-init.d/timegaps.sh @@ -0,0 +1,16 @@ +#! /usr/bin/with-contenv bashio + +bashio::log.info "Configuring pruning with timegaps..." +TIMEGAPS_RULES=$(bashio::config 'prune.rules') +TIMEGAPS_CRON=$(bashio::config 'prune.cron') +TIMEGAPS_DELETE="" +if bashio::config.true 'prune.delete'; then + TIMEGAPS_DELETE="--delete" +fi + +if [ -z "$TIMEGAPS_RULES" ] && [ -z "$TIMEGAPS_CRON" ]; then + COMMAND="timegaps $TIMEGAPS_DELETE $TIMEGAPS_RULES /backups/*.tar" + + echo "$TIMEGAPS_CRON $COMMAND" >> /etc/crontabs/root + crontab -l +fi