Skip to content

Commit

Permalink
Merge pull request #14 from ikifar2012/2021.4.0
Browse files Browse the repository at this point in the history
2021.4.0
  • Loading branch information
ikifar2012 authored Apr 4, 2021
2 parents a899b77 + 30f62c3 commit 565ad7a
Show file tree
Hide file tree
Showing 6 changed files with 67 additions and 39 deletions.
10 changes: 10 additions & 0 deletions remote-backup/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,13 @@
# 2021.4.0

- Changed snapshot date scheme `%Y-%m-%d %H-%M` to improve compatibility
- Added `custom_prefix` Allows you to change the name prefixing the date of the snapshot, by default this is set to `Automated backup`
- Added `friendly_name` Allows the snapshot to be renamed on the destination server to match the name in the Home Assistant UI
- Upgraded Base to 9.1.6
- Upgraded Home Assistant CLI to 4.11.3
- Reformatted code
- Addresses issue #13

# 2021.3.0

- Upgraded Home Assistant CLI to 4.11.0
Expand Down
8 changes: 6 additions & 2 deletions remote-backup/DOCS.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
Below is an example configuration:
```yaml
ssh_enabled: true
friendly_name: true
custom_prefix: Automated backup
ssh_host: ip address
ssh_port: 22
ssh_user: username
Expand All @@ -18,11 +20,13 @@ rsync_password: ''
# Options
|Parameter|Required|Description|
|---------|--------|-----------|
|`ssh_enabled`|No|Allows you to disable or enable the SSH function
|`ssh_enabled`|No|Allows you to disable or enable the SSH function|
|`friendly_name`|Yes|Allows the snapshot to be renamed on the destination server to match the name in the Home Assistant UI|
|`custom_prefix`|Yes|Allows you to change the name prefixing the date of the snapshot, by default this is set to `Automated backup`|
|`ssh_host`|Yes|The hostname or IP address of the file server|
|`ssh_port`|Yes|The port used for `SCP`|
|`ssh_user`|Yes|The username used for `SCP`|
|`ssh_key`|Yes|The filename of the SSH key, this must be located in the `ssl` directory of Home Assistant which can be accessed through SAMBA under the share name `ssl`|
|`remote_directory`|Yes|The destination directory where the snapshots will be placed|
|`zip_password`|No|If set then the backup will be contained in a password protected zip file|
|`keep_local_backup`|No|Control how many local backups you want to preserve on the Home Assistant host. The default (`""`) is to keep no local backups created from this addon. To keep all backups set this to `all` then all local backups will be preserved. This can also be set with a number to preserve only the specified amount
|`keep_local_backup`|No|Control how many local backups you want to preserve on the Home Assistant host. The default (`""`) is to keep no local backups created from this addon. To keep all backups set this to `all` then all local backups will be preserved. This can also be set with a number to preserve only the specified amount|
2 changes: 1 addition & 1 deletion remote-backup/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ RUN apk add --no-cache jq openssh-client zip sshpass rsync wget
# Hass.io CLI
ARG BUILD_ARCH
ARG CLI_VERSION
RUN wget -O /usr/bin/ha "https://github.com/home-assistant/cli/releases/download/4.11.0/ha_${BUILD_ARCH}" \
RUN wget -O /usr/bin/ha "https://github.com/home-assistant/cli/releases/download/4.11.3/ha_${BUILD_ARCH}" \
&& chmod a+x /usr/bin/ha

# Copy data
Expand Down
10 changes: 5 additions & 5 deletions remote-backup/build.json
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
{
"squash": false,
"build_from": {
"aarch64": "ghcr.io/hassio-addons/base/aarch64:9.1.5",
"amd64": "ghcr.io/hassio-addons/base/amd64:9.1.5",
"armhf": "ghcr.io/hassio-addons/base/armhf:9.1.5",
"armv7": "ghcr.io/hassio-addons/base/armv7:9.1.5",
"i386": "ghcr.io/hassio-addons/base/i386:9.1.5"
"aarch64": "ghcr.io/hassio-addons/base/aarch64:9.1.6",
"amd64": "ghcr.io/hassio-addons/base/amd64:9.1.6",
"armhf": "ghcr.io/hassio-addons/base/armhf:9.1.6",
"armv7": "ghcr.io/hassio-addons/base/armv7:9.1.6",
"i386": "ghcr.io/hassio-addons/base/i386:9.1.6"
},
"args": {}
}
6 changes: 5 additions & 1 deletion remote-backup/config.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "Remote Backup",
"version": "2021.3.0",
"version": "2021.4.0",
"slug": "remote_backup",
"description": "Automatically create and backup HA snapshots using SCP",
"url": "https://github.com/ikifar2012/remote-backup-addon/blob/master/README.md",
Expand All @@ -19,6 +19,8 @@
"map": ["config", "addons", "share", "ssl", "backup:rw"],
"options": {
"ssh_enabled": true,
"friendly_name": true,
"custom_prefix":"Automated backup",
"ssh_host": "",
"ssh_port": 22,
"ssh_user": "",
Expand All @@ -34,6 +36,8 @@
},
"schema": {
"ssh_enabled": "bool",
"friendly_name": "bool",
"custom_prefix":"str",
"ssh_host": "str",
"ssh_port": "int",
"ssh_user": "str",
Expand Down
70 changes: 40 additions & 30 deletions remote-backup/run.sh
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

# parse inputs from options
SSH_ENABLED=$(bashio::config "ssh_enabled")
FRIENDLY_NAME=$(bashio::config "friendly_name")
CUSTOM_PREFIX=$(bashio::config "custom_prefix")
SSH_HOST=$(bashio::config "ssh_host")
SSH_PORT=$(bashio::config "ssh_port")
SSH_USER=$(bashio::config "ssh_user")
Expand Down Expand Up @@ -41,6 +43,13 @@ function add-ssh-key {
fi
}

function create-local-backup {
name="${CUSTOM_PREFIX} $(date +'%Y-%m-%d %H-%M')"
echo "Creating local backup: \"${name}\""
slug=$(ha snapshots new --raw-json --name="${name}" | jq --raw-output '.data.slug')
echo "Backup created: ${slug}"
}

function copy-backup-to-remote {

if [ "$SSH_ENABLED" = true ] ; then
Expand All @@ -53,40 +62,18 @@ function copy-backup-to-remote {
zip -P "$ZIP_PASSWORD" "${slug}.zip" "${slug}".tar
scp -F "${HOME}/.ssh/config" "${slug}.zip" remote:"${REMOTE_DIRECTORY}" && rm "${slug}.zip"
fi
fi
}

function delete-local-backup {

ha snapshots reload

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

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

ha snapshots list --raw-json | 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)"
ha snapshots remove "$(echo ${backup} | jq -r .slug)"
if [ "$FRIENDLY_NAME" = true ] ; then
if [[ -z $ZIP_PASSWORD ]]; then
echo "Renaming ${slug}.tar to ${name}.tar"
ssh remote "mv "${REMOTE_DIRECTORY}"/${slug}.tar "${REMOTE_DIRECTORY}"/\"${name}\".tar"
else
echo "Renaming ${slug}.zip to ${name}.zip"
ssh remote "mv "${REMOTE_DIRECTORY}"/${slug}.zip "${REMOTE_DIRECTORY}"/\"${name}\".zip"
fi
done

fi
fi
}

function create-local-backup {
name="Automated backup $(date +'%Y-%m-%d %H:%M')"
echo "Creating local backup: \"${name}\""
slug=$(ha snapshots new --raw-json --name="${name}" | jq --raw-output '.data.slug')
echo "Backup created: ${slug}"
}

function rsync_folders {

if [ "$RSYNC_ENABLED" = true ] ; then
Expand All @@ -111,6 +98,29 @@ function rsync_folders {
fi
}

function delete-local-backup {

ha snapshots reload

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

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

ha snapshots list --raw-json | 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)"
ha snapshots remove "$(echo ${backup} | jq -r .slug)"
fi
done

fi
}

add-ssh-key
create-local-backup
Expand Down

0 comments on commit 565ad7a

Please sign in to comment.