diff --git a/README.md b/README.md index 182275c..0def184 100644 --- a/README.md +++ b/README.md @@ -279,6 +279,19 @@ root@ws1:~# zfs-autobackup test --verbose This also allows you to make several snapshots during the day, but only backup the data at night when the server is not busy. +## Running custom commands before and after snapshotting + +If you need, e.g. to quiesce a couple of mysql databases to make on-disk data consistent before snapshotting, try: + +```sh +zfs-autobackup \ + --pre-snapshot-cmd 'daemon -fP /tmp/mysql1_lock.pid jexec mysqljail1 mysql -s -e "set autocommit=0;flush logs;flush tables with read lock;\\! sleep 60"' \ + --pre-snapshot-cmd 'daemon -fP /tmp/mysql2_lock.pid jexec mysqljail2 mysql -s -e "set autocommit=0;flush logs;flush tables with read lock;\\! sleep 60"' \ + --post-snapshot-cmd 'pkill -F /tmp/mysql1_lock.pid' \ + --post-snapshot-cmd 'pkill -F /tmp/mysql2_lock.pid' \ + test +``` + ## Thinning out obsolete snapshots The thinner is the thing that destroys old snapshots on the source and target. @@ -493,7 +506,8 @@ Look in man ssh_config for many more options. ```console usage: zfs-autobackup [-h] [--ssh-config CONFIG-FILE] [--ssh-source USER@HOST] [--ssh-target USER@HOST] [--keep-source SCHEDULE] - [--keep-target SCHEDULE] [--other-snapshots] + [--keep-target SCHEDULE] [--pre-snapshot-cmd COMMAND] + [--post-snapshot-cmd COMMAND] [--other-snapshots] [--no-snapshot] [--no-send] [--no-thinning] [--no-holds] [--min-change BYTES] [--allow-empty] [--ignore-replicated] [--strip-path N] @@ -531,6 +545,12 @@ optional arguments: --keep-target SCHEDULE Thinning schedule for old target snapshots. Default: 10,1d1w,1w1m,1m1y + --pre-snapshot-cmd COMMAND + Run COMMAND before snapshotting (can be used multiple + times. + --post-snapshot-cmd COMMAND + Run COMMAND after snapshotting (can be used multiple + times. --other-snapshots Send over other snapshots as well, not just the ones created by this tool. --no-snapshot Don't create new snapshots (useful for finishing diff --git a/zfs_autobackup/ZfsAutobackup.py b/zfs_autobackup/ZfsAutobackup.py index 9e97fb1..37ae634 100644 --- a/zfs_autobackup/ZfsAutobackup.py +++ b/zfs_autobackup/ZfsAutobackup.py @@ -45,10 +45,10 @@ class ZfsAutobackup: help='Target ZFS filesystem (optional: if not specified, zfs-autobackup will only operate ' '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('--pre-snapshot-cmd', metavar="COMMAND", default=[], action='append', + help='Run COMMAND before snapshotting (can be used multiple times.') + parser.add_argument('--post-snapshot-cmd', metavar="COMMAND", default=[], action='append', + help='Run COMMAND after snapshotting (can be used multiple times.') parser.add_argument('--other-snapshots', action='store_true', help='Send over other snapshots as well, not just the ones created by this tool.') parser.add_argument('--no-snapshot', action='store_true', diff --git a/zfs_autobackup/ZfsNode.py b/zfs_autobackup/ZfsNode.py index e25792e..08b36d3 100644 --- a/zfs_autobackup/ZfsNode.py +++ b/zfs_autobackup/ZfsNode.py @@ -192,9 +192,9 @@ class ZfsNode(ExecuteNode): self.verbose("No changes anywhere: not creating snapshots.") return - if pre_snapshot_cmd: - self.verbose("Running pre-snapshot-cmd:\n\t{}".format(pre_snapshot_cmd)) - self.run(cmd=shlex.split(pre_snapshot_cmd), readonly=False) + for cmd in pre_snapshot_cmd: + self.verbose("Running pre-snapshot-cmd") + self.run(cmd=shlex.split(cmd), readonly=False) # create consistent snapshot per pool for (pool_name, snapshots) in pools.items(): @@ -205,9 +205,9 @@ class ZfsNode(ExecuteNode): self.verbose("Creating snapshots {} in pool {}".format(snapshot_name, pool_name)) self.run(cmd, readonly=False) - if post_snapshot_cmd: - self.verbose("Running post-snapshot-cmd:\n\t{}".format(post_snapshot_cmd)) - self.run(cmd=shlex.split(post_snapshot_cmd), readonly=False) + for cmd in post_snapshot_cmd: + self.verbose("Running post-snapshot-cmd") + self.run(cmd=shlex.split(cmd), readonly=False) def selected_datasets(self, exclude_received, exclude_paths): """determine filesystems that should be backupped by looking at the special autobackup-property, systemwide