帶有特殊字元檔案的 Rsync 在 Mac 和 Linux 之間不起作用

帶有特殊字元檔案的 Rsync 在 Mac 和 Linux 之間不起作用

我想使用 rsync 透過 Mac 上的磁碟備份我的 Ubuntu 伺服器。但我無法使其正常工作,因為每次在初始時間後重新運行 rsync 操作時,帶有特殊字元的檔案都會首先被刪除,然後重新同步。看來不同的字符集有問題。

首選解決方案似乎是使用該--iconv選項:

您可以使用 rsync 的 --iconv 選項在 UTF-8 NFC 和 NFD 之間進行轉換,至少如果您使用的是 Mac。有一個特殊的 utf-8-mac 字元集,代表 UTF-8 NFD。因此,要將檔案從 Mac 複製到 NAS,您需要執行以下命令:

rsync -a --iconv=utf-8-mac,utf-8 localdir/ mynas:remotedir/

這會將遠端伺服器上的所有本機檔案名稱從 UTF-8 NFD 轉換為 UTF-8 NFC。文件的內容不會受到影響。

感謝@Jan,我更新了我的 mac 上的 rsync 版本從 2.6.9 開始。至 3.1.1。儘管如此,我仍然沒有完全做到這一點,因為我現在收到了進一步的錯誤:

iconv_open("UTF-8", "utf-8-mac") failed
rsync error: requested action not supported (code 4) at rsync.c(118) [sender=3.0.9]
rsync: connection unexpectedly closed (0 bytes received so far) [Receiver]
rsync error: error in rsync protocol data stream (code 12) at io.c(226) [Receiver=3.1.1]

我不明白為什麼“不支援請求的操作”,因為我的 Ubuntu(12.04)上的 rsync 版本似乎是 3.xx 之後的版本,因此應該支援該--iconv選項。

編輯:讓我補充一點,當我(在 Mac 上,nota bene)啟動從 Mac 到 Linux 的 rsync 時,一切都運作良好:

rsync -av --delete --iconv=utf-8-mac,utf-8 localdir/ mynas:remotedir/

但從 Mac 上走「另一種方式」是行不通的。奇怪的是,測試從 Linux 機器啟動 rsync 會出現以下奇怪的訊息:

rsync: on remote machine: --iconv=UTF-8-MAC: unknown option
rsync error: syntax or usage error (code 1) at /SourceCache/rsync/rsync-45/rsync/main.c(1333) [server=2.6.9]
rsync: connection unexpectedly closed (0 bytes received so far) [sender]
rsync error: error in rsync protocol data stream (code 12) at io.c(605) [sender=3.0.9]

包括非常奇怪的說法[server=2.6.9],儘管我已經在Mac上更新到3.1.1。由於某些原因,我的 Linux 機器看起來只能「看到」Mac 上的原始 rsync 版本。

關於如何解決這個問題有什麼建議嗎?

答案1

解決方案非常簡單:很大程度上是由於我在研究該問題時讀到的評論,我認為您應該按照轉換的順序指定字元集;但這似乎不是正確的語法。相反,人們應該總是--iconv=utf-8-mac,utf-8從 Mac 初始化 rsync 時使用,並且總是從linux機器初始化rsync時使用--iconv=utf-8,utf-8-mac,無論我想從mac還是linux機器同步檔案。

然後它就像魔術一樣起作用!

編輯:確實,有時仔細檢查手冊頁是件好事。這是白底黑字:

--iconv=CONVERT_SPEC
              Rsync  can  convert  filenames between character sets using this
              option.  Using a CONVERT_SPEC of "." tells rsync to look up  the
              default  character-set via the locale setting.  Alternately, you
              can fully specify what conversion to do by giving a local and  a
              remote   charset   separated   by   a   comma   in   the   order
              --iconv=LOCAL,REMOTE, e.g.  --iconv=utf8,iso88591.   This  order
              ensures  that the option will stay the same whether you're push-
              ing  or  pulling  files.

答案2

我可以確認這有效,我也遇到了同樣的問題。就我而言,任何帶有重音字元的檔案在目標上都是無法讀取的。我只是通過使用“比較資料夾”應用程式在 Mac 上運行資料夾比較才發現它: https://itunes.apple.com/gb/app/compare-folders/id816042486?mt=12

增加了上面的 --iconv=utf-8-mac,utf-8 和 BOOM! rsync 將每個帶有重音的檔案替換為新檔案。

要添加一些信息,因為上面的鏈接似乎不再起作用,要將 rsync 升級到 3.1.2,請安裝 Macports 並運行: sudo port install rsync

您看到遠端伺服器傳回版本 2.6.9 的原因是因為舊版本實際上仍然存在,並且遠端伺服器看到的是舊版本而不是新版本。

版本 2.6.9 位於 /usr/bin

相關內容