Implement Issue #245 Snapshot exclude patterns

This commit is contained in:
Reno Reckling 2024-08-16 01:34:20 +02:00 committed by DatuX
parent 9e2476ac84
commit c5f1e38b18
5 changed files with 31 additions and 4 deletions

View File

@ -1,4 +1,5 @@
import argparse
import re
import sys
from .CliBase import CliBase
@ -112,6 +113,14 @@ class ZfsAuto(CliBase):
help=argparse.SUPPRESS)
def regex_argument_type(input_line):
"""Parses regex arguments into re.Pattern objects"""
try:
return re.compile(input_line)
except:
raise ValueError("Could not parse argument '{}' as a regular expression".format(input_line))
group.add_argument('--exclude-snapshot-pattern', action='append', default=[], type=regex_argument_type, help="Regular expression to match snapshots that will be ignored.")
return parser
def print_error_sources(self):

View File

@ -472,7 +472,8 @@ class ZfsAutobackup(ZfsAuto):
snapshot_time_format=self.snapshot_time_format, hold_name=self.hold_name, logger=self,
ssh_config=self.args.ssh_config,
ssh_to=self.args.ssh_source, readonly=self.args.test,
debug_output=self.args.debug_output, description=description, thinner=source_thinner)
debug_output=self.args.debug_output, description=description, thinner=source_thinner,
exclude_snapshot_patterns=self.args.exclude_snapshot_pattern)
################# select source datasets
self.set_title("Selecting")

View File

@ -235,7 +235,8 @@ class ZfsAutoverify(ZfsAuto):
snapshot_time_format=self.snapshot_time_format, hold_name=self.hold_name, logger=self,
ssh_config=self.args.ssh_config,
ssh_to=self.args.ssh_source, readonly=self.args.test,
debug_output=self.args.debug_output, description=description)
debug_output=self.args.debug_output, description=description,
exclude_snapshot_patterns=self.args.exclude_snapshot_pattern)
################# select source datasets
self.set_title("Selecting")

View File

@ -124,6 +124,20 @@ class ZfsDataset:
def is_snapshot(self):
"""true if this dataset is a snapshot"""
return self.name.find("@") != -1
@property
def is_excluded(self):
"""true if this dataset is a snapshot and matches the exclude pattern"""
if not self.is_snapshot:
return False
for pattern in self.zfs_node.exclude_snapshot_patterns:
if pattern.search(self.name) is not None:
self.debug("Excluded (path matches snapshot exclude pattern)")
return True
def is_selected(self, value, source, inherited, exclude_received, exclude_paths, exclude_unchanged):
"""determine if dataset should be selected for backup (called from
@ -1171,7 +1185,7 @@ class ZfsDataset:
target_snapshot = target_dataset.find_snapshot(source_snapshot) # still virtual
# does target actually want it?
if target_snapshot not in target_obsoletes:
if target_snapshot not in target_obsoletes and not source_snapshot.is_excluded:
# do the rollback, one time at first transfer
if do_rollback:

View File

@ -20,7 +20,7 @@ class ZfsNode(ExecuteNode):
def __init__(self, logger, utc=False, snapshot_time_format="", hold_name="", ssh_config=None, ssh_to=None, readonly=False,
description="",
debug_output=False, thinner=None):
debug_output=False, thinner=None, exclude_snapshot_patterns=[]):
self.utc = utc
self.snapshot_time_format = snapshot_time_format
@ -30,6 +30,8 @@ class ZfsNode(ExecuteNode):
self.logger = logger
self.exclude_snapshot_patterns = exclude_snapshot_patterns
if ssh_config:
self.verbose("Using custom SSH config: {}".format(ssh_config))