inotify+rsync实现文件实时同步备份
主服务器M1: 192.168.1.106
从服务器M2: 192.168.1.107
1.安装inotify 与 rsync
M1和M2均安装
安装rsync
[root@localhost ~]# yum -y install xinetd
[root@localhost ~]# yum -y install rsync
[root@localhost ~]# chkconfig rsync on
[root@localhost ~]# service xinetd restart
M1上操作(在M1拉取M2服务器上的107.txt文件,指定接收目录/root):
[root@localhost ~]# rsync -av root@192.168.1.107:/root/107.txt /root
M2上操作(在M2将文件推送到M1上,指定接收目录/root,指定端口为22):
[root@localhost ~]# rsync -av -e “ssh -p 22” /root/107test.txt root@192.168.1.106:/root
3.双机互信配置(ROOT用户互信)
M1和M2互相保存对方秘钥
M1上操作:
[root@localhost ~]# ssh-keygen -t rsa -f /root/.ssh/id_rsa -P ”
[root@localhost ~]# ssh-copy-id -i /root/.ssh/id_rsa.pub root@192.168.1.107
M2上操作:
[root@localhost ~]# ssh-keygen -t rsa -f /root/.ssh/id_rsa -P ”
[root@localhost ~]# ssh-copy-id -i /root/.ssh/id_rsa.pub root@192.168.1.106
在M1上实现无密码登录M2服务器上。
[root@localhost ~]# ssh ‘root@192.168.1.107’
[root@localhost ~]# ssh root@192.168.1.107 -t “ls -a”
设置www用户互信
M1上操作:
[root@localhost ~]# su -l www
[www@localhost ~]$ mkdir .ssh
[www@localhost ~]$ chmod 700 .ssh
[www@localhost ~]$ ssh-keygen -t rsa -f /home/www/.ssh/id_rsa -P ”
[www@localhost ~]$ ssh-copy-id -i /home/www/.ssh/id_rsa.pub www@192.168.1.107
测试:
[www@localhost ~]$ ssh www@192.168.1.107 -t “ls -a” 或
[www@localhost ~]$ ssh ‘www@192.168.1.107’
Last login: Tue Nov 24 17:02:40 2015 from 192.168.1.106
则成功。
M2上操作:
[root@localhost ~]# su -l www
[www@localhost ~]$ mkdir .ssh
[www@localhost ~]$ chmod 700 .ssh
[www@localhost ~]$ ssh-keygen -t rsa -f /home/www/.ssh/id_rsa -P ”
[www@localhost ~]$ ssh-copy-id -i /home/www/.ssh/id_rsa.pub www@192.168.1.106
测试:
[www@localhost ~]$ ssh www@192.168.1.106 -t “ls -a”
[www@localhost ~]$ ssh ‘www@192.168.1.106’
Last login: Tue Nov 24 17:03:46 2015 from 192.168.1.107
则成功。
4.安装inotify-tools [发布机上安装,M1作为发布机] 下载地址:http://pan.baidu.com/s/1jGvteZ0
[root@localhost /]# mkdir /data/software
[root@localhost /]# cd /data/software/
[root@localhost software]# rz 【上传inotify-tools-3.14.tar.gz】
[root@localhost software]# tar -zxvf inotify-tools-3.14.tar.gz
[root@localhost software]# cd inotify-tools-3.14
[root@localhost inotify-tools-3.14]# ./configure
…
checking for gcc… no
checking for cc… no
checking for cl.exe… no
出现错误:
解决错误 #yum install -y gcc-c++
[root@localhost inotify-tools-3.14]# ./configure
[root@localhost inotify-tools-3.14]# make
[root@localhost inotify-tools-3.14]# make install
查看inotify-tools是否运行正常:
[root@localhost ~]# inotifywait -m /test
Setting up watches.
Watches established.
新开一个终端:
[root@localhost ~]# cd /test
[root@localhost test]# touch bb.txt
5.写web文件同步脚本。
test.sh 为要运行网站实时同步脚本
其中定义了要同步的网站的路径,要同步到的ip地址,哪些后缀名的文件忽略监控,同步的用户名,同步的文件列表,哪些文件不需要同步。
只需要把test.sh这个文件上传到服务器上并且更改所有者为www,然后加上x的权限,运行即可
在M1上操作
[root@localhost]# mkdir -p /data/conf/shell/inotify_rsync/
[root@localhost]# cd /data/conf/shell/inotify_rsync/
[root@localhost]# rz 【上传test.sh test_inotify_exclude.list test_rsync_include.list test_inotify_exclude.list】
[root@localhost]# chown www:www test.sh test_inotify_exclude.list test_rsync_include.list test_rsync_exclude.list
[root@localhost]# chmod u+x test.sh
[root@localhost]# su -l www
[www@localhost ~]$ cd /data/conf/shell/inotify_rsync/
[www@localhost ~]$ nohup ./test.sh &
在看M2中是否实现相应的文件。
顶下再看哈