Skip to content
This repository has been archived by the owner on Jan 23, 2024. It is now read-only.

Commit

Permalink
New input keep_local_backup to control how many local snapshots the…
Browse files Browse the repository at this point in the history
…re should be preserved. This fixes #2.
  • Loading branch information
Nicolai Bjerre Pedersen authored and NixBiks committed Mar 11, 2018
1 parent 802e028 commit da2a597
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 7 deletions.
8 changes: 8 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,14 @@ All notable changes to this project will be documented in this file.

The format is based on [Keep a Changelog][keep-a-changelog] this project adheres to [Semantic Versioning][semantic-versioning].

## [v0.3.0] (2018-03-12)

[Full Changelog](https://github.com/mr-bjerre/hassio-remote-backup/compare/v0.2.1...v0.3.0)

### Added

- New input `keep_local_backup` to control how many local snapshots there should be preserved.

## [v0.2.1] (2018-03-11)

[Full Changelog](https://github.com/mr-bjerre/hassio-remote-backup/compare/v0.2.0...v0.2.1)
Expand Down
6 changes: 4 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -47,10 +47,11 @@ See my [repository of addons][hassio-addons] for more information.
|`ssh_key`|Yes|The ssh key to use. Not that it should *NOT* be password protected.|
|`remote_directory`|Yes|The directory to put the backups on the remote server.|
|`zip_password`|No|If set then the backup will be contained in a password protected zip|
|`keep_local_backup`|No|Control how many local backups you want to preserve. Default (`""`) is to keep no local backups created from this addon. If `all` then all loocal backups will be preserved. A positive integer will determine how many of the latest backups will be preserved. Note this will delete other local backups created outside this addon.

## <a name='example'></a>Example: daily backups at 4 AM

Personally I've added the following automation to make a daily backup:
Personally I've added the following automation to make a daily backup. It is password-protected and the last two weeks of snapshots are kept locally as well.

_configuration.yaml_
```yaml
Expand Down Expand Up @@ -80,7 +81,8 @@ _Add-on configuration_:
"-----END RSA PRIVATE KEY-----"
],
"remote_directory": "~/hassio-backups",
"zip_password": "password_protect_it"
"zip_password": "password_protect_it",
"keep_local_backup": "14"
}
```

Expand Down
6 changes: 4 additions & 2 deletions remote-backup/config.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,16 @@
"ssh_user": "",
"ssh_key": [],
"remote_directory": "",
"zip_password": ""
"zip_password": "",
"keep_local_backup": ""
},
"schema": {
"ssh_host": "str",
"ssh_port": "int",
"ssh_user": "str",
"ssh_key": ["str"],
"remote_directory": "str",
"zip_password": "str"
"zip_password": "str",
"keep_local_backup": "match(^(all|[+]?\\d?)$)"
}
}
27 changes: 24 additions & 3 deletions remote-backup/run.sh
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#!/bin/bash
set -e

CONFIG_PATH=/data/options.json

Expand All @@ -9,6 +10,7 @@ SSH_USER=$(jq --raw-output ".ssh_user" $CONFIG_PATH)
SSH_KEY=$(jq --raw-output ".ssh_key[]" $CONFIG_PATH)
REMOTE_DIRECTORY=$(jq --raw-output ".remote_directory" $CONFIG_PATH)
ZIP_PASSWORD=$(jq --raw-output '.zip_password' $CONFIG_PATH)
KEEP_LOCAL_BACKUP=$(jq --raw-output '.keep_local_backup' $CONFIG_PATH)

# create variables
SSH_ID="${HOME}/.ssh/id"
Expand Down Expand Up @@ -36,7 +38,7 @@ function add-ssh-key {
function copy-backup-to-remote {

cd /backup/
if [[ ! $ZIP_PASSWORD ]]; then
if [[ -z $ZIP_PASSWORD ]]; then
echo "Copying ${slug}.tar to ${REMOTE_DIRECTORY} on ${SSH_HOST} using SCP"
scp -F "${HOME}/.ssh/config" "${slug}.tar" remote:"${REMOTE_DIRECTORY}"
else
Expand All @@ -48,8 +50,27 @@ function copy-backup-to-remote {
}

function delete-local-backup {
hassio snapshots remove -name "${slug}"
echo "Deleted local backup: ${slug}"

hassio snapshots reload

if [[ ${KEEP_LOCAL_BACKUP} == "all" ]]; then
:
elif [[ -z ${KEEP_LOCAL_BACKUP} ]]; then
echo "Deleting local backup: ${slug}"
hassio snapshots remove -name "${slug}"
else

last_date_to_keep=$(hassio snapshots list | jq .data.snapshots[].date | sort -r | \
head -n "${KEEP_LOCAL_BACKUP}" | tail -n 1 | xargs date -D "%Y-%m-%dT%T" +%s --date )

hassio snapshots list | jq -c .data.snapshots[] | while read backup; do
if [[ $(echo ${backup} | jq .date | xargs date -D "%Y-%m-%dT%T" +%s --date ) -lt ${last_date_to_keep} ]]; then
echo "Deleting local backup: $(echo ${backup} | jq -r .slug)"
hassio snapshots remove -name "$(echo ${backup} | jq -r .slug)"
fi
done

fi
}

function create-local-backup {
Expand Down

0 comments on commit da2a597

Please sign in to comment.