This is a script to filter the users out of etc/passwd and etc/group. So if you want to migrate of restore a server you can use this script from a backup to restore and make the same users on you had..
Please feedback and comments.
#!/bin/bash
prompt_list () {
# haal uit de argumenten de prompt en de keuzes
ARRAY=("${@}")
LEN=${#ARRAY[@]}
PROMPT=${ARRAY[0]}
CHOICES=${ARRAY[@]:1:$LEN}
#echo len: $LEN
#echo prompt: $PROMPT
#echo keuzes: $CHOICES
echo -e "\e[01;31m${PROMPT}\e[00m"
PS3="Maak je keuze ('s' voor stop): "
CHOICE=false
select ANSWER in ${CHOICES}; do
case ${ANSWER} in
* ) CHOICE=${ANSWER}; break;;
esac
done
check_stop ${CHOICE}
}
check_stop () {
if [ -z ${1} ]
then
echo -e "\n\e[01;31mJe hebt er voor gekozen om te stoppen\e[00m\n"
exit
fi
}
contains() {
string="$1"
substring="$2"
if test "${string#*$substring}" != "$string"
then
CONTAINS=ja
else
CONTAINS=nee
fi
}
echo "--------------------------------------"
if [ $# = 0 ]
then
echo "Usage: $0 <username>"
exit
fi
USERS=$@
echo "Users zijn $USERS"
updatedb
prompt_list "Which tar.gz you want to use for userid?" `locate *etc*passwd.tar.gz`
PASSWD_OBJECT=${CHOICE}
echo "--------------------------------------"
prompt_list "Which tar.gz you want to use for groupid?" `locate *etc*group.tar.gz`
GROUP_OBJECT=${CHOICE}
tar -zxvf ${GROUP_OBJECT}
tar -zxvf ${PASSWD_OBJECT}
for USER in $USERS
do
read -e -p "Which user you want to know the userid and groupid? " -i "$USER" USER
USERID=`cat passwd | grep "^${USER}:" | cut -d":" -f3`
if [ "${USERID}x" = "x" ]
then
echo "User $USER not in passwd"
else
echo -e "userid is: \t$USERID"
GROUPID=`cat passwd | grep "^${USER}:" | cut -d":" -f4`
echo -e "groupid is: \t$GROUPID"
HOMEID=`cat passwd | grep "^${USER}:" | cut -d":" -f6`
echo -e "homedir is: \t$HOMEID"
SHELLID=`cat passwd | grep "^${USER}:" | cut -d":" -f7`
echo -e "shellid is: \t$SHELLID"
GROUPID2=`cat group | grep "^${USER}:" | cut -d":" -f3`
echo -e "groupid in group is: \t$GROUPID2"
if [ ${GROUPID} -eq ${GROUPID2} ]
then
SUDOERS=`cat group | grep "^sudo:" | cut -d":" -f4`
echo -e "sudoers: \t$SUDOERS"
contains $SUDOERS $USER
echo $CONTAINS
if [ "$CONTAINS" == "ja" ]
then
SUDOPARAM="-G sudo"
else
SUDOPARAM=""
fi
echo "This is ${SUDOPARAM}"
groupadd -g ${GROUPID2} ${USER}
RESULT=$?
echo -e "\e[01;31mResult Add to groep is ${RESULT}\e[00m"
useradd -u ${USERID} -g ${GROUPID} ${SUDOPARAM} -d ${HOMEID} -m -s ${SHELLID} ${USER}
RESULT=$?
echo -e "\e[01;31mAdd result user is ${RESULT}\e[00m"
else
echo "groupid of passwd is not equal from groupid of group"
fi
fi
done