renamed snapshot to backup
This commit is contained in:
parent
1e3d28dc79
commit
f3ce46e4d4
@ -5,10 +5,10 @@ ENV LANG C.UTF-8
|
|||||||
|
|
||||||
# Copy scripts for add-on
|
# Copy scripts for add-on
|
||||||
COPY run.sh /
|
COPY run.sh /
|
||||||
COPY snapshot.sh /
|
COPY backup.sh /
|
||||||
|
|
||||||
RUN apk add -U jq bc curl && \
|
RUN apk add -U jq bc curl && \
|
||||||
chmod a+x /run.sh && \
|
chmod a+x /run.sh && \
|
||||||
chmod a+x /snapshot.sh
|
chmod a+x /backup.sh
|
||||||
|
|
||||||
CMD [ "/run.sh" ]
|
CMD [ "/run.sh" ]
|
4
auto-backup/README.md
Normal file
4
auto-backup/README.md
Normal 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
47
auto-backup/backup.sh
Normal 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
|
@ -1,9 +1,9 @@
|
|||||||
{
|
{
|
||||||
"name": "Auto Snapshot",
|
"name": "Auto Backup",
|
||||||
"url": "https://git.asperti.com/paspo/hassio-addons",
|
"url": "https://git.asperti.com/paspo/hassio-addons",
|
||||||
"version": "0.1.2",
|
"version": "0.1.2",
|
||||||
"slug": "auto-snapshot",
|
"slug": "auto-backup",
|
||||||
"description": "Take hassio snapshots with fixed timings and manage retention",
|
"description": "Take hassio backups with fixed timings and manage retention",
|
||||||
"startup": "before",
|
"startup": "before",
|
||||||
"boot": "auto",
|
"boot": "auto",
|
||||||
"arch": [
|
"arch": [
|
||||||
@ -12,14 +12,15 @@
|
|||||||
"armhf",
|
"armhf",
|
||||||
"i386"
|
"i386"
|
||||||
],
|
],
|
||||||
|
"homeassistant": "2021.9",
|
||||||
"hassio_api": true,
|
"hassio_api": true,
|
||||||
"hassio_role": "backup",
|
"hassio_role": "backup",
|
||||||
"options": {
|
"options": {
|
||||||
"cron": "15 3 * * *",
|
"cron": "15 3 * * *",
|
||||||
"num_snapshots": 10
|
"num_backups": 10
|
||||||
},
|
},
|
||||||
"schema": {
|
"schema": {
|
||||||
"cron": "str",
|
"cron": "str",
|
||||||
"num_snapshots": "int"
|
"num_backups": "int"
|
||||||
}
|
}
|
||||||
}
|
}
|
Before Width: | Height: | Size: 58 KiB After Width: | Height: | Size: 58 KiB |
Before Width: | Height: | Size: 58 KiB After Width: | Height: | Size: 58 KiB |
@ -5,12 +5,12 @@ CONFIG_PATH=/data/options.json
|
|||||||
|
|
||||||
CRON=$(jq --raw-output ".cron" $CONFIG_PATH)
|
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
|
# change perms
|
||||||
chmod 600 /var/spool/cron/crontabs/root
|
chmod 600 /var/spool/cron/crontabs/root
|
||||||
|
|
||||||
echo "Auto Snapshot ready."
|
echo "Auto backup ready."
|
||||||
crond
|
crond
|
||||||
touch /var/log/cron.log
|
touch /var/log/cron.log
|
||||||
tail -f /var/log/cron.log
|
tail -f /var/log/cron.log
|
@ -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.
|
|
@ -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
|
|
Loading…
Reference in New Issue
Block a user