rsync實現文件同步
工作模型 一臺作爲server端一臺最爲client端,client把數據差異同步到server上。從而實現數據的同步。
1.在server端和client上分別安裝
yum install rsync -y
2.爲server端提供配置文件
vim /etc/rsyncd.conf
uid = root//如果是nobody的話後期再同步的時候會有一些文件無法同步會有權限的問題
gid = root//同上
use chroot = no
max connections = 200
#strict modes = yes
timeout = 100
lock file = /var/run/rsync.lock
pid file = /var/run/rsyncd.pid
log file = /var/log/rsyncd.log
[my]
path = /wcf/ //指定要同步的目錄接收地址
ignore errors
read only = no
#write noly = no
hosts allow = 172.1.0.0/16
hosts deny = 0.0.0.0/32
list = false
auth users = myusers
secrets file = /etc/rsync.passwd
3.創建密碼文件
vim /etc/rsync.passwd
myusers:mypassword
4.修改密碼文件的權限必須爲600
chmod 600 /etc/rsync.passwd
5.啓動服務
rsync --daemon
6.檢查服務是否運行
netstat -tnlp | grep rsync
如果出現端口爲873的表示服務已經啓動
7.查看防火牆是否放行873的服務
iptables -L
如果不放行可以加規則放行rsync服務的請求和響應
8.寫腳本實現檢測服務是否正常運行如果沒有這自動啓動服務
vim /root/monitoringrsync.sh
#!/bin/bash
stat=`/usr/bin/netstat -tnlp | /usr/bin/grep 873 | /usr/bin/wc -l`
if [ $stat -eq 0 ]; then
/usr/bin/rsync --daemon
fi
9.把腳本加到定時器中
crontab -e
*/10 * * * * /root/monitoringrsync.sh
配置client端
10.創建密碼文件這裏邊的密碼一定要和服務器端的一樣
vim /etc/rsync.pass
mypassword
11.修改權限必須爲600
chmod 600 /etc/rsync.pass
12.在客戶端寫腳本就可以實現自動同步了
vim /root/rsync.sh
#!/bin/bash
rsync -avz /users/ rsync://[email protected]/my --password-file=/etc/rsync.passwd && echo "Complete synchronization at `date`" >> /tmp/rsync.txt
if [ $? -ne 0 ]; then
echo "Synchronization failure at `date`" >> /tmp/rsync.txt
/sbin/sendmail [email protected] < /tmp/rsync.txt
fi
此腳本也可以檢測是否完成同步可以查看腳本指定的文件/tmp/rsync.txt如何同步失敗會給指定的用戶發送電子郵件通知
13.把腳本加到crontab中
crontab -e
01 01 * * * /root/rsync.sh
實現每天的晚上凌晨1:01開始同步