Centos7 rsync 實現檔案同步

rsync(remote sync)是unix及類unix平臺下的資料映象備份軟體,它不像FTP那樣需要全備份,rsync可以根據資料的變化進行差異備份,從而減少資料流量,提高工作效率

序號 型別 ip

1 server 10。200。132。141

2 client 10。200。132。142

從server端同步資料到client端

一、安裝rsync

centos7 自帶rsync,所以不需要額外的安裝。

二、server配置

1、編輯配置檔案

vim /etc/rsync。conf

uid = root

gid = root

use chroot = no

port = 873

max connections = 2000

timeout = 200

# 指定日誌檔案位置,由於日誌是放到log下面,所以需要手動建立rsyncd目錄,否則啟動失敗

log file = /var/log/rsyncd/rsyncd。log

# 指定rsync的pid目錄

pid file = /var/run/rsyncd。pid

lock file = /var/run/rsyncd。lock

log format = %t %a %m %f %b

# 同步的模組名

[dcim_saas]

# 同步地址

path=/opt/dcim-saas

# 與同步模組名保持一致

comment =dcim_saas

list =yes

read only =no

write only =no

uid = root

# 認證資訊地址

secrets file =/etc/rsyncd。passwd

ignore errors = yes

# 允許同步的客戶端地址

hosts allow = 10。200。132。142

2、編輯認證檔案

echo “rsyncuser:123456” > /etc/rsyncd。passwd

[root@dcim-saas-master rsyncd]# echo “rsyncuser:123456” > /etc/rsyncd。passwd

[root@dcim-saas-master rsyncd]# cat /etc/rsyncd。passwd

rsyncuser:123456

3、啟動

[root@dcim-saas-master rsyncd]# systemctl restart rsyncd

檢視埠

[root@dcim-saas-master rsyncd]# netstat -anpt|grep rsync

tcp 0 0 0。0。0。0:873 0。0。0。0:* LISTEN 6581/rsync

tcp6 0 0 :::873 :::* LISTEN 6581/rsync

檢視日誌

[root@dcim-saas-master rsyncd]# tail -f /var/log/rsyncd/rsyncd。log

2018/09/06 03:57:10 [6581] rsyncd version 3。0。9 starting, listening on port 873

4、關閉防火牆,否則客戶端不能同步資料

[root@dcim-saas-master rsyncd]# systemctl stop firewalld

[root@dcim-saas-master rsyncd]# systemctl disabled firewalld

三、client配置

client基本上不用做配置

在客戶端機器上執行同步命令

rsync -auv ——password-file=/etc/rsyncd。passwd rsyncuser@10。200。132。141::dcim_saas /opt/dcim-saas/

rsync -auv ——password-file=/etc/rsyncd。passwd 使用者名稱@server_ip::同步模組名 client_path

如果只同步某個子目錄,加上引數 ——include 比如只同步js目錄 ——include=js/

rsync -auv ——include=js/ ——password-file=/etc/rsyncd。passwd rsyncuser@10。200。132。141::dcim_saas /opt/dcim-saas/

如果同步部分目錄,可以多加幾個——include=,這樣會比較麻煩,更簡便的方式是加上引數——include-from

先新增一個檔案

vim /opt/dcim-saas/conf/rsync-include。conf

[root@dcim-saas-slave conf]# vim /opt/dcim-saas/conf/rsync-include。conf

agent/

grafana/

nginx/

zutai/

然後執行

rsync ——size-only -azvP ——include-from=“/opt/dcim-saas/conf/rsync-include。conf” ——exclude=/* ——password-file=/etc/rsyncd。passwd rsyncuser@10。200。132。141::dcim_saas /opt/dcim-saas/

[root@dcim-saas-slave conf]# rsync ——size-only -azvP ——include-from=“/opt/dcim-saas/conf/rsync-include。conf” ——exclude=/* ——password-file=/etc/rsyncd。passwd rsyncuser@10。200。132。141::dcim_saas /opt/dcim-saas/

如果排除一些檔案同步,可以加上引數——exclude=

如果排除的檔案比較多,型別也不一樣 可以加上 ——exclude-from

vim /opt/dcim-saas/conf/rsync-exclude。conf

[root@dcim-saas-slave conf]# vim /opt/dcim-saas/conf/rsync-exclude。conf

agent/*。log

agent/agent。2*

agent/prometheus/*。log

然後執行

rsync ——size-only -azvP ——include-from=“/opt/dcim-saas/conf/rsync-include。conf” ——exclude-from=“/opt/dcim-saas/conf/rsync-exclude。conf” ——exclude=/* ——password-file=/etc/rsyncd。passwd rsyncuser@10。200。132。141::dcim_saas /opt/dcim-saas/

[root@dcim-saas-slave conf]# rsync ——size-only -azvP ——include-from=“/opt/dcim-saas/conf/rsync-include。conf” ——exclude-from=“/opt/dcim-saas/conf/rsync-exclude。conf” ——exclude=/* ——password-file=/etc/rsyncd。passwd rsyncuser@10。200。132。141::dcim_saas /opt/dcim-saas/

同步過程中會產生日誌,可以去server端檢視同步日誌情況

四、配置雙向同步

雙向同步就是反過來配置一遍就好了,將server的配置複製到client,修改裡面的host allows,其他不變,將client的配置複製到server