diumenge, 10 de novembre del 2013

migracion usuarios servidor a otro

ja havia consultat un parell de pàgines on fan un script per fer la migració pero en els dos casos m'estava donant algún tipus d'error, la solució em semblava molt elegant, no descarto el afinar-la pero determinar aquest errors

 https://support.rbtechvt.com/index.php?/Knowledgebase/Article/View/38

https://tamainut.com/blog/administracion-sistemas-unix/item/mover-cuentas-linux-a-otro-servidor-con-bash


#!/bin/bash
# Copyright (c)2010-2012 rbTechnologies, LLC
# By Rubin Bennett rbennett@rbtechvt.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
# Intended to be run on the NEW server, and expecting that you have performed
# ssh key exchange for password-less login to the OLD server.
# 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:$home:$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
        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/group
                fi
        done
        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
fi
}
echo "Copying user accounts and passwords from /etc/passwd"
syncusers
exit 0


i així la resta de fitxers, afegeixo al fitxer /etc/passwd del nou ordinador tots els usuarios del vell ara queda moure la carpeta /home que ho faré via rsync

rsync -e ssh --progress --delete -lprtz /home/alumnes root@192.168.3.135:/home