centos系統rsync檔案同步 安裝配置

來源:互聯網
上載者:User

rsync的目錄主要是為了同步檔案用的,現在把使用的配置記錄一下

測試機器2台
rsync伺服器IP:192.168.171.22
用戶端IP:192.168.171.134

rsync伺服器端操作
檢查是否安裝rsync

rpm -qa | grep rsync
安裝rsync,並通過xinetd管理rsync

yum -y install rsync xinetd
檢查啟動,預設連接埠873

netstat -an | grep 873
rsync隨xinetd啟動而啟動

vi /etc/xinetd.d/rsync
將disable = yes 修改為 disable = no

建立rsync需要的檔案

mkdir /etc/rsyncd
touch /etc/rsyncd/rsyncd.conf #主設定檔
touch /etc/rsyncd/rsyncd.secrets #驗證使用者和密碼存放地
touch /etc/rsyncd/rsyncd.motd #伺服器描述資訊
ln /etc/rsyncd/rsyncd.conf /etc/rsyncd.conf #建立連結
為了密碼的安全性,我們把許可權設為600

chown root:root /etc/rsyncd/rsyncd.secrets
chmod 600 /etc/rsyncd/rsyncd.secrets
修改主設定檔

vi /etc/rsyncd/rsyncd.conf
添加以下內容

#伺服器傳輸檔案時用root 許可權執行
uid = root                       
gid = root
#指定服務連接埠,預設873
port = 873
use chroot = no
#唯讀,不讓用戶端上傳檔案到伺服器
read only = yes
#limit access to private LANs
#限制能訪問該服務的網段
hosts allow=192.168.0.0/255.255.0.0
hosts deny=*
#最大串連數
max connections = 5                     
#告訴進程寫到該檔案中
pid file = /var/run/rsyncd.pid            

secrets file = /etc/rsyncd/rsyncd.secrets  
#lock file = /var/run/rsync.lock          
#伺服器資訊定義指向該檔案,該檔案可寫伺服器資訊,歡迎的文字
motd file = /etc/rsyncd/rsyncd.motd      

#This will give you a separate log file
log file = /var/log/rsync.log              

#This will log every file transferred - up to 85,000+ per user, per sync
transfer logging = yes                   
#記錄檔格式
log format = %t %a %m %f %b
syslog facility = local3
timeout = 300 #逾時時間

# MODULE OPTIONS
[testhome]
#同步的目錄 
path = /home/test/                    
list=yes  
#忽略IO錯誤
ignore errors
#驗證使用者,我的使用者是cs 
auth users = cs                        
comment = test home   
#不同步的目錄
exclude = .git/
修改rsyncd.secrets 和rsyncd.motd 檔案

vi /etc/rsyncd/rsyncd.secrets
新增內容

cs:123456
cs該使用者不要求是系統使用者

vi /etc/rsyncd/rsyncd.motd
新增內容

++++++++++++++++++++++++++++++++
Welcome
++++++++++++++++++++++++++++++++
防火牆設定(rsync預設連接埠 837)

vi /etc/sysconfig/iptables
添加

-A INPUT -p tcp -m state --state NEW -m tcp --dport 873 -j ACCEPT
重啟服務

service xinetd restart
用戶端操作
安裝rsync

yum -y install rsync
測試

rsync -avzP cs@192.168.171.22::testhome /home/test
上面命令意思就是用cs使用者登入伺服器,將testhome指定的目錄資料儲存到我本地/home/test

同步命令說明:

1 顯示目錄內容

命令
——
a) rsync
b) rsync -r
c) rsync jack@192.168.0.1::
d) rsync ssh_user@192.168.0.1:

命令說明
———
a) 顯示目錄內容(第一層)
b) 遞迴顯示目錄內容
c) 顯示遠程主機目錄內容
*注1:連接埠模式, 基於rsync使用者的身分識別驗證
*注2:rsync server上的目錄必須具有xx7的許可權.

d) 查看遠程主機目錄內容
*注1:remote shell模式, 通過ssh串連的基於系統本機使用者的身分識別驗證
*注2:這裡只使用了一個冒號(:),同時使用者名稱是遠程主機的ssh 使用者,密碼也是ssh使用者對應的密碼。
*注3:使用””,則列出檔案夾本身的資訊。若要列出檔案夾內容,應使用”/”。

參數說明
———
-r 對目錄進行遞迴操作

2 本地目錄之間同步

命令
——
a) rsync -av –progress / *** 注意(/) ***
b) rsync -av –progress
c) rsync -avu –progress –delete /
d) rsync -av –progress –temp-dir=/tmp /

命令說明
———
a) 同步src-dir目錄下所有檔案到dst-dir目錄下
b) 同步src-dir目錄下所有檔案到dst-dir/src-dir目錄下
c) 對src-dir目錄內容向dst-dir目錄下進行差異更新,有增加/更新則添加替換,有減少則對其刪減
d) 比a)多了–temp-dir=/tmp,即指定/tmp為臨時交換區,這樣可以避免因目標目錄空間不夠引起的無法同步檔案的錯誤。

參數說明
———
-a 相當於 -rlptgoD 的集合
-u 等同於 –update,在目標檔案比源檔案新的情況下不更新
-v 顯示同步的檔案
–progress 顯示檔案同步時的百分比進度、傳輸速率
–delete 刪除目標目錄中多於來源目錄的檔案

3 異地主機之間同步
命令
——
a) rsync -avz –progress jack@192.168.0.1::/
b) rsync -avz –progress jack@192.168.0.1::/ –password-file=/home/jack/rsync.jack
c) rsync -avuz –progress –delete jack@192.168.0.1::/ –password-file=/home/jack/rsync.jack
d) rsync -avz –progress jack@192.168.0.1::/

命令說明
———
a) 同步本地目錄的內容到遠程主機192.168.0.1的目錄下,jack是rsync資料庫使用者(參見3. /etc/rsync.secrets)
b) 通過自動讀取使用者密碼而實現非互動登入檔案同步
c) 較b)多了-u和–delete
d) 同步遠程主機內容到本地目錄

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.