可以与目标所有者和权限进行 Rsync 吗?

可以与目标所有者和权限进行 Rsync 吗?

源服务器有一个所有者,即 root。

----/home/sub1/test1 
----/home/sub2/test1

我的本地机器是目的地。

----/home/sub1/test1 owner by user1
----/home/sub2/test1 owner by user2

如何将新的更新文件从源服务器同步到本地计算机并且不更改本地所有者?

编辑

我需要在一个命令中同步所有源,因为有许多文件夹和本地计算机也有许多所有者。也许有可能吗?

谢谢。

答案1

听起来您不希望它们在转移后发生变化。

尝试以下命令:

rsync -avr -o -g /source/directory user@:destinationHost/destination/directory

如果不使用这些选项,用户和组将更改为接收端的调用用户。如果您想指定其他用户,则需要在脚本中添加 chown 命令。

-o, --owner
    This option causes rsync to set the owner of the destination file to be 
    the same as  the source file, but only if the receiving rsync is being run 
    as the super-user (see also the --super and --fake-super options). Without 
    this option, the owner of new and/or transferred files are set to the invoking 
    user on the receiving side...

-g, --group
    This option causes rsync to set the group of the destination file to be the same as 
    the source file. If the receiving program is not running as the super-user (or if
    --no-super was specified), only groups that the invoking user on the receiving side
    is a member of will be preserved. Without this option, the group is set to the default
    group of the invoking user on the receiving side...

SEE MAN rsync

答案2

我认为对您来说一个不错的选择可能是正常地从源到目标进行 rsync,假设它是一个单独的物理文件系统传输,使用类似

rsync -e 'ssh -p 22' -quaxz --bwlimit=1000 --del --no-W --delete-excluded --exclude-from=/excludedstufflist /sourcedir serverIP:/destinationdir

然后,当它们全部复制到新系统上的正确位置时,找出您的系统为源系统中的数据提供了哪些新 UID。它可能是 1001、1002 等。在这种情况下,您可以轻松地执行

find /destinationdir -user 1001 -exec chown username '{}' \;
find /destinationdir -user 1002 -exec chown otherusername '{}' \;
etc.

是的,您必须执行最后一件事 100 次,但是如果您知道 rsync 期间使用的 UID 序列,您可以轻松编写脚本。我已经完成了一次,将大约 60 个用户从一台服务器迁移到另一台服务器,而且效果还不错。我还需要更换组权限,类似的处理;

chown --from=oldguy newguy * -R
chown --from=:friends :family * -R

像这样的东西。希望你能用这个。

答案3

您无法通过一个命令来完成此操作。然而,即使有 100 多个用户,自动化该任务也非常简单,所以我不明白为什么你坚持它必须是一个命令。

你需要在这种情况下来自目标计算机的数据。理论上,可以通过在rsync反向ssh隧道上建立隧道来驱动来自源服务器的传输,但这要复杂得多。

for testdir in /home/sub*/test1
do
    owner=$(stat -c %u "$testdir")
    rsync -avP --chown "$u" sourceserver:"$testdir"/ "$testdir"/
done

如果您没有该--chown标志,您可以通过两步过程来模拟:

for testdir in /home/sub*/test1
do
    owner=$(stat -c %u "$testdir")
    rsync -avP --no-owner sourceserver:"$testdir"/ "$testdir"/
    chown -R "$u" "$testdir"                   # If possible
    # find "$testdir" -exec chown "$u" {} +    # Otherwise
done

如果您需要使用该find变体并且您find不理解+,请将其替换为效率稍低的变体\;(即反冲分号)。

相关内容