diff --git a/CHANGELOG.md b/CHANGELOG.md index c36d3d6..f980126 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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) diff --git a/README.md b/README.md index 7887cf0..c740a01 100644 --- a/README.md +++ b/README.md @@ -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. ## 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 @@ -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" } ``` diff --git a/remote-backup/config.json b/remote-backup/config.json index 7c80f8a..390e5fb 100644 --- a/remote-backup/config.json +++ b/remote-backup/config.json @@ -14,7 +14,8 @@ "ssh_user": "", "ssh_key": [], "remote_directory": "", - "zip_password": "" + "zip_password": "", + "keep_local_backup": "" }, "schema": { "ssh_host": "str", @@ -22,6 +23,7 @@ "ssh_user": "str", "ssh_key": ["str"], "remote_directory": "str", - "zip_password": "str" + "zip_password": "str", + "keep_local_backup": "match(^(all|[+]?\\d?)$)" } } diff --git a/remote-backup/run.sh b/remote-backup/run.sh index 1aa5007..dbd25b4 100755 --- a/remote-backup/run.sh +++ b/remote-backup/run.sh @@ -1,4 +1,5 @@ #!/bin/bash +set -e CONFIG_PATH=/data/options.json @@ -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" @@ -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 @@ -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 {