rsync+inotify自動備份服務器的配置
1 備份服務器IP : 192.168.1.106
客戶端IP : 192.168.1.107
2 .
#yum -y install rsync xinetd gcc gcc-c++
安裝這兩個服務,同時也將C/C++編譯器也安上(如果事先沒安的話,避免我們在編譯二進制文件時找不到編譯器的情況)
3 .在服務器端(192.168.1.106)的/etc下創建一個rsyncd.conf
#service iptables stop 關閉防火牆,與selinux
#vi /etc/selinux/config 將SELINUX=disabled
#vi /etc/rsyncd.conf
uid = nobody
gid = nobody
max connections = 200
timeout = 600
use chroot = no
hosts allow = *
pid file=/var/run/rsyncd.pid
#syslog facility = local7
log file=/var/log/rsyncd.log
#rsync con**
#The 'standard' things
[backup]
path = /backup //在服務器的根下面創建一個目錄用來備份客戶端的文件
ignore errors //忽略一些錯誤
read only = no
list = no
auth users = test //認證用戶
secrets file = /etc/rsyncd.secrets 認證用戶的用戶名與密碼存放位置
4 . 在服務器端(192.168.1.106下面編譯/etc/xinetd.d/rsync
#vi /etc/xinetd.d/rsync
service rsync
{
disable = no //將原來的YES改爲NO就OK了
flags = IPv6
socket_type = stream
wait = no
user = root
server = /usr/bin/rsync
server_args = --daemon
log_on_failure += USERID
}
5 . 在服務器端創建建認證的用戶與密碼文件
#vi /etc/rsyncd.secrets
test:test
:wq
保存退出
6 . 啓動rsync服務
#service xinetd restart
#/usr/bin/rsync --daemon --config=/etc/rsyncd.conf
7 . 注意權限的問題引起客戶端無法連接的原因
#chmod 777 /backup
#chmod 600 /etc/rsyncd.secrets
密碼文件必須要改爲600的權限,不然客端連接的時候會報錯
客戶端的配置(192.168.1.107)
#yum install gcc gcc-c++ rsync xinetd
#service xinetd restart
#service iptables stop
#netstat -tnl |grep 873
1 . vi /etc/rsyncd.secrets
testpasswd
:wq
保存退出
2 . 修改權限
#chmod 600 /etc/rsyncd.secrets
3 . 在客戶端
#rsync -vzrtopg --password-file=/etc/rsyncd.secrets /aaa [email protected]::backup
其中aaa是本地要備份到服務器上面的目錄
這樣的是用於手動同步的,命令也是很麻煩的可以將其寫到一個shell角本通過crontab調用進行自動同步
但還有一種效率更高的方法就是下面我們要說的rsync+inotify的相結合的一種有效方式
http://inotify-tools.sourceforge.net下載相應的安裝包
# tar -zxvf inotify-tools-3.14.tar.gz
#cd inotify-tools-3.14
#./configure && make && make install
安裝完成後寫一個shell角本
#vi rsync.sh
#!/bin/bash
src=/aaa
des=backup
ip=192.168.1.106
/usr/local/bin/inotifywait -mrq --timefmt '%d/%m/%y %H:%M' --format '%T %w %f' -e modify,delete,create,attrib $src | while read file
do
rsync -vzrtopg --delete --progress $src test@$ip::$des --password-file=/etc/rsyncd.secrets &&
echo "$src was rsynced"
done
:wq
#nohup sh /root/rsync.sh & 注意中間的空格
這樣就可以保證當你修改客戶端的/aaa裏面的文件時會立即向服務器端進行更新,達到數據的快速一致