2013-07-16

Script to Migrate Users with home directory in Linux

Generally Scripting help us to save our lots of time, which we spend in our daily day to day activity. It is very easy to create a script if you have a good understanding on command or if you know how to use your skill to make task automatic with shell scripting. But Good scripting not only include commands, it is the combination of logics, commands and your dedication to get the desire result from your script

Lets comes to the main section, Below is the example of simple script which migrate Users with their group and home Directory and  move it on a temp directory , i.e move Then you can easily copy move directory and put it on your new Machine (New Linux Machine). or you can keep as a backup.

Please copy past below script in a file and named it as mig.sh,and give the executable permission to your script, I am considering here you know how to run scripts

Please use root user to run this script, becasue normal user will not able to read the password file.

********************Script begin you can copy past after this line ********************

 #!/bin/bash

#### This script will use to migrate the users login and Data  ######

id=500
pasd=/etc/passwd
grp=/etc/group
shad=/etc/shadow
grpshad=/etc/gshadow

### Make a Dir to put the migration files #####

mkdir move

#### Getting Details of the users comes between id and 65534 and move it in a file#######
awk -v L=$id -F: '($3>=L) && ($3!=65534)' $pasd >move/passwd.mig

#### Getting details of Groups between  id and 65534 and move it in a file #########

awk -v L=$id -F: '($3>=L) && ($3!=65534)' $grp >move/group.mig

##### Now Copy Shadow File ##########
awk -v L=$id -F: '($3>=L) && ($3!=65534) {print $1}' $pasd | tee - |egrep -f - $shad >move/shadow.mig

########## Copy group shadow  file ################
 cp $grpshad move/gshadow.mig

 ###### Create a Backup of User Home Dir by compressing it with tar ############

awk -F: '{print $1}' move/passwd.mig >move/temp.file
awk -F: '{print $6}' move/passwd.mig >move/temp2.file

for user in `cat move/temp.file`
do 
ho=`grep $user move/temp2.file`
tar -zcvpf move/$user.tar.gz --directory=`dirname $ho` $user 
tar -zcvpf move/$user-mail.tar.gz --directory=/var/spool/mail $user
done

rm-rf /move/temp.file temp2.file

******************************Script End please don't copy this line **********************

Now login as root by executing command below 
#su -l

and set permission to the mig.sh 
#chmod a+x mig.sh

to run the above script you can use command 
#sh mig.sh
or 
#./mig.sh

Once you get the output of your script in move folder copy it on new Linux machine and start moving the home dir of users on desired location.

Please Note default location of user home dir is /home, if you required any other location then you can manually change the home with your newhome or you can use sed  to change all at once in passwd.mig file 

In New Linux machine 
Login as a root in new machine, and go to move directory, now we are going to add the account's one by one.
#cd move

untar all the migrated home dir present in the form of user.tar.gz  in /home by executing command one by one for all the users 

Please note :- you can untar the dir after adding user entery in /etc/password which is mention in next line.
#tar -zxvpf  user.tar.gz -C /home/

Now copy passwd , shadow and group, gshadow files in case if anything goes wrong you can move to the original again  
#cp /etc/passwd /etc/passwd.orig
#cp /etc/shadow /etc/shadow.orig
#cp /etc/group /etc/group.orig
#mv /etc/gshadow /etc/gshadow.orig

and append the data

# cat passwd.mig >>/etc/passwd
#cat shadow.mig >>/etc/shadow
#cat group.mig >>/etc/group
#mv gshadow.mig  /etc/gshadow

Now restart or logout your machine to see the changes.

Thank you for Reading my full post 








No comments:

Post a Comment