Nov 27

We need to migrate the data from a server to a new server often and it can be because of several factors like

  • Old server is having hardware issues and need to be replaced completely
  • Server is being upgraded and full data needs to be transferred
  • The servers are being transferred from one data center to another

and many more. This script can be used to migrate all user data and the cPanel config files from Old server to new Server. Install Cpanel (without the need to activate license). Update php/apache for required modules etc. Or any other software to be installed via ssh exactly same as of old server. Then create ssh keys from source to destination server. Keep the services down on destination server. Here is the migration script:

#!/bin/sh
# /////////////////////////////////////////
# //      SG 09/2004 CPanel              //
# //    CPanel transfer config script    //
# /////////////////////////////////////////
#
# HOST = User + IP / Hostname to copy files TO.
# All files copied from this server to the destinations server.
# Use this iscript to transfer THIS server to a new server.
HOST=”root@$1″
EMAIL=”user@domain.com”
error_log=/root/migration_error.log
backup_results=/root/migration.results
if [ "$1" == "" ]; then
echo “Usage: $0 IP”
echo “Example: $0 123.123.123.123″
echo “Will remotely transfer all files from THIS server over to this server IP 123.123.123.123″
exit 1
fi
echo “    WARNING – YOU ARE GOING TO COPY FILES FROM THIS SERVER TO $1″
echo “  THIS WILL OVERWRITE THE FILES ON THE REMOTE HOST AT $1 !!”
echo “”
echo “    Is this correct? Enter y/n”
read confirm
if [ "$confirm" == "n" ]; then
echo “    User aborted. Type y to transfer files.”
exit 1
elif [ "$confirm" == "N" ]; then
echo “  User aborted. Type y to transfer files.”
exit 1
elif [ "$confirm" != "y" ]; then
echo “    Sorry I don’t know what you neeed, enter y or n”
exit 1
else
#Begin moving files

# removed /etc/ips/
#Moving files in /etc/
for file in passwd shadow group quota.conf domainalias remotedomains reservedipreasons reservedips secondarymx localdomains userdomains valiases vfilters vmail trueuserdomains trueuserowners vdomainaliases domainips services sysconfig/pure-ftpd pure-ftpd.conf.* pure-ftpd proftpd proftpd.conf my.cnf named.conf rndc.conf exim rndc.key
do
if [ -d /etc/$file -o -f /etc/$file ]
then
rsync -zaHlv –safe-links –force –delete –delete-excluded –progress -e /usr/bin/ssh /etc/$file $HOST:/etc/ 2>> $error_log
fi
done
#Moving directories in /var/
rsync -zaHlv –safe-links –force –delete –delete-excluded –progress -e /usr/bin/ssh /var/cpanel $HOST:/var/ 2>> $error_log
rsync -zaHlv –safe-links –force –delete –delete-excluded –progress -e /usr/bin/ssh /var/log/bandwidth $HOST:/var/log/ 2>> $error_log
rsync -zaHlv –safe-links –force –delete –delete-excluded –progress -e /usr/bin/ssh /var/named $HOST:/var/ 2>> $error_log
rsync -zaHlv –safe-links –force –delete –delete-excluded –progress -e /usr/bin/ssh /var/lib/mysql/mysql $HOST:/var/lib/mysql/ 2>> $error_log
#Moving misc files
if [ -f /root/.my.cnf ]
then
rsync -zaHlv –safe-links –force –delete –delete-excluded –progress -e /usr/bin/ssh /root/.my.cnf $HOST:/root/ 2>> $error_log
fi

if [ -d /var/spool/cron ]
then
rsync -zaHlv –safe-links –force –delete –delete-excluded –progress -e /usr/bin/ssh /var/spool/cron $HOST://var/spool/ 2>> $error_log
fi

if [ -d /etc/ssl ]
then
rsync -zaHlv –safe-links –force –delete –delete-excluded –progress -e /usr/bin/ssh /etc/ssl $HOST:/etc/ 2>> $error_log
fi

if [ -d /usr/local/cpanel/3rdparty/mailman ]
then
rsync -zaHlv –safe-links –force –delete –delete-excluded –progress -e /usr/bin/ssh /usr/local/cpanel/3rdparty/mailman $HOST:/usr/local/cpanel/3rdparty/ 2>> $error_log
fi

if [ -d /usr/local/cpanel/base/frontend ]
then
rsync -zaHlv –safe-links –force –delete –delete-excluded –progress -e /usr/bin/ssh /usr/local/cpanel/base/frontend $HOST:/usr/local/cpanel/base/ 2>> $error_log
fi

if [ -d /usr/local/apache/conf ]
then
rsync -zaHlv –safe-links –force –delete –delete-excluded –progress -e /usr/bin/ssh /usr/local/apache/conf $HOST:/usr/local/apache/ 2>> $error_log
fi

if [ -d /usr/local/frontpage ]
then
rsync -zaHlv –safe-links –force –delete –delete-excluded –progress -e /usr/bin/ssh /usr/local/frontpage/*.cnf $HOST:/usr/local/frontpage/ 2>> $error_log
fi

if [ -d /usr/local/apache/domlogs ]
then
rsync -zaHlv –safe-links –force –delete –delete-excluded –progress -e /usr/bin/ssh /usr/local/apache/domlogs/* $HOST:/usr/local/apache/domlogs/ 2>> $error_log
fi

for SITE in `ls /var/cpanel/users`
do
if [ -d /home/$SITE ]
then rsync -zaHlv –safe-links –force –delete –delete-excluded –progress -e /usr/bin/ssh /home/$SITE $HOST:/home/ 2>> $error_log
fi

#Migrating users MySQL databases

if [ -d /var/lib/mysql ]
then rsync -zaHlv –safe-links –force –delete –delete-excluded –progress -e /usr/bin/ssh /var/lib/mysql/$SITE* $HOST:/var/lib/mysql/ 2>> $error_log
fi
done

rsync -zaHlv –safe-links –force –delete –delete-excluded –progress -e /usr/bin/ssh /var/lib/mysql $HOST:/var/lib/ 2>> $error_log

echo “**** SERVER MIGRATION COMPLETED: `date`…” >> $backup_results
cat $error_log >> $backup_results

date | mail -s “Restore completed for $HOST” $EMAIL < $backup_results
fi
exit 0

In case of server migration where Ips are changing, you need to run IP Migration Wizard inWHM, and setup rinetd on old server, and update dns zones to point to new IP. But in case Ips are moving as well, these steps are not required. rinetd can be installed on old server by getting from http://www.boutell.com/rinetd/. Its very easy to install  and the configuration is very simple:

The config is /etc/rinetd.conf.  <OLD IP> <OLD PORT> <NEW IP> <NEW PORT>.

You can use netstat -lpn on the server to see the forwarding in action.  If you make any changes to /etc/rinetd.conf you can reload it easily with “kill -1 <RINETD PID>”.

NOTE: This script can be downloaded directly from here.

Leave a Reply

You must be logged in to post a comment.