------------------------------以下部分在两个节点上执行
1、防火墙开放873端口(两个节点)
# vim /etc/sysconfig/iptables -A INPUT -p tcp --dport 873 -j ACCEPT # /etc/init.d/iptables restart # /etc/init.d/iptables save
2、关闭selinux(两个节点)
# sed -i "s/SELINUX=.*/SELINUX=disabled/g" /etc/selinux/config # cat /etc/selinux/config
3、建立所需目录,上传软件到soft下(两个节点)
# mkdir -p /usr/local/download (软件下载目录) # mkdir -p /usr/local/backup (备份文件放置目录) # mkdir -p /usr/local/script (脚本目录)
安装相关依赖包(两个节点)
# yum -y install gcc gcc-c++ make perl wget
安装rsync(两个节点)
# cd /usr/local/download # wget https://www.samba.org/ftp/rsync/src/rsync-3.1.2.tar.gz # tar xf rsync-3.1.2.tar.gz # cd rsync-3.1.2 # ./configure --prefix=/usr/local/rsync-3.1.2 # echo $? # make # echo $? # make install && echo $? # ln -s /usr/local/rsync-3.1.2/ /usr/local/rsync
------------------------------以下在备份节点上配置
1、建立配置文件(备份节点)
# vim /etc/rsync.conf port = 873 log file=/var/log/rsync.log pid file=/var/run/rsync.pid lock file=/var/run/rsync.lock secrets file=/etc/rsync.passwd motd file = /etc/rsyncd.motd transfer logging = yes log format = %t %a %m %f %b syslog facility = local3 #hosts deny= [backup] comment= backup path=/usr/local/backup use chroot = no max connections=4 read only = no uid=root gid=root list = no exclude=test auth users=work #hosts allow = 172.16.0.233
添加如下内容:
#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
#自定义名称
[backup]
#设置需要同步的目录
web path=/usr/local/backup
#模块名称与[backup]自定义名称相同
comment = backup
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 = 172.16.0.233
#可以设置多个目录
2、建立密码认证文件(备份节点)
# vim /etc/rsync.pw root:123456
3、设置权限(备份节点)
# chmod 600 /etc/rsync.conf # chmod 600 /etc/rsync.pw
4、配置rsyncd.motd文件,开始传送的时候会显示(备份节点)
# vi /etc/rsyncd.motd
输入以下内容:
###############################
# #
# hello everyone #
# #
###############################
_____ &&&&_) )
\/,---< &&&&&&\ \
( )c~c~~@~@ ) — —&&\ \
C >/ \< |&/
\_O/ - _`*-‘_/ /
,- >o<-. / ____ _/
/ \/ \ / /\ _) _)
/ /| | |\ \ / / ) |
\ \| | |/ / \ \ / |
\_\ | |_/ \ \_ |
/_/`___|_\ /_/\____|
| | | \ \ |
| | | `. )
| | | / /
|__|_|_ /_/|
(____)_) |\_\_
5、启动rsync服务(备份节点)
# /usr/local/rsync/bin/rsync --daemon --config=/etc/rsync.conf # ps -ef |grep rsync|grep -v grep root 38142 1 0 19:36 ? 00:00:00 /usr/local/rsync/bin/rsync --daemon --config=/etc/rsync.conf
6、设置开机启动(备份节点)
# vim /etc/rc.local /usr/local/rsync/bin/rsync --daemon --config=/etc/rsync.conf
------------------------------以下在源节点执行
1、建立密码认证文件(源节点)
# vim /etc/rsync.pw 123456
2、设置权限(源节点)
# chmod 600 /etc/rsync.pw
3、测试(源节点)
# mkdir /usr/local/backup # /usr/local/rsync/bin/rsync -avzP --port=873 /usr/local/backup/ root@172.16.0.234::backup --password-file=/etc/rsync.pw
查看172.16.0.234 上是否有相应的目录
4、安装inotify-tools(源节点)
Linux下支持inotify的内核最小为2.6.13
4.1、查看是否支持inotify,有如下三个文件则表示支持(源节点)
# ll /proc/sys/fs/inotify -rw-r--r-- 1 root root 0 Aug 17 12:49 max_queued_events -rw-r--r-- 1 root root 0 Aug 17 12:49 max_user_instances -rw-r--r-- 1 root root 0 Aug 17 12:49 max_user_watches
4.2、安装inotify-tools(源节点)
# cd /usr/local/download/ # wget http://github.com/downloads/rvoicilas/inotify-tools/inotify-tools-3.14.tar.gz # tar xf inotify-tools-3.14.tar.gz # cd inotify-tools-3.14 # ./configure --prefix=/usr/local/inotify-tools-3.14 # echo $? # make # echo $? # make install && echo $? # ln -s /usr/local/inotify-tools-3.14/ /usr/local/inotify-tools
4.3、查看是否支持,显示如下表示支持(源节点)
# ll /usr/local/inotify-tools/bin/inotifywa* -rwxr-xr-x 1 root root 44287 Aug 17 12:54 /usr/local/inotify-tools/bin/inotifywait -rwxr-xr-x 1 root root 41409 Aug 17 12:54 /usr/local/inotify-tools/bin/inotifywatch
5、修改内核参数(源节点)
# vim /etc/sysctl.conf fs.inotify.max_queued_events=99999999 fs.inotify.max_user_watches=99999999 fs.inotify.max_user_instances=65535
添加以下参数:
#inotify队列最大长度,如果值太小,会出现"** Event Queue Overflow **"错误,导致监控文件不准确
fs.inotify.max_queued_events=99999999
#要同步的文件包含多少目录
fs.inotify.max_user_watches=99999999
#每个用户创建inotify实例最大值
fs.inotify.max_user_instances=65535
6、编写监控脚本(源节点)
# vim /usr/local/script/inotify.sh #!/bin/bash #目标服务器ip,多个ip用空格分开 dstip="172.16.0.234" #源服务器同步目录 src=/usr/local/backup #目标服务器rsync同步目录模块名称 dst=backup #目标服务器rsync同步用户名 user=root #目标服务器rsync同步用户的密码在源服务器的存放路径 pwdir=/etc/rsync.pw #目标服务器rsync同步日志文件 logfile=/var/log/rsync.log /usr/local/inotify-tools/bin/inotifywait -mrq --timefmt ‘%d/%m/%y %H:%M‘ --format ‘%T %w%f%e‘ -e modify,delete,create,attrib,move $src | while read file do for ip in $dstip do /usr/local/rsync/bin/rsync -avzP --port=873 $src $user@$ip::$dst --password-file=$pwdir echo "${file} was rsynced" >>$logfile 2>&1 done done
7、设置脚本权限并启动脚本(源节点)
# chmod 700 /usr/local/script/inotify.sh # sh /usr/local/script/inotify.sh &
8、设置开机启动(源节点)
# echo "/usr/local/script/inotify.sh & ">>/etc/rc.local
9、测试
在源服务器上/usr/local/backup 下创建文件,查看相应的目标服务器是否有文件
原文地址:http://dongxin.blog.51cto.com/3486403/1839554