码迷,mamicode.com
首页 > 其他好文 > 详细

rsync

时间:2016-06-02 00:53:33      阅读:249      评论:0      收藏:0      [点我收藏+]

标签:

一、服务器端:
1、安装Rsync
yum install rsync xinetd
cat /etc/xinetd.d/rsync            #编辑配置文件,设置开机启动rsync
disable = no                          #修改为no
/etc/init.d/xinetd start           
 
2、创建rsyncd.conf配置文件
cat /etc/rsyncd.conf  
log file = /var/log/rsyncd.log
pidfile = /var/run/rsyncd.pid
lock file = /var/run/rsync.lock
secrets file = /etc/rsync.pass
motd file = /etc/rsyncd.Motd
 
[zeus_tasks]           #模块名称
path = /data/rsync/                  #目标路径
comment = home_www.juanpi.com
uid = root
gid = root
port=873
use chroot = no
read only = no
list = no
max connections = 200
timeout = 600
auth users = rsync_backup              #验证用户
hosts allow = 192.168.30.23           #允许的主机
 
3、创建用户认证文件
vi /etc/rsync.pass  
rsync_backup:123456             #格式,用户名:密码,可以设置多个,每行一个用户名:密码
 
4、设置文件权限并启动
chmod 600 /etc/rsyncd.conf    
chmod 600 /etc/rsync.pass    
/etc/init.d/xinetd restart
 

二,客户端:
1,安装Rsync
yum install rsync xinetd
vi /etc/xinetd.d/rsync 
disable = no 
/etc/init.d/xinetd restart  
 
2、创建认证密码文件
vi /etc/passwd.txt         
123456               #密码
chmod 600 /etc/passwd.txt      #设置文件权限
 
 
三、安装Inotify-tools
1、查看服务器内核是否支持inotify
ll /proc/sys/fs/inotify           #列出文件目录,出现下面的内容,说明服务器内核支持inotify
-rw-r--r-- 1 root root 0 Mar 7 02:17 max_queued_events
-rw-r--r-- 1 root root 0 Mar 7 02:17 max_user_instances
-rw-r--r-- 1 root root 0 Mar 7 02:17 max_user_watches
 
2、安装inotify-tools
yum install make gcc gcc-c++  
wget http://github.com/downloads/rvoicilas/inotify-tools/inotify-tools-3.14.tar.gz
tar zxvf inotify-tools-3.14.tar.gz 
cd inotify-tools-3.14 
./configure --prefix=/usr/local/inotify 
make && make install 
 
3、设置系统环境变量,添加软连接
echo ‘export PATH=/usr/local/inotify/bin:$PATH‘ > /etc/profile.d/inotify.sh
source /etc/profile.d/inotify.sh       
echo "/usr/local/inotify/lib" >/etc/ld.so.conf.d/inotify.conf
ln -s /usr/local/inotify/include /usr/include/inotify
 
4、修改inotify默认参数(inotify默认内核参数值太小)
查看系统默认参数值
sysctl -a | grep max_queued_events
结果是:fs.inotify.max_queued_events = 16384
sysctl -a | grep max_user_watches
结果是:fs.inotify.max_user_watches = 8192
sysctl -a | grep max_user_instances
结果是:fs.inotify.max_user_instances = 128
 
修改参数:
sysctl -w fs.inotify.max_queued_events="99999999"
sysctl -w fs.inotify.max_user_watches="99999999"
sysctl -w fs.inotify.max_user_instances="65535"
 
vi /etc/sysctl.conf #添加以下代码
fs.inotify.max_queued_events=99999999
fs.inotify.max_user_watches=99999999
fs.inotify.max_user_instances=65535
 
参数说明:
max_queued_events: inotify队列最大长度,如果值太小,会出现"** Event Queue Overflow **"错误,导致监控文件不准确
max_user_watches:要同步的文件包含多少目录,可以用:find /home/www.osyunwei.com -type d | wc -l 统计,必须保证max_user_watches值大于统计结果(这里/home/www.osyunwei.com为同步文件目录)
max_user_instances:每个用户创建inotify实例最大值
 
5、创建脚本,实时触发rsync进行同步
vi /usr/local/inotify/rsync.sh 
#!/bin/bash
destip="zeus002.jp"                   #destip的ip地址
srcdir=/home/hadoop/juanpi_workspace/software/lib        #本地监控的目录
destdir=lib                         #rsync服务的模块名
log=/tmp/rsync_lib.log
 
user=rsync_backup              #rsync服务的虚拟用户
rsync_passfile=/etc/passwd.txt   #本地调用rsync服务的密码文件
 
#judge
if [ ! -e "$srcdir" ] \
|| [ ! -e "${rsync_passfile}" ] \
|| [ ! -e "/usr/local/inotify/bin/inotifywait" ] \
|| [ ! -e "/usr/bin/rsync" ];
then
echo "Check File and Folder"
exit 9
fi
 
/usr/local/inotify/bin/inotifywait -mrq --timefmt ‘%d/%m/%y %H:%M‘ --format ‘%T %w%f‘ -e close_write,delete,create,attrib $srcdir \
| while read file
do
    #rsync -avzP --delete --timeout=100 --password-file=${rsync_passfile} $src $user@$host01::$dst >/dev/null 2>&1
    cd $srcdir && rsync -aruz -R --delete ./  --timeout=100 $user@$destip::$destdir --password-file=${rsync_passfile} >/dev/null 2>&1
    echo " ${file} was rsynced" >> $log 2>&1
done
 
exit 0
 
脚本参数说明:
srcdir=/home/www.osyunwei.com/      #源服务器同步目录
dstdir=home_www.osyunwei.com      #目标服务器rsync同步目录模块
excludedir=/usr/local/inotify/exclude.list
rsyncuser=home_www.osyunwei.com_user      #目标服务器rsync同步用户名
rsyncpassdir=/etc/passwd.txt                          #目标服务器rsync同步用户的密码在源服务器的存放路径
dstip="192.168.21.127 192.168.21.128"           #目标服务器ip,多个ip用空格分开
/tmp/rsync.log #脚本运行日志记录
 
 
inotify参数
-m 是保持一直监听
-r 是递归查看目录
-q 是打印出事件
-e create,move,delete,modify,attrib 是指 “监听 创建 移动 删除 写入 权限” 事件
 

rsync

标签:

原文地址:http://www.cnblogs.com/tankt101/p/5551570.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!