Linux Rsync+inotify实现文件实时同步

  在生产环境中,对于文件服务器上的数据,我们要求实时的做备份,推送到备份服务器上,避免因服务器宕机造成的文件数据丢失问题。通常我们会采取rsync+inotify(或sersync)的方案,来实现文件实时同步的效果。

Rsync+inotify基础架构

[Rsync环境搭建]

[Center-A]:备份服务器

 1 #配置rsync
  [root@Center-A ~]# vim /etc/rsyncd.conf 2 #rsync_config_______________start 3 uid = rsync 4 gid = rsync 5 use chroot = no 6 max connections = 200 7 timeout = 300 8 pid file = /var/run/rsyncd.pid 9 lock file = /var/run/rsync.lock 10 log file = /var/log/rsyncd.log 11 12 [skyex] 13 path = /skyex/ 14 ignore errors 15 read only = false 16 list = false 17 hosts allow = 10.0.0.0/24 18 hosts deny = 0.0.0.0/32 19 auth users = rsync_backup 20 secrets file = /etc/rsync.password 21 #rsync_config_______________end 22 "/etc/rsyncd.conf" [ÐÂ] 21L, 456C ÒÑдÈë
23 #查看确认配置 24 [root@Center-A ~]# cat /etc/rsyncd.conf 25 #rsync_config_______________start 26 uid = rsync 27 gid = rsync 28 use chroot = no 29 max connections = 200 30 timeout = 300 31 pid file = /var/run/rsyncd.pid 32 lock file = /var/run/rsync.lock 33 log file = /var/log/rsyncd.log 34 35 [skyex] 36 path = /skyex/ 37 ignore errors 38 read only = false 39 list = false 40 hosts allow = 10.0.0.0/24 41 hosts deny = 0.0.0.0/32 42 auth users = rsync_backup 43 secrets file = /etc/rsync.password 44 #rsync_config_______________end 45
  #建立rsync用户 46 [root@Center-A ~]# useradd rsync -s /sbin/nologin -M 47 [root@Center-A ~]# grep rsync /etc/passwd 48 rsync:x:602:602::/home/rsync:/sbin/nologin 49
  #建立rsync目的路径 50 [root@Center-A ~]# mkdir /skyex 51 [root@Center-A ~]# ls -ld /skyex/ 52 drwxr-xr-x 2 root root 4096 05-31 05:27 /skyex/ 53 [root@Center-A ~]# chown rsync.rsync /skyex/ 54 [root@Center-A ~]# ls -ld /skyex/ 55 drwxr-xr-x 2 rsync rsync 4096 05-31 05:27 /skyex/ 56
  #配置rsync密码文件 57 [root@Center-A ~]# echo "rsync_backup:skyex">>/etc/rsync.password 58 [root@Center-A ~]# cat /etc/rsync.password 59 rsync_backup:skyex 60
  #更改密码文件权限(安全) 61 [root@Center-A ~]# chmod 600 /etc/rsync.password 62 [root@Center-A ~]# ls -ld /etc/rsync.password 63 -rw------- 1 root root 19 05-31 05:28 /etc/rsync.password 64
  #启动rsync守护进程 65 [root@Center-A ~]# rsync --daemon 66 [root@Center-A ~]# ps -ef|grep rsync 67 root 2731 1 0 05:29 ? 00:00:00 rsync --daemon 68 root 2737 2665 0 05:29 pts/0 00:00:00 grep rsync 69 [root@Center-A ~]# 70 [root@Center-A ~]# lsof -i tcp:873 71 COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME 72 rsync 2731 root 4u IPv4 8378 0t0 TCP *:rsync (LISTEN) 73 [root@Center-A ~]#

[Client-B]:文件服务器

[root@Client-B ~]# echo "skyex">/etc/rsync.password
[root@Client-B ~]# cat /etc/rsync.password 
skyex
[root@Client-B ~]# ls -ld /etc/rsync.password 
-rw-r--r-- 1 root root 6 05-31 05:34 /etc/rsync.password
[root@Client-B ~]# chmod 600 /etc/rsync.password 
[root@Client-B ~]# ls -ld /etc/rsync.password    
-rw------- 1 root root 6 05-31 05:34 /etc/rsync.password

[root@Client-B html]# touch 1.txt
[root@Client-B html]# rsync -avzP 1.txt  rsync_backup@10.0.0.56::skyex --password-file=/etc/rsync.password
sending incremental file list

sent 26 bytes  received 8 bytes  68.00 bytes/sec
total size is 0  speedup is 0.00
[root@Client-B html]#

[root@Center-A skyex]# ll
total 0
-rw-r--r-- 1 rsync rsync 0 May 31 07:23 1.txt
[root@Center-A skyex]#

[Inotify环境搭建]

  inotify-tools 是为linux下inotify文件监控工具提供的一套c的开发接口库函数,同时还提供了一系列的命令行工具,这些工具可以用来监控文件系统的事件。 inotify-tools是用c编写的,除了要求内核支持inotify外,不依赖于其他。inotify-tools提供两种工具,一是inotifywait,它是用来监控文件或目录的变化,二是inotifywatch,它是用来统计文件系统访问的次数。

  官网:http://inotify.aiken.cz/

 [Client-B:主]:文件服务器

 1 #安装inotify
 2 [root@Client-B soft]# ll
 3 total 356
 4 -rw-r--r-- 1 root root 358772 May 26 18:56 inotify-tools-3.14.tar.gz
 5 [root@Client-B soft]# tar zxvf inotify-tools-3.14.tar.gz
 6 [root@Client-B soft]# cd inotify-tools-3.14
 7 [root@Client-B inotify-tools-3.14]# ./configure --prefix=/usr/local/inotify-tools-3.14
 8 [root@Client-B inotify-tools-3.14]# make && make install
 9 [root@Client-B inotify-tools-3.14]# cd /usr/local/
10 [root@Client-B local]# ll
11 total 76
12 drwxr-xr-x 6 root root 4096 May 31 05:47 inotify-tools-3.14
13 [root@Client-B local]# ln -s inotify-tools-3.14/ inotify
14 [root@Client-B local]# ll
15 total 76
16 lrwxrwxrwx 1 root root   19 May 31 05:48 inotify -> inotify-tools-3.14/
17 drwxr-xr-x 6 root root 4096 May 31 05:47 inotify-tools-3.14
18 [root@Client-B local]#
 1 #事件监控脚本
 2 #!/bin/bash
 3 #para
 4 host01=10.0.0.56
 5 src=/var/www/html/
 6 dst=skyex
 7 user=rsync_backup
 8 rsync_passfile=/etc/rsync.password
 9 inotify_home=/usr/local/inotify-tools-3.14/
10 
11 #judge
12 if [ ! -e "$src" ] \
13 || [ ! -e "${rsync_passfile}" ] \
14 || [ ! -e "${inotify_home}/bin/inotifywait" ] \
15 || [ ! -e "/usr/bin/rsync" ];
16 then
17   echo "Check File and Folder"
18   exit 9
19 fi
20 
21 #inotify监控文件系统的变化
22 ${inotify_home}/bin/inotifywait -mrq --timefmt '%d/%m/%y %H:%M' --format '%T %w%f' -e close_write,delete,create,attrib $src \
23 | while read file
24         do
25          # rsync -avzP --delete --timeout=100 --password-file=${rsync_passfile} $src $user@$host01::$dst >/dev/null 2>&1
26          cd $src && rsync -aruz -R --delete ./  --timeout=100 $user@$host01::$dst --password-file=${rsync_passfile} >/dev/null 2>&1 
27         done
28 exit 0
 1 [root@Client-B html]# sh /scripts/inotify.sh &
 2 [3] 14149
 3 [root@Client-B html]# ps -ef|grep inotify
 4 root      2585  2478  0 07:20 pts/0    00:00:00 sh inotify.sh
 5 root      2587  2585  0 07:20 pts/0    00:00:00 sh inotify.sh
 6 root     14149  2478  0 07:26 pts/0    00:00:00 sh /scripts/inotify.sh
 7 root     14150 14149  0 07:26 pts/0    00:00:00 /usr/local/inotify-tools-3.14//bin/inotifywait -mrq --timefmt %d/%m/%y %H:%M --format %T %w%f -e close_write,delete,create,attrib /var/www/html/
 8 root     14151 14149  0 07:26 pts/0    00:00:00 sh /scripts/inotify.sh
 9 root     14200  2478  0 07:26 pts/0    00:00:00 grep inotify
10 [root@Client-B html]#

--->测试:

[Client-B:主]:文件服务器

 1 [root@Client-B html]# for i in `seq 10`;do touch $i;done   
 2 [root@Client-B html]# ll
 3 total 0
 4 -rw-r--r-- 1 root root 0 May 31 07:25 1
 5 -rw-r--r-- 1 root root 0 May 31 07:23 1.txt
 6 -rw-r--r-- 1 root root 0 May 31 07:25 10
 7 -rw-r--r-- 1 root root 0 May 31 07:25 2
 8 -rw-r--r-- 1 root root 0 May 31 07:25 3
 9 -rw-r--r-- 1 root root 0 May 31 07:25 4
10 -rw-r--r-- 1 root root 0 May 31 07:25 5
11 -rw-r--r-- 1 root root 0 May 31 07:25 6
12 -rw-r--r-- 1 root root 0 May 31 07:25 7
13 -rw-r--r-- 1 root root 0 May 31 07:25 8
14 -rw-r--r-- 1 root root 0 May 31 07:25 9
15 [root@Client-B html]#

[Center-A:从]:备份服务器

 1 [root@Center-A skyex]# ll
 2 total 0
 3 -rw-r--r-- 1 rsync rsync 0 May 31 07:25 1
 4 -rw-r--r-- 1 rsync rsync 0 May 31 07:23 1.txt
 5 -rw-r--r-- 1 rsync rsync 0 May 31 07:25 10
 6 -rw-r--r-- 1 rsync rsync 0 May 31 07:25 2
 7 -rw-r--r-- 1 rsync rsync 0 May 31 07:25 3
 8 -rw-r--r-- 1 rsync rsync 0 May 31 07:25 4
 9 -rw-r--r-- 1 rsync rsync 0 May 31 07:25 5
10 -rw-r--r-- 1 rsync rsync 0 May 31 07:25 6
11 -rw-r--r-- 1 rsync rsync 0 May 31 07:25 7
12 -rw-r--r-- 1 rsync rsync 0 May 31 07:25 8
13 -rw-r--r-- 1 rsync rsync 0 May 31 07:25 9

至此完毕!

posted @ 2013-08-15 21:22  木子吾雨  阅读(321)  评论(0)    收藏  举报