1.两台机器192.168.1.2,192.168.1.3,想把192.168.1.2的数据同步到192.168.1.3中
2.测试开始,可以先关闭防火墙和内核linux的selinux的防火墙,避免防火墙影响
关闭防火墙,例如centos7,其他系统版本自己查询如何关闭
$ systemctl stop firewalld.service #停止firewall
$ systemctl disable firewalld.service #禁止firewall开机启动
关闭linux的selinux防火墙
永久性关闭:生效需要重启
$ vi /etc/selinux/config
SELINUX=disabled
临时性关闭:生效无需重启
$ setenforce 0
3.安装rsync(两台服务器)
前往rsync官网下载最新版本 http://rsync.samba.org/ftp/rsync/src 找到最新的rsync-*.*.*.tar.gz
$ tar zxvf rsync-*.*.*.tar.gz
$ cd rsync-*.*.*
$ ./configure --prefix=/usr/local/rsync
$ make && make install
4.配置rsyncd.conf (192.168.1.3)
$ vi /etc/rsync.conf
#pid文件的存放位置
pid file = /var/run/rsync.pid
#日志文件位置,启动rsync后自动产生这个文件,无需提前创建
log file = /var/log/rsync.log
#支持max connections参数的锁文件
lock file=/var/run/rsync.lock
#用户认证配置文件,里面保存用户名称和密码
secrets file = /etc/rsync.pw
#rsync启动时欢迎信息页面文件位置
motd file = /etc/rsyncd.motd
transfer logging = yes
log format = %t %a %m %f %b
syslog facility = local3
#自定义名称
[data]
#设置需要同步的目录
path = /data/test/
#模块名称与[data]自定义名称相同
comment = data
exclude = blank.png ; spinner.gif ; downsimple.png ; rails.png ; WEB-INF/
#默认端口
port = 873
#设置rsync运行权限为root
uid = root
#设置rsync运行权限为root
gid = root
#设置超时时间
timeout = 600
#最大连接数
max connections = 200
#默认为true,修改为no,增加对目录文件软连接的备份
use chroot = no
#设置rsync服务端文件为读写权限
read only = no
#不显示rsync服务端资源列表
list = no
#允许进行数据同步的客户端IP地址
hosts allow = 192.168.1.3
可以设置多个目录( 192.168.1.3)
#增加test1目录
[test1]
path = /data/test1
list = yes
ignore errors
comment = ucweb-file system
secrets file = /etc/rsync.pw
exclude = blank.png ; spinner.gif ; downsimple.png ; rails.png ; WEB-INF/
建立密码认证文件( 192.168.1.3)
$ vi /etc/rsync.pw
root:123456
配置rsyncd.motd文件,开始传送的时候会显示(192.168.1.3)
$ vi /etc/rsyncd.motd
###############################
# #
# start rsync #
# #
###############################
5.启动rsync
开机启动rsync
$ echo '/usr/local/rsync/bin/rsync --daemon --config=/etc/rsyncd.conf'>>/etc/rc.d/rc.local
6.建立密码认证文件(192.168.1.2)
$ vi /etc/rsync.pw
123456
7.测试开始
8.安装inotify-tools(192.168.1.2)
$ wget https://codeload.github.com/rvoicilas/inotify-tools/zip/master
$ tar zxvf inotify-tools-*.*.tar.gz
$ cd inotify-tools-*.*
$ ./configure --prefix=/usr/local/inotify
$ make && make install
9.查看是否安装成功
#!/bin/sh
# get the current path
CURPATH=`pwd`
/usr/local/inotify/bin/inotifywait -mr --timefmt '%d/%m/%y %H:%M' --format '%T %w %f' \
-e close_write /data | while read date time dir file; do
FILECHANGE=${dir}${file}
# convert absolute path to relative
FILECHANGEREL=`echo "$FILECHANGE" | sed 's_'$CURPATH'/__'`
rsync -avH --port=873 --progress --delete /data/test/ root@192.168.1.3::data --password-file=/etc/rsync.pw
echo "At ${time} on ${date}, file $FILECHANGE was backed up via rsync"
done