[Linux] 使用 scp 時無法保留 symbolic link file 屬性?

[Linux] 使用 scp 時無法保留 symbolic link file 屬性?

今天同事回報一個問題,說一些 symbolic link file 在做過某些動作之後,

變成不是 symbolic link file,而是跟指向的檔案一模一樣了~

自己試著重現問題無法成功,後來才發現同事做了一個關鍵的動作:scp

試了一下,原來 scp 是沒辦法保留 symbolic link 的…

 

舉例來說,目錄裡面有下面三個檔案,其中 libev.so 和 libev.so.4 是 symbolic link,

指向 libev.so.4.0.0 這個真正的檔案:

root@machine1 ~ ll
lrwxrwxrwx 1 root root     14 Mar 11 03:57 libev.so -> libev.so.4.0.0*
lrwxrwxrwx 1 root root     14 Mar 11 03:57 libev.so.4 -> libev.so.4.0.0*
-rwxr-xr-x 1 root root 151689 Mar 11 03:57 libev.so.4.0.0*

 

當把這個目錄用 scp 整個傳到另一台主機上後,屬性就改變了~

libev.so 和 libev.so.4 不再是 symbolic link file,

變成了真正的檔案,大小內容也都跟指向的 libev.so.4.0.0 一模一樣:

root@machine2 ~ ll
-rwxr-xr-x. 1 root root 151689 Mar 15 23:08 libev.so
-rwxr-xr-x. 1 root root 151689 Mar 15 23:08 libev.so.4
-rwxr-xr-x. 1 root root 151689 Mar 15 23:08 libev.so.4.0.0

 

查了一下,原來 scp 是不支援 symbolic link 的,

想要完整地將目錄從一台主機複製到另一台主機,得用 rsync 才行,例如:

rsync -Wav root@machine1:/lib ./

 

這裡的 -a 選項代表的是 –archive,它會隱含了 -rlptgoD 幾個選項,

其中的 -l 選項就是保留 symbolic link:

 -a, --archive      archive mode; equals -rlptgoD (no -H,-A,-X)
-r, --recursive    recurse into directories
-l, --links        copy symlinks as symlinks
-p, --perms        preserve permissions
-o, --owner        preserve owner (super-user only)
-g, --group        preserve group
-D                 same as --devices --specials
-t, --times        preserve modification times

 

使用 rsync 加上 -a 選項之後,原本的 symbolic link file 在傳輸到別台主機上後,

也依然維持是 symbolic link file 囉~

 

參考資料:superuser: scp + copy the same links

 

(本頁面已被瀏覽過 2,896 次)

發佈留言

發佈留言必須填寫的電子郵件地址不會公開。 必填欄位標示為 *

這個網站採用 Akismet 服務減少垃圾留言。進一步了解 Akismet 如何處理網站訪客的留言資料