From 36d32eaabcfe6b3f3145651984b528e5c68de4bd Mon Sep 17 00:00:00 2001 From: Erik Montnemery Date: Tue, 25 Mar 2025 09:52:45 +0100 Subject: [PATCH] Improve backup exclude filters (#141311) * Improve backup exclude filters * Add comment --- homeassistant/components/backup/const.py | 4 ++-- homeassistant/components/backup/manager.py | 4 +++- tests/components/backup/conftest.py | 1 + tests/components/backup/test_manager.py | 2 ++ 4 files changed, 8 insertions(+), 3 deletions(-) diff --git a/homeassistant/components/backup/const.py b/homeassistant/components/backup/const.py index c2070a37b2d..773deaef174 100644 --- a/homeassistant/components/backup/const.py +++ b/homeassistant/components/backup/const.py @@ -16,8 +16,8 @@ DATA_MANAGER: HassKey[BackupManager] = HassKey(DOMAIN) LOGGER = getLogger(__package__) EXCLUDE_FROM_BACKUP = [ - "__pycache__/*", - ".DS_Store", + "**/__pycache__/*", + "**/.DS_Store", ".HA_RESTORE", "*.db-shm", "*.log.*", diff --git a/homeassistant/components/backup/manager.py b/homeassistant/components/backup/manager.py index 4bcdf7597b2..43a7be6db8d 100644 --- a/homeassistant/components/backup/manager.py +++ b/homeassistant/components/backup/manager.py @@ -1726,7 +1726,9 @@ class CoreBackupReaderWriter(BackupReaderWriter): """Filter to filter excludes.""" for exclude in excludes: - if not path.match(exclude): + # The home assistant core configuration directory is added as "data" + # in the tar file, so we need to prefix that path to the filters. + if not path.full_match(f"data/{exclude}"): continue LOGGER.debug("Ignoring %s because of %s", path, exclude) return True diff --git a/tests/components/backup/conftest.py b/tests/components/backup/conftest.py index 8c0e0ef63ac..d391df44475 100644 --- a/tests/components/backup/conftest.py +++ b/tests/components/backup/conftest.py @@ -76,6 +76,7 @@ CONFIG_DIR = { Path("backups/not_backup"), ], "/another_subdir": [ + Path("another_subdir/.DS_Store"), Path("another_subdir/backups"), Path("another_subdir/tts"), ], diff --git a/tests/components/backup/test_manager.py b/tests/components/backup/test_manager.py index f518d7c59bc..04072dae864 100644 --- a/tests/components/backup/test_manager.py +++ b/tests/components/backup/test_manager.py @@ -70,8 +70,10 @@ _EXPECTED_FILES = [ ".storage", "another_subdir", "another_subdir/backups", + "another_subdir/backups/backup.tar", "another_subdir/backups/not_backup", "another_subdir/tts", + "another_subdir/tts/voice.mp3", "backups", "backups/not_backup", "tmp_backups",