diff --git a/zfs-backup.py b/zfs-backup.py index e0d2252..dc1a686 100755 --- a/zfs-backup.py +++ b/zfs-backup.py @@ -96,16 +96,13 @@ class Manager: @staticmethod def _find_snapshots_with_backup_tag(dataset: Dataset) -> list[Snapshot]: - regex = Manager._get_regex_matching_backup_tags() + regex = _get_regex_matching_snapshots_with_tags( + [config.snapshot_tag, config.snapshot_interval] + ) return [ snapshot for snapshot in dataset.snapshots if snapshot.matches_regex(regex) ] - @staticmethod - # TODO make function - def _get_regex_matching_backup_tags(): - return "@" + ".*".join([config.snapshot_tag, config.snapshot_interval]) - @staticmethod def _search_matching_dataset_in_remote_pool(dataset: Dataset, pool: Pool): dataset_to_search = dataset.replace_pool(pool.name) @@ -125,6 +122,10 @@ class Manager: return common_snapshots[-1] +def _get_regex_matching_snapshots_with_tags(tags: list[str]): + return "@" + ".*".join(tags) + + class ZFSPath: def __init__(self, elements: list[str], snapname: Optional[str] = None): self._elements = elements @@ -433,16 +434,19 @@ class Dataset: self._clean_old_snapshots_for_interval(interval, number) def _clean_old_snapshots_for_interval(self, interval: str, max_count: int): - # TODO this is somewhat similar to the regex method in Manager - regex = ".*".join(["", config.snapshot_tag, interval, ""]) + regex = _get_regex_matching_snapshots_with_tags( + [config.snapshot_tag, interval] + ) snapshots = [ snapshot for snapshot in self.snapshots if snapshot.matches_regex(regex) ] - # TODO function _get_n_oldest_snapshots - if len(snapshots) > max_count: - snapshots.sort() - for snap in snapshots[:-max_count]: - snap.destroy() + old_snapshots = self._get_n_oldest_snapshots(snapshots, max_count) + for snapshot in old_snapshots: + snapshot.destroy() + + def _get_n_oldest_snapshots(self, snapshots: list[Snapshot], count: int) -> list[Snapshot]: + snapshots.sort() + return snapshots[:count] class Snapshot: