Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

I am using rsync to replicate a web folder structure from a local server to a remote server. Both servers are ubuntu linux. I use the following command, and it works well:

rsync -az /var/www/ [email protected]:/var/www/

The usernames for the local system and the remote system are different. From what I have read it may not be possible to preserve all file and folder owners and groups. That is OK, but I would like to preserve owners and groups just for the www-data user, which does exist on both servers.

Is this possible? If so, how would I go about doing that?

** EDIT **

There is some mention of rsync being able to preserve ownership and groups on remote file syncs here: http://lists.samba.org/archive/rsync/2005-August/013203.html

** EDIT 2 **

I ended up getting the desired affect thanks to many of the helpful comments and answers here. Assuming the IP of the source machine is 10.1.1.2 and the IP of the destination machine is 10.1.1.1. I can use this line from the destination machine:

sudo rsync -az [email protected]:/var/www/ /var/www/

This preserves the ownership and groups of the files that have a common user name, like www-data. Note that using rsync without sudo does not preserve these permissions.

like image 419
jeffery_the_wind Avatar asked Mar 06 '12 15:03

jeffery_the_wind


People also ask

Does rsync preserve ownership?

'rsync -a' option preserves the permissions, ownership, timestamp of files and folders that are to be transferred using rsync. This will synchronize the two folders or files and will also maintain the same timestamp as that of the source.

Is rsync over SSH secure?

When rsync is used on the command line, a separate protocol, usually SSH, must be specified for the transfer. However, the rsync daemon does not encrypt traffic. This means that an rsync process can potentially be sniffed in transit by a third party, granting them access to whatever information is being transferred.

What is rsync verbose?

-v, --verbose This option increases the amount of information you are given during the transfer. By default, rsync works silently. A single -v gives you information about what files are being transferred and a summary at the end.

Do both computers need rsync?

If you are synchronizing directories between two separate computers, they both must have rsync installed.

How to preserve permissions and ownership of files using rsync?

In order to preserve permissions, we will use -p flag (–perms). We can also use -a flag (–archive), which is an aggregation of -p and several other useful ones. Archive mode consists of the following flags: ‘rsync -a’ option preserves the permissions, ownership, timestamp of files and folders that are to be transferred using rsync.

How to use rsync over SSH in Linux?

Most recent Linux distributions have rsync by default. To verify your system has rsync installed, run the installation command. If rsync is already installed, the output shows the current version of the tool. Before you can start transferring files and directories with rsync over SSH, make sure you can use SSH to connect to a remote server.

How to synchronize two folders or files using rsync?

rsync source destination To preserve the above-mentioned permissions, ownership, and timestamp we can use the following command: rsync -avz source destination This will synchronize the two folders or files and will also maintain the same timestamp as that of the source.

What is rsync-a option in Linux?

‘rsync -a’ option preserves the permissions, ownership, timestamp of files and folders that are to be transferred using rsync. To transfer a file or folder from source to destination using rsync, we use the format:


1 Answers

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.

like image 138
ghoti Avatar answered Sep 20 '22 19:09

ghoti