Plesk

Dropbox Backup Extension

Plesk Dropbox Backup Extension

Introduction To Plesk Dropbox Backup Extension

From my own experience of VPS usage, I noticed the need for cloud backup storage. The current version of Backup Manager for Parallels Plesk Panel does not support any remote storage except FTP, but I wanted to utilize a cloud storage for that purpose. As a developer, I wanted to implement a complete solution for the problem and share it with the Plesk community. Starting from version 11.0.9, you can add new functionality to Plesk with extensions – this is exactly what I needed. For my experiment, I chose Dropbox as a place for my stuff. It has a rather simple REST API with a lot of implementations for different programming languages. I used PHP SDK by Ben, but now the official Dropbox SDK is available.

Backend

In order not to reinvent the wheel, I decided to use native backup and restore utilities in Plesk:

/usr/local/psa/bin/pleskbackup domains-id --output-file={$outputFile} {$domainId}
/usr/local/psa/bin/pleskrestore --restore {$outputFile} -level domains -filter list:{$domainName}

Plesk Dropbox Backup extension that I created executes command line scripts on behalf of the `psaadm` user. Unfortunately `psaadm` does not have the permission to restore a backup for security reasons. The utilities have to be executed as `root`. Moreover, backend operations are rather heavy and take a lot of time for execution. They should be executed asynchronously.

In order to resolve these issues, I implemented a task manager: a scheduled script in cron that is run every minute with `root` privileges:

$task = new pm_Scheduler_Task();
$task->setSchedule(pm_Scheduler::$EVERY_MIN);
$task->setCmd("plesk-task-manager.php");
pm_Scheduler::getInstance()->putTask($task);

The script of the task manager is not run, if a previous operation has not been completed yet. To achieve this, I used a locking technique, which is based on flock system call, so there will be no blocked or waiting processes of the task manager. Tasks are stored in a SQLite database, so we can track tasks statuses in the UI and pass arguments to the backend script.

The utilities execution is very simple:

$cmd = "/path/to/utility --command -args 2>&1";

exec($cmd, $output, $code);

The $output and $code variables are written by corresponding output and return the utility status. In order to have stderr dump, the `2>&1 ` redirect should be used.

User Interface

The major goal of the Dropbox Backup extension is to back up a Plesk subscription. Thus, the extension entry point should be placed on the Websites & Domains screen in Plesk.

The post-installation script should create a custom button using API-RPC gateway:

$request = <<<APICALL
<ui>
    <create-custombutton>
        <owner>
            <admin/>
        </owner>
        <properties>
            <conhelp>$description</conhelp>
            <file>$iconPath</file>
            <public>$isPublic</public>
            <internal>true</internal>
            <noframe>true</noframe>
            <place>domain</place>
            <url>$entryPointUrl</url>
            <text>Dropbox Backup</text>
        </properties>
    </create-custombutton>
</ui>
APICALL;

$response = pm_ApiRpc::getService()->call($request);
$result = $response->ui->{"create-custombutton"}->result;
if ('ok' == $result->status) {
    // success
} else {
    throw new pm_Exception("Failed to add custom button: code $result->errcode: $result->errtext");
}

Let’s see the result:

Plesk Dropbox Backup extension

Dropbox uses the OAuth protocol for authorization. The first handling of an API call will redirect you to your Dropbox account in order to get a token for the Dropbox Backup extension:

The extension gets tokens for each Plesk subscription – different subscriptions may use different accounts. The tokens are encrypted and stored in the SQLite database. When a Dropbox account is successfully authorized, the `/Apps/plesk-backup/` folder is created in this account. Your backup files will be stored in this folder.

The general screen of the Dropbox Backup extension shows the list of available backups. You can extend the pm_View_List_Simple class to view your own data, paging, sorting controls and other tools in this list.

To back up files for the current subscription immediately, click the ‘Back Up Now’ button. All backup and restoration tasks are shown on the ‘Current Tasks’ tab:

The ‘Settings’ tab allows you to set up scheduled backups. Its implementation extends the pm_Form_Simple class and consists of the basic elements: a checkbox and a text input area.

A backup file is named after the subscription and uploaded to Dropbox. So, in case a previous backup file already exists, it will be overwritten. But Dropbox keeps changes for 30 days. There is no need to rotate the filename, because previous files are still available.

If you are going to restore the subscription (I hope you won’t), click on the backup name and choose the version of the backup file:

There is a small pleasant thing: Dropbox client notifies you about any file update:

Resume

The following features are available in the implemented extension:

  • Immediate backup of a subscription.
  • Scheduled (daily) backup of a subscription.
  • Restoration of a subscription backup (choose one of 10 previous versions).
  • Files are rotated by Dropbox: your storage will never be full.

There are some known problems and limitations:

  • Only Linux OS is supported by now.
  • Backup/restoration of the server/customer/reseller is not implemented.
  • Unable to restore a backup on a server different from the one where it was created.

As a VPS owner, I have the desired solution for cloud backup storage. I spent several days developing and the result was worth it.

The extension is free and can be downloaded here.

You can ask any questions related to the extension and give your feedback in the thread on our official forum.