Allow multiple --pre/post-snapshot-cmd options. Add a usage example.

This commit is contained in:
Phil Krylov 2021-06-16 23:19:29 +03:00
parent ec9459c1d2
commit 55ff14f1d8
3 changed files with 31 additions and 11 deletions

View File

@ -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. 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 ## Thinning out obsolete snapshots
The thinner is the thing that destroys old snapshots on the source and target. 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 ```console
usage: zfs-autobackup [-h] [--ssh-config CONFIG-FILE] [--ssh-source USER@HOST] usage: zfs-autobackup [-h] [--ssh-config CONFIG-FILE] [--ssh-source USER@HOST]
[--ssh-target USER@HOST] [--keep-source SCHEDULE] [--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] [--no-snapshot] [--no-send] [--no-thinning] [--no-holds]
[--min-change BYTES] [--allow-empty] [--min-change BYTES] [--allow-empty]
[--ignore-replicated] [--strip-path N] [--ignore-replicated] [--strip-path N]
@ -531,6 +545,12 @@ optional arguments:
--keep-target SCHEDULE --keep-target SCHEDULE
Thinning schedule for old target snapshots. Default: Thinning schedule for old target snapshots. Default:
10,1d1w,1w1m,1m1y 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 --other-snapshots Send over other snapshots as well, not just the ones
created by this tool. created by this tool.
--no-snapshot Don't create new snapshots (useful for finishing --no-snapshot Don't create new snapshots (useful for finishing

View File

@ -45,10 +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, parser.add_argument('--pre-snapshot-cmd', metavar="COMMAND", default=[], action='append',
help='Run COMMAND before snapshotting.') help='Run COMMAND before snapshotting (can be used multiple times.')
parser.add_argument('--post-snapshot-cmd', metavar="COMMAND", type=str, parser.add_argument('--post-snapshot-cmd', metavar="COMMAND", default=[], action='append',
help='Run COMMAND after snapshotting.') help='Run COMMAND after snapshotting (can be used multiple times.')
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',

View File

@ -192,9 +192,9 @@ 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: for cmd in pre_snapshot_cmd:
self.verbose("Running pre-snapshot-cmd:\n\t{}".format(pre_snapshot_cmd)) self.verbose("Running pre-snapshot-cmd")
self.run(cmd=shlex.split(pre_snapshot_cmd), readonly=False) self.run(cmd=shlex.split(cmd), 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():
@ -205,9 +205,9 @@ 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: for cmd in post_snapshot_cmd:
self.verbose("Running post-snapshot-cmd:\n\t{}".format(post_snapshot_cmd)) self.verbose("Running post-snapshot-cmd")
self.run(cmd=shlex.split(post_snapshot_cmd), readonly=False) self.run(cmd=shlex.split(cmd), 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