Skip to content

Hooks

Hooks in Backrest allow you to respond to various operation lifecycle events, enabling automation and monitoring of your backup operations. This document explains how to configure and use hooks effectively.

Event Types

Hooks can be triggered by the following events:

Snapshot Events

  • CONDITION_SNAPSHOT_START: Triggered when a backup operation begins and will complete before the snapshot starts. The Error Handling configuration can be used to stop the backup if the command isn't successful.
  • CONDITION_SNAPSHOT_END: Triggered when a backup operation completes (regardless of success/failure)
  • CONDITION_SNAPSHOT_SUCCESS: Triggered when a backup operation completes successfully
  • CONDITION_SNAPSHOT_ERROR: Triggered when a backup operation fails
  • CONDITION_SNAPSHOT_WARNING: Triggered when a backup operation encounters non-fatal issues

Prune Events

  • CONDITION_PRUNE_START: Triggered when a prune operation begins
  • CONDITION_PRUNE_SUCCESS: Triggered when a prune operation completes successfully
  • CONDITION_PRUNE_ERROR: Triggered when a prune operation fails

Check Events

  • CONDITION_CHECK_START: Triggered when a check operation begins
  • CONDITION_CHECK_SUCCESS: Triggered when a check operation completes successfully
  • CONDITION_CHECK_ERROR: Triggered when a check operation fails

Forget Events

  • CONDITION_FORGET_START: Triggered when a forget operation begins
  • CONDITION_FORGET_SUCCESS: Triggered when a forget operation completes successfully
  • CONDITION_FORGET_ERROR: Triggered when a forget operation fails

General Events

  • CONDITION_ANY_ERROR: Triggered when any operation fails

Notification Services

Backrest supports multiple notification services for hook delivery:

ServiceDescriptionDocumentation
DiscordSend notifications to Discord channelsDiscord Webhooks Guide
SlackSend notifications to Slack channelsSlack Webhooks Guide
GotifySend notifications via Gotify serverGotify Documentation
ShoutrrrMulti-provider notification serviceShoutrrr Documentation
HealthchecksPing Healthchecks.io monitoring URLsHealthchecks API
CommandExecute custom commandsSee command cookbook

Healthchecks.io Integration

The Healthchecks hook type is specifically designed to integrate with Healthchecks.io or compatible self-hosted instances.

When configured, Backrest automatically appends the correct status endpoint to your webhook URL based on the event type:

  • Start events (e.g., CONDITION_SNAPSHOT_START): Appends /start to the URL.
  • Error events (e.g., CONDITION_SNAPSHOT_ERROR): Appends /fail to the URL.
  • Log events: Appends /log to the URL.
  • Success & Other events: Pings the base URL directly.

It also sends the formatted template summary as the HTTP POST body in plain text, which Healthchecks.io captures as the "ping payload". This is particularly useful for reading error messages or backup statistics directly from the Healthchecks.io dashboard.

Error Handling

Command hooks support specific error behaviors that determine how Backrest responds to hook failures:

  • ON_ERROR_IGNORE: Continue execution despite hook failure
  • ON_ERROR_CANCEL: Stop the operation but don't trigger error handlers
  • ON_ERROR_FATAL: Stop the operation and trigger error handler hooks

Template System

Hooks use Go templates for formatting notifications and scripts. The following variables and functions are available:

Available Variables

VariableTypeDescriptionExample Usage
Eventv1.Hook_ConditionThe triggering event{{ .Event }}
TaskstringTask name{{ .Task }}
Repov1.RepoRepository information{{ .Repo.Id }}
Planv1.PlanPlan information{{ .Plan.Id }}
SnapshotIdstringID of associated snapshot{{ .SnapshotId }}
SnapshotStatsrestic.BackupProgressEntryBackup operation statisticsSee example below
CurTimetime.TimeCurrent timestamp{{ .FormatTime .CurTime }}
Durationtime.DurationOperation duration{{ .FormatDuration .Duration }}
ErrorstringError message if applicable{{ .Error }}

Helper Functions

FunctionDescriptionExample
.SummaryGenerates default event summary{{ .Summary }}
.FormatTimeFormats timestamp{{ .FormatTime .CurTime }}
.FormatDurationFormats time duration{{ .FormatDuration .Duration }}
.FormatSizeBytesFormats byte sizes{{ .FormatSizeBytes 1048576 }}
.ShellEscapeEscapes strings for shell usage{{ .ShellEscape "my string" }}
.JsonMarshalConverts value to JSON{{ .JsonMarshal .SnapshotStats }}

Default Summary Template

Below is the implementation of the .Summary function, which you can use as a reference for creating custom templates:

text
Task: "{{ .Task }}" at {{ .FormatTime .CurTime }}
Event: {{ .EventName .Event }}
Repo: {{ .Repo.Id }}
Plan: {{ .Plan.Id }}
Snapshot: {{ .SnapshotId }}
{{ if .Error -}}
Failed to create snapshot: {{ .Error }}
{{ else -}}
{{ if .SnapshotStats -}}

Overview:
- Data added: {{ .FormatSizeBytes .SnapshotStats.DataAdded }}
- Total files processed: {{ .SnapshotStats.TotalFilesProcessed }}
- Total bytes processed: {{ .FormatSizeBytes .SnapshotStats.TotalBytesProcessed }}

Backup Statistics:
- Files new: {{ .SnapshotStats.FilesNew }}
- Files changed: {{ .SnapshotStats.FilesChanged }}
- Files unmodified: {{ .SnapshotStats.FilesUnmodified }}
- Dirs new: {{ .SnapshotStats.DirsNew }}
- Dirs changed: {{ .SnapshotStats.DirsChanged }}
- Dirs unmodified: {{ .SnapshotStats.DirsUnmodified }}
- Data blobs: {{ .SnapshotStats.DataBlobs }}
- Tree blobs: {{ .SnapshotStats.TreeBlobs }}
- Total duration: {{ .SnapshotStats.TotalDuration }}s
{{ end }}
{{ end }}