Script to copy permissions from one server to antoher even if gid and uid are different

From Urandom
Jump to: navigation, search
* You might need this if the usernames and uid-s don't match on servers but you have to copy permissions
* This is the export script.
find /home > all_files.txt
while read line
do
ls -ld $line
done < "all_files.txt" > all_perm_files.txt
rm -rf all_files.txt
* Now copy the all_perm_files.txt to the other server where you already have the same files in the same location
while read line
do
 owner=`echo $line | cut -d" " -f3`
 group=`echo $line | cut -d" " -f4`
 file=`echo $line | cut -d" " -f9-`
 chown $owner:$group $file
 chmod u=`echo ${line:1:3} | sed 's/-//g'` $file
 chmod g=`echo ${line:4:3} | sed 's/-//g'` $file
 chmod o=`echo ${line:7:3} | sed 's/-//g'` $file
done < "all_perm_files.txt"

Copy groups and users that are not present on the remote machine

* export the users and groups to files
cat /etc/group > all_groups.txt
cat /etc/passwd > all_users.txt
* now add groups and users that are not present already in the new server
#groups
existing=`cat /etc/group | cut -d":" -f1`
while read line
do
 name=`echo $line | cut -d":" -f1`
 present=1
 for ex in $existing
 do
  if [ $name == $ex ]
  then
   echo "already present $ex"
   present=2
   break
  fi
 done
 if [ $present -eq 1 ]
 then
  echo "adding group $name"
  groupadd $name
 fi
done < "all_groups.txt"

#users
existing=`cat /etc/passwd | cut -d":" -f1`
while read line
do
 name=`echo $line | cut -d":" -f1`
 present=1
 for ex in $existing
 do
  if [ $name == $ex ]
  then
   echo "already present $ex"
   present=2
   break
  fi
 done
 if [ $present -eq 1 ]
 then
  group=`echo $line | cut -d":" -f4`
  newgroup=`cat all_groups.txt | grep $group | cut -d":" -f1`
  home=`echo $line | cut -d":" -f6`
  shell=`echo $line | cut -d":" -f7`
  useradd -d $home -s $shell -g $newgroup $name
  echo "adding user $name"
 fi
done < "all_users.txt"
Personal tools