How to Lock Suspended Account's Backups

The following Bash script, based on the blog post JetBackup: Better Safe Than Sorry, allows you to automatically lock backups for a set number of days when an account gets suspended and subsequently unlocks when it gets unsuspended. When the script is triggered (via cPanel hooks), it subsequently locks LOCK_BACKUPS number of backups for a LOCK_PERIOD number of days.

Here are the steps to create the Bash script and register to the cPanel Hooks System:

  1. Create an executable bash file.
touch /root/lock_unlock_acct.sh
chmod +x /root/lock_unlock_acct.sh
  1. Copy the following script to the bash file you created on Step 1.
#!/bin/bash
STAGE=$1
LOCK_PERIOD=360
LOCK_BACKUPS=5
LOCK_REASON="locked-by-hook"

ACCT=$( cat /dev/stdin  | grep -oE '"args":{[^}]*"user":"[^"]+"' | sed "s#^.*\"user\":\"\([^\"]\+\)\"#\1#g" )
#ACCT=$( cat /dev/stdin )

if [ $STAGE = "suspend" ]; then

        #GET LATEST LOCK_BACKUPS NUMBER OF SNAP IDs
        sIFS=$IFS
        IFS=$'\n'
        snaps=($( /usr/bin/jetapi backup -F listBackups -D "accounts[]=$ACCT&type=127&limit=$LOCK_BACKUPS&sort[created]=-1" | grep -E '^\s*_id' | awk '{print $2}' ))
        IFS=$sIFS


        for (( i=0; i<${#snaps[@]}; i++ )); do
                #LOCK SNAP BY ID
                /usr/bin/jetapi backup -F manageBackup  -D "_id=${snaps[$i]}&notes=$LOCK_REASON&locked=1&lock_ttl=$LOCK_PERIOD"
        done

fi

if [ $STAGE = "unsuspend" ]; then

        #GET LOCKED SNAPS BY THE LOCK REASON AND THE ACCOUNT NAME
        sIFS=$IFS
        IFS=$'\n'
        snaps=($( /usr/bin/jetapi backup -F listBackups -D "accounts[]=$ACCT&type=127" | grep "notes: $LOCK_REASON" -B 15 | grep -E '^\s*_id' | awk '{print $2}' ))
        IFS=$sIFS

        for (( i=0; i<${#snaps[@]}; i++ )); do
                #UNLOCK SNAP BY ID
                /usr/bin/jetapi backup -F manageBackup  -D "_id=${snaps[$i]}&notes=&locked=0"
        done
fi
  1. Register the Bash script inside cPanel's Hooks System by executing the following commands:
/usr/local/cpanel/bin/manage_hooks add script "/root/lock_unlock_acct.sh suspend" --manual 1 --category Whostmgr --event Accounts::suspendacct --stage post
/usr/local/cpanel/bin/manage_hooks add script "/root/lock_unlock_acct.sh unsuspend" --manual 1 --category Whostmgr --event Accounts::unsuspendacct --stage post