The efficient way to Backup cPanel accounts to another server

When you are working with websites or servers, backups are your safety net. It’s important to take backups regularly, so you’re protected if something goes wrong. There are a few ways to take backups, but doing it the most efficient way is the key to making sure you don’t use too much of your server’s CPU, RAM, or storage space is key. 

Let’s talk about the most effective way to take backups of all your cPanel accounts, syncing them to another server daily and deleting the 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.

Keep 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 cron job using the following method 1.

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

Backing up your website and server regularly is a critical step in keeping your data safe. To take your backups efficiently, use the most effective method provided above. Back up all cPanel accounts, sync them to another server regularly and delete old backups automatically. Doing this will ensure you security of your valuable data, and protected from any untoward incidents.

fiz Written by:

Be First to Comment

Leave a Reply