2019-02-04 07:20:59 +01:00
|
|
|
---
|
2019-07-05 18:03:08 +02:00
|
|
|
title: Add preparation and cleanup steps to backups
|
2019-02-04 07:20:59 +01:00
|
|
|
---
|
|
|
|
## Preparation and cleanup hooks
|
|
|
|
|
|
|
|
If you find yourself performing prepraration tasks before your backup runs, or
|
|
|
|
cleanup work afterwards, borgmatic hooks may be of interest. Hooks are
|
|
|
|
shell commands that borgmatic executes for you at various points, and they're
|
|
|
|
configured in the `hooks` section of your configuration file.
|
|
|
|
|
|
|
|
For instance, you can specify `before_backup` hooks to dump a database to file
|
|
|
|
before backing it up, and specify `after_backup` hooks to delete the temporary
|
|
|
|
file afterwards. Here's an example:
|
|
|
|
|
|
|
|
```yaml
|
|
|
|
hooks:
|
|
|
|
before_backup:
|
|
|
|
- dump-a-database /to/file.sql
|
|
|
|
after_backup:
|
|
|
|
- rm /to/file.sql
|
|
|
|
```
|
|
|
|
|
|
|
|
borgmatic hooks run once per configuration file. `before_backup` hooks run
|
|
|
|
prior to backups of all repositories. `after_backup` hooks run afterwards, but
|
|
|
|
not if an error occurs in a previous hook or in the backups themselves.
|
|
|
|
|
|
|
|
|
|
|
|
## Error hooks
|
|
|
|
|
2019-07-19 18:25:01 +02:00
|
|
|
borgmatic also runs `on_error` hooks if an error occurs, either when creating
|
|
|
|
a backup or running another hook. Here's an example configuration:
|
2019-02-04 07:20:59 +01:00
|
|
|
|
|
|
|
```yaml
|
|
|
|
hooks:
|
|
|
|
on_error:
|
2019-07-19 18:25:01 +02:00
|
|
|
- echo "Error while creating a backup or running a hook."
|
2019-02-04 07:20:59 +01: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-06-12 22:09:04 +02:00
|
|
|
href="https://torsion.org/borgmatic/docs/how-to/inspect-your-backups.md">inspecting
|
|
|
|
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
|
|
|
|
(`chmod 0700`) on borgmatic configuration files and scripts invoked by hooks.
|
|
|
|
|
|
|
|
|
|
|
|
## Related documentation
|
|
|
|
|
2019-06-01 22:23:48 +02:00
|
|
|
* [Set up backups with borgmatic](https://torsion.org/borgmatic/docs/how-to/set-up-backups.md)
|
|
|
|
* [Make per-application backups](https://torsion.org/borgmatic/docs/how-to/make-per-application-backups.md)
|
2019-06-12 22:09:04 +02:00
|
|
|
* [Inspect your backups](https://torsion.org/borgmatic/docs/how-to/inspect-your-backups.md)
|