Image the following scenario:
chown root.root * -R
OOPS
You have just destroyed the server.
This, and other similar mishaps (i.e. chmod 777 / -R) occur more often than one would imagine.
If you are in luck like me, you have access to more than one linux server, and in most cases these servers are similar enough in order to enable a quick restoration of the many file permissions you just destroyed.
This of course comes in handy when you don’t want to rsync the files, as the content is different on both servers, but the owner, group and permissions must be the same.
Anyhow here is the code – I commented out the chmod section, as it functions exclusively to the owner/group part. in order to use it, comment out the first two parts first and then uncomment (also included are instructions in the code).
# syncperms.sh - synchronize user/group from remote server to local dir
# the script should only change values for files that exist on both servers
# should NOT COPY CONTENT (otherwise rsync would be great)
# also added the access rights properties for syncing chmod
# uses ssh, so key based authentication is best for scripting purposes
# source_server can be IP or hostname
source_server="good.sourcehost.com"
# path must be same on both servers obviously
sync_path="/home/tom/test/"
# get full list of files to operate on with 'find'
dest_list=$(find $sync_path)
for i in $dest_list; do
### begin owner/group section - comment out if want to do permission sync ###
echo "$i is current file"
locstat=$(stat -c "%U|%G" $i)
remotestat=$(ssh ${source_server} "stat -c '%U|%G' $i")
locuser=$(echo -ne $locstat | awk -F "|" '{print $1}')
remoteuser=$(echo -ne $remotestat | awk -F "|" '{print $1}')
echo "local user of $i is $locuser"
if [[ $remoteuser != "" && $locuser != $remoteuser ]]; then
echo "remote user of $i is $remoteuser"
echo "$locuser is not the same as $remoteuser"
chown $remoteuser $i
elif [[ $remoteuser == $locuser ]]; then
echo "remote user: $remoteuser is the same as local user: $locuser"
fi
locgroup=$(echo -ne $locstat | awk -F "|" '{print $2}')
remotegroup=$(echo -ne $remotestat | awk -F "|" '{print $2}')
echo "local group of $i is $locgroup"
if [[ $remotegroup != "" && $locgroup != $remotegroup ]]; then
echo "remote group of $i is $remotegroup"
echo "$locgroup is not the same as $remotegroup"
chgrp $remotegroup $i
elif [[ $remotegroup == $locgroup ]]; then
echo "remote user: $remotegroup is the same as local user: $locgroup"
fi
### end owner/group section ###
### begin chmod section ###
# locmod=$(stat -c "%a" $i)
# remotemod=$(ssh ${source_server} "stat -c '%a' $i")
# echo "local mod of $i is $locmod"
# if [[ $remotemod != "" && $locmod != $remotemod ]]; then
# echo "remote mod of $i is $remotemod"
# echo "local mod: $locuser is not the same as remote mod: $remotemod"
# chown $remotemod $i
# elif [[ $remotemod == $locmod ]]; then
# echo "remote mod: $remotemod is the same as local mod: $locmod"
# fi
### end chmod section ###
done
That should do the trick.
Have fun!
Tom