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

Commit da2a597

Browse files
Nicolai Bjerre PedersenNixBiks
authored andcommitted
New input keep_local_backup to control how many local snapshots there should be preserved. This fixes #2.
1 parent 802e028 commit da2a597

File tree

4 files changed

+40
-7
lines changed

4 files changed

+40
-7
lines changed

CHANGELOG.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,14 @@ All notable changes to this project will be documented in this file.
33

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

6+
## [v0.3.0] (2018-03-12)
7+
8+
[Full Changelog](https://github.com/mr-bjerre/hassio-remote-backup/compare/v0.2.1...v0.3.0)
9+
10+
### Added
11+
12+
- New input `keep_local_backup` to control how many local snapshots there should be preserved.
13+
614
## [v0.2.1] (2018-03-11)
715

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

README.md

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,10 +47,11 @@ See my [repository of addons][hassio-addons] for more information.
4747
|`ssh_key`|Yes|The ssh key to use. Not that it should *NOT* be password protected.|
4848
|`remote_directory`|Yes|The directory to put the backups on the remote server.|
4949
|`zip_password`|No|If set then the backup will be contained in a password protected zip|
50+
|`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.
5051

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

53-
Personally I've added the following automation to make a daily backup:
54+
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.
5455

5556
_configuration.yaml_
5657
```yaml
@@ -80,7 +81,8 @@ _Add-on configuration_:
8081
"-----END RSA PRIVATE KEY-----"
8182
],
8283
"remote_directory": "~/hassio-backups",
83-
"zip_password": "password_protect_it"
84+
"zip_password": "password_protect_it",
85+
"keep_local_backup": "14"
8486
}
8587
```
8688

remote-backup/config.json

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,14 +14,16 @@
1414
"ssh_user": "",
1515
"ssh_key": [],
1616
"remote_directory": "",
17-
"zip_password": ""
17+
"zip_password": "",
18+
"keep_local_backup": ""
1819
},
1920
"schema": {
2021
"ssh_host": "str",
2122
"ssh_port": "int",
2223
"ssh_user": "str",
2324
"ssh_key": ["str"],
2425
"remote_directory": "str",
25-
"zip_password": "str"
26+
"zip_password": "str",
27+
"keep_local_backup": "match(^(all|[+]?\\d?)$)"
2628
}
2729
}

remote-backup/run.sh

Lines changed: 24 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
#!/bin/bash
2+
set -e
23

34
CONFIG_PATH=/data/options.json
45

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

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

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

5052
function delete-local-backup {
51-
hassio snapshots remove -name "${slug}"
52-
echo "Deleted local backup: ${slug}"
53+
54+
hassio snapshots reload
55+
56+
if [[ ${KEEP_LOCAL_BACKUP} == "all" ]]; then
57+
:
58+
elif [[ -z ${KEEP_LOCAL_BACKUP} ]]; then
59+
echo "Deleting local backup: ${slug}"
60+
hassio snapshots remove -name "${slug}"
61+
else
62+
63+
last_date_to_keep=$(hassio snapshots list | jq .data.snapshots[].date | sort -r | \
64+
head -n "${KEEP_LOCAL_BACKUP}" | tail -n 1 | xargs date -D "%Y-%m-%dT%T" +%s --date )
65+
66+
hassio snapshots list | jq -c .data.snapshots[] | while read backup; do
67+
if [[ $(echo ${backup} | jq .date | xargs date -D "%Y-%m-%dT%T" +%s --date ) -lt ${last_date_to_keep} ]]; then
68+
echo "Deleting local backup: $(echo ${backup} | jq -r .slug)"
69+
hassio snapshots remove -name "$(echo ${backup} | jq -r .slug)"
70+
fi
71+
done
72+
73+
fi
5374
}
5475

5576
function create-local-backup {

0 commit comments

Comments
 (0)