//Technology

Write cPanel Hook Scripts to Trigger JetBackup Snapshots Pre‑Upgrade

Automate JetBackup snapshots before every cPanel upgrade with a simple pre‑upgrade hook—ensure consistent backups, minimal downtime, and easy rollback.

5 min read
Write cPanel Hook Scripts to Trigger JetBackup Snapshots Pre‑Upgrade

Keeping a cPanel‑based VPS or dedicated server up to date is essential for security and performance. Each upgrade, however, carries a small risk: a mis‑step could corrupt data or leave a backup system in an inconsistent state. JetBackup, the backup solution that ships with many AtoZNode servers, can protect against these scenarios—if you trigger a snapshot at the right moment.

Why a Hook Script Makes Sense for JetBackup Snapshots

  • Automation – Forgetting to create a snapshot manually is easy. A hook runs automatically.
  • Timing – The hook fires exactly when cPanel begins the upgrade, so the snapshot reflects the pre‑upgrade state.
  • Low overhead – JetBackup snapshots are incremental and fast; the extra step adds negligible downtime.
  • Easy rollback – If the upgrade fails, you can revert with a single click in the JetBackup UI.

Understanding cPanel Hook Types

cPanel places hook scripts under /usr/local/cpanel/hooks. For core upgrades the preupgrade directory is the most relevant, as it runs just before /scripts/upcp starts. Other directories such as postupgrade or preinstall can be useful for different scenarios.

In this guide we’ll create a preupgrade hook that starts a JetBackup snapshot for every cPanel account on the server.

Prerequisites

  1. Root access to a server running cPanel & WHM (AlmaLinux 8, Rocky Linux 8, or RHEL 8).
  2. JetBackup 5 or later installed and configured.
  3. Basic familiarity with the Linux command line and a text editor such as vim or nano.

Step 1: Verify JetBackup CLI Availability

JetBackup ships a command‑line utility called jetbackup. Confirm it exists and is executable:

# which jetbackup
/usr/local/jetapps/jetbackup/jetbackup

If the command is not found, ensure JetBackup is installed correctly or contact support.

Step 2: Create the Hook Script

All hook scripts must reside in /usr/local/cpanel/hooks and be executable. We’ll create 99_jetbackup_snapshot_preupgrade.sh so it runs after any default hooks but before the upgrade starts.

AlmaLinux / Rocky Linux / RHEL (dnf‑based)

# cd /usr/local/cpanel/hooks
# cat > 99_jetbackup_snapshot_preupgrade.sh <<'EOF'
#!/bin/bash
# cPanel pre‑upgrade hook to trigger a JetBackup snapshot for all accounts
# Runs as root; no arguments are passed.

LOGFILE="/var/log/jetbackup_preupgrade.log"
echo "$(date '+%Y-%m-%d %H:%M:%S') Starting JetBackup pre‑upgrade snapshot" >> "$LOGFILE"

# Loop through each user in /etc/passwd
for user in $(cut -d: -f1 /etc/passwd); do
    # Skip system accounts – only back up real cPanel accounts
    if id -u "$user" >= 1000; then
        echo "Creating snapshot for $user" >> "$LOGFILE"
        /usr/local/jetapps/jetbackup/jetbackup snapshot create --account "$user" --type full >> "$LOGFILE" 2>&1
    fi
done

echo "$(date '+%Y-%m-%d %H:%M:%S') Snapshot process completed" >> "$LOGFILE"
exit 0
EOF

# Make the script executable
chmod 750 99_jetbackup_snapshot_preupgrade.sh

# Ensure root owns the file
chown root:root 99_jetbackup_snapshot_preupgrade.sh

Key points in the script:

  • cat > … <<'EOF' – creates the file with the script contents.
  • #!/bin/bash – specifies the interpreter.
  • LOGFILE – path where progress is written.
  • cut -d: -f1 /etc/passwd – lists all system users.
  • id -u >= 1000 – filters out system accounts (UID < 1000).
  • jetbackup snapshot create – starts a full snapshot for the account.
  • chmod 750 – allows root to execute while keeping the script safe from other users.

Step 3: Test the Hook Manually

Run the script directly to verify it completes without errors:

# /usr/local/cpanel/hooks/99_jetbackup_snapshot_preupgrade.sh

Check the log file for progress:

# tail -n 20 /var/log/jetbackup_preupgrade.log

You should see entries for each account and a final “Snapshot process completed” line. If any errors appear, double‑check the jetbackup binary path and the account list.

Step 4: Simulate a cPanel Upgrade to Verify Hook Execution

cPanel offers a dry‑run mode that lets you see how the upgrade would behave without making changes. Use it to confirm the hook runs at the correct time:

# /scripts/upcp --dry-run

After the command finishes, review the log file again. A new timestamped block should indicate the snapshot creation occurred during the dry run.

Step 5: (Optional) Add a Post‑Upgrade Hook for Cleanup

If you want to delete pre‑upgrade snapshots after a successful upgrade, create a postupgrade hook. This keeps storage usage tidy.

AlmaLinux / Rocky Linux / RHEL

# cat > /usr/local/cpanel/hooks/99_jetbackup_cleanup_postupgrade.sh <<'EOF'
#!/bin/bash
LOGFILE="/var/log/jetbackup_postupgrade.log"
echo "$(date '+%Y-%m-%d %H:%M:%S') Starting post‑upgrade cleanup" >> "$LOGFILE"

# Delete snapshots older than 7 days (adjust as needed)
/usr/local/jetapps/jetbackup/jetbackup snapshot prune --days 7 >> "$LOGFILE" 2>&1

echo "$(date '+%Y-%m-%d %H:%M:%S') Cleanup finished" >> "$LOGFILE"
exit 0
EOF

chmod 750 /usr/local/cpanel/hooks/99_jetbackup_cleanup_postupgrade.sh
chown root:root /usr/local/cpanel/hooks/99_jetbackup_cleanup_postupgrade.sh

This script uses JetBackup’s snapshot prune command to remove snapshots older than a configurable period.

Step 6: Document the Change and Inform Your Team

Even though the hook runs automatically, documenting the addition helps future troubleshooting. Record:

  • Hook script file paths and names.
  • Creation date and administrator responsible.
  • Log file locations (/var/log/jetbackup_preupgrade.log and /var/log/jetbackup_postupgrade.log).
  • Log rotation policy (e.g., rotate weekly with logrotate).

Step 7: Windows Server Note

cPanel does not run on Windows Server, so the hook methodology described here does not apply. If you manage Windows‑based web servers, consider using JetBackup’s Windows agent or a scheduled PowerShell script to create snapshots before any manual software updates.

Conclusion

By adding a simple preupgrade hook script, you can automate JetBackup snapshots right before any cPanel core upgrade. The approach eliminates manual steps, guarantees a consistent backup point, and gives you confidence when applying critical updates. The script works out of the box on AlmaLinux, Rocky Linux, or RHEL systems that host cPanel, and it can be extended with a complementary postupgrade hook for cleanup. Remember to test the hook in dry‑run mode, keep logs rotated, and document the change—best practices that keep your server reliable and your data safe.

cpaneljetbackupvpsserver automationbackuplinuxhooks

Try it on your own server

Follow along on a Cloud VPS with full root access, or read the step-by-step knowledge base guides.