From f3ce46e4d46635a844d3a728567f32e061e7f692 Mon Sep 17 00:00:00 2001 From: Paolo Asperti Date: Tue, 7 Sep 2021 23:37:54 +0200 Subject: [PATCH] renamed snapshot to backup --- {auto-snapshot => auto-backup}/Dockerfile | 4 +- auto-backup/README.md | 4 ++ auto-backup/backup.sh | 47 +++++++++++++++++++++ {auto-snapshot => auto-backup}/config.json | 11 ++--- {auto-snapshot => auto-backup}/icon.png | Bin {auto-snapshot => auto-backup}/logo.png | Bin {auto-snapshot => auto-backup}/run.sh | 4 +- auto-snapshot/README.md | 4 -- auto-snapshot/snapshot.sh | 47 --------------------- 9 files changed, 61 insertions(+), 60 deletions(-) rename {auto-snapshot => auto-backup}/Dockerfile (79%) create mode 100644 auto-backup/README.md create mode 100644 auto-backup/backup.sh rename {auto-snapshot => auto-backup}/config.json (62%) rename {auto-snapshot => auto-backup}/icon.png (100%) rename {auto-snapshot => auto-backup}/logo.png (100%) rename {auto-snapshot => auto-backup}/run.sh (58%) delete mode 100644 auto-snapshot/README.md delete mode 100644 auto-snapshot/snapshot.sh diff --git a/auto-snapshot/Dockerfile b/auto-backup/Dockerfile similarity index 79% rename from auto-snapshot/Dockerfile rename to auto-backup/Dockerfile index b13a620..c27c37d 100644 --- a/auto-snapshot/Dockerfile +++ b/auto-backup/Dockerfile @@ -5,10 +5,10 @@ ENV LANG C.UTF-8 # Copy scripts for add-on COPY run.sh / -COPY snapshot.sh / +COPY backup.sh / RUN apk add -U jq bc curl && \ chmod a+x /run.sh && \ - chmod a+x /snapshot.sh + chmod a+x /backup.sh CMD [ "/run.sh" ] diff --git a/auto-backup/README.md b/auto-backup/README.md new file mode 100644 index 0000000..38e30c0 --- /dev/null +++ b/auto-backup/README.md @@ -0,0 +1,4 @@ +## Auto Backup + +This plugins takes a backup of your hassio instance based on the timing you specify in config. Just use standard cron format (min hour day month weekday). +You can specify how many backups you want to mantain (should be >= 1). Note that backups are deleted after a new one is taken, so if something goes wrong, you end up with no recent backup and the oldest valid ones. diff --git a/auto-backup/backup.sh b/auto-backup/backup.sh new file mode 100644 index 0000000..5bc9e8a --- /dev/null +++ b/auto-backup/backup.sh @@ -0,0 +1,47 @@ +#!/bin/sh + +echo +echo "Taking a new backup" + +CONFIG_PATH=/data/options.json +NUM_BACKUPS=$(jq --raw-output ".num_backups" $CONFIG_PATH) +if [ $NUM_BACKUPS -lt 1 ] ; then + NUM_BACKUPS=1 +fi + +# retrieve current backup list +CURRENT_BACKUPS=$( curl -s -H "X-HASSIO-KEY: $HASSIO_TOKEN" http://hassio/backups ) +STATUS=$(echo "$CURRENT_BACKUPS" | jq --raw-output '.result') +if [ ! "$STATUS" = "ok" ] ; then + echo "ERROR: Can't retrieve current backup list." + exit 1 +fi + + +# take backup (this can take a long time) +START_TIMESTAMP=$(date "+%s") +RESULT=$( curl -s -H "X-HASSIO-KEY: $HASSIO_TOKEN" --data '{"name":"Automatic Backup"}' -X POST http://hassio/backups/new/full ) +BACKUP_TIMESTAMP=$(date "+%s") +BACKUP_TIME=$(echo "$BACKUP_TIMESTAMP - $START_TIMESTAMP" | bc) + +# check if backup is ok +STATUS=$(echo "$RESULT" | jq --raw-output '.result') +if [ ! "$STATUS" = "ok" ] ; then + echo "Backup FAILED after $BACKUP_TIME seconds." + exit 1 +fi + +SLUG=$(echo "$RESULT" | jq --raw-output '.data.slug') +echo "Backup $SLUG taken SUCCESSFULLY in $BACKUP_TIME seconds." + +# housekeeping: +# we take the list of backups (which was taken before this backup) +# we sort by date (reversed), remove the protected backups and take +# only a list of slugs +# then we skip the first NUM_BACKUPS slugs and delete all the rest +FIRST_SLUG_TO_DELETE=$(echo "1 + $NUM_BACKUPS" | bc) +echo $CURRENT_BACKUPS | jq --raw-output ".data.backups | sort_by(.date) | reverse[] | select(.slug != \"$SLUG\") | select (.protected==false) | .slug " | tail -n +$FIRST_SLUG_TO_DELETE | while read SLUG_TO_DELETE + do + echo "Removing backup $SLUG_TO_DELETE" + curl -s -H "X-HASSIO-KEY: $HASSIO_TOKEN" -X POST http://hassio/backups/$SLUG_TO_DELETE/remove + done diff --git a/auto-snapshot/config.json b/auto-backup/config.json similarity index 62% rename from auto-snapshot/config.json rename to auto-backup/config.json index 2ae36c3..12eb841 100644 --- a/auto-snapshot/config.json +++ b/auto-backup/config.json @@ -1,9 +1,9 @@ { - "name": "Auto Snapshot", + "name": "Auto Backup", "url": "https://git.asperti.com/paspo/hassio-addons", "version": "0.1.2", - "slug": "auto-snapshot", - "description": "Take hassio snapshots with fixed timings and manage retention", + "slug": "auto-backup", + "description": "Take hassio backups with fixed timings and manage retention", "startup": "before", "boot": "auto", "arch": [ @@ -12,14 +12,15 @@ "armhf", "i386" ], + "homeassistant": "2021.9", "hassio_api": true, "hassio_role": "backup", "options": { "cron": "15 3 * * *", - "num_snapshots": 10 + "num_backups": 10 }, "schema": { "cron": "str", - "num_snapshots": "int" + "num_backups": "int" } } diff --git a/auto-snapshot/icon.png b/auto-backup/icon.png similarity index 100% rename from auto-snapshot/icon.png rename to auto-backup/icon.png diff --git a/auto-snapshot/logo.png b/auto-backup/logo.png similarity index 100% rename from auto-snapshot/logo.png rename to auto-backup/logo.png diff --git a/auto-snapshot/run.sh b/auto-backup/run.sh similarity index 58% rename from auto-snapshot/run.sh rename to auto-backup/run.sh index a8c470d..9bc287c 100644 --- a/auto-snapshot/run.sh +++ b/auto-backup/run.sh @@ -5,12 +5,12 @@ CONFIG_PATH=/data/options.json CRON=$(jq --raw-output ".cron" $CONFIG_PATH) -echo "$CRON /snapshot.sh >> /var/log/cron.log" > /var/spool/cron/crontabs/root +echo "$CRON /backup.sh >> /var/log/cron.log" > /var/spool/cron/crontabs/root # change perms chmod 600 /var/spool/cron/crontabs/root -echo "Auto Snapshot ready." +echo "Auto backup ready." crond touch /var/log/cron.log tail -f /var/log/cron.log \ No newline at end of file diff --git a/auto-snapshot/README.md b/auto-snapshot/README.md deleted file mode 100644 index b0eadf8..0000000 --- a/auto-snapshot/README.md +++ /dev/null @@ -1,4 +0,0 @@ -## Auto Snapshot - -This plugins takes a snapshot of your hassio instance based on the timing you specify in config. Just use standard cron format (min hour day month weekday). -You can specify how many snapshots you want to mantain (should be >= 1). Note that snapshots are deleted after a new one is taken, so if something goes wrong, you end up with no recent snapshots and the oldest valid ones. diff --git a/auto-snapshot/snapshot.sh b/auto-snapshot/snapshot.sh deleted file mode 100644 index 19afb65..0000000 --- a/auto-snapshot/snapshot.sh +++ /dev/null @@ -1,47 +0,0 @@ -#!/bin/sh - -echo -echo "Taking a new snapshot" - -CONFIG_PATH=/data/options.json -NUM_SNAPSHOTS=$(jq --raw-output ".num_snapshots" $CONFIG_PATH) -if [ $NUM_SNAPSHOTS -lt 1 ] ; then - NUM_SNAPSHOTS=1 -fi - -# retrieve current snapshot list -CURRENT_SNAPSHOTS=$( curl -s -H "X-HASSIO-KEY: $HASSIO_TOKEN" http://hassio/snapshots ) -STATUS=$(echo "$CURRENT_SNAPSHOTS" | jq --raw-output '.result') -if [ ! "$STATUS" = "ok" ] ; then - echo "ERROR: Can't retrieve current snapshot list." - exit 1 -fi - - -# take snapshot (this can take a long time) -START_TIMESTAMP=$(date "+%s") -RESULT=$( curl -s -H "X-HASSIO-KEY: $HASSIO_TOKEN" --data '{"name":"Automatic Snapshot"}' -X POST http://hassio/snapshots/new/full ) -SNAPSHOT_TIMESTAMP=$(date "+%s") -SNAPSHOT_TIME=$(echo "$SNAPSHOT_TIMESTAMP - $START_TIMESTAMP" | bc) - -# check if snapshot is ok -STATUS=$(echo "$RESULT" | jq --raw-output '.result') -if [ ! "$STATUS" = "ok" ] ; then - echo "Snapshot FAILED after $SNAPSHOT_TIME seconds." - exit 1 -fi - -SLUG=$(echo "$RESULT" | jq --raw-output '.data.slug') -echo "Snapshot $SLUG taken SUCCESSFULLY in $SNAPSHOT_TIME seconds." - -# housekeeping: -# we take the list of snapshots (which was taken before this snapshot) -# we sort by date (reversed), remove the protected snapshots and take -# only a list of slugs -# then we skip the first NUM_SNAPSHOTS slugs and delete all the rest -FIRST_SLUG_TO_DELETE=$(echo "1 + $NUM_SNAPSHOTS" | bc) -echo $CURRENT_SNAPSHOTS | jq --raw-output ".data.snapshots | sort_by(.date) | reverse[] | select(.slug != \"$SLUG\") | select (.protected==false) | .slug " | tail -n +$FIRST_SLUG_TO_DELETE | while read SLUG_TO_DELETE - do - echo "Removing snapshot $SLUG_TO_DELETE" - curl -s -H "X-HASSIO-KEY: $HASSIO_TOKEN" -X POST http://hassio/snapshots/$SLUG_TO_DELETE/remove - done