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
  • 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

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
CommandExecute custom commandsSee command cookbook

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:

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 }}