8

The goal is to sync local and remote folders over ssh.

My current user is user1, and I have a password-less access setup over ssh to a server server1. I want to sync local folder with a folder on server1 by means of rsync utility. Normally I would run:

rsync -rtvz /path/to/local/folder server1:/path/to/remote/folder

ssh access works as expected, rsync is able to connect over ssh, but it returns "Permission denied" error because on server1 the folder /path/to/remote/folder is owned by user2:user2. File permissions of the folder do not allow it to be altered by anyone else. user1 is a sudoer on server1 so sudo su - user2 works during ssh session. How to forse rsync to switch the user when it ssh'ed to the server?

Adding user1 to the group user2 is not an option because all user/group management on the server is done automatically and replicated from a central repo every X mins, that I have not access to.

Same for changing permissions/ownership of the destination folder: it is updated automatically on a regular basis with a reset of all permissions.

Possible solution coming to my mind is a script that syncs the local folder with a temporary intermediate remote folder owned by user1 on the server, and then syncs two remotes folders as user2.

Googling for a shorter and prettier solution did not yield any success.

schatten
  • 1,497
  • 1
  • 12
  • 19

3 Answers3

6

I have not tried it by myself, but how about using rsync's '--rsync-path' option?

rsync -rtvz --rsync-path='sudo -u user2 rsync' /path/to/local/folder server1:/path/to/remote/folder
ymnk
  • 1,145
  • 7
  • 7
1

To fix the permissions problem you need to run rsync over an an SSH session that logs in remotely as user2:

rsync avz -e 'ssh -i privatekeyfile' /path/to/local/folder/ user2@server1:/path/to/local/folder

The following answer explains how to setup the SSH keys.

Community
  • 1
  • 1
Mark O'Connor
  • 76,015
  • 10
  • 139
  • 185
  • There is no permissions problem. _user1_ has SSH keys set up and _user1_ is a sudoer on the remote machine. So potentially _user1_ has all needed privileges to perform rsync task. – schatten Jan 18 '13 at 02:53
  • @schatten It is a permissions issue. By default rsync will not use sudo as your accepted answer has demonstrated. Actually it's quite clever how rsync can be adapted in this way! – Mark O'Connor Jan 18 '13 at 08:10
0

Set up password-less access for user1 to access user2@server1, then do:

rsync -rtvz /path/to/local/folder user2@server1:/path/to/remote/folder
John Kugelman
  • 349,597
  • 67
  • 533
  • 578
  • 1
    Do you mean adding _user1_'s public key to the list of authorized ssh keys of _user2_? If yes, those get overwritten as well. – schatten Jan 16 '13 at 06:32