4

Source server have one owner is root.

----/home/sub1/test1 
----/home/sub2/test1

My local machine is destination.

----/home/sub1/test1 owner by user1
----/home/sub2/test1 owner by user2

How I can sync new update file form Source server to local machine and don't change local owner?

Edit

I need to sync all source in one command because have many folder and local machine have many owner too. Maybe possible?

Thanks.

3 Answers3

5

It sounds like that you do not want them to change after the transfer.

Try below command:

rsync -avr -o -g /source/directory user@:destinationHost/destination/directory

If you don't use these options, the user and group will be changed to the invoking user on the receiving end. If you want to specify some other user, you will need to add a chown command to your script.

-o, --owner
    This option causes rsync to set the owner of the destination file to be 
    the same as  the source file, but only if the receiving rsync is being run 
    as the super-user (see also the --super and --fake-super options). Without 
    this option, the owner of new and/or transferred files are set to the invoking 
    user on the receiving side...

-g, --group
    This option causes rsync to set the group of the destination file to be the same as 
    the source file. If the receiving program is not running as the super-user (or if
    --no-super was specified), only groups that the invoking user on the receiving side
    is a member of will be preserved. Without this option, the group is set to the default
    group of the invoking user on the receiving side...

SEE MAN rsync
Rahul
  • 13,589
api1411
  • 124
  • Thanks you that not work for me. For your command source server mush have user and group same local server. But I have 100 user it hard to do. Thanks. – Chonlathit Wanpean Jun 25 '16 at 12:39
  • 4
    I know this is old, but for others that stumble on this, the destination server must know of the users for -o and -g to work. Also, -a already includes -o and -g – Dario Zadro Mar 18 '20 at 01:38
1

I think a good option for you might be to just rsync from source to destination normally, assuming it's a separate physical file-system transfer, using something like

rsync -e 'ssh -p 22' -quaxz --bwlimit=1000 --del --no-W --delete-excluded --exclude-from=/excludedstufflist /sourcedir serverIP:/destinationdir

and then when it's all copied to the right place on the new system, find out what new UIDs your system gave the data from the source system. It's probably 1001, 1002 etc. In which case you can easily do a

find /destinationdir -user 1001 -exec chown username '{}' \;
find /destinationdir -user 1002 -exec chown otherusername '{}' \;
etc.

Yes, you'd have to do that last thing 100 times, but you could easily script that, if you know the UID sequence used during rsync.. I've done exactly this once, migrating about 60 users from one server to another, and it worked reasonably well. I also needed to replace group permissions, similar deal;

chown --from=oldguy newguy * -R
chown --from=:friends :family * -R

Stuff like that. Hope you can use this.

Julius
  • 466
0

You can't do this in one command. However, even with 100+ users it's pretty straightforward to automate the task, so I don't see why you're insistent it has to be one command.

You need to pull the data from the destination machine in this scenario. It would theoretically be possible to drive the transfer from the source server by tunnelling an rsync over a reverse ssh tunnel that that would be considerably more complicated.

for testdir in /home/sub*/test1
do
    owner=$(stat -c %u "$testdir")
    rsync -avP --chown "$u" sourceserver:"$testdir"/ "$testdir"/
done

If you don't have the --chown flag you can emulate this with a two step process:

for testdir in /home/sub*/test1
do
    owner=$(stat -c %u "$testdir")
    rsync -avP --no-owner sourceserver:"$testdir"/ "$testdir"/
    chown -R "$u" "$testdir"                   # If possible
    # find "$testdir" -exec chown "$u" {} +    # Otherwise
done

If you need to use the find variant and your find does not understand +, replace it with the slightly less efficient \; (that's backlash semicolon).

Chris Davies
  • 116,213
  • 16
  • 160
  • 287
  • Thanks for the loop. Im doing this on Mac but no --chown flag. Does Unix/Linux rsync have a --chown option? I did not see that on the http://linuxcommand.org/man_pages/rsync1.html . Yet, the loop could still add the chown task right after the rsync. – ndasusers Dec 13 '16 at 13:46
  • @ndasusers alternative solution provided for you – Chris Davies Nov 05 '17 at 16:46