A super elegant way to migrate user accounts from one Linux server to another
Thanks to nixcraft for his how-to here:
http://www.cyberciti.biz/faq/howto-move-migrate-user-accounts-old-to-new-server/.
I had written a very overcomplicated script to accomplish all this (never underestimate the power of AWK!!!), and this simplified my life immensely :)
Setup UID filter limit:
# export UGIDLIMIT=500
Now copy /etc/passwd accounts to /root/passwd.mig using awk to filter out system account (i.e. only copy user accounts)
# awk -v LIMIT=$UGIDLIMIT -F: '($3>=LIMIT) && ($3!=65534)' /etc/passwd > /root/passwd.mig
Copy /etc/group
# awk -v LIMIT=$UGIDLIMIT -F: '($3>=LIMIT) && ($3!=65534)' /etc/group > /root/group.mig
Copy /etc/shadow
# awk -v LIMIT=$UGIDLIMIT -F: '($3>=LIMIT) && ($3!=65534) {print $1}' /etc/passwd | tee - |egrep -f - /etc/shadow > /root/shadow.mig
If you want the whole enchilada for moving users, here it is. Note: This is to be run AS ROOT from the NEW server (e.g. the server being migrated TO), and expects ssh key exchange to be done between the systems.
Be aware that you can well and hose your systems running things as root, and we hereby expressly disclaim any and all responsibility for any damage or problems of any sort caused by your use of this script. This code is supplied as a convenience and carries NO WARRANTY whatsoever.
#!/bin/bash
# Copyright (c)2010 rbTechnologies, LLC
# By Rubin Bennett <rbennett@thatitguy.com>
# Released under the terms and conditions of the GNU Public License version 2.
# A simple script to assist in server migrations from Linux to Linux
# IP address or hostname of source server (e.g. server your're migrating
# AWAY from
sourceServer=172.16.1.55
function syncusers() {
echo -n "Do you have backups of your existing passwd files? [y|N] "
read
if [ "$REPLY" != "y" ]
then
echo "Please back your files up and run this script again."
exit 1
else
scp $sourceServer:/etc/passwd /tmp/passwd.$sourceServer
scp $sourceServer:/etc/group /tmp/group.$sourceServer
scp $sourceServer:/etc/shadow /tmp/shadow.$sourceServer
# First, make a list of non-system users that need to be moved.
export UGIDLIMIT=500
awk -v LIMIT=$UGIDLIMIT -F: '($3 >= LIMIT) && ($3 != 65534)' /tmp/passwd.$sourceServer > /tmp/passwd.mig
awk -v LIMIT=$UGIDLIMIT -F: '($3 >= LIMIT) && ($3 != 65534)' /tmp/group.$sourceServer >/tmp/group.mig
awk -v LIMIT=$UGIDLIMIT -F: '($3 >= LIMIT) && ($3 != 65534) { print $1 }' /tmp/passwd.$sourceServer | tee - |egrep -f - /tmp/shadow.$sourceServer > /tmp/shadow.mig
# Now copy non-duplicate entries in to the new server files...
while IFS=: read user pass uid gid full home shell
do
line="$user:$pass:$uid:$gid:$full:$shell"
exists=`grep $user /etc/passwd`
if [ ! -z "$exists" ]
then
echo "Copying entry for user $user to new system"
echo $line #>> /etc/passwd
fi
done </tmp/passwd.mig
while IFS=: read group pass gid userlist
do
line="$group:$pass:$gid:$userlist"
exists=`grep $group /etc/group`
if [ ! -z "$exists" ]
then
echo "Copying entry for group $group to new system"
echo $line #>> /etc/passwd
fi
done </tmp/group.mig
while IFS=: read user pass lastchanged minimum maximum warn
do
line="$user:$pass:$lastchanged:$minimum:$maximum:$warn"
exists=`grep $user /etc/passwd`
if [ ! -z "$exists" ]
then
echo "Copying entry for user $user to new system"
echo $line #>> /etc/shadow
fi
done </tmp/shadow.mig
fi
}
echo "Copying user accounts and passwords from /etc/passwd"
syncusers
exit 0
echo "Backing up config files"
for file in /etc/httpd/conf/httpd.conf
do
scp $sourceServer:$file $file
done
echo "Scalix Data"
rsync -av --delete 172.16.1.5:/var/opt/scalix/xx/s/data/ /var/opt/scalix/xx/s/data/
echo "Samba Profiles"
rsync -av --delete 172.16.1.5:/var/lib/samba/profiles/ /var/lib/samba/profiles/
echo "Home directories"
rsync -av --delete 172.16.1.5:/home/ /home/
echo "Named data"
rsync -av --delete --exclude proc 172.16.1.5:/var/named/ /var/named/
echo "Copying webserver data"
rsync -av 172.16.1.5:/var/www/ /var/www/
