Howto backup cpanel via rsync

Cpanel has a nice backup system especially in case you’ll have to restore the server. What I don’t like about it is the absence of ssh as a protocol to transfer the data to a remote server. There are two ways to backup to a remote server :

  • ftp

Using ftp the server has to tar or tar.gz the accounts first which is a heavy and time consuming process. In case you’ll have to restore the server you’ll have to place all the tar files to your server and process the accounts one by one.

  • nfs

Using nfs you can have incremental backups and you don’t have to tar the cpanel accounts but I haven’t managed to get decent speeds if the source and destination servers are in different datacenters. Also in case something goes wrong while backup is active and nfs goes down for some reason, a total disaster is about to happen.

Setting-up backups with ssh & rsync

To backup with rsync and ssh you’ll have to install rsync to your servers and setup a passwordless key based authentication for ssh.

When you’re done setting up rsync and ssh keys, create /scripts/postcpbackup

vi /scripts/postcpbackup

and insert the following code

  • /backupdir is the local directory where you store your cpanel backups
  • remoteuser is the user of remote server you’re connecting
  • IP_OR_FQDN is the ip or the fqdn of the remote server
#!/bin/sh

/usr/bin/rsync -va --progress  --delete  --rsh='ssh -p22' /backupdir remoteuser@IP_OR_FQDN:~/

make the script executable

chmod +x /scripts/postcpbackup

After that login to whm and go to “Main >> Backup >> Configure Backup” to configure your backup plan. Set “Backup Destination” to a local directory eg. “/backupdir”, ideally this directory should be mounted on a different hdd than your “/home” and “/”. At the bottom end of the page you’ll see “Execute Pre/Post Backup Script” feature added. Mark “/scripts/postcpbackup” and save.

Next time cpbackup will be executed, it will back up everything to your local directory and when it is done, it will rsync your local backup directory to your remote backup server.

Warning: Since everything is backedup at the cpanel server. Make sure that you have enough free space at your hdd. Unfortunately the “Use Hard Links” option is still in experimental stage 🙁 .

eg. If you setup cpbackup to take daily, weekly, monthly backups you’ll have to make sure you have more free hdd space than 3 times the /home .

Leave a comment ?

14 Comments.

  1. Thanks for the tip. We will try it out and see how it goes.

    Does rsynch and ssh really have to be passwordless key? Isn’t that insecure?

    • The key has to be passphase-less in order to have the backup process executed automatically. Otherwise every time the script is executed, someone has to be there to enter the passphase.

    • It doesn’t have to be passwordless because you can use password option over tcp port 873

  2. Hi

    which version of cpanel has that option “Execute Pre/Post Backup Script” i have whm 11.32.3 and i dont have such option.

    Thanks.

  3. This is exactly what I’m looking for. Though this tutorial would have been complete if more details of below statement is included.

    “To backup with rsync and ssh you’ll have to install rsync to your servers and setup a passwordless key based authentication for ssh.”

    Anyhow, I’m Googling to see how to setup rsync. 🙂

    • I’ll create an additional post sometime on howto use rsync with passwordless keybased authentication. In the meanwhile a google search will give you plenty of tutorials

      For rsync installation a simple

      yum install rsync

      for red-hat based distributions or

      apt-get install rsync

      for debian based distributions will do.

  4. Hi,

    Great tutorial but one question. Will this script delete the data from the local backup folder after it’s been sent to the remote backup server?

    Thanks!

    • No it won’t. You can easily delete the data from the local backup folder by adding

      rm -Rf /backupdir/*

      at the end of “/scripts/postcpbackup”

      But you will lose the incremental part of the backup, since everything will be recreated every time cpbackup is executed.

      Maybe the default ways of cpbackup (via ftp or nfs) suit you better.

  5. There’s a reason rsync to a remote nfs disk is slow. There is only one rsync instance reading files that are to be backed up, then reading the same files from the nfs mount and comparing. Rsync not only has to read every byte of local files it also has to read every byte of the files on the remote nfs mount.

    With rsync over ssh there is an rsync at each end. Each reads every byte of the files on the local disc but they send differences over the network.

  6. Hi there. Thanks so much for this tutorial. I have a question. I do not have enough disk space to backup my cpanel server. I have a second server installed that I want to use rsync so that I can get the new server up and running with WHM and CPANEL. However, I have only seen where I can create a local backup to move to a remote server. I need the ability to use rsync or anything you could mention that would allow me to do a “live” move. Is that possible? Any suggestions on moving a CPANEL server with rysnc – like what to turn off and the caveats. I have zero experience moving cpanel servers…

    Wishing you very well,
    Douglas

    • Hi Douglas,

      Cpanel has the feature you need for the migration. When you’re done setting up the new server, you can login to the new servers whm and go to :

      Home » Transfers » Copy Multiple Accounts/Packages From Another Server

      From there give the credentials for the old server and choose the packages and the accounts you want to migrate, dns zone files are being updated automatically.

      If you’re totally out of hhd space you could try migrating the whole server with rsync but you must be careful about what to exclude and the permissions/owners the files have.

      Another thing you could do if you’re totally out of hdd space is to manually migrate one cpanel account that takes lots of hdd space and afterwards delete it from the old server and migrate the rest using whm copy accounts feature.

Leave a Comment

NOTE - You can use these HTML tags and attributes:
<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>