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
Why Doing I/O in Linux Is Uninterruptible
How to Use Netcat for Windows to Send a Binary File to a Tcp Connection
Size() VS Ls -La VS Du -H Which One Is Correct Size
Posix Shared Memory and Semaphores Permissions Set Incorrectly by Open Calls
Bash: Run an Executable File in Background
How to Confirm Sftp File Delivery
Replace Every N'Th Occurrence in Huge Line in a Loop
How to Get Multi-Line String Between Two Braces Containing a Specific Search String
Replace All Lines That Do Not Contain Matched String
How to Rename Multiple Files Beginning with a Unix Timestamp - Imapsync Issue
Oracle:Io Exception: the Network Adapter Could Not Establish the Connection
Using Gnu/Linux System Call 'Splice' for Zero-Copy Socket to Socket Data Transfers in Haskell
Resizing The Image with Padding Using Convert on Ubuntu
Install Rabbitmqadmin on Linux
How to Chmod 0777 a File and Commit as Is to Git on Windows
Jpegtran Optimize Without Changing Filename