Handle TarError/OSError in backup routine (#3468)

* Handle TarError/OSError in Core config backup routine

Make sure to handle TarError and OSError exceptions and print a warning
when backing up the Home Assistant Core config folder. This restores
behavior before the introduction of Backup notification in #3305.

* Reraise errors during HA Core backup

* Raise BackupError when encountering error during Add-on Backup

* Log to error as well

Co-authored-by: Pascal Vizeli <pascal.vizeli@syshack.ch>
Co-authored-by: Joakim Sørensen <hi@ludeeus.dev>
This commit is contained in:
Stefan Agner 2022-04-07 20:37:51 +02:00 committed by GitHub
parent b701e1917e
commit 9847e456cd
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 26 additions and 17 deletions

View File

@ -387,30 +387,29 @@ class Backup(CoreSysAttributes):
return
# Take backup
try:
_LOGGER.info("Backing up folder %s", name)
with SecureTarFile(
tar_name, "w", key=self._key, gzip=self.compressed
) as tar_file:
atomic_contents_add(
tar_file,
origin_dir,
excludes=[],
arcname=".",
)
_LOGGER.info("Backing up folder %s", name)
with SecureTarFile(
tar_name, "w", key=self._key, gzip=self.compressed
) as tar_file:
atomic_contents_add(
tar_file,
origin_dir,
excludes=[],
arcname=".",
)
_LOGGER.info("Backup folder %s done", name)
self._data[ATTR_FOLDERS].append(name)
except (tarfile.TarError, OSError) as err:
_LOGGER.warning("Can't backup folder %s: %s", name, err)
_LOGGER.info("Backup folder %s done", name)
self._data[ATTR_FOLDERS].append(name)
# Save folder sequential
# avoid issue on slow IO
for folder in folder_list:
try:
await self.sys_run_in_executor(_folder_save, folder)
except Exception as err: # pylint: disable=broad-except
_LOGGER.warning("Can't save folder %s: %s", folder, err)
except (tarfile.TarError, OSError) as err:
raise BackupError(
f"Can't backup folder {folder}: {str(err)}", _LOGGER.error
) from err
async def restore_folders(self, folder_list: list[str]):
"""Backup Supervisor data into backup."""

View File

@ -467,3 +467,7 @@ class StoreJobError(StoreError, JobException):
class BackupError(HassioError):
"""Raise if an error during backup is happening."""
class HomeAssistantBackupError(BackupError, HomeAssistantError):
"""Raise if an error during Home Assistant Core backup is happening."""

View File

@ -34,6 +34,7 @@ from ..const import (
from ..coresys import CoreSys, CoreSysAttributes
from ..exceptions import (
ConfigurationFileError,
HomeAssistantBackupError,
HomeAssistantError,
HomeAssistantWSError,
)
@ -351,6 +352,11 @@ class HomeAssistant(FileConfiguration, CoreSysAttributes):
_LOGGER.info("Backing up Home Assistant Core config folder")
await self.sys_run_in_executor(_write_tarfile)
_LOGGER.info("Backup Home Assistant Core config folder done")
except (tarfile.TarError, OSError) as err:
raise HomeAssistantBackupError(
f"Can't backup Home Assistant Core config folder: {str(err)}",
_LOGGER.error,
) from err
finally:
try:
await self.sys_homeassistant.websocket.async_send_command(