inotify+rsync实现文件实时同步备份

inotify+rsync实现文件实时同步备份

服务器M1: 192.168.1.106
服务器M2: 192.168.1.107

inotify+rsync实现文件实时同步备份

1.安装inotify 与 rsync
M1和M2均安装
安装rsync
[[email protected] ~]# yum -y install xinetd
[[email protected] ~]# yum -y install rsync
[[email protected] ~]# chkconfig rsync on
[[email protected] ~]# service xinetd restart

M1上操作(在M1拉取M2服务器上的107.txt文件,指定接收目录/root):
[[email protected] ~]# rsync -av [email protected]:/root/107.txt /root

M2上操作(在M2将文件推送到M1上,指定接收目录/root,指定端口为22):
[[email protected] ~]# rsync -av -e "ssh -p 22" /root/107test.txt [email protected]:/root

3.双机互信配置(ROOT用户互信)
M1和M2互相保存对方秘钥
M1上操作:
    [[email protected] ~]# ssh-keygen -t rsa -f /root/.ssh/id_rsa -P ''
    [[email protected] ~]# ssh-copy-id -i /root/.ssh/id_rsa.pub [email protected]
M2上操作:
    [[email protected] ~]# ssh-keygen -t rsa -f /root/.ssh/id_rsa -P ''
    [[email protected] ~]# ssh-copy-id -i /root/.ssh/id_rsa.pub [email protected]

在M1上实现无密码登录M2服务器上。
[[email protected] ~]# ssh '[email protected]'
[[email protected] ~]# ssh  [email protected] -t "ls -a"

设置www用户互信
M1上操作:
[[email protected] ~]# su -l www
[[email protected] ~]$ mkdir .ssh
[[email protected] ~]$ chmod 700 .ssh
[[email protected] ~]$ ssh-keygen -t rsa -f /home/www/.ssh/id_rsa -P ''
[[email protected] ~]$ ssh-copy-id -i /home/www/.ssh/id_rsa.pub [email protected]
测试:
[[email protected] ~]$ ssh  [email protected] -t "ls -a" 或
[[email protected] ~]$ ssh '[email protected]'
Last login: Tue Nov 24 17:02:40 2015 from 192.168.1.106
则成功。

M2上操作:
[[email protected] ~]# su -l www
[[email protected] ~]$ mkdir .ssh
[[email protected] ~]$ chmod 700 .ssh
[[email protected] ~]$ ssh-keygen -t rsa -f /home/www/.ssh/id_rsa -P ''
[[email protected] ~]$ ssh-copy-id -i /home/www/.ssh/id_rsa.pub [email protected]
测试:
[[email protected] ~]$ ssh  [email protected] -t "ls -a"
[[email protected] ~]$ ssh '[email protected]'
Last login: Tue Nov 24 17:03:46 2015 from 192.168.1.107
则成功。

4.安装inotify-tools    [发布机上安装,M1作为发布机] 下载地址:http://pan.baidu.com/s/1jGvteZ0
[[email protected] /]# mkdir /data/software
[[email protected] /]# cd /data/software/
[[email protected] software]# rz  【上传inotify-tools-3.14.tar.gz】
[[email protected] software]# tar -zxvf inotify-tools-3.14.tar.gz
[[email protected] software]# cd inotify-tools-3.14
[[email protected] inotify-tools-3.14]# ./configure
...
checking for gcc... no
checking for cc... no
checking for cl.exe... no
出现错误:
解决错误 #yum install -y gcc-c++

[[email protected] inotify-tools-3.14]# ./configure
[[email protected] inotify-tools-3.14]# make
[[email protected] inotify-tools-3.14]# make install

查看inotify-tools是否运行正常:
[[email protected] ~]# inotifywait -m /test
Setting up watches.
Watches established.
新开一个终端:
[[email protected] ~]# cd /test
[[email protected] test]# touch bb.txt

5.写web文件同步脚本。
test.sh 为要运行网站实时同步脚本
其中定义了要同步的网站的路径,要同步到的ip地址,哪些后缀名的文件忽略监控,同步的用户名,同步的文件列表,哪些文件不需要同步。
只需要把test.sh这个文件上传到服务器上并且更改所有者为www,然后加上x的权限,运行即可

在M1上操作
[[email protected]]# mkdir -p /data/conf/shell/inotify_rsync/
[[email protected]]# cd /data/conf/shell/inotify_rsync/
[[email protected]]# rz 【上传test.sh test_inotify_exclude.list test_rsync_include.list test_inotify_exclude.list】
[[email protected]]# chown www:www test.sh test_inotify_exclude.list test_rsync_include.list test_rsync_exclude.list
[[email protected]]# chmod u+x test.sh
[[email protected]]# su -l www
[[email protected] ~]$ cd /data/conf/shell/inotify_rsync/
[[email protected] ~]$ nohup ./test.sh &

在看M2中是否实现相应的文件。

未经允许不得转载:好玩吧 » inotify+rsync实现文件实时同步备份