jetbackup

As hosting providers, you sometimes have to deal with huge account backups (over 100GB per account for example). JetBackup will make your life easier backing up these huge accounts, however restoring them is not always something we want to do.

Why would we want to prevent restoring huge accounts ?

When using JetBackup remote destinations, user’s data is saved directly on the backup destination, making your local space highly optimized. However, if you would like to perform a “cPanel full account restore”, JetBackup will first have to copy all the data into a temporary folder on your local drive, creating manually “cpmove” restore package and then executing cPanel’s internal restore command. This could easily consume your entire drive space, filling it up to 100%, and then the troubles will start 

Best approach in these cases will be preventing users that match certain conditions from downloading / restoring their account, forcing them to contact you for these restore, so you can consider your actions in a safely manner.

Utilizing JetBackup hooks to prevent download / restore an account

In the following example we will learn how to create JetBackup hooks that will prevent accounts matching certain conditions from performing restore or download (or any other action we would like).

Step 1 :: Navigate to hooks page, click on “Create new hook”

Step 2 :: Create your “Download” hook

Hook name – name your hook for you to better understand it’s action.

Hook Position – Choose PRE so we can abort action before it starts

Hook type – Choose DOWNLOAD

Download Types – We choose “Accounts Files“, “Accounts Full“, this should cover all download scenarios

Hook Script – Put the path to your script, in the given example our script was PHP script so we used JetBackup’s internal php and executed our file with it – “/etc/jetapps/3rdparty/php70/bin/php-cgi -q /root/disklimithook.php

DO THE SAME STEP AGAIN FOR THE RESTORE HOOK, UNDER “HOOK TYPE” CHOOSE “RESTORE”. You should now see two hooks in the hooks page, one for “download” and one for “restore”.

Working with our script

We linked our hook to “/etc/jetapps/3rdparty/php70/bin/php-cgi -q /root/disklimithook.php” script, here is the script –

#!/etc/jetapps/3rdparty/php70/bin/php-cgi -q
<?php

define('DISK_TO_ABORT', 5120000000); // 5Gb

$stdin = fopen('php://stdin', 'r');

$data = fread($stdin, 102400);

parse_str(urldecode($data), $output);

if(trim($output['stage']) == "onDownloadStart") {
$cmd = "jetapi backup -F getQueueItem -D \"_id=" . trim($output['queue_id']) . "\" -O json";
exec($cmd, $out, $exitCode);

if(!isset($out[0])) die(0);
$out = json_decode($out[0], true);
if($out === false) die(0);

$output = $out['data'];
}

$cmd = "/usr/bin/jetapi backup -F getAccount -D \"_id=" . trim($output['account']) . "\" -O json";
exec($cmd, $out, $exitCode);

if(!isset($out[0])) die(0);
$out = json_decode($out[0], true);
if($out === false) die(0);

if($out['success'] && intval($out['data']['disk_space_usage']) > DISK_TO_ABORT) {
fwrite(fopen('php://stderr', 'w'), "Aborting: " . trim($output['account']) . " disk size is greater then " . DISK_TO_ABORT . PHP_EOL);
die(1);
}

?>


This code gets the needed params from the hook, then checks the defined ‘DISK_TO_ABORT‘ value (in our case 5120000000 which are 5GB, you can change that). The script was written to handle either a download hook, or a restore hook to ease the use.

To work with the script simply copy & paste it and put at the following location – “/root/disklimithook.php” and it should work out of the box.

Testing the hooks – Let’s try to download from an affected account !

As you can see, our script returned “EXIT CODE 1” (false) so JetBackup aborted the action, download in that case.

For more information about JetBackup hooks, please see https://docs.jetbackup.com