renamed snapshot to backup

This commit is contained in:
2021-09-07 23:37:54 +02:00
parent 1e3d28dc79
commit f3ce46e4d4
9 changed files with 61 additions and 60 deletions

14
auto-backup/Dockerfile Normal file
View File

@@ -0,0 +1,14 @@
ARG BUILD_FROM
FROM $BUILD_FROM
ENV LANG C.UTF-8
# Copy scripts for add-on
COPY run.sh /
COPY backup.sh /
RUN apk add -U jq bc curl && \
chmod a+x /run.sh && \
chmod a+x /backup.sh
CMD [ "/run.sh" ]

4
auto-backup/README.md Normal file
View File

@@ -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.

47
auto-backup/backup.sh Normal file
View File

@@ -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

26
auto-backup/config.json Normal file
View File

@@ -0,0 +1,26 @@
{
"name": "Auto Backup",
"url": "https://git.asperti.com/paspo/hassio-addons",
"version": "0.1.2",
"slug": "auto-backup",
"description": "Take hassio backups with fixed timings and manage retention",
"startup": "before",
"boot": "auto",
"arch": [
"aarch64",
"amd64",
"armhf",
"i386"
],
"homeassistant": "2021.9",
"hassio_api": true,
"hassio_role": "backup",
"options": {
"cron": "15 3 * * *",
"num_backups": 10
},
"schema": {
"cron": "str",
"num_backups": "int"
}
}

BIN
auto-backup/icon.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 58 KiB

BIN
auto-backup/logo.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 58 KiB

16
auto-backup/run.sh Normal file
View File

@@ -0,0 +1,16 @@
#!/bin/bash
set -e
CONFIG_PATH=/data/options.json
CRON=$(jq --raw-output ".cron" $CONFIG_PATH)
echo "$CRON /backup.sh >> /var/log/cron.log" > /var/spool/cron/crontabs/root
# change perms
chmod 600 /var/spool/cron/crontabs/root
echo "Auto backup ready."
crond
touch /var/log/cron.log
tail -f /var/log/cron.log