可以與目標擁有者和權限進行 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 序列,您可以輕鬆編寫腳本。伺服器,而且效果還不錯。我還需要更換群組權限,類似的處理;

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不理解+,請將其替換為效率稍低的變體\;(即反沖分號)。

相關內容