Efficient way to Backup cPanel accounts to another Server

Backup is your safety net when you are handling a website or a server. If you don’t take backups regularly, just stop right there and take a backup first. Because if you don’t take backups you are going to mess things up one day and nobody will have your back. Luckily. I am here to tell you how to have your back when you mess things up. There are a lot of ways to take backups but you should take backups as efficiently as you can to avoid spiking the server CPU or RAM usage and especially running out of space when working with huge websites.

Let me tell you the most efficient way to take backups of your all cPanel accounts and Sync them to another server every day automatically not just that but also get rid of old backups automatically. For this you will need the following:

On the source server (that has the cPanel accounts), Have an SSH script similar to this one.

 

#!/bin/sh

exec 1>/backup-ssh-logfile 2>&1

echo "Server Backup Start - $(date)";

echo "Making Today's Backup Folder..";

mkdir /auto_backup_script_$(date +"%d-%m-%y");

echo "Starting Backup Home Folder without Home Directory...";

/scripts/pkgacct --skiphomedir cpanel_username /auto_backup_script_$(date +"%d-%m-%y");

echo "Deleting Backups from day before yesterday..";

rm -rf /auto_backup_script_$(date -d "-2 day" '+%d-%m-%y');

echo "Old Backups Deleted";

(echo "Subject:Backup Log: $(date +"%d-%m-%y") "; cat /backup-ssh-logfile) |  sendmail -i  email@ibeehost.com;

 

What is happening in the script?

  • Create a log file name backup-ssh-logfile and put the script response in it
  • echo date and info that you might need in future
  • Make a backup directory with today’s date
  • Create a cPanel backup without the user’s home directory
  • Remove old backups
  • Send an email to the desired address with the log file content.

 

And have a script similar to this one on Destination Server (Backup Server):

 

#!/bin/sh

exec 1>/backup-ssh-logfile 2>&1

echo "Copying cPanel Accounts Backup without Home Directory - $(date +"%d-%m-%y")";

/usr/bin/rsync -avz -e "ssh -p 22" source.ibeehost.com:/auto_backup_script_$(date +"%d-%m-%y") /home/destination_folder/;

/usr/bin/rsync -avz -e "ssh -p 22" --exclude /path/to/exclude --exclude /path/to/exclude/ server.ibeehost.com:/home/source_username/ /home/destination_folder/cpanel_username;

(echo "Subject:Backups Sync Log: $(date +"%d-%m-%y") "; cat /backup-ssh-logfile) |  sendmail -i  email@ibeehost.com;

 

What is happening in the script?

  • Create a log file name backup-ssh-logfile and put the script response in it
  • Echo date and info that you might need in future
  • Sync Automated generated backups on source server without home directory using rsync
  • Sync user’s home directory using rsync (add as many users as you want)
  • Send an email to the desired address with the log file content.
  • You can remove old backups automatically if you want (Take help from first script)

 

Add both of these files in corn job using the following method or any alternative if you know.

1.

nano /etc/crontab

2. Paste the following in the crontab file:

5 3 * * *    root    ./backup.sh

(this means cron will run everyday at 3:05 AM)

3. Save the file and restart cron service using the command:

service crond restart

4. Apply correct permissions to backup.sh files in order to let cron execute it.

chmod +x backup.sh

If you need any help, do not hesitate to reach me out in the comment section.