Rsync Over Ssh Preserve Ownership Only for Www-Data Owned Files

rsync over SSH preserve ownership only for www-data owned files

You can also sudo the rsync on the target host by using the --rsync-path option:

# rsync -av --rsync-path="sudo rsync" /path/to/files user@targethost:/path

This lets you authenticate as user on targethost, but still get privileged write permission through sudo. You'll have to modify your sudoers file on the target host to avoid sudo's request for your password. man sudoers or run sudo visudo for instructions and samples.

You mention that you'd like to retain the ownership of files owned by www-data, but not other files. If this is really true, then you may be out of luck unless you implement chown or a second run of rsync to update permissions. There is no way to tell rsync to preserve ownership for just one user.

That said, you should read about rsync's --files-from option.

rsync -av /path/to/files user@targethost:/path
find /path/to/files -user www-data -print | \
rsync -av --files-from=- --rsync-path="sudo rsync" /path/to/files user@targethost:/path

I haven't tested this, so I'm not sure exactly how piping find's output into --files-from=- will work. You'll undoubtedly need to experiment.

How to preserve ownership of file while transferring it to remote server to local server?

If you want to preserve the ownership of the file as USER1, you probably have to login as USER1:

scp -pqr  /apps/test/scripts/cronbak.sh USER1@remoteserver:/apps/test/scripts

For sure USER1 must be created on remoteserver

How does rsync preserve ownership when uid/gid differs?

Yes, by default rsync matches owners and groups by name. Details are in the docs for --numeric-ids.

--numeric-ids

With this option rsync will transfer numeric group and user IDs rather than using user and group names and mapping them at both ends.

By default rsync will use the username and groupname to determine what ownership to give files. The special uid 0 and the special group 0 are never mapped via user/group names even if the --numeric-ids option is not specified.

If a user or group has no name on the source system or it has no match on the destination system, then the numeric ID from the source system is used instead.

rsync presumably uses getpwnam and getgrnam to look up the uid and gid associated with the user and group names.



Related Topics



Leave a reply



Submit