Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make local backup a backup agent #130623

Merged
merged 25 commits into from
Nov 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
25 commits
Select commit Hold shift + click to select a range
400f792
Make local backup a backup agent
emontnemery Nov 14, 2024
0ce1fd7
Adjust
emontnemery Nov 14, 2024
f103835
Adjust
emontnemery Nov 14, 2024
a91b4db
Merge remote-tracking branch 'upstream/allthebackupchanges' into loca…
emontnemery Nov 15, 2024
0e128d2
Adjust
emontnemery Nov 15, 2024
8bdc819
Adjust tests
emontnemery Nov 15, 2024
8f6ae45
Adjust
emontnemery Nov 16, 2024
1858092
Adjust
emontnemery Nov 18, 2024
8edf464
Adjust docstring
emontnemery Nov 18, 2024
068e2d4
Adjust
emontnemery Nov 18, 2024
19c8ebb
Protect members of CoreLocalBackupAgent
emontnemery Nov 18, 2024
f9b3cb7
Remove redundant check for file
emontnemery Nov 18, 2024
2c1e58f
Make the backup.create service use the first local agent
emontnemery Nov 18, 2024
b06e8d6
Add BackupAgent.async_get_backup
emontnemery Nov 18, 2024
10d0fec
Fix some TODOs
emontnemery Nov 18, 2024
e788d00
Add support for downloading backup from a remote agent
emontnemery Nov 18, 2024
8a9cdfa
Fix restore
emontnemery Nov 18, 2024
244416b
Fix test
emontnemery Nov 18, 2024
6ab3e5d
Adjust kitchen_sink test
emontnemery Nov 18, 2024
46b085e
Remove unused method BackupManager.async_get_backup_path
emontnemery Nov 18, 2024
9609b55
Merge remote-tracking branch 'upstream/allthebackupchanges' into loca…
emontnemery Nov 18, 2024
7f45aa9
Re-enable kitchen sink test
emontnemery Nov 18, 2024
a251813
Remove BaseBackupManager.async_upload_backup
emontnemery Nov 18, 2024
4edf64c
Support restore from remote agent
emontnemery Nov 18, 2024
5baa39e
Fix review comments
emontnemery Nov 18, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions homeassistant/components/backup/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
async def async_setup(hass: HomeAssistant, config: ConfigType) -> bool:
"""Set up the Backup integration."""
hass.data[DOMAIN] = backup_manager = BackupManager(hass)
await backup_manager.async_setup()

with_hassio = is_hassio(hass)

Expand All @@ -48,8 +49,10 @@ async def async_setup(hass: HomeAssistant, config: ConfigType) -> bool:

async def async_handle_create_service(call: ServiceCall) -> None:
"""Service handler for creating backups."""
agent_id = list(backup_manager.local_backup_agents)[0]
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Side note: We should probably handle missing local agents here and raise HomeAssistantError.

await backup_manager.async_create_backup(
addons_included=None,
agent_ids=[agent_id],
database_included=True,
folders_included=None,
name=None,
Expand Down
20 changes: 20 additions & 0 deletions homeassistant/components/backup/agent.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,26 @@ async def async_upload_backup(
async def async_list_backups(self, **kwargs: Any) -> list[UploadedBackup]:
"""List backups."""

@abc.abstractmethod
async def async_get_backup(
self,
*,
slug: str,
**kwargs: Any,
) -> UploadedBackup | None:
"""Return a backup."""


class LocalBackupAgent(BackupAgent):
"""Local backup agent."""

@abc.abstractmethod
def get_backup_path(self, slug: str) -> Path:
"""Return the local path to a backup.

The method should return the path to the backup file with the specified slug.
"""


class BackupAgentPlatformProtocol(Protocol):
"""Define the format of backup platforms which implement backup agents."""
Expand Down
150 changes: 150 additions & 0 deletions homeassistant/components/backup/backup.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,150 @@
"""Local backup support for Core and Container installations."""

from __future__ import annotations

from dataclasses import asdict, dataclass
import json
from pathlib import Path
from tarfile import TarError
from typing import Any

from homeassistant.core import HomeAssistant

from .agent import BackupAgent, LocalBackupAgent, UploadedBackup
from .const import LOGGER
from .models import BackupUploadMetadata
from .util import read_backup


async def async_get_backup_agents(
hass: HomeAssistant,
**kwargs: Any,
) -> list[BackupAgent]:
"""Return the local backup agent."""
return [CoreLocalBackupAgent(hass)]


@dataclass(slots=True)
class LocalBackup(UploadedBackup):
"""Local backup class."""

path: Path

def as_dict(self) -> dict:
"""Return a dict representation of this backup."""
return {**asdict(self), "path": self.path.as_posix()}
emontnemery marked this conversation as resolved.
Show resolved Hide resolved


class CoreLocalBackupAgent(LocalBackupAgent):
"""Local backup agent for Core and Container installations."""

name = "local"
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In a separate PR, we may want to make name human readable and add a slug-type id.


def __init__(self, hass: HomeAssistant) -> None:
"""Initialize the backup agent."""
super().__init__()
MartinHjelmare marked this conversation as resolved.
Show resolved Hide resolved
self._hass = hass
self._backup_dir = Path(hass.config.path("backups"))
self._backups: dict[str, LocalBackup] = {}
self._loaded_backups = False

async def load_backups(self) -> None:
"""Load data of stored backup files."""
backups = await self._hass.async_add_executor_job(self._read_backups)
LOGGER.debug("Loaded %s local backups", len(backups))
self._backups = backups
self._loaded_backups = True

def _read_backups(self) -> dict[str, LocalBackup]:
"""Read backups from disk."""
backups: dict[str, LocalBackup] = {}
for backup_path in self._backup_dir.glob("*.tar"):
try:
base_backup = read_backup(backup_path)
backup = LocalBackup(
id=base_backup.slug,
slug=base_backup.slug,
name=base_backup.name,
date=base_backup.date,
path=backup_path,
size=round(backup_path.stat().st_size / 1_048_576, 2),
protected=base_backup.protected,
)
backups[backup.slug] = backup
except (OSError, TarError, json.JSONDecodeError, KeyError) as err:
LOGGER.warning("Unable to read backup %s: %s", backup_path, err)
return backups

async def async_download_backup(
self,
*,
id: str,
path: Path,
**kwargs: Any,
) -> None:
"""Download a backup file."""
raise NotImplementedError
Comment on lines +78 to +86
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe a local agent should not have to implement this method?


async def async_upload_backup(
self,
*,
path: Path,
metadata: BackupUploadMetadata,
**kwargs: Any,
) -> None:
"""Upload a backup."""
self._backups[metadata.slug] = LocalBackup(
id=metadata.slug, # Do we need another ID?
Copy link
Contributor Author

@emontnemery emontnemery Nov 14, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same comment as above about the ID

slug=metadata.slug,
name=metadata.name,
date=metadata.date,
path=path,
size=round(path.stat().st_size / 1_048_576, 2),
protected=metadata.protected,
)

async def async_list_backups(self, **kwargs: Any) -> list[UploadedBackup]:
"""List backups."""
if not self._loaded_backups:
await self.load_backups()
return list(self._backups.values())

async def async_get_backup(
MartinHjelmare marked this conversation as resolved.
Show resolved Hide resolved
self,
*,
slug: str,
**kwargs: Any,
) -> UploadedBackup | None:
"""Return a backup."""
if not self._loaded_backups:
await self.load_backups()

if not (backup := self._backups.get(slug)):
return None

if not await self._hass.async_add_executor_job(backup.path.exists):
LOGGER.debug(
(
"Removing tracked backup (%s) that does not exists on the expected"
" path %s"
),
backup.slug,
backup.path,
)
self._backups.pop(slug)
return None

return backup

def get_backup_path(self, slug: str) -> Path:
"""Return the local path to a backup."""
return self._backup_dir / f"{slug}.tar"

async def async_remove_backup(self, *, slug: str, **kwargs: Any) -> None:
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This isn't part of the agent interface yet. I'm working on adding async_delete_backup.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We can keep it for now. I'll replace it later.

"""Remove a backup."""
if (backup := await self.async_get_backup(slug=slug)) is None:
return

await self._hass.async_add_executor_job(backup.path.unlink, True) # type: ignore[attr-defined]
LOGGER.debug("Removed backup located at %s", backup.path) # type: ignore[attr-defined]
self._backups.pop(slug)
1 change: 1 addition & 0 deletions homeassistant/components/backup/const.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
from .manager import BaseBackupManager
from .models import BaseBackup

BUF_SIZE = 2**20 * 4 # 4MB
DOMAIN = "backup"
DATA_MANAGER: HassKey[BaseBackupManager[BaseBackup]] = HassKey(DOMAIN)
LOGGER = getLogger(__package__)
Expand Down
34 changes: 29 additions & 5 deletions homeassistant/components/backup/http.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@
from .const import DATA_MANAGER
from .manager import BackupManager

# pylint: disable=fixme
# TODO: Don't forget to remove this when the implementation is complete


@callback
def async_register_http_views(hass: HomeAssistant) -> None:
Expand All @@ -39,15 +42,32 @@ async def get(
"""Download a backup file."""
if not request["hass_user"].is_admin:
return Response(status=HTTPStatus.UNAUTHORIZED)
try:
agent_id = request.query.getone("agent_id")
except KeyError:
return Response(status=HTTPStatus.BAD_REQUEST)

manager = cast(BackupManager, request.app[KEY_HASS].data[DATA_MANAGER])
backup = await manager.async_get_backup(slug=slug)

if backup is None or not backup.path.exists():
if agent_id not in manager.backup_agents:
return Response(status=HTTPStatus.BAD_REQUEST)
agent = manager.backup_agents[agent_id]
backup = await agent.async_get_backup(slug=slug)

# We don't need to check if the path exists, aiohttp.FileResponse will handle
# that
if backup is None:
return Response(status=HTTPStatus.NOT_FOUND)

if agent_id in manager.local_backup_agents:
local_agent = manager.local_backup_agents[agent_id]
path = local_agent.get_backup_path(slug=slug)
else:
path = manager.temp_backup_dir / f"{slug}.tar"
await agent.async_download_backup(id=backup.id, path=path)

# TODO: We need a callback to remove the temp file once the download is complete
return FileResponse(
path=backup.path.as_posix(),
path=path.as_posix(),
headers={
CONTENT_DISPOSITION: f"attachment; filename={slugify(backup.name)}.tar"
},
Expand All @@ -63,12 +83,16 @@ class UploadBackupView(HomeAssistantView):
@require_admin
async def post(self, request: Request) -> Response:
"""Upload a backup file."""
try:
agent_ids = request.query.getall("agent_id")
except KeyError:
return Response(status=HTTPStatus.BAD_REQUEST)
manager = request.app[KEY_HASS].data[DATA_MANAGER]
reader = await request.multipart()
contents = cast(BodyPartReader, await reader.next())

try:
await manager.async_receive_backup(contents=contents)
await manager.async_receive_backup(contents=contents, agent_ids=agent_ids)
except OSError as err:
return Response(
body=f"Can't write backup file {err}",
Expand Down
Loading
Loading