Skip to content

Instantly share code, notes, and snippets.

@chriscantu
Created August 19, 2013 21:56
Show Gist options
  • Save chriscantu/6274660 to your computer and use it in GitHub Desktop.
Save chriscantu/6274660 to your computer and use it in GitHub Desktop.
Rsync Script to Migrate Linux Servers. All the heavy lifting was done here. (http://www.mariusv.com/migrate-linux-servers-across-cloud-providers/) I just added more system directories to be ignored for Ubuntu and other flavors.
#!/bin/bash
# @Description MigrateME
# @Usage Copy to the source server and run with: sh migrateMe.sh
# @Version 1.0.2
# @Date Last Modified 22/01/2011
# @Author Marius Voila <http://www.mariusv.com>
# @Copyright (c) 2011 mariusv under GNU GPL v2.0+
# @Returns success or failure
function is_root_user() {
[ $(id -u) -eq 0 ] && return 0 || return 1
}
# @Returns text and if not root user exits with code 1 (error)
function require_root_user() {
if is_root_user
then
echo "Are We Runnning As Root? [ Yes ]"
else
echo "Are We Running As Root? [ No ]"
echo "Fatal Error: You must be logged in as root to execute this shell script."
exit 1
fi
}
require_root_user
read -p "Provide the destination server IP address or hostname: " destination_ip_address
echo -e "\n"
echo "##########################################################################"
echo "### WHEN PROMPTED, PROVIDE THE DESTINATION SERVERS ROOT PASSWORD. ###"
echo "##########################################################################"
echo -e "\n"
#Install rsync on the destination
ssh root@$destination_ip_address apt-get -y install rsync
#Make sure we were able to ssh successfully
rc=$?
[ "$rc" = "255" ] && exit 1
#Install rsync on the source
apt-get -y install rsync
#Yum clean all on the source, to free up some disk space
apt-get clean all
#Create the rsync_excludes file on the source
echo -e "/boot\n/proc\n/sys\n/tmp\n/dev\n/etc/fstab\n/etc/mdadm.conf\n/etc/mtab\n/etc/resolv.conf\n/etc/conf.d/net\n/etc/network/interfaces\n/etc/networks\n/etc/sysconfig/network*\n/etc/sysconfig/hwconf\n/etc/sysconfig/ip6tables-config\n/etc/sysconfig/kernel\n/etc/hostname\n/etc/HOSTNAME\n/etc/hosts\n/etc/modprobe*\n/etc/modules\n/etc/udev\n/net\n/lib/modules\n/etc/rc.conf\n/etc/sysconfig/network-scripts/ifcfg-eth*" > /rsync_excludes.txt
#Stop critical services on source
for i in lighttpd httpd nginx mysqld postgresql proftpd postfix
do
#Set rc equal to status code
service $i status 2>/dev/null
rc=$?
#If status equals 0, we know its running, and can stop it
[ "$rc" == "0" ] && service $i stop
done
echo -e "\n"
echo "##########################################################################"
echo "### WHEN PROMPTED, PROVIDE THE DESTINATION SERVERS ROOT PASSWORD. ###"
echo "##########################################################################"
echo -e "\n"
#Do the rsync
rsync -avz --delete --exclude-from=/rsync_excludes.txt / root@${destination_ip_address}:/
#Make sure we were able to login successfully
rc=$?
[ "$rc" = "255" ] && exit 1
echo -e "\n"
echo "##########################################################################"
echo "### WHEN PROMPTED, PROVIDE THE DESTINATION SERVERS ROOT PASSWORD. ###"
echo "##########################################################################"
echo -e "\n"
#First we have to clear the source known_hosts file
echo > ~/.ssh/known_hosts
#Delete the rsync_excludes.txt file on the destination
ssh root@$destination_ip_address rm -f /rsync_excludes.txt
#Complete
echo -e "\n"
echo "########################################################################"
echo "# ! VERY IMPORTANT ! #"
echo "# You MUST go into the destination server and modify the network file. #"
echo "# The destination GATEWAY ADDRESS must be updated before you reboot. #"
echo "# #"
echo "# Modify /etc/sysconfig/network #"
echo "# #"
echo "# Update the gateway address to the proper destination server address. #"
echo "# #"
echo "# AGAIN, THE DESTINATION GATEWAY ADDRESS MUST BE UPDATED BEFORE YOU #"
echo "# REBOOT. FAILURE TO DO THIS, MAY RESULT IN THE DESTINATION SERVER #"
echo "# BEING INACCESSIBLE. #"
echo "########################################################################"
echo -e "\n"
echo "########################################################################"
echo "### PLEASE --REBOOT-- THE DESTINATION SERVER TO FINALIZE MIGRATION. ###"
echo "########################################################################"
echo -e "\n"
exit 0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment