Add --pre-snapshot-cmd and --post-snapshot-cmd options

This commit is contained in:
Phil Krylov 2021-06-16 16:12:20 +03:00
parent 1a5e4a9cdd
commit b2bf11382c
2 changed files with 16 additions and 2 deletions

View File

@ -45,6 +45,10 @@ class ZfsAutobackup:
help='Target ZFS filesystem (optional: if not specified, zfs-autobackup will only operate ' help='Target ZFS filesystem (optional: if not specified, zfs-autobackup will only operate '
'as snapshot-tool on source)') 'as snapshot-tool on source)')
parser.add_argument('--pre-snapshot-cmd', metavar="COMMAND", type=str,
help='Run COMMAND before snapshotting.')
parser.add_argument('--post-snapshot-cmd', metavar="COMMAND", type=str,
help='Run COMMAND after snapshotting.')
parser.add_argument('--other-snapshots', action='store_true', parser.add_argument('--other-snapshots', action='store_true',
help='Send over other snapshots as well, not just the ones created by this tool.') help='Send over other snapshots as well, not just the ones created by this tool.')
parser.add_argument('--no-snapshot', action='store_true', parser.add_argument('--no-snapshot', action='store_true',
@ -506,7 +510,9 @@ class ZfsAutobackup:
if not self.args.no_snapshot: if not self.args.no_snapshot:
self.set_title("Snapshotting") self.set_title("Snapshotting")
source_node.consistent_snapshot(source_datasets, source_node.new_snapshotname(), source_node.consistent_snapshot(source_datasets, source_node.new_snapshotname(),
min_changed_bytes=self.args.min_change) min_changed_bytes=self.args.min_change,
pre_snapshot_cmd=self.args.pre_snapshot_cmd,
post_snapshot_cmd=self.args.post_snapshot_cmd)
################# sync ################# sync
# if target is specified, we sync the datasets, otherwise we just thin the source. (e.g. snapshot mode) # if target is specified, we sync the datasets, otherwise we just thin the source. (e.g. snapshot mode)

View File

@ -161,7 +161,7 @@ class ZfsNode(ExecuteNode):
"""determine uniq new snapshotname""" """determine uniq new snapshotname"""
return self.backup_name + "-" + time.strftime("%Y%m%d%H%M%S") return self.backup_name + "-" + time.strftime("%Y%m%d%H%M%S")
def consistent_snapshot(self, datasets, snapshot_name, min_changed_bytes): def consistent_snapshot(self, datasets, snapshot_name, min_changed_bytes, pre_snapshot_cmd=None, post_snapshot_cmd=None):
"""create a consistent (atomic) snapshot of specified datasets, per pool. """create a consistent (atomic) snapshot of specified datasets, per pool.
""" """
@ -191,6 +191,10 @@ class ZfsNode(ExecuteNode):
self.verbose("No changes anywhere: not creating snapshots.") self.verbose("No changes anywhere: not creating snapshots.")
return return
if pre_snapshot_cmd:
self.verbose("Running pre-snapshot-cmd:\n\t{}".format(pre_snapshot_cmd))
self.run(cmd=pre_snapshot_cmd.split(" "), readonly=False)
# create consistent snapshot per pool # create consistent snapshot per pool
for (pool_name, snapshots) in pools.items(): for (pool_name, snapshots) in pools.items():
cmd = ["zfs", "snapshot"] cmd = ["zfs", "snapshot"]
@ -200,6 +204,10 @@ class ZfsNode(ExecuteNode):
self.verbose("Creating snapshots {} in pool {}".format(snapshot_name, pool_name)) self.verbose("Creating snapshots {} in pool {}".format(snapshot_name, pool_name))
self.run(cmd, readonly=False) self.run(cmd, readonly=False)
if post_snapshot_cmd:
self.verbose("Running post-snapshot-cmd:\n\t{}".format(post_snapshot_cmd))
self.run(cmd=post_snapshot_cmd.split(" "), readonly=False)
def selected_datasets(self, exclude_received, exclude_paths): def selected_datasets(self, exclude_received, exclude_paths):
"""determine filesystems that should be backupped by looking at the special autobackup-property, systemwide """determine filesystems that should be backupped by looking at the special autobackup-property, systemwide