Skip to content

Commit

Permalink
feat: add encrypted option for default backup encryption (#186)
Browse files Browse the repository at this point in the history
  • Loading branch information
jcwillox authored Jan 10, 2025
1 parent d7ad1ca commit 0afe34d
Show file tree
Hide file tree
Showing 6 changed files with 75 additions and 39 deletions.
4 changes: 3 additions & 1 deletion custom_components/auto_backup/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
DEFAULT_BACKUP_TIMEOUT,
DATA_AUTO_BACKUP,
DOMAIN,
ATTR_ENCRYPTED,
)
from .handlers import SupervisorHandler, BackupHandler
from .helpers import is_backup
Expand All @@ -53,7 +54,8 @@
vol.Optional(ATTR_PASSWORD): vol.Any(None, cv.string),
vol.Optional(ATTR_KEEP_DAYS): vol.Any(None, vol.Coerce(float)),
vol.Optional(ATTR_DOWNLOAD_PATH): vol.All(cv.ensure_list, [cv.isdir]),
vol.Optional(ATTR_COMPRESSED): cv.boolean,
vol.Optional(ATTR_ENCRYPTED, default=False): cv.boolean,
vol.Optional(ATTR_COMPRESSED, default=True): cv.boolean,
vol.Optional(ATTR_LOCATION): vol.All(
cv.string, lambda v: None if v == "/backup" else v
),
Expand Down
1 change: 1 addition & 0 deletions custom_components/auto_backup/const.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
ATTR_EXCLUDE_FOLDERS = "exclude_folders"
ATTR_DOWNLOAD_PATH = "download_path"
ATTR_COMPRESSED = "compressed"
ATTR_ENCRYPTED = "encrypted"
ATTR_LOCATION = "location"

ATTR_LAST_FAILURE = "last_failure"
Expand Down
14 changes: 14 additions & 0 deletions custom_components/auto_backup/manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
from os.path import join, isfile
from typing import List, Dict, Tuple, Optional

from homeassistant.components.backup.manager import DATA_MANAGER
from homeassistant.components.hassio import (
ATTR_FOLDERS,
ATTR_ADDONS,
Expand Down Expand Up @@ -34,6 +35,7 @@
ATTR_EXCLUDE,
ATTR_KEEP_DAYS,
ATTR_DOWNLOAD_PATH,
ATTR_ENCRYPTED,
)
from .handlers import HassioAPIError, HandlerBase

Expand All @@ -44,6 +46,7 @@ class AutoBackup:
def __init__(self, hass: HomeAssistant, options: Dict, handler: HandlerBase):
self._hass = hass
self._handler = handler
self._manager = hass.data[DATA_MANAGER]
self._auto_purge = options[CONF_AUTO_PURGE]
self._backup_timeout = options[CONF_BACKUP_TIMEOUT] * 60
self._state = 0
Expand Down Expand Up @@ -217,6 +220,17 @@ async def _async_create_backup(self, data: Dict, partial: bool = False):
keep_days = data.pop(ATTR_KEEP_DAYS, None)
download_paths: Optional[List[str]] = data.pop(ATTR_DOWNLOAD_PATH, None)

# support default encryption key
if (
not data.get(ATTR_PASSWORD, "")
and data.get(ATTR_ENCRYPTED)
and self._manager
):
data[ATTR_PASSWORD] = self._manager.config.data.create_backup.password
del data[ATTR_ENCRYPTED]
elif ATTR_ENCRYPTED in data:
del data[ATTR_ENCRYPTED]

### LOG DEBUG INFO ###
# ensure password is scrubbed from logs
password = data.get(ATTR_PASSWORD)
Expand Down
12 changes: 11 additions & 1 deletion custom_components/auto_backup/services.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -60,9 +60,17 @@ backup:
value: media
- label: Local Add-ons
value: addons
encrypted: &encrypted
name: Encrypted
description: |
Encrypt backups with the default encryption key found in backup settings,
if encrypted is false and password is not set backups will be unencrypted.
default: false
selector:
boolean:
password: &password
name: Password
description: Optional password to secure backup.
description: Optional custom password to encrypt the backup with.
example: "1234"
selector:
text:
Expand Down Expand Up @@ -113,6 +121,7 @@ backup_full:
default: { "addons": ["MariaDB"], "folders": ["Local add-ons", "share"] }
selector:
object:
encrypted: *encrypted
password: *password
keep_days: *keep_days
location: *location
Expand Down Expand Up @@ -150,6 +159,7 @@ backup_partial:
value: media
- label: Local Add-ons
value: addons
encrypted: *encrypted
password: *password
keep_days: *keep_days
location: *location
Expand Down
2 changes: 1 addition & 1 deletion docs/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ Auto Backup is an Improved Backup Service for Home Assistant that can Automatica
- [x] Provides a [sensor](sensors.md) to monitor the status of your backups.
- [x] Creates [events](events.md) for when backups are started/created/failed/deleted.
- [x] Supports [generational backup](advanced-examples.md#generational-backups) schemes.
- [x] Supports unencrypted and encrypted backups, including the default encryption added in 2025.1 see [#181](https://github.com/jcwillox/hass-auto-backup/issues/181).
- [x] Supports unencrypted and [encrypted](services.md#encryption) backups, including the default encryption added in 2025.1.

## Blueprints

Expand Down
Loading

0 comments on commit 0afe34d

Please sign in to comment.