Skip to main content

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)
Common use cases:
  • 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:
  1. Edit repository settings
  2. Scroll to Backup Hooks section
  3. Add scripts to:
    • Pre-Backup Script: Runs before backup
    • Post-Backup Script: Runs after backup
  4. Configure timeouts and behavior
  5. 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:
  1. Create or edit a scheduled job
  2. Enable Run Repository Scripts to execute per-repository hooks
  3. Add schedule-level scripts:
    • Pre-Backup Script: Runs once before all repositories
    • Post-Backup Script: Runs once after all repositories
Schedule-level hooks are ideal for global preparation/cleanup tasks, while repository-level hooks handle repository-specific operations.

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 the borg 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
Do not use sudo in scripts. Mount Docker socket or volumes with appropriate permissions instead.

Common Patterns

Stop/Start Docker Containers

Ensure consistent backups of containerized applications:
Docker Compose Setup:
Add the borg user to the docker group inside the container for socket access without root.

Database Dumps

Create consistent database backups:
Post-Backup Cleanup:

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:
Full archive verification (borg check) can take hours on large repositories. Use sparingly or in separate scheduled jobs.

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)
  1. Edit repository
  2. Scroll to Backup Hooks
  3. Set Pre-Hook Timeout and Post-Hook Timeout
  4. Save changes
Increase timeouts for long-running operations like database dumps or snapshots.

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)
When to enable:
  • Pre-hook is non-critical (e.g., optional notification)
  • Partial hook failure is acceptable
  • Want logs from failed hooks
When to disable:
  • Pre-hook creates essential state (e.g., database dump)
  • Hook failure indicates system problem
  • Consistency is critical
Post-hooks always run after backup, regardless of this setting. They see BORG_UI_BACKUP_STATUS to determine success/failure.

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:
  1. Navigate to repository or schedule settings
  2. Click Test Script button
  3. Script executes with 30-second timeout
  4. 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 ScriptsLibraryCreate 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.
Script library promotes reusability across repositories and schedules.

Best Practices

Always use set -e and handle errors gracefully:
Log script actions for debugging:
Add internal timeouts for external commands:
Make scripts safe to run multiple times:
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:
  1. Navigate to Backup Jobs
  2. Click on the failed job
  3. View logs for hook stderr output

Docker Commands Fail

Ensure Docker socket is mounted:
And user has docker group permissions:

Permission Denied

For host file access:
Or mount with appropriate permissions:

Next Steps

Schedule Automated Backups

Use hooks in scheduled backup jobs

Notification Setup

Configure built-in notifications