Unix – How to change the owner for a rsync

rsyncunix

I understand preserving the permissions for rsync.
However in my case my local computer does not have the user the files need to under for the webserver. So when I rsync I need the owner and group to be apache on the webserver, but be my username on my local computer. Any suggestions?

I wanted to clarify to explain exactly what I need done.

My personal computer: named 'home' with the user account 'michael'
My web server: named 'server' with the user account 'remote' and user account 'apache'

Current situation: My website is on 'home' with the owner 'michael' and on 'server' with the owner 'apache'. 'home' needs to be using the user 'michael' and 'server' needs to be using the user 'apache'

Task: rsync my website on 'home' to 'server' but have all the files owner by 'apache' and the group 'apache'

Problem: rsync will preseve the permissions, owner, and group; however, I need all the files to be owner by apache. I know the not preserving the owner will put the owner of the user on 'server' but since that user is 'remote' then it uses that instead of 'apache'. I can not rsync with the user 'apache' (which would be nice), but a security risk I'm not willing to open up.

My only idea on how to solve: after each rsync manually chown -R and chgrp -R, but it's a huge system and this takes a long time, especially since this is going to production.

Does anyone know how to do this?

Current command I use to rsync:

rsync --progress -rltpDzC --force --delete -e "ssh -p22" ./ remote@server.com:/website

Best Answer

If you have access to rsync v.3.1.0 or later, use the --chown option:

rsync -og --chown=apache:apache [src] [dst]

More info in an answer from a similar question here: ServerFault: Rsync command issues, owner and group permissions doesn´t change