How to create a Plesk Backup from the command line in CentOS

Plesk-LogoEven in Plesk 12 we can only schedule a single backup, and we must decide where said backup should be saved: either on the local server, or on an off-site FTP server. Sadly we can’t have it both ways.

Many of us – me included – find this very limiting.

Both local and remote backups have their advantages though, and greedy as I am I want the best of both worlds:

  • an FTP backup is stored off-site, which is great for disaster recovery.
  • but a local backup can be create and to restore much faster, which is great for development
  • a local server wide backup creates individual domain backups automatically, whereas an FTP backup does not – great for convenience

The good news is that Plesk has a set of command line tools which we can use to create a backup manually. In this scenario you can schedule a local backup in the Plesk web interface, and create another one using a cron job.

The tool I’m talking about is called pleskbackup and it lives in the Plesk utilities directory. On CentOS this is in /usr/local/psa/bin.

If called without parameters you’ll see a long list of options you can pass to this command:

Usage: pleskbackup  <command></command> [] 

Commands:

  server         Backs up whole Plesk.

  resellers-name Backs up selected resellers. Reseller's logins are read from command line,
                 space-separated. If no resellers provided, backs up all resellers
                 on the host.

  resellers-id   Backs up selected resellers. Reseller's identificators are read from command line,
                 space-separated. If no resellers provided, backs up all resellers
                 on the host.

  clients-name   Backs up selected clients. Client's logins are read from command line,
                 space-separated. If no clients provided, backs up all clients
                 on the host.

  clients-id     Backs up selected clients. Client's identificators are read from command line,
                 space-separated. If no clients provided, backs up all clients
                 on the host.

  domains-name   Backs up selected domains. Domain's names are read from command line,
                 space-separated. If no domains provided, backs up all domains
                 on the host.

  domains-id     Backs up selected domains. Domain's identificators are read from command line,
                 space-separated. If no domains provided, backs up all domains
                 on the host.

                 Use Exclude options to exclude some resellers/clients/domains.

  help           Shows this help page

...

Output file option:
  --output-file=
        /fullpath/filename      - regular file,
        -                       - use stdout for output,

        [ftp|ftps]://[[:]@]/fullpath/filename - storing the backup to ftp server.
                       FTP_PASSWORD environment variable can be used for setting password.
                       FTP option '--ftp-login' can be used for setting login.
                       FTP option '--ftp-password' (with '--ftp-login') can be used for setting password.
                       If your password contains slash, you have to use '--ftp-password' option or
                       FTP_PASSWORD environment variable.
                       'ftps' protocol can be specified to use FTP over SSL, instead of plain FTP.
                       With ftps specify 990 port to use implicit FTPS, otherwise explicit mode will be used.

        Used to import dump from repository into the single file.

Usage Example: Local Backups

To create a local server-wide backup, all we have to do is this:

pleskbackup server

This will create a backup just like the Backup Manager would in the Plesk web interface. Those are stored in a variety of files in /var/lib/psa/dumps and also in the respective domains/resellers/customers directories.

To create a single domain backup, simply replace “server” with “domains-name yourdomain.com” like this:

pleskbackup domains-name your yourdomain.com

Add more domains on the same line, separated by spaces.

Usage Example: FTP Backups

FTP backups are slightly more complex to create. We must pass all our credentials as part of the –output-file parameter, which requires us to specify a file name. Since the tool will create a .tar file, make sure you add this as part of your file name. Let’s look at a full example.

Imagine we’d like to create a server wide backup, and our FTP credentials are

– server (FTP Host)
– user (FTP User Name)
– password (FTP Password)
– filename.tar (our backup file name)

then we can call the command like this:

pleskbackup server --output-file=ftp://user:password@server/filename.tar

If all goes well you’ll receive no feedback. This is a synchronous task which on larger installations can take several hours – but it should be done within seconds on systems with dummy domains.

First the tool creates a local backup, then ZIPs up the tarball and sends it to the FTP server. Once finished, the tool comes back to the command line.

Automatically deleting older backups from FTP

I love how the Plesk Backup Manager takes care of older backups and deletes them automatically. The pleskbackup command line tool on the other hand isn’t so friendly and instead is a low-level command that only does one thing. If we’d like to see automatic deletions we’ll have to employ a script that will do this for us.

I’ve written such a script in PHP and put it on GitHub – feel free to examine it, test it, use it, redistribute it:

To use the script, take a look at the top of the file and amend your details accordingly:

// add your FTP credentials here
$ftp_server = "ftpserver";
$ftp_user = "ftpuser";
$ftp_password = "password";

// maximum number of backup files to keep on FTP
$maxbackups = 30;

// add a prefix for your backup
$prefix = 'BACKUP-'; 

// PLESK utilities directory
$pleskbin = "/usr/local/psa/bin/";

Save your changes and make sure you add execute permissions to the file:

chmod +x ./backup.php

Now you can call the file from the command line or via a Cron Job (Scheduled Tasks in Plesk). Execute the script as root and make as many backups as you like – in addition to what Backup Manager is doing from the web interface.

The one caveat I found is that the pleskbackup tool does not maintain the correct timestamp when it copies the tarball over to the FTP server. It’s an issue with pleskbackup rather than my script – I don’t understand how Backup Manager can do this fine in the web interface, which presumably relies on the same tool.

As a workaround I’m saving the date as part of the filename, i.e. BACKUP-YYYYMMDD-HHMMSS.tar. For the auto-deletion to work, make sure to use a dedicated directory for your FTP backups with no other files – otherwise the latest backup may be deleted.

To save your backups in a subfolder, amend the prefix like this:

// add a prefix for your backup
$prefix = 'subfolder/BACKUP-'; 

And that’s how we can create Plesk backups from the command line!





Jay is the CEO and founder of WP Hosting, a boutique style managed WordPress hosting and support service. He has been working with Plesk since version 9 and is a qualified Parallels Automation Professional. In his spare time he likes to develop iOS apps and WordPress plugins, or draw on tablet devices. He blogs about his coding journey at http://wpguru.co.uk and http://pinkstone.co.uk.