CPanel Migration Script

Here it is a small bash script for full CPanel migration (or partial migration if you want it so :)). Using web interface is a little complicated when you have many accounts to transfer, because your connection might get down or you can even close the browser by mistake.


#!/bin/sh

#
# CPanel-CPanel migration script
# ver 0.3
# Felician Moldovan @ 2008-2012
# felician.moldovan@gmail.com
##########################

# 20120725 - ver 0.3 - added tempdir variable (to save cpmove archive in case /home is full)
# 20090808 - ver 0.2 - added logging to file, a few tests, cpanel force restore option

#Usage $0 destination account_list_file
# to create users list:
# ls -1 /var/cpanel/users > user_list.txt
: ${2?"Usage: $0 destination_server account_list_file [ tempdir ]"}

LOG_FILE="/tmp/cpanel_migration.log"
{
REMOTE_SSH_PORT="22"

remote_server=$1

# The script must be run on the source host as root

if [ ! -e /root/.ssh/id_dsa.pub ]; then
echo "Public dsa key does not exist!!! Generating ..."
ssh-keygen -t dsa
fi

key=`cat /root/.ssh/id_dsa.pub`
ssh $remote_server -p ${REMOTE_SSH_PORT} "mkdir -p /root/.ssh;echo $key >> /root/.ssh/authorized_keys"
if [ $? -gt 0 ]; then
echo
echo "Could not connect to $remote_server on port ${REMOTE_SSH_PORT}. Exiting ..."
echo
exit 1
else
echo "SSH key copied to destination $?"
fi

if [ ! -e $2 ]; then
echo
echo -e "Accounts list does not exist. Use:\n"
echo -e "ls -1 /var/cpanel/users > user_list.txt\n"
echo -e "to generate the list. Exiting ...\n"
exit 1
fi

if [ -z $3 ]; then
tempdir="/home"
else 
tempdir=$3
fi

echo
echo -e "===========================================\n"
echo -e "Starting migration to ${remote_server}"
echo -e "Log file: ${LOG_FILE}\n"
echo -e "Temporary dir to save archive(s): $tempdir\n"
echo "===========================================\n"

accounts_list=`cat $2`
for account in $accounts_list; do
echo
echo "=============================================="
echo -e "Packing account ${account} ...\n"
/scripts/pkgacct ${account} $tempdir
echo
echo "=============================================="
echo -e "Copying account ${account} to destination ...\n"
echo
scp -P ${REMOTE_SSH_PORT} ${tempdir}/cpmove-${account}.tar.gz $remote_server:/home/
echo
echo "=============================================="
echo -e "Restoring account ${account} ..."
echo
ssh $remote_server -p ${REMOTE_SSH_PORT} /scripts/restorepkg --force ${account}
echo
echo "=============================================="
echo -e "Cleaning archive /home/cpmove-${account}.tar.gz ...\n"
rm -f /home/cpmove-${account}.tar.gz
done
echo
echo -e "============================================="
echo -e "Migration Done!!!!!!!!!!\n"
} 2>&1 | tee ${LOG_FILE}

exit 0

Published on 2012/07/25 at 17h17 by Bashar, tags , , ,

comment CPanel Migration Script

Powered by Publify | Photo Startup stock photos