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 Example: Local Backups

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

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:

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:

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:

  • https://github.com/versluis/Plesk-Backup

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

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

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:

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.

4 thoughts on “How to create a Plesk Backup from the command line in CentOS

  1. Hi Jay, greetings and many THX from Germany,
    your script made it, makes me happy, works fine !

    So, thank you again for your work and your time to wrote all!

    joerg

  2. In windows changes required —>> ‘ (Single Quotes) add ” (double Quotes) else error

    Further script does not run and throws below error. Tried all combinations / Changes

    “The filename, directory name, or volume label syntax is incorrect.”

Add your voice!