Post Reply  Post Thread 
Server migration script
Author Message
Nilesh
Super Moderator
******


Posts: 42
Group: Super Moderators
Joined: Jul 2007
Status: Offline
Reputation: 0
Thank 0
0 was given thank in 0 posts
Post: #1
Server migration script

Server migration script

Following is the script which use to migration the domains from one server to another.

Quote:
#!/bin/bash


remoteip='12.234.56.789' # Remote server's IP
sshport='22' # Remote ssh port number
ALERT='95' # Maximum drive space usage before script shuts down.
loadcheck='1' # Set to '1' will monitor load on both servers. Set to '0' to disable.
LL='4' # Max load on this server before processing each account.
RL='6' # Max load on remote server before processing each account.

#_________________________________

clear
export LANG=en_US.ISO8859-1
export LC_ALL=en_US.ISO8859-1


if [ ! -d /etc/mig/data ]; then
mkdir /etc/mig/data
fi
cat < /dev/null > /etc/mig/data/report
cat < /dev/null > /etc/mig/data/error

report='/etc/mig/data/report'

if [ ! -s /root/.my.cnf ]; then
echo "File: /root/.my.cnf does exist or is empty... Exiting..."
exit
fi
if ! cat /root/.my.cnf | grep "pass=" | replace 'pass=' '' | replace '"' '' > /dev/null; then
echo "Can\'t find password in /root/.my.cnf... Exiting..."
exit
fi

# Grabbing list of databases
PASSWORD=`cat /root/.my.cnf | grep "pass=" | replace 'pass=' '' | replace '"' ''`
mysql --user=root --password=$PASSWORD --batch -N -e "show databases" &> /etc/mig/data/databases

if cat /etc/mig/data/databases | grep "Access denied" > /dev/null; then
echo "Cannot grab database list. Bad password. Try updating password in WHM. Exiting..."
exit
fi

echo
echo -n "Checking remote server for ssh access... "
(ssh -p $sshport $remoteip "uptime" &> /dev/null) || {
echo
echo
echo "Problem accessing remote server... exiting....."
exit
}
echo "OKAY"

if [ "$1" = "--retry" ]; then
cat < /etc/mig/data/retry > /etc/mig/data/users
else
if [ -s /etc/mig/USERS ]; then
cat < /etc/mig/USERS > /etc/mig/data/users
else
ls -A /var/cpanel/users > /etc/mig/data/users
fi
fi

for user in `cat /etc/mig/data/users`; do

echo
echo -n "Checking disk space on remote server... "
ssh -p $sshport $remoteip "df -H | grep -vE '^Filesystem|tmpfs|cdrom'" > /etc/mig/data/dCheck 2> /dev/null
cat /etc/mig/data/dCheck | awk '{ print $5 " " $1 }' | while read output;
do
usep=`echo $output | awk '{ print $1}' | cut -d'%' -f1`
partition=`echo $output | awk '{ print $2 }'`
if [ $usep -ge $ALERT ]; then
echo
echo
echo "RUNNING OUT OF SPACE: \"$partition ($usep%)\""
echo
echo "SHUTTING DOWN NOW"
exit
fi
done

sleep 1
echo "OKAY"

if [ $loadcheck = 1 ]; then
echo
echo -n "Checking server load... "
cpu=`awk '{print $1}' /proc/loadavg | replace '.' ' ' | awk '{print $1}'`
if [ "$cpu" -gt "$LL" ]; then
echo
echo
echo -n "Waiting for load to go down"
ct='0'
while [ "$cpu" -gt "$LL" ]; do ct=`expr $ct + 1`
echo -n "."
sleep 1
done
fi
echo "OKAY"
fi

echo
echo
echo "Processing $user..."
echo

if [ ! -e /var/cpanel/users/$user ]; then
echo
echo "$user not found on this server... skipping..."
echo "$user ERROR N/A" >> $report
echo "$user - not found on this server" >> /etc/mig/data/error
continue
fi

echo
echo -n "Looking for $user on remote server... "
ssh -p $sshport $remoteip "ls -A /var/cpanel/users | grep $user" > /etc/mig/data/uCheck 2> /dev/null

if [ -s /etc/mig/data/uCheck ]; then
echo
echo
echo "Found $user on remote server... skipping..."
echo "$user ERROR $DC" >> $report
echo "$user - already on remote server" >> /etc/mig/data/error
continue
else
echo "OKAY"
fi

if ! grep "DNS=" /var/cpanel/users/$user > /dev/null; then
echo
echo
echo "No DNS info in /var/cpanel/users/$user... skipping..."
echo "$user ERROR N/A" >> $report
continue
fi

if cat /etc/mig/data/databases | replace '_' ' _' | grep -w "$user" | replace ' _' '_' > /etc/mig/data/udb; then
echo
echo
echo "Found databases for this user. Checking for errors and reparing if needed... Please wait......"
for udb in `cat /etc/mig/data/udb`; do
mysqlcheck -r --user=root --password=$PASSWORD $udb
done
else
echo "No databases found for this user"
fi

head -n 1 /etc/nameserverips | replace '=' ' ' | awk '{print $2}' > /etc/mig/data/nameservers
cat /var/cpanel/resellers-nameservers | replace ',,,,' '' | replace ',,,' '' | replace ':' ' ' | replace ',' ' ' | awk '{print $2}' | sort | sed '/./,/^$/!d' >> /etc/mig/data/nameservers

echo
echo "Packing up $user... please wait..."
(/scripts/pkgacct $user) || {
echo "Problem packing $user... skipping..."
echo "$user ERROR $DC" >> $report
echo "$user - error packing" >> /etc/mig/data/error
continue
}
echo
echo -n "Checking package integrity... "
gzip -t /home/cpmove-$user.tar.gz > /etc/mig/data/gzt
if [ -s /etc/mig/data/gzt ]; then
echo
echo
echo "Problem found with package... trying again..."
rm -rf /home/cpmove-$user.tar.gz
(/scripts/pkgacct $user)
echo
echo "Rechecking package integrity..."
gzip -t /home/cpmove-$user.tar.gz > /etc/mig/data/gzt
if [ -s /etc/mig/data/gzt ]; then
echo "Failed again... moving on..."
echo "$user ERROR $DC" >> $report
echo "$user - integrity error" >> /etc/mig/data/error
continue
fi
else
sleep 1
echo "OKAY"
fi

echo
echo "Transferring package..."
(scp -P $sshport /home/cpmove-$user.tar.gz $remoteip:/home) || {
echo "Problem transferring package... skipping..."
echo "$user ERROR $DC" >> $report
echo "$user - transfer error" >> /etc/mig/data/error
continue
}
echo

MDS=`md5sum /home/cpmove-$user.tar.gz`
ssh -p $sshport $remoteip "md5sum /home/cpmove-$user.tar.gz" > /etc/mig/data/pCheck 2> /dev/null
RMDS=`cat /etc/mig/data/pCheck`

if [ "$MDS" != "$RMDS" ]; then
echo
echo "Problem found with remote package... resending..."
(scp -P $sshport /home/cpmove-$user.tar.gz $remoteip:/home)
ssh -p $sshport $remoteip "md5sum /home/cpmove-$user.tar.gz" > /etc/mig/data/pCheck 2> /dev/null
RMDS=`cat /etc/mig/data/pCheck`
if [ "$MDS" != "$RMDS" ]; then
echo
echo "Still having issues with remote package... skipping..."
echo "$user ERROR $DC" >> $report
echo "$user - md5sum mismatch" >> /etc/mig/data/error
continue
fi
fi

if [ $loadcheck = 1 ]; then
echo
echo -n "Checking remote server load... "
cc='0'
while true; do
ssh -p $sshport $remoteip "awk '{print $1}' /proc/loadavg" > /etc/mig/data/lCheck 2> /dev/null
cpu=`cat /etc/mig/data/lCheck | replace '.' ' ' | awk '{print $1}'`
if [ "$cpu" -gt "$RL" ]; then
if [ $cc = 0 ]; then
echo
echo
echo -n "Waiting for load to go down"
fi
ct='0'
while true; do ct=`expr $ct + 1`
echo -n "."
sleep 1
if [ $ct = 30 ]; then
cc='1'
break
fi
done
else
break
fi
done
echo "OKAY"
fi

echo
echo "Setting up $user on remote server....."
echo
ssh -tq -p $sshport $remoteip "/scripts/restorepkg $user"

ssh -tq -p $sshport $remoteip "ls -A /var/cpanel/users | grep -w $user" > /etc/mig/data/sCheck
if [ ! -s /etc/mig/data/sCheck ]; then
echo "Problem setting up $user on remote server... skipping..."
echo "$user ERROR $DC" >> $report
echo "$user - problem with setup on remote server" >> /etc/mig/data/error
continue
else
ssh -tq -p $sshport $remoteip "rm -rf /home/cpmove-$user.tar.gz"
fi

echo
echo "Editting DNS zone..."
dom=`grep "DNS=" /var/cpanel/users/$user | sort | head -n 1 | replace 'DNS='`
uip=`grep "IP=" /var/cpanel/users/$user | replace 'IP=' ''`
replace -s "$uip" "$remoteip" -- /var/named/$dom.db
echo
echo "Restarting bind..."
(/scripts/restartsrv_bind)

for dns in `cat /etc/mig/data/nameservers`; do
if whois $dom | egrep -i "$dns" > /dev/null; then
DC='LOCAL'
else
DC='REMOTE'
fi
done

echo
echo "$user OKAY $DC" >> $report

rm -rf cpmove-$user.tar.gz
done

echo
echo "All done...."

clear
echo
echo "Preparing report..."
echo
sleep 3

awk 'BEGIN { format = "%-17s %-17s %s\n"
printf format, "User", "Status", "DNS Check"
printf format, "----", "------", "---------" }
{ printf format, $1, $2, $3 }' $report
echo
echo

if [ -e /etc/mig/data/error ]; then
echo "Accounts with an Error status are listed in the \"/etc/mig/data/error\" file."
echo "You may run the follow commands to retry failed accounts."
echo
echo "cat /etc/mig/data/error | awk '{print \$1}' > /etc/mig/data/retry"
echo "mig --retry"
fi

echo
echo

exit


~~~ Cool Buddy ~~~

ADD TO DEL.ICIO.US  ADD TO DIGG  ADD TO FURL  ADD TO NEWSVINE  ADD TO NETSCAPE 
ADD TO TECHNORATI FAVORITES  Technorati ADD TO SQUIDOO  ADD TO WINDOWS LIVE  ADD TO YAHOO MYWEB  ADD TO ASK 
ADD TO REDDIT  ADD TO STUMBLEUPON  ADD TO GOOGLE   

07-16-2007 07:48 PM
Visit this user's website Find all posts by this user Quote this message in a reply
Post Reply  Post Thread 

Possibly Related Threads...
Thread: Author Replies: Views: Last Post
  Ban an IP Address From The Server admin 0 320 09-28-2007 11:58 PM
Last Post: admin
  Quick Log server configuration admin 0 335 07-25-2007 03:41 AM
Last Post: admin
  How authentication work between web server and Browser admin 0 242 07-25-2007 03:13 AM
Last Post: admin
  Shell script to Backup all MySQL databases to files of their own admin 0 251 07-21-2007 10:02 PM
Last Post: admin
  Configure Quota on RedHat server Nilesh 0 333 07-15-2007 10:55 AM
Last Post: Nilesh
  Server boot process Nilesh 0 286 07-15-2007 09:53 AM
Last Post: Nilesh

View a Printable Version
Send this Thread to a Friend
Subscribe to this Thread | Add Thread to Favorites

Forum Jump: