🪝Hooks
Execute custom commands at different stages of backup or copy job.
Overview
Hooks allow you to run custom scripts or commands before, on success/failure of backup or copy job.
Available Hooks
before- Run before backup or copy job startssuccess- Run after successful backup or copyfailure- Run after failed backup or copy
Configuration
jobs:
- type: backup
name: documents
from: [/home/user/Documents]
to: local-repo
hooks:
before:
- echo "Starting backup..."
- /usr/local/bin/snapshot-database.sh
success:
- echo "Backup successful!"
- curl -X POST https://your-webhook.com/success
failure:
- echo "Backup failed!" >&2
- /usr/local/bin/alert-admin.shEnvironment Variables
Hooks have access to these environment variables:
CRESTIC_JOB_NAME- Name of the jobCRESTIC_EXIT_CODE- Exit code of the operationCRESTIC_ERROR- Error message (only in failure hooks)
Examples
Database Backup Before Files
jobs:
- type: backup
name: full-backup
from: [/home/user]
to: local-repo
hooks:
before:
- pg_dump mydb > /tmp/mydb.sql
- mysqldump mydb > /tmp/mydb.sql
success:
- rm /tmp/mydb.sqlCustom Notifications
jobs:
- type: backup
name: important-backup
from: [/important/data]
to: remote-repo
hooks:
success:
- curl -X POST https://hooks.slack.com/services/YOUR/WEBHOOK/URL \
-d '{"text":"Backup completed successfully"}'
failure:
- curl -X POST https://hooks.slack.com/services/YOUR/WEBHOOK/URL \
-d '{"text":"Backup failed: $CRESTIC_ERROR"}'Mount/Unmount Volumes
jobs:
- type: backup
name: external-drive
from: [/mnt/external]
to: local-repo
hooks:
before:
- mount /dev/sdb1 /mnt/external
success:
- umount /mnt/external
failure:
- umount /mnt/externalExit Codes
- Hooks run sequentially
- If a
beforehook fails (non-zero exit code), the job is aborted
See Also
- Configuration Guide - Complete configuration reference
- Healthchecks - Built-in monitoring integration