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

后端 未结 6 1491
轮回少年
轮回少年 2021-02-05 16:36

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:

6条回答
  •  既然无缘
    2021-02-05 17:12

    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.

提交回复
热议问题