︿
Top

CentOS 7.6 上安裝 Rsyncd 遠端檔案同步伺服器

rsync是 remote sync的縮寫、Linux上好用的檔案目錄同步指令,雖然可以用在本地端,但一般都是用在異地,例如 A主機要將某目錄下的所有檔案同步備份至 B主機,那什麼時候會需要架設 Rsyncd遠端檔案同步伺服器呢?一般是用在該主機經常性的需要接受外地或各地同步而來的檔案,也可以想做是建置一個專用的檔案備份伺服器。


同步、是 rsync很重要的一個功能,這也是它與 cp、scp不同的地方,rsync只有在第一次傳輸的時候才會完整備份所有的檔案目錄至目的端,若是之後再有同樣位置的檔案目錄傳輸備份,則只會對有差異的部分進行傳輸,大幅地降低檔案目錄的傳輸數量,提升備份效率!

而建置為 Daemon的好處是可以針對不同 rsync用戶設置不同的權限與專屬的任務備份空間,以下將示範如何在 CentOS 7.6x64上建立 Rsyncd服務,並透過 rsync client對 Server端進行資料的同步備份:

[Server端]
IP Address:192.168.1.10
[Client端]
IP Address:192.168.1.199



Server端


安裝 rsync、備份原始設定檔並編輯加入所需設定
[root@server ~]# yum install rsync
[root@server ~]# cp /etc/rsyncd.conf /etc/rsyncd.conf-bak
[root@server ~]# nano /etc/rsyncd.conf
直接於 rsyncd.conf文件底部新增以下設定後存檔離開
uid = root 
gid = root 
pid file = /var/run/rsyncd.pid 
log file=/var/log/rsyncd.log 
secrets file = /etc/rsyncd.passwd 
dont compress = *.gz *.tgz *.zip *.z *.Z *.rpm *.deb *.bz2 

[my_storage] 
        path = /mnt/IronWolf10TB/ 
        comment = Storage-PC 
        read only = no 
        auth users = rsync_user
建立一個存放遠端 rsync用戶帳密的檔案、名稱對齊 rsyncd.conf裡的 secrets file即可
[root@server ~]# nano /etc/rsyncd.passwd
用戶帳密的格式為[Username]:[Password]
rsync_user:mypassword
test:mypassword
基於安全性理由,將密碼檔案設置為只有 root可讀取
[root@server ~]# chmod 600 /etc/rsyncd.passwd
這一步非必須,但原理上我會擔心檔案同步的時間落差造成問題,建議對個時
[root@server ~]# ntpdate time.google.com
啟用並將服務設定為開機時啟動
[root@server ~]# systemctl enable rsyncd
[root@server ~]# systemctl start rsyncd
防火牆放行 Daemon會用到的 TCP 873並關閉 Linux安全模組
[root@server ~]# firewall-cmd --permanent --add-port=873/tcp
[root@server ~]# firewall-cmd --reload
[root@server ~]# setenforce 0
[root@server ~]# nano /etc/sysconfig/selinux
註解掉原本的 enforcing並新增設定為 disabled後存檔離開
#SELINUX=enforceing
SELINUX=disabled


Client端


設定一個 rsync密碼的文字檔以便執行同步時不需要手動輸入密碼,有利於未來排程自動化
[root@192-168-1-199 ~]# nano /root/rsync_user.passwd
rsync密碼文字檔的格式是只需要輸入密碼即可
mypassword
基於安全性理由,將密碼檔案設置為只有 root可讀取
[root@192-168-1-199 ~]# chmod 600 /root/rsync_user.passwd
試看看同步一個檔案到你的遠端的備份伺服器上吧!
[root@192-168-1-199 ~]# rsync -avzu --progress --password-file=/root/rsync_user.passwd /tmp/windows7.iso rsync_user@192.168.1.10::my_storage

sending incremental file list
windows7.iso
  2990276608 100%   62.95MB/s    0:00:45 (xfer#1, to-check=0/1)

sent 2991280501 bytes  received 27 bytes  64328613.51 bytes/sec
total size is 2990276608  speedup is 1.00


Troubleshooting


如果你在 rsync的時候遇上了「rsync: mkstemp failed: Permission denied (13) UNKNOWN」這樣的訊息,記得將 Rsyncd Server端的 selinux關閉。




tomy

來自台灣的系統工程師,一直熱衷於 Open source 相關技術的學習、建置、應用與分享。

  • Image
  • Image
  • Image
  • Image
  • Image
    Blogger Comment

2 Comments:



  1. 基於安全性理由,將密碼檔案設置為只有 root可讀取
    [root@192-168-1-199 ~]# chmod 600 /etc/rsync_user.passwd

    這邊是不是有錯? etc? 是不是要把etc改root?

    回覆刪除
    回覆
    1. 謝謝你的提醒,已經更正筆誤的地方。

      刪除