不同服務器之間 實現某個文件夾內容同步(使用rsync)

第一步:先準備2臺服務器(虛擬機即可)

虛擬機1 :IP爲 192.168.1.160 (主服務器,也可以稱服務端)
虛擬機2:IP爲 192.168.1.161(副服務器,也可以稱客戶端)

第二步:分別在兩臺服務器中安裝async

yum install rsync

第三步:先進入虛擬機1

1.先修改配置 vim /etc/rsyncd.conf,大致配置如下
uid = root
gid = root
port = 873
use chroot = no
max connections = 200
pid file = /var/run/rsyncd.pid
lock file = /var/run/rsyncd.lock
log file = /var/log/rsyncd.log
timeout = 900
[rsync] 
    path = /home/wwwroot/test  需要被同步的文件夾
    comment = ftp export area
	ignore errors
	read only = false
	list = no
	auth users = root 
	secrets file = /etc/rsyncd.passwd
2.創建認證密碼文件
touch /etc/rsyncd.passwd
vim /etc/rsyncd.passwd
寫入 root:123456  
root代表是當前用戶
3.修改權限 chmod 600 /etc/rsyncd.passwd
4.因爲rsync默認會監聽873端口,所以需要開放873端口,這裏不詳細說明。
5.啓動服務 /usr/bin/rsync --daemon

第四步-進入虛擬機2

1.創建認證密碼文件
touch /etc/rsyncd.passwd
echo "123456" >> /etc/rsyncd.passwd   #將之前設置的密碼加入進去,爲了不用輸入密碼
2.修改權限 chmod 600 /etc/rsyncd.passwd
touch /etc/rsyncd.conf
3.啓動服務 /usr/bin/rsync --daemon

第五步-開始同步

1.先在虛擬機1 /home/wwwroot/test 隨便創建一個文件
[root@bogon test]# touch change.php
[root@bogon test]# ls
change.php
2.再去虛擬機2 輸入以下命令
rsync -avz rsync://[email protected]/rsync/ /home/wwwroot/test 
--password-file=/etc/rsyncd.passwd

拆分說明
1. rsync -avz rsync:// 爲固定寫法
2. [email protected] 爲所要同步的服務器用戶名和ip
3. /rsync/ 爲虛擬機1 /etc/rsyncd.conf 中的配置 [rsync] ,保持一致即可
4. /home/wwwroot/test 爲所要同步的文件夾名稱
5. --password-file=/etc/rsyncd.passwd 加載密碼文件

第六步-查詢結果

輸入命令之後
[root@bogon test]# rsync -avz rsync://[email protected]/rsync/ /home/wwwroot/test --password-file=/etc/rsyncd.passwd
receiving incremental file list
./
change.php  爲剛剛新增的文件

sent 51 bytes  received 159 bytes  420.00 bytes/sec
total size is 0  speedup is 0.00
[root@bogon test]# ls
change.php
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章