Bash: how to copy permissions recursively

Imagine you were copying an, eg, virtual server from /server to /newserver, and messed permissions in /etc; thre's a way to copy permissions with this simple script:

 
#!/bin/bash
 
source=$1
target=$2
 
echo "Copy files from $source to $target"
 
cd $source
files=`find $source`
 
for file in $files
do
  #this is some bash voodoo - string part replacement
  targetFile=$target${file#$source}
  #stat command can accept -c parameter and return data in needed format
  #where %u and %g are numeric user/group ids, and %a - octal permissions
  permissions=`stat -c%a $file`
  ownership=`stat -c%u:%g $file`
  echo $targetFile to $ownership $permissions
  chmod $permissions $targetFile
  chown $ownership $targetFile
done
 

invoke this like

 
./permissions.sh /server/etc /newserver/etc
 

change %u:%g to %U:%G if you prefer string id's (www-data:www-data) more than numeric (0:0)