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

View File

@ -467,3 +467,7 @@ class StoreJobError(StoreError, JobException):
class BackupError(HassioError): class BackupError(HassioError):
"""Raise if an error during backup is happening.""" """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 ..coresys import CoreSys, CoreSysAttributes
from ..exceptions import ( from ..exceptions import (
ConfigurationFileError, ConfigurationFileError,
HomeAssistantBackupError,
HomeAssistantError, HomeAssistantError,
HomeAssistantWSError, HomeAssistantWSError,
) )
@ -351,6 +352,11 @@ class HomeAssistant(FileConfiguration, CoreSysAttributes):
_LOGGER.info("Backing up Home Assistant Core config folder") _LOGGER.info("Backing up Home Assistant Core config folder")
await self.sys_run_in_executor(_write_tarfile) await self.sys_run_in_executor(_write_tarfile)
_LOGGER.info("Backup Home Assistant Core config folder done") _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: finally:
try: try:
await self.sys_homeassistant.websocket.async_send_command( await self.sys_homeassistant.websocket.async_send_command(