Moving Between Two FreeBSD Servers

Due to various reasons, I’ve had to move the VPS that hosts my website and other things two different times now, and I’ve compiled a basic list of things you need to do to create a complete copy of the old server to the new one.

When you perform these steps, the it would be best to disable as many services as you can on the old server (so that some services doesn’t write a new file while you’re copying things over).

This is, of course a basic list of things to do, but it has done me good thusfar. There will be a few times where you may have to “go it on your own” (looking at KERNCONFs, here), but you should be able to figure that out yourself using Online Documentation.

Here we go! All these steps are stated as though you’re performing this on server 2, which is the one you’re moving to, unless noted otherwise.

#Install rsync
portsnap fetch
portsnap extract
cd /usr/ports/net/rsync
make install clean
 
#Copy users and groups over
scp -r root@server.one:/usr/home/* /usr/home/
scp root@server.one:/etc/passwd /etc/
scp root@server.one:/etc/group /etc/
scp root@server.one:/etc/master.passwd /etc/
 
#Copy /etc , /usr, /root, /var to new server
rsync -vrplogDtH root@server.one:/var/db/ /var/db
rsync -vrplogDtH --exclude 'bin' --exclude 'lib' --exclude 'lib32' --exclude 'libexec' root@server.one:/usr/ /usr
rsync -vrplogDtH root@server.one:/root/ /root
pwd_mkdb -p /etc/master.passwd 
 
#Update Kernel  ->  http://www.freebsd.org/doc/handbook/makeworld.html
csup /root/supfile
###check /usr/src/UPDATING
###check /etc/make.conf
#Update files in /etc
cd /usr/src/usr.sbin/mergemaster
./mergemaster.sh -p
#Drop to single-user mode
shutdown now
#From here, you should be able to hit enter to get a /bin/sh in recovery mode
fsck -p
mount -u /
mount -a -t ufs
swapon -a
adjkerntz -i  #If hw clock is set to local time.  `date` will show incorrect time and zone
 
#remove files from /usr/obj
cd /usr/obj
chflags -R noschg *
rm -rf *
 
#Save compile output
script /var/tmp/build.log
 
#Compile base system
cd /usr/src
make -j6 buildworld
 
#compile kernel  -- You should create your own KERNCONF here, or use GENERIC
make buildkernel KERNCONF=GALLY
make installkernel KERNCONF=GALLY
 
#Reboot the machine into single-user mode and ensure kernel works
shutdown -r now
### Option 4 at the FreeBSD boot loader
### Remount drives
fsck -p
mount -u /
mount -a -t ufs
swapon -a
 
#Install new system binaries if kernel is working correctly.
cd /usr/src
make installworld
 
#Run mergemaster again 
/usr/sbin/mergemaster
 
#New kernel and world are now installed.  Reboot into your new system
shutdown -h now
 
#Re-install ports
portmaster --list-origins > /root/installed-port-list  ## Perform this step on server 1
scp root@server.one:/root/installed-port-list /root/
portsnap fetch update
portmaster -y --clean-distfiles
portmaster --check-port-dbdir
portmaster -Faf
pkg_delete '*'
rm -rf /usr/local/lib/compat/pkg
#check /usr/local to make sure that only config files, etc are still installed
rm -rf /var/db/pkg
#install portmaster again
cd /usr/ports/ports-mgmt/portmaster && make install clean
portmaster -D --update-if-newer `cat /root/installed-port-list`
 
###  Do minor manual configurating.