Auto Snapshot

This commit is contained in:
Paolo Asperti 2019-03-10 18:30:11 +01:00
parent 0ac819cc26
commit 62b6cb3ffe
Signed by: paspo
GPG Key ID: 06D46905D19D5182
4 changed files with 25 additions and 8 deletions

View File

@ -1,4 +1,4 @@
## Auto Snapshot ## 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). 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 at >=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. 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.

View File

@ -1,9 +1,9 @@
{ {
"name": "Auto Snapshot", "name": "Auto Snapshot",
"url": "https://git.asperti.com/paspo/hassio-addons", "url": "https://git.asperti.com/paspo/hassio-addons",
"version": "0.1.0", "version": "0.1.1",
"slug": "auto-snapshot", "slug": "auto-snapshot",
"description": "Take hassio snapshots with fixed timings and manage retention.", "description": "Take hassio snapshots with fixed timings and manage retention",
"startup": "before", "startup": "before",
"boot": "auto", "boot": "auto",
"arch": [ "arch": [
@ -13,7 +13,7 @@
"i386" "i386"
], ],
"hassio_api": true, "hassio_api": true,
"hassio_role": "admin", "hassio_role": "backup",
"options": { "options": {
"cron": "15 3 * * *", "cron": "15 3 * * *",
"num_snapshots": 10 "num_snapshots": 10

View File

@ -10,4 +10,5 @@ echo "$CRON /snapshot.sh" > /var/spool/cron/crontabs/root
# change perms # change perms
chmod 600 /var/spool/cron/crontabs/root chmod 600 /var/spool/cron/crontabs/root
echo "Auto Snapshot ready."
crond -f crond -f

View File

@ -1,19 +1,30 @@
#!/bin/sh #!/bin/sh
echo
echo "Taking a new snapshot"
CONFIG_PATH=/data/options.json CONFIG_PATH=/data/options.json
NUM_SNAPSHOTS=$(jq --raw-output ".num_snapshots" $CONFIG_PATH) NUM_SNAPSHOTS=$(jq --raw-output ".num_snapshots" $CONFIG_PATH)
# TODO: check if NUM_SNAPSHOTS>1 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 ) CURRENT_SNAPSHOTS=$( curl -s -H "X-HASSIO-KEY: $HASSIO_TOKEN" http://hassio/snapshots )
# TODO: check if CURRENT_SNAPSHOTS returns ok STATUS=$(echo "$CURRENT_SNAPSHOTS" | jq --raw-output '.result')
START_TIMESTAMP=$(date "+%s") if [ ! "$STATUS" = "ok" ] ; then
echo "ERROR: Can't retrieve current snapshot list."
exit 1
fi
# take snapshot (this can take a long time) # 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 ) 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_TIMESTAMP=$(date "+%s")
SNAPSHOT_TIME=$(echo "$SNAPSHOT_TIMESTAMP - $START_TIMESTAMP" | bc) SNAPSHOT_TIME=$(echo "$SNAPSHOT_TIMESTAMP - $START_TIMESTAMP" | bc)
# check if snapshot is ok
STATUS=$(echo "$RESULT" | jq --raw-output '.result') STATUS=$(echo "$RESULT" | jq --raw-output '.result')
if [ ! "$STATUS" = "ok" ] ; then if [ ! "$STATUS" = "ok" ] ; then
echo "Snapshot FAILED after $SNAPSHOT_TIME seconds." echo "Snapshot FAILED after $SNAPSHOT_TIME seconds."
@ -23,6 +34,11 @@ fi
SLUG=$(echo "$RESULT" | jq --raw-output '.data.slug') SLUG=$(echo "$RESULT" | jq --raw-output '.data.slug')
echo "Snapshot $SLUG taken SUCCESSFULLY in $SNAPSHOT_TIME seconds." 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) 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 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 do