Overview
Backup hooks allow you to run custom scripts at key points in the backup lifecycle:- Pre-backup: Execute before backup starts
- Post-backup: Execute after backup completes (success, failure, or warning)
- Stop/start services during backups
- Create database dumps
- Snapshot volumes
- Send custom notifications
- Clean up temporary files
Hook Types
Repository-Level Hooks
Run every time a specific repository is backed up:- Edit repository settings
- Scroll to Backup Hooks section
- Add scripts to:
- Pre-Backup Script: Runs before backup
- Post-Backup Script: Runs after backup
- Configure timeouts and behavior
- Save changes
Repository hooks run for both manual and scheduled backups of that repository.
Schedule-Level Hooks
Run once per scheduled job, regardless of how many repositories:- Create or edit a scheduled job
- Enable Run Repository Scripts to execute per-repository hooks
- Add schedule-level scripts:
- Pre-Backup Script: Runs once before all repositories
- Post-Backup Script: Runs once after all repositories
Script Execution Environment
Available Variables
Hooks have access to environment variables with backup context:Working Directory
Scripts execute inside the Borg UI container:- Container path:
/home/borg - Host access: Use
/local/prefix - Mounted volumes: As configured in Docker
Permissions
Scripts run as theborg user (UID 1000):
- Full access to
/data(container data volume) - Access to mounted host directories via
/local/ - Can execute Docker commands if socket is mounted
Common Patterns
Stop/Start Docker Containers
Ensure consistent backups of containerized applications:Database Dumps
Create consistent database backups:LVM Snapshots
Create consistent snapshots of logical volumes:Require
--privileged or device mapping for LVM access from container.Custom Notifications
Send notifications beyond Borg UI’s built-in options:Verify Backup Integrity
Check backup health after completion:Hook Configuration
Timeout Settings
Control how long hooks can run before being terminated:- Pre-Hook Timeout: Default 300 seconds (5 minutes)
- Post-Hook Timeout: Default 300 seconds (5 minutes)
- Repository Settings
- Schedule Settings
- Edit repository
- Scroll to Backup Hooks
- Set Pre-Hook Timeout and Post-Hook Timeout
- Save changes
Continue on Failure
Control backup behavior when pre-hooks fail:- Enabled: Backup proceeds even if pre-hook fails
- Disabled: Backup is cancelled on pre-hook failure (default)
- Pre-hook is non-critical (e.g., optional notification)
- Partial hook failure is acceptable
- Want logs from failed hooks
- Pre-hook creates essential state (e.g., database dump)
- Hook failure indicates system problem
- Consistency is critical
Testing Scripts
Before using hooks in production, test them thoroughly:Test Script Syntax
Test with Variables
Simulate the backup environment:Use the Test API
Borg UI provides a script testing endpoint:- Navigate to repository or schedule settings
- Click Test Script button
- Script executes with 30-second timeout
- View stdout, stderr, and exit code
Test API uses sandboxed environment without full Docker access.
Script Library
Borg UI includes a script library for reusable templates:1
Create Script
Navigate to Scripts → Library → Create Script.
2
Write Script
Enter your script with parameterized variables:
3
Define Parameters
Add parameters for customization:
- Name:
CONTAINER_NAME - Description: “Docker container to stop”
- Default:
myapp
4
Use in Repository
When configuring hooks, select script from library and provide parameter values.
Best Practices
Error Handling
Error Handling
Always use
set -e and handle errors gracefully:Logging
Logging
Log script actions for debugging:
Timeouts
Timeouts
Add internal timeouts for external commands:
Idempotency
Idempotency
Make scripts safe to run multiple times:
Credentials
Credentials
Store secrets securely, not in scripts:Configure in Docker:
Troubleshooting
Hook Times Out
- Increase timeout in repository/schedule settings
- Optimize script performance
- Add debug logging to identify slow operations
Hook Fails Silently
Check backup job logs:- Navigate to Backup Jobs
- Click on the failed job
- View logs for hook stderr output
Docker Commands Fail
Ensure Docker socket is mounted:Permission Denied
For host file access:Next Steps
Schedule Automated Backups
Use hooks in scheduled backup jobs
Notification Setup
Configure built-in notifications