2019-02-04 07:20:59 +01:00
|
|
|
---
|
2019-10-24 00:35:37 +02:00
|
|
|
title: How to add preparation and cleanup steps to backups
|
2020-08-21 23:27:47 +02:00
|
|
|
eleventyNavigation:
|
|
|
|
key: Add preparation and cleanup steps
|
|
|
|
parent: How-to guides
|
|
|
|
order: 8
|
2019-02-04 07:20:59 +01:00
|
|
|
---
|
|
|
|
## Preparation and cleanup hooks
|
|
|
|
|
2022-04-22 07:08:25 +02:00
|
|
|
If you find yourself performing preparation tasks before your backup runs, or
|
2019-10-24 00:35:37 +02:00
|
|
|
cleanup work afterwards, borgmatic hooks may be of interest. Hooks are shell
|
2022-04-22 07:08:25 +02:00
|
|
|
commands that borgmatic executes for you at various points as it runs, and
|
|
|
|
they're configured in the `hooks` section of your configuration file. But if
|
|
|
|
you're looking to backup a database, it's probably easier to use the [database
|
|
|
|
backup
|
2019-10-24 00:35:37 +02:00
|
|
|
feature](https://torsion.org/borgmatic/docs/how-to/backup-your-databases/)
|
|
|
|
instead.
|
2019-02-04 07:20:59 +01:00
|
|
|
|
2019-10-24 00:35:37 +02:00
|
|
|
You can specify `before_backup` hooks to perform preparation steps before
|
|
|
|
running backups, and specify `after_backup` hooks to perform cleanup steps
|
|
|
|
afterwards. Here's an example:
|
2019-02-04 07:20:59 +01:00
|
|
|
|
|
|
|
```yaml
|
|
|
|
hooks:
|
|
|
|
before_backup:
|
2019-10-24 00:35:37 +02:00
|
|
|
- mount /some/filesystem
|
2019-02-04 07:20:59 +01:00
|
|
|
after_backup:
|
2019-10-24 00:35:37 +02:00
|
|
|
- umount /some/filesystem
|
2019-02-04 07:20:59 +01:00
|
|
|
```
|
|
|
|
|
2022-04-22 07:08:25 +02:00
|
|
|
The `before_backup` and `after_backup` hooks each run once per repository in a
|
|
|
|
configuration file. `before_backup` hooks runs right before the `create`
|
|
|
|
action for a particular repository, and `after_backup` hooks run afterwards,
|
|
|
|
but not if an error occurs in a previous hook or in the backups themselves.
|
2022-04-23 04:58:28 +02:00
|
|
|
(Prior to borgmatic 1.6.0, these hooks instead ran once per configuration file
|
|
|
|
rather than once per repository.)
|
2019-02-04 07:20:59 +01:00
|
|
|
|
2022-02-09 23:33:12 +01:00
|
|
|
There are additional hooks that run before/after other actions as well. For
|
2022-04-22 07:08:25 +02:00
|
|
|
instance, `before_prune` runs before a `prune` action for a repository, while
|
|
|
|
`after_prune` runs after it.
|
2020-01-27 20:07:07 +01:00
|
|
|
|
2022-03-14 21:34:14 +01:00
|
|
|
## Variable interpolation
|
|
|
|
|
|
|
|
The before and after action hooks support interpolating particular runtime
|
|
|
|
variables into the hook command. Here's an example that assumes you provide a
|
|
|
|
separate shell script:
|
|
|
|
|
|
|
|
```yaml
|
|
|
|
hooks:
|
|
|
|
after_prune:
|
2022-04-22 07:08:25 +02:00
|
|
|
- record-prune.sh "{configuration_filename}" "{repository}"
|
2022-03-14 21:34:14 +01:00
|
|
|
```
|
|
|
|
|
|
|
|
In this example, when the hook is triggered, borgmatic interpolates runtime
|
|
|
|
values into the hook command: the borgmatic configuration filename and the
|
2022-04-22 07:08:25 +02:00
|
|
|
paths of the current Borg repository. Here's the full set of supported
|
2022-03-14 21:34:14 +01:00
|
|
|
variables you can use here:
|
|
|
|
|
|
|
|
* `configuration_filename`: borgmatic configuration filename in which the
|
|
|
|
hook was defined
|
2022-04-22 07:08:25 +02:00
|
|
|
* `repository`: path of the current repository as configured in the current
|
|
|
|
borgmatic configuration file
|
2022-03-14 21:34:14 +01:00
|
|
|
|
2022-03-14 21:50:22 +01:00
|
|
|
## Global hooks
|
|
|
|
|
2019-09-29 01:18:10 +02:00
|
|
|
You can also use `before_everything` and `after_everything` hooks to perform
|
|
|
|
global setup or cleanup:
|
|
|
|
|
|
|
|
```yaml
|
|
|
|
hooks:
|
|
|
|
before_everything:
|
|
|
|
- set-up-stuff-globally
|
|
|
|
after_everything:
|
|
|
|
- clean-up-stuff-globally
|
|
|
|
```
|
|
|
|
|
|
|
|
`before_everything` hooks collected from all borgmatic configuration files run
|
|
|
|
once before all configuration files (prior to all actions), but only if there
|
|
|
|
is a `create` action. An error encountered during a `before_everything` hook
|
|
|
|
causes borgmatic to exit without creating backups.
|
|
|
|
|
|
|
|
`after_everything` hooks run once after all configuration files and actions,
|
|
|
|
but only if there is a `create` action. It runs even if an error occurs during
|
|
|
|
a backup or a backup hook, but not if an error occurs during a
|
|
|
|
`before_everything` hook.
|
2019-02-04 07:20:59 +01:00
|
|
|
|
2022-03-14 21:34:14 +01:00
|
|
|
## Error hooks
|
|
|
|
|
2019-07-19 18:25:01 +02:00
|
|
|
borgmatic also runs `on_error` hooks if an error occurs, either when creating
|
2019-10-15 19:49:14 +02:00
|
|
|
a backup or running a backup hook. See the [monitoring and alerting
|
2019-10-24 00:35:37 +02:00
|
|
|
documentation](https://torsion.org/borgmatic/docs/how-to/monitor-your-backups/)
|
2019-10-01 21:23:16 +02:00
|
|
|
for more information.
|
2019-09-29 01:18:10 +02:00
|
|
|
|
2019-06-12 22:09:04 +02:00
|
|
|
## Hook output
|
|
|
|
|
2019-06-27 23:41:21 +02:00
|
|
|
Any output produced by your hooks shows up both at the console and in syslog
|
|
|
|
(when run in a non-interactive console). For more information, read about <a
|
2019-10-24 00:35:37 +02:00
|
|
|
href="https://torsion.org/borgmatic/docs/how-to/inspect-your-backups/">inspecting
|
2019-06-12 22:09:04 +02:00
|
|
|
your backups</a>.
|
2019-02-04 07:20:59 +01:00
|
|
|
|
|
|
|
## Security
|
|
|
|
|
|
|
|
An important security note about hooks: borgmatic executes all hook commands
|
|
|
|
with the user permissions of borgmatic itself. So to prevent potential shell
|
|
|
|
injection or privilege escalation, do not forget to set secure permissions
|
2019-09-29 01:18:10 +02:00
|
|
|
on borgmatic configuration files (`chmod 0600`) and scripts (`chmod 0700`)
|
|
|
|
invoked by hooks.
|