Don't try to unmount a ZFS snapshot if it's already deleted (#80).

This commit is contained in:
2024-12-04 15:39:04 -08:00
parent d0e92493f6
commit 49b8b693af
5 changed files with 24 additions and 10 deletions

View File

@@ -60,7 +60,7 @@ def get_subvolumes_for_filesystem(btrfs_command, filesystem_mount_point):
)
Subvolume = collections.namedtuple('Subvolume', ('path', 'contained_source_directories'))
Subvolume = collections.namedtuple('Subvolume', ('path', 'contained_source_directories'), defaults=(()))
def get_subvolumes(btrfs_command, findmnt_command, source_directories=None):

View File

@@ -330,6 +330,10 @@ def remove_data_source_dumps(hook_config, config, log_prefix, borgmatic_runtime_
if not dry_run:
shutil.rmtree(snapshot_mount_path, ignore_errors=True)
# If the delete was successful, that means there's nothing to unmount.
if not os.path.isdir(snapshot_mount_path):
continue
logger.debug(
f'{log_prefix}: Unmounting LVM snapshot at {snapshot_mount_path}{dry_run_label}'
)

View File

@@ -24,8 +24,8 @@ def get_contained_directories(parent_directory, candidate_contained_directories)
contained = tuple(
candidate
for candidate in candidate_contained_directories
if parent_directory == candidate
or pathlib.PurePosixPath(parent_directory) in pathlib.PurePath(candidate).parents
if pathlib.PurePath(parent_directory) == pathlib.PurePath(candidate)
or pathlib.PurePath(parent_directory) in pathlib.PurePath(candidate).parents
)
candidate_contained_directories -= set(contained)

View File

@@ -330,6 +330,10 @@ def remove_data_source_dumps(hook_config, config, log_prefix, borgmatic_runtime_
if not dry_run:
shutil.rmtree(snapshot_mount_path, ignore_errors=True)
# If the delete was successful, that means there's nothing to unmount.
if not os.path.isdir(snapshot_mount_path):
continue
logger.debug(
f'{log_prefix}: Unmounting ZFS snapshot at {snapshot_mount_path}{dry_run_label}'
)

View File

@@ -51,9 +51,12 @@ def test_get_subvolumes_collects_subvolumes_matching_source_directories_from_all
'btrfs', '/mnt2'
).and_return(('/three', '/four'))
for path in ('/one', '/two', '/three', '/four'):
flexmock(module.borgmatic.hooks.data_source.snapshot).should_receive('get_contained_directories').with_args(path).and_return((path,))
assert module.get_subvolumes(
'btrfs', 'findmnt', source_directories=['/one', '/four', '/five', '/six', '/mnt2', '/mnt3']
) == ('/one', '/mnt2', '/four')
) == (module.Subvolume('/one'), module.Subvolume('/mnt2'), module.Subvolume('/four'))
def test_get_subvolumes_without_source_directories_collects_all_subvolumes_from_all_filesystems():
@@ -65,13 +68,16 @@ def test_get_subvolumes_without_source_directories_collects_all_subvolumes_from_
'btrfs', '/mnt2'
).and_return(('/three', '/four'))
for path in ('/one', '/two', '/three', '/four'):
flexmock(module.borgmatic.hooks.data_source.snapshot).should_receive('get_contained_directories').with_args(path).and_return((path,))
assert module.get_subvolumes('btrfs', 'findmnt') == (
'/mnt1',
'/one',
'/two',
'/mnt2',
'/three',
'/four',
module.Subvolume('/mnt1'),
module.Subvolume('/one'),
module.Subvolume('/two'),
module.Subvolume('/mnt2'),
module.Subvolume('/three'),
module.Subvolume('/four'),
)