|
@@ -33,6 +33,9 @@ commands:
|
|
- prune
|
|
- prune
|
|
run:
|
|
run:
|
|
- echo "After create and/or prune!"
|
|
- echo "After create and/or prune!"
|
|
|
|
+ - after: error
|
|
|
|
+ run:
|
|
|
|
+ - echo "Something went wrong!"
|
|
```
|
|
```
|
|
|
|
|
|
If a `run:` command contains a special YAML character such as a colon, you may
|
|
If a `run:` command contains a special YAML character such as a colon, you may
|
|
@@ -55,9 +58,12 @@ Each command in the `commands:` list has the following options:
|
|
* `configuration` runs before or after all actions and repositories in the current configuration file.
|
|
* `configuration` runs before or after all actions and repositories in the current configuration file.
|
|
* `everything` runs before or after all configuration files. This replaces the deprecated `before_everything` and `after_everything`.
|
|
* `everything` runs before or after all configuration files. This replaces the deprecated `before_everything` and `after_everything`.
|
|
* `error` runs after an error occurs—and it's only available for `after`. This replaces the deprecated `on_error` hook.
|
|
* `error` runs after an error occurs—and it's only available for `after`. This replaces the deprecated `on_error` hook.
|
|
- * `when`: Actions (`create`, `prune`, etc.) for which the commands will be run. Defaults to running for all actions.
|
|
|
|
|
|
+ * `when`: Only trigger the hook when borgmatic is run with particular actions (`create`, `prune`, etc.) listed here. Defaults to running for all actions.
|
|
* `run`: List of one or more shell commands or scripts to run when this command hook is triggered.
|
|
* `run`: List of one or more shell commands or scripts to run when this command hook is triggered.
|
|
|
|
|
|
|
|
+Note that borgmatic does not run `error` hooks if an error occurs within an
|
|
|
|
+`everything` hook.
|
|
|
|
+
|
|
There's also another command hook that works a little differently:
|
|
There's also another command hook that works a little differently:
|
|
|
|
|
|
```yaml
|
|
```yaml
|
|
@@ -91,10 +97,10 @@ borgmatic for the `create` and `prune` actions. Here's the order of execution:
|
|
* Run `before: dump_data_sources` hooks (e.g. for the PostgreSQL hook).
|
|
* Run `before: dump_data_sources` hooks (e.g. for the PostgreSQL hook).
|
|
* Actually dump data sources (e.g. PostgreSQL databases).
|
|
* Actually dump data sources (e.g. PostgreSQL databases).
|
|
* Run `after: dump_data_sources` hooks (e.g. for the PostgreSQL hook).
|
|
* Run `after: dump_data_sources` hooks (e.g. for the PostgreSQL hook).
|
|
- * Actually run the `create` action.
|
|
|
|
|
|
+ * Actually run the `create` action (e.g. `borg create`).
|
|
* Run `after: action` hooks for `create`.
|
|
* Run `after: action` hooks for `create`.
|
|
* Run `before: action` hooks for `prune`.
|
|
* Run `before: action` hooks for `prune`.
|
|
- * Actually run the `prune` action.
|
|
|
|
|
|
+ * Actually run the `prune` action (e.g. `borg prune`).
|
|
* Run `after: action` hooks for `prune`.
|
|
* Run `after: action` hooks for `prune`.
|
|
* Run `after: repository` hooks (for the first repository).
|
|
* Run `after: repository` hooks (for the first repository).
|
|
* Run `after: configuration` hooks (from the first configuration file).
|
|
* Run `after: configuration` hooks (from the first configuration file).
|
|
@@ -149,25 +155,80 @@ but not if an error occurs in a previous hook or in the backups themselves.
|
|
(Prior to borgmatic 1.6.0, these hooks instead ran once per configuration file
|
|
(Prior to borgmatic 1.6.0, these hooks instead ran once per configuration file
|
|
rather than once per repository.)
|
|
rather than once per repository.)
|
|
|
|
|
|
|
|
+You can also use `before_everything` and `after_everything` hooks to perform
|
|
|
|
+global setup or cleanup:
|
|
|
|
+
|
|
|
|
+```yaml
|
|
|
|
+before_everything:
|
|
|
|
+ - set-up-stuff-globally
|
|
|
|
+
|
|
|
|
+after_everything:
|
|
|
|
+ - clean-up-stuff-globally
|
|
|
|
+```
|
|
|
|
+
|
|
|
|
+<span class="minilink minilink-addedin">Prior to version 1.8.0</span> Put
|
|
|
|
+these options in the `hooks:` section of your configuration.
|
|
|
|
+
|
|
|
|
+`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.
|
|
|
|
+
|
|
|
|
+`on_error` hooks run when an error occurs, but only if there is a `create`,
|
|
|
|
+`prune`, `compact`, or `check` action. For instance, borgmatic can run
|
|
|
|
+configurable shell commands to fire off custom error notifications or take other
|
|
|
|
+actions, so you can get alerted as soon as something goes wrong. Here's a
|
|
|
|
+not-so-useful example:
|
|
|
|
+
|
|
|
|
+```yaml
|
|
|
|
+on_error:
|
|
|
|
+ - echo "Error while creating a backup or running a backup hook."
|
|
|
|
+```
|
|
|
|
+
|
|
|
|
+<span class="minilink minilink-addedin">Prior to version 1.8.0</span> Put
|
|
|
|
+this option in the `hooks:` section of your configuration.
|
|
|
|
+
|
|
|
|
+The `on_error` hook supports interpolating particular runtime variables into
|
|
|
|
+the hook command. Here's an example that assumes you provide a separate shell
|
|
|
|
+script to handle the alerting:
|
|
|
|
+
|
|
|
|
+```yaml
|
|
|
|
+on_error:
|
|
|
|
+ - send-text-message.sh
|
|
|
|
+```
|
|
|
|
+
|
|
|
|
+borgmatic does not run `on_error` hooks if an error occurs within a
|
|
|
|
+`before_everything` or `after_everything` hook.
|
|
|
|
+
|
|
|
|
|
|
## Variable interpolation
|
|
## Variable interpolation
|
|
|
|
|
|
The command action hooks support interpolating particular runtime variables into
|
|
The command action hooks support interpolating particular runtime variables into
|
|
-the commands that are run. Here's an example that assumes you provide a separate
|
|
|
|
-shell script:
|
|
|
|
|
|
+the commands that are run. Here's are a couple examples that assume you provide
|
|
|
|
+separate shell scripts:
|
|
|
|
|
|
```yaml
|
|
```yaml
|
|
commands:
|
|
commands:
|
|
- after: action
|
|
- after: action
|
|
when: [prune]
|
|
when: [prune]
|
|
run:
|
|
run:
|
|
- - record-prune.sh "{configuration_filename}" "{repository}"
|
|
|
|
|
|
+ - record-prune.sh {configuration_filename} {repository}
|
|
|
|
+ - after: error
|
|
|
|
+ when: [create]
|
|
|
|
+ run:
|
|
|
|
+ - send-text-message.sh {configuration_filename} {repository}
|
|
```
|
|
```
|
|
|
|
|
|
In this example, when the hook is triggered, borgmatic interpolates runtime
|
|
In this example, when the hook is triggered, borgmatic interpolates runtime
|
|
-values into the hook command: the borgmatic configuration filename and the
|
|
|
|
-paths of the current Borg repository. Here's the full set of supported
|
|
|
|
-variables you can use here:
|
|
|
|
|
|
+values into each hook command: the borgmatic configuration filename and the
|
|
|
|
+paths of the current Borg repository.
|
|
|
|
+
|
|
|
|
+Here's the full set of supported variables you can use here:
|
|
|
|
|
|
* `configuration_filename`: borgmatic configuration filename in which the
|
|
* `configuration_filename`: borgmatic configuration filename in which the
|
|
hook was defined
|
|
hook was defined
|
|
@@ -175,51 +236,45 @@ variables you can use here:
|
|
<span class="minilink minilink-addedin">New in version 1.7.12</span>:
|
|
<span class="minilink minilink-addedin">New in version 1.7.12</span>:
|
|
path of the borgmatic log file, only set when the `--log-file` flag is used
|
|
path of the borgmatic log file, only set when the `--log-file` flag is used
|
|
* `repository`: path of the current repository as configured in the current
|
|
* `repository`: path of the current repository as configured in the current
|
|
- borgmatic configuration file
|
|
|
|
|
|
+ borgmatic configuration file, if applicable to the current hook
|
|
* `repository_label` <span class="minilink minilink-addedin">New in version
|
|
* `repository_label` <span class="minilink minilink-addedin">New in version
|
|
1.8.12</span>: label of the current repository as configured in the current
|
|
1.8.12</span>: label of the current repository as configured in the current
|
|
- borgmatic configuration file
|
|
|
|
|
|
+ borgmatic configuration file, if applicable to the current hook
|
|
|
|
+ * `error`: the error message itself, only applies to `error` hooks
|
|
|
|
+ * `output`: output of the command that failed, only applies to `error` hooks
|
|
|
|
+ (may be blank if an error occurred without running a command)
|
|
|
|
|
|
Not all command hooks support all variables. For instance, the `everything` and
|
|
Not all command hooks support all variables. For instance, the `everything` and
|
|
`configuration` hooks don't support repository variables because those hooks
|
|
`configuration` hooks don't support repository variables because those hooks
|
|
-don't run in the context of a single repository.
|
|
|
|
-
|
|
|
|
-Note that you can also interpolate in [arbitrary environment
|
|
|
|
-variables](https://torsion.org/borgmatic/docs/how-to/provide-your-passwords/).
|
|
|
|
-
|
|
|
|
-
|
|
|
|
-## Global hooks
|
|
|
|
|
|
+don't run in the context of a single repository. But the deprecated command
|
|
|
|
+hooks (`before_backup`, `on_error`, etc.) do generally support variable
|
|
|
|
+interpolation.
|
|
|
|
|
|
-You can also use `before_everything` and `after_everything` hooks to perform
|
|
|
|
-global setup or cleanup:
|
|
|
|
|
|
+borgmatic automatically escapes these interpolated values to prevent shell
|
|
|
|
+injection attacks. One implication is that you shouldn't wrap the interpolated
|
|
|
|
+values in your own quotes, as that will interfere with the quoting performed by
|
|
|
|
+borgmatic and result in your command receiving incorrect arguments. For
|
|
|
|
+instance, this won't work:
|
|
|
|
|
|
```yaml
|
|
```yaml
|
|
-before_everything:
|
|
|
|
- - set-up-stuff-globally
|
|
|
|
-after_everything:
|
|
|
|
- - clean-up-stuff-globally
|
|
|
|
|
|
+commands:
|
|
|
|
+ - after: error
|
|
|
|
+ run:
|
|
|
|
+ # Don't do this! It won't work, as the {error} value is already quoted.
|
|
|
|
+ - send-text-message.sh "Uh oh: {error}"
|
|
```
|
|
```
|
|
|
|
|
|
-<span class="minilink minilink-addedin">Prior to version 1.8.0</span> Put
|
|
|
|
-these options in the `hooks:` section of your configuration.
|
|
|
|
|
|
+Do this instead:
|
|
|
|
|
|
-`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.
|
|
|
|
-
|
|
|
|
-
|
|
|
|
-## Error hooks
|
|
|
|
|
|
+```yaml
|
|
|
|
+commands:
|
|
|
|
+ - after: error
|
|
|
|
+ run:
|
|
|
|
+ - send-text-message.sh {error}
|
|
|
|
+```
|
|
|
|
|
|
-borgmatic also runs `on_error` hooks if an error occurs, either when creating
|
|
|
|
-a backup or running a backup hook. See the [monitoring and alerting
|
|
|
|
-documentation](https://torsion.org/borgmatic/docs/how-to/monitor-your-backups/)
|
|
|
|
-for more information.
|
|
|
|
|
|
+Note that you can also interpolate [arbitrary environment
|
|
|
|
+variables](https://torsion.org/borgmatic/docs/how-to/provide-your-passwords/).
|
|
|
|
|
|
|
|
|
|
## Hook output
|
|
## Hook output
|