系统环境:centos 6.4
需要软件:inotify,rsync
主机规划:172.16.1.220 172.16.1.218
需求:将172.16.1.218上面的/usr/local/218backup文件夹下面的数据实时同步到/usr/local/220backup文件夹下面,(生产环境慎用,因为一台服务器上面的数据丢失,可能导致远程备份服务器上面的数据也丢失)。
步骤:
首先在两台主机上面分别安装rsync软件,然后在客户端,也就是218上面安装inotify软件
在172.16.1.218主机上:
[root@218 rsyncd]# ls
rsyncd.secrets rsyncd.secretsbak
[root@218 rsyncd]# pwd
/etc/rsyncd
[root@218 rsyncd]# vim rsyncd.secrets
12345678
[root@218 rsyncd]#chmod 600 /etc/rsyncd/rsyncd.secrets
安装inotify软件,安装在configure及编译路径指向/usr/local/inotify下面
源码安装编译,直到OK
OK之后,inotify文件夹下会有四个目录,如下:
[root@218 inotify]# ls
bin include lib logs
bin目录下有两个命令:
[root@218 bin]# ls
inotifywait inotifywatch
安装已经OK了,下面需要编写同步脚本,脚本如下:
[root@218 local]# vim inotify_rsync.sh
#!/bin/sh
#date:2013-1-11
#function:rysnc 172.16.1.218 to 172.16.1.220
if [ ! -f /etc/rsyncd/rsyncd.secrets ];then
echo "12345678">/etc/rsyncd/rsyncd.secrets
/bin/chmod 600 /etc/1.pas
fi
log=/usr/local/inotify/logs/rsync.log
src="/usr/local/218backup/"
host="172.16.1.220"
module="220backup"
/usr/local/inotify/bin/inotifywait -mr --timefmt '%d/%m/%y %H:%M' --format '%T %w %f' -e close_write,modify,delete,create,attrib $src | while read DATE TIME DIR FILE; do
FILECHANGE=${DIR}${FILE}
/usr/bin/rsync -avH --delete --progress --password-file=/etc/rsyncd/rsyncd.secrets $src --exclude-from="/usr/local/inotify/logs/rules.txt" rsync@$host::$module &
echo "At ${TIME} on ${DATE}, file $FILECHANGE was backed up via rsync" >> $log
done
在主机220上面需要做如下配置:
首先:安装好rsync软件,确认无误后启动程序
[root@backup]#rsync --daemon
[root@backup 220backup]# netstat -antup|grep 873
tcp 0 0 0.0.0.0:873 0.0.0.0:* LISTEN 9857/rsync
tcp 0 0 :::873 :::* LISTEN 9857/rsync
873端口已经开启了,代表rsync安装成功。
它的配置文件需要手动建立,如下所示。
[root@backup 220backup]# vim /etc/rsyncd.conf
pid file = /var/run/rsyncd.pid
lock file = /var/run/rsync.lock
log file = /var/log/rsyncd.log
port = 873
read only = no
max connections = 10
uid = root
gid = root
use chroot = yes
auth users = rsync
secrets file = /etc/rsyncd/rsyncd.secrets
[apache]
hosts allow = 172.16.1.0/24
path = /usr/local/backup/
[220backup]
path = /usr/local/220backup/
hosts allow = 172.16.1.0/24
#[blogweb02]
#hosts allow = 192.168.71.0/24
#path = /usr/local/apache2/htdocs/
#[bbs-nginx]
#hosts allow = 192.168.71.0/24
#path = /usr/local/apache2/htdocs/
同步之前先在218上面开启同步脚本,脚本路径在/usr/local/inotify_rsync.sh
[root@218 218backup]# pwd 却换到218backup文件夹下
/usr/local/218backup
[root@218 218backup]# touch {1..100}
[root@218 218backup]# ls
1 12 16 2 23 27 30 34 38 41 45 49 52 56 6 63 67 70 74 78 81 85 89 92 96
10 13 17 20 24 28 31 35 39 42 46 5 53 57 60 64 68 71 75 79 82 86 9 93 97
100 14 18 21 25 29 32 36 4 43 47 50 54 58 61 65 69 72 76 8 83 87 90 94 98
11 15 19 22 26 3 33 37 40 44 48 51 55 59 62 66 7 73 77 80 84 88 91 95 99
在这个目录下面建立100个文件
然后在220上面登录,查看/usr/local/220backup文件夹下面是否有文件
[root@backup 220backup]# ifconfig
eth0 Link encap:Ethernet HWaddr 00:0C:29:7B:72:F9
inet addr:172.16.1.220 Bcast:172.16.1.255 Mask:255.255.255.0
inet6 addr: fe80::20c:29ff:fe7b:72f9/64 Scope:Link
UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1
RX packets:10652680 errors:0 dropped:0 overruns:0 frame:0
TX packets:5458288 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:1000
RX bytes:15093282373 (14.0 GiB) TX bytes:323043775 (308.0 MiB)
lo Link encap:Local Loopback
inet addr:127.0.0.1 Mask:255.0.0.0
inet6 addr: ::1/128 Scope:Host
UP LOOPBACK RUNNING MTU:16436 Metric:1
RX packets:264 errors:0 dropped:0 overruns:0 frame:0
TX packets:264 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:0
RX bytes:18961 (18.5 KiB) TX bytes:18961 (18.5 KiB)
[root@backup 220backup]# pwd
/usr/local/220backup
[root@backup 220backup]# ls
1 12 16 2 23 27 30 34 38 41 45 49 52 56 6 63 67 70 74 78 81 85 89 92 96
10 13 17 20 24 28 31 35 39 42 46 5 53 57 60 64 68 71 75 79 82 86 9 93 97
100 14 18 21 25 29 32 36 4 43 47 50 54 58 61 65 69 72 76 8 83 87 90 94 98
11 15 19 22 26 3 33 37 40 44 48 51 55 59 62 66 7 73 77 80 84 88 91 95 99
同时,在172.16.1.220下面也建立了100个文件,实验成功。
然后尝试在172.16.1.218下面的/usr/local/218backup文件夹下删除所有的文件。
[root@218 218backup]# rm -f *
[root@218 218backup]# ls
在查看220backup下面的文件是否还存在
[root@backup 220backup]# ls
[root@backup 220backup]#
220backup下面的文件已经被同步删除了,实验成功。
QQ交流:406564728