前言
rsync作用:man rsync可以看到解釋為a fast, versatile, remote (and local) file-copying tool,主要進行文件的同步。
inotify作用:man inotify可以看到解釋為 monitoring file system events,主要是監(jiān)控文件狀態(tài)。
配置環(huán)境
本文中備份主機ip為192.168.1.159,hostname為inotify-slave
宿主機ip為192.168.1.185,hostname為inotify-master
在備份主機中運行rsync程序,然后在宿主機中使用inotify對files的狀態(tài)進行監(jiān)控,從而再使用rsync命令對文件進行同步
rsync的配置
1.slave端rsync的安裝
centos6一般默認有rsync,沒有的話可以
yum -y install rsync
2.配置slave端rsync基礎環(huán)境
[root@inotify-slave ~]# useradd rsync -s /sbin/nologin ##新建rsync用戶,不需要登陸
[root@inotify-slave ~]# grep rsync /etc/passwd
rsync:x:500:500::/home/rsync:/sbin/nologin
[root@inotify-slave ~]# mkdir /rsyncbackup #創(chuàng)建rsync daemon工作模式的模塊目錄
[root@inotify-slave ~]# chown -R rsync.rsync /rsyncbackup
#更改模塊目錄的用戶組,如果rsync用戶沒有權(quán)限對模塊目錄進行寫的話,后續(xù)會影響同步
3.slave端rsync的配置文件
[root@inotify-slave ~]# cat /etc/rsync.conf ##沒有此文件的自己新建即可
uid = rsync
gid = rsync
use chroot = no
max connections = 200
timeout = 300
pid file = /var/run/rsync.pid
lock file = /var/run/rsync.lock
log file = /var/log/rsync.log
[backup] ##模塊名稱,可隨意取
path = /data/rsyncbackup ##同步保存文件的路徑
ignore errors
read only = no ##網(wǎng)絡可寫
list = no ##設置的ip是否同步
hosts allow = 192.168.1.0/24 ##同步的ip網(wǎng)段
auth users = rsync_backup ##rsync的虛擬用戶,可隨意
secrets file = /etc/rsync.passwd ##rsync虛擬用戶的密碼
4.slave端rsync虛擬用戶配置
[root@inotify-slave ~]# echo "rsync_backup:chunlanyy" > /etc/rsync.passwd ##格式為虛擬用戶:虛擬用戶密碼
[root@inotify-slave ~]# chmod 600 /etc/rsync.passwd
##更改密碼文件的權(quán)限,必須更改,否則在同步的時候會出現(xiàn)auth fail的錯誤
5.啟動slave端rsync服務
[root@inotify-slave ~]# rsync --daemon --config=/etc/rsync.config ##以daemon模式啟動,同時加載對應的配置文件
[root@inotify-slave ~]# ps aux | grep rsync
root 5642 0.0 0.0 103308 860 pts/3 S+ 18:17 0:00 grep rsync
root 26647 0.0 0.0 107628 552 ? Ss 00:25 0:05 rsync --daemon --config=/etc/rsync.conf
root 29663 0.0 0.0 182668 1196 ? Ss 02:30 0:00 svnserve -d -r /data/rsyncbackup/
[root@inotify-slave ~]# ss -tnlp | grep rsync
LISTEN 0 5 :::873 :::* users:(("rsync",26647,5))
LISTEN 0 5 *:873 *:* users:(("rsync",26647,4))
rsync啟動端口為873端口
將rsync服務加載至開機啟動中
echo "/usr/bin/rsync --daemon --config=/etc/rsync.config" >>/etc/rc.local
6.master端rsync配置
[root@inotify-master ~]# cat /etc/rsync.passwd
chunlanyy ##此處密碼為slave端rsync虛擬用戶的密碼,與slave端不是,這里只有密碼。
7.slave-master同步測試
[root@inotify-master ~]# echo "marility to chunlanyy" > test.txt
[root@inotify-master ~]# rsync -avz test.txt rsync_backup@192.168.1.159::backup --password-file=/etc/rsync.passwd
sending incremental file list
test.txt
sent 91 bytes received 33 bytes 82.67 bytes/sec
total size is 22 speedup is 0.18
其中rsync_backup為slave端rsync的虛擬用戶
192.168.1.159為slave端ip
::backup,雙冒號,其中backup為slave端設定的rsync的工作模塊,并非文件存放路徑,這與scp的格式略有不同,
切換至slave端的rsync工作目錄,可以查看到生成的文件已經(jīng)出現(xiàn)
[root@inotify-slave rsyncbackup]# pwd
/data/rsyncbackup
[root@inotify-slave rsyncbackup]# ll
total 4
-rw-r--r--. 1 rsync rsync 22 Feb 15 2017 test.txt
[root@inotify-slave rsyncbackup]# cat test.txt
marility to chunlanyy
inotify配置
1.inotify的安裝配置
[root@inotify-master ~]# yum install -y inotify-tools
2.inotify自動檢測腳本
[root@inotify-master ~]# cat inotify.sh
#!/bin/bash
## moniter the file
host01=192.168.1.159 ##slave主機ip
src=/data/svn/repos ##master需要同步文件的路徑
dst=backup ##slave中rsync的模塊
user=rsync_backup ##slave中rsync的虛擬用戶
rsync_passfile=/etc/rsync.passwd
/usr/bin/inotifywait -mrq --timefmt '%d/%m/%y %H:%M' --format '%T %w%f' -e close_write,delete,create,attrib $src | while read file
do
cd $src && rsync -aruz -R --delete ./ --timeout=100 $user@$host01::$dst --password-file=${rsync_passfile} >/dev/null 2>&1
done
將該腳本啟動放置于后臺
[root@inotify-master ~]# nohup /bin/bash inotify.sh 2>1&
[1] 11315
[root@inotify-master ~]# ps aux | grep inotify
root 11315 0.0 0.0 106096 1168 pts/1 S 19:02 0:00 /bin/bash inotify.sh
root 11316 0.0 0.0 6320 752 pts/1 S 19:02 0:00 /usr/bin/inotifywait -mrq --timefmt %d/%m/%y %H:%M
--format %T %w%f -e close_write,delete,create,attrib /data/svn/repos
root 11317 0.0 0.0 106096 668 pts/1 S 19:02 0:01 /bin/bash inotify.sh
root 14666 0.0 0.0 103308 864 pts/1 S+ 19:37 0:00 grep inotify
3.自動檢測測試
[root@inotify-master repos]# mkdir -pv /data/svn/repos/test
mkdir: created directory `/data/svn/repos/test'
[root@inotify-master repos]# cd /data/svn/repos/test
[root@inotify-master test]# ls
[root@inotify-master test]# for a in `seq 200`;do touch $a.txt;done ##新建200個文件
[root@inotify-master test]# ll
total 0
-rw-r--r--. 1 root root 0 Feb 15 19:12 100.txt
-rw-r--r--. 1 root root 0 Feb 15 19:12 101.txt
.
.
.
-rw-r--r--. 1 root root 0 Feb 15 19:12 9.txt
查看slave端目錄
[root@inotify-slave test]# pwd
/data/rsyncbackup/test
[root@inotify-slave test]# ll
total 0
-rw-r--r--. 1 rsync rsync 0 Feb 15 2017 100.txt
-rw-r--r--. 1 rsync rsync 0 Feb 15 2017 101.txt
.
.
.
-rw-r--r--. 1 rsync rsync 0 Feb 15 2017 9.txt
可以看到slave端已經(jīng)將文件同步過來
[root@inotify-master repos]# ls
conf db format hooks locks README.txt test
[root@inotify-master repos]# du -sh
125M .
[root@inotify-slave rsyncbackup]# ls
conf db format hooks locks README.txt test
[root@inotify-slave rsyncbackup]# du -sh
125M .
可以看到兩者的文件及大小一致
刪除master端的目錄,檢測是否能同步
[root@inotify-master repos]# rm test/ -rf
[root@inotify-master repos]# ls
conf db format hooks locks README.txt
[root@inotify-slave rsyncbackup]# ls
conf db format hooks locks README.txt
可以看到當master端刪除文件后,slave端也同步刪除
4.將該腳本置于開機啟動中
echo "/usr/bin/nohup /root/inotify.sh 2>&1" >>/etc/rc.local
chmod +x /etc/rc.local
同步之后的備份
雖然inotify+rsync可以將文件進行備份,但是由于設定在master端進行文件刪除時,也會跟著刪除(主要是為了保證slave端與master端文件的一致性),故為避免目錄文件被誤刪引起的悲劇,故每周或者每日,還需要對文件進行全量備份,即tar
+scp
命令的使用