A smarter way to migrate two cPanel servers

One of our cPanel server was running fine without any problem since an year. Suddenly cPanel started sending me emails saying ‘Possible Hard disk Failure’ . Contacted the datacenter to inquire more about this error and perform a log auditing. Dc advised to take the server offline for full hardware test so we did it. The result wasn’t good.

The datacenter advised to reinstall the OS and cPanel on a new hard disk as soon as possible. However this may cause a minimum of 5 hours downtime. So I’ve decided to keep the existing server online and order a new one.  This way, when the new server is up I could move accounts from the old server to the new server and change the nameserver ips so that there will be zero downtime.

Now, I have the both servers up and next thing is to migrate  around 1000 low traffic domains. Most of them are dummy domains and used only for mails.

Listed below are the available methods of migration from one server to another.

  1. Use the WHM inbuilt tool to transfer accounts
  2. Rsync or Scp all the files including site contents, emails as well as databases

Most  server owners are familiar with the first method because it is rather easy to do. However it requires an active connection to the server always. Copying 1000 accounts will take around 6-10 hours and we need to have internet connection ON and ofcourse, our computer must be ON as well.

I am on a limited bandwidth internet connection at this time and I really don’t want to keep my computer on for 10 hours as it is  wastage of energy, plus money. So I made a new way to transfer files automatically.

Step 1

Lets assign the old server as SERVER1 and new server as SERVER2

The first step we have to do is to connect these servers with the help of SSH Hash so that it won’t ask for password when we transfer accounts. There are plenty of documentation available on net so that I am not covering this part here. After doing it you may need to check it by transferring a simple file using “scp”

Step 2.

What we are going to do is to make individual archives of the user accounts using /script/pkgacct but without any compression. By disabling the compression, the speed will be increased. This has another plus point too, that the server load won’t be increased to a large extend. One shot, two birds. After making the archive, we need to transfer it to the Server2 using SCP command which is quite simple to do. Thus, we have to archive 1000 accounts and send it to the remote server.

The advantage of archiving the accounts over using rsync is that it won’t take more time to send the file. Rsync isn’t a best choice in this case as it isn’t fast here.

So, lets write a simple php script that will read all usernames on the SERVER1 , Make Backup and sent to the SERVER2.

We create two folders for placing our files

[bash]
mkdir /root/test
mkdir /root/test/users

[/bash]

Now, copy paste below lines and save it as /root/test/backup.php

[code]
<?php
echo "<pre>";
$files = ListFiles("/var/cpanel/users");
foreach($files as $user)
{
if(!is_file("users/$user"))
{
echo "Backing up user $user";
system("/scripts/pkgacct $user –nocompress");
system("scp /home/cpmove-$user.tar root@SERVER2:/home");
filewrite("users/$user","");
system("rm -f /home/cpmove-$user.tar");
}

}

function ListFiles($folder)
{
$dir = opendir($folder);
while($file = readdir($dir))
{
if ($file != "." && $file != "..")
$files[] = $file;
}

@sort($files);
return $files ;
}

function filewrite($file,$what)
{
$fh = fopen($file, ‘w’);
fwrite($fh, $what);
fclose($fh);
}

?>
[/code]

Let’s go through the script.
What this script does is to loop through the entire user accounts on the SERVER1 and make a full cpanel backup without compression.
It will also write the usernames as files in the folder “user” to make sure that it is not backing up the same usernames again in case if the server is rebooted or the script is executed again. Smart, isn’t it ?
Make sure that you change SERVER2 to the ip address of the SERVER 2

Save the file as backup.php in /root/test

Now, execute the backup.php file, and it will start backing up accounts. However you’ll notice that if you disconnect from the ssh session the script will terminate.
So you have to be online all these time. Well , there is a solution for this, Screen.

[bash]
screen -S backup
cd /root/test/ ; /usr/local/cpanel/3rdparty/bin/php backup.php
[/bash]

Note that we are using cPanel PHP instead of EasyApache PHP.

If you disconnect from the console, you can reconnect using the command
[bash]
screen -r backup
[/bash]

Note: if you get error, try screen -d -r backup

Ok, the first part is over. Now the second part

Step 3

Login to the new server, SERVER 2

create a file restore.php in /home

[bash]
cd /home
nano restore.php
[/bash]

and save the file with this content

[code]
<?php

$files = glob("*.tar");
foreach($files as $file)
{
$explode = explode("-",$file);
$explode = explode(".",$explode[1]);
$user = $explode[0];
if(!is_file("/var/cpanel/users/$user"))
system("/scripts/restorepkg $user");
}
?>
[/code]

That’s it. We also need to make a cron to execute this script so that whenever the Server1’s script send a backup file to this server, restore.php restores the backup file.
I used 10 minutes interval which is quite fine.

[bash]
crontab -e
[/bash]
[bash]*/10 * * * * cd /home; /usr/local/cpanel/3rdparty/bin/php restore.php [/bash]

That’s all. We are done! Now we can go and take some test. These scripts will do the needful. I am writing this blog because I have free time now as these scripts are executing on my servers at this moment.

3 thoughts on “A smarter way to migrate two cPanel servers

Leave a Reply to vignes Cancel reply

Your email address will not be published.