Rsync配置
注意:
服务端需要打开防火墙的873端口
服务端rsync.scrt的格式为:用户名:密码
客户端的rsync.pas的格式为:密码
rsync可以:service xinetd restart \\ 以xinetd守护进程启动
或者还可以:rsync --daemon /etc/rsyncd.conf \\ 不以xinetd守护进程启动
服务端需要配置文件:
/etc/rsync.conf
/etc/rsync.scrt
客户端需要配置文件:
/etc/rsync.pas
另外客户端密码忘记的权限必须是600
[root@t ~]# chmod 600 /etc/rsyncd.scrt
[root@t ~]# ./rsync.sh
receiving incremental file list
sent 26 bytes received 36 bytes 24.67 bytes/sec
total size is 2502 speedup is 98.36
[root@t ~]#
============================================================
http://jedy82.blog.51cto.com/425872/891341
2012-06-07 15:51:18
原创 服务器
第一:安装前准备
1, 说明:
要求将192.168.1.147的 /usr/gameserver 目录同步到 192.168.1.130的 /usr/gameserver
也就是说 192.168.1.147是rsync 源服务器,192.168.1.130 是rsync 目的服务器
只允许客户端从服务器同步内容到本地 不允许客户端上传数据。如果有多台源服务器要同步到同一台目的服务器上,那么最好是将源服务器设置成客户端,目的服务器设置成服务器端,当然配置文件中要允许写(read only = no)。
在本例中 我们只到一台源服务器和一台目的服务器上,所以两种方法都可以。
本文参照:
http://hi.baidu.com/fiber212121/blog/item/9db4351f7e5fcbc0a68669fc.html(主要参照)
http://www.lingzhong.cn/tech/23765.htm
2, 本文的系统环境
[root@localhost ~]# # uname -a
Linux FWQ-SZ-NSJ-0909-GS-160-147 2.6.32-220.17.1.el6.x86_64 #1 SMP Wed May 16 00:01:37 BST 2012 x86_64 x86_64 x86_64 GNU/Linux
[root@localhost ~]# # more /etc/redhat-release
CentOS release 6.2 (Final)
3, 检查必须的系统组件是否安装
[root@localhost ~]# # rpm -q xinetd openssh-clients
xinetd-2.3.14-33.el6.x86_64
openssh-clients-5.3p1-70.el6_2.2.x86_64
[root@localhost ~]# #
如果没有安装,请自行安装,最方便的方法是:
[root@localhost ~]# #yum -y install xinetd openssh-clients安装
第二:rsync 的安装和配置:
1, 安装软件
[root@localhost ~]# # yum -y install rsync
2, 修改相关配置文件(服务器端)
1) 新建rsync服务器的配置文件
[root@localhost ~]# # vi /etc/rsyncd.conf
#################################脚本开始####################################
全局设置
secrets file = /etc/rsyncd.secrets \\ rsync 同步时密码文件
motd file = /etc/rsyncd.motd \\ 欢迎词存放文件
read only = yes \\ 只读模式(下载模式),只允许客户端下载,不允许上传,适合一对多的同步方式。如果是多对一的同步,请将这里改成no。
list = yes
uid = root \\ 可以自行创建备份用户,也可用root,默认是nobody
gid = root
hosts allow = 192.168.1.130 \\ 允许同步的机器,可以是一个网段
hosts deny = 0.0.0.0/0 \\ 拒绝同步的机器,这里是只允许上面指定的机器
max connections =2 \\ 最大连接数
log file = /var/log/rsyncd.log
pid file = /var/run/rsyncd.pid
lock file = /var/run/rsnc.lock
[gameserver] \\ 定义要同步的目录域
comment = gameserver's directoty form 192.168.1.147 \\ 介绍说明
path = /usr/gameserver
#secrets file = /etc/rsyncd.secrets \\ rsync 同步时密码文件
#motd file = /etc/rsyncd.motd \\ 欢迎词存放文件
auth users = rsync \\ 允许连接用户,使用逗号分隔多个用户
要定义多个目录可以重复上面的格式
#################################脚本结束####################################
2) 新建rsync登陆的欢迎文件
[root@localhost ~]# # vi /etc/rsyncd.motd
#################################脚本开始####################################
Welcome to use the rsync services!
#################################脚本结束####################################
[root@localhost ~]# #
3) 新建rsync登陆的用户名密码文件
[root@localhost ~]# # vi /etc/rsyncd.secrets
#################################脚本开始####################################
用户名:密码
rsync:javjav
#################################脚本结束####################################
[root@localhost ~]# #
4) 新建rsync的服务进程
[root@localhost ~]# # vi /etc/xinetd.d/rsync
#################################脚本开始####################################
# default: off
# description: The rsync server is a good addition to an ftp server, as it \
# allows crc checksumming etc.
service rsync
{
disable = no \\ 由xinetd 进程 启动rsync,如果要由独立的进行启动rsync 那么这里改成yes
flags = IPv6
socket_type = stream
wait = no
user = root
server = /usr/bin/rsync
server_args = --daemon
log_on_failure += USERID
}
#################################脚本结束####################################
[root@localhost ~]# #
5) 更改文件权限
[root@localhost ~]# # chmod 600 /etc/rsyncd.*
6) 启动相关服务
以下两种启动方式任选一种,当然/etc/xinetd.d/rsync 中要做相应的修改
[root@localhost ~]# service xinetd restart \\ 以xinetd守护进程启动
[root@localhost ~]# rsync --daemon /etc/rsyncd.conf \\ 不以xinetd守护进程启动
[root@localhost ~]# echo “rsync --daemon /etc/rsyncd.conf”>> /etc/rc.local 开机自动启动
7) 查看服务端口状态
[root@localhost ~]# netstart -tlnp | grep 873 如果有873 说明服务启动成功
8) 在防火墙中打开873端口
[root@localhost ~]# iptables -A INPUT -s 192.168.1.130 -m state --state NEW -m tcp -p tcp --dport 873 -j ACCEPT
[root@localhost ~]# iptables -A INPUT -s 192.168.1.130 -p udp --dport 873 -j ACCEPT[root@localhost ~]# iptables-save
3, 修改相关配置文件(客户端)
1) 新建rsync登陆的密码文件
这样就可以自动同步了
[root@localhost ~]# vi /etc/rsyncd.secrets
#################################脚本开始####################################
客户端只需密码无须用户
密码
javjav
#################################脚本结束####################################
2) 更改文件权限
[root@localhost ~]# chmod 600 /etc/rsyncd.*
3) 在防火墙中打开873端口
iptables -A INPUT -d 192.168.1.147 -m state --state NEW -m tcp -p tcp --dport 873 -j ACCEPT
iptables -A INPUT -d 192.168.1.147 -p udp --dport 873 -j ACCEPT
4, 在客户端进行测试
[root@localhost ~]# rsync -azvp rsync@192.168.1.147::gameserver /usr/gameserver
rsync -参数 用户名@同步服务器的IP::rsyncd.conf中那个方括号里的内容 本地存放路径
说明:
-a 参数,相当于-rlptgoD,-r 是递归 -l 是链接文件,意思是拷贝链接文件;-p 表示保持文件原有权限;-t 保持文件原有时间;-g 保持文件原有用户组;-o 保持文件原有属主;-D 相当于块设备文件;
-z 传输时压缩;
-P 传输进度;
-v 传输时的进度等信息,和-P有点关系,自己试试。可以看文档;
可以将以上命令加入到crontab中自动执行 也可以加入到脚本中。
4, 其它说明(错误解决)
1)报错 rsync: failed to set permissions on 和rsync error:如下
rsync -参数 本地存放路径 用户名@同步服务器的IP::rsyncd.conf中那个方括号里的内容
[root@localhost ~]# rsync -azvp --delete --exclude-from=/etc/exclude.list --password-file=/etc/rsync.pass /usr/gameserver/ rsync@192.168.1.147::gameserver
sending incremental file list
./
rsync: failed to set permissions on "." (in web): Permission denied (13)sent 43614 bytes received 93 bytes 5142.00 bytes/sec
total size is 108128586 speedup is 2473.94
rsync error: some files/attrs were not transferred (see previous errors) (code 23) at main.c(1039) [sender=3.0.6]
[root@localhost ~]#以上错误常常是在linux 和windows 之间出现的,此错误的原因是因为 在linux目录下有 “.”这个目录 而在windows下没有,所以要想同步整个目录 要加一个星号(*)。如下
[root@localhost ~]# rsync -azvp --delete --exclude-from=/etc/exclude.list --password-file=/etc/rsync.pass /usr/gameserver/* rsync@192.168.1.147::gameserver
windows 下的rsync安装请上baidu自行查找,很多的。也可以参照http://hi.baidu.com/cong_rong520/blog/item/e42ac64fd6fb3cf4d62afcbc.html
在linux和windows进行同步时,有一些地方要注意一下:如
本文出自 “jedy 网络技术&linux学习” 博客,请务必保留此出处http://jedy82.blog.51cto.com/425872/891341
@ERROR: chdir failed
rsync error: error starting client-server protocol (code 5) at main.c(1296) [receiver=2.6.8]
原因及解决办法:
SELinux;
setsebool -P rsync_disable_trans on
@ERROR: auth failed on module GacErrLog
rsync error: error starting client-server protocol (code 5) at main.c(1296) [sender=2.6.8]
原因:
rsynd里面配置passwd是
username:password 这样的格式
客户端使用的时候,不能用这种格式,文件里面只能有password
解决方法:
1. 文件内容修改
2. rsync -av --password-file <(echo PASSWORD) src dest
1 架设rsync服务器过程;
|
直接保存,chmod权限就可以用了 (脚本改自centosbz的)
- #!/bin/bash
- echo "Please input the rsync username:"
- read username
- echo "Please input the rsync username password:"
- read password
- echo "Please input the server ip address:"
- read serverip
- echo "Please input the allow ip address:"
- read allowip
- echo "Please input the path you want to rsync:"
- read rsyncpath
- echo "==========================input all completed========================"
- echo "==========================install rsync========================"
- #wget http://samba.anu.edu.au/ftp/rsync/src/rsync-3.0.7.tar.gz
- yum -y install rsync
- useradd $username
- mkdir /etc/rsyncd
- cat >/etc/rsyncd/rsyncd.conf<<eof
- # Minimal configuration file for rsync daemon
- # See rsync(1) and rsyncd.conf(5) man pages for help
- # This line is required by the /etc/init.d/rsyncd script
- pid file = /var/run/rsyncd.pid
- port = 873
- address = $serverip
- #uid = nobody
- #gid = nobody
- uid = root
- gid = root
- use chroot = yes
- read only = yes
- #limit access to private LANs
- hosts allow=192.168.1.0/255.255.255.0 10.0.1.0/255.255.255.0 $allowip
- hosts deny=*
- max connections = 5
- motd file = /etc/rsyncd/rsyncd.motd
- #This will give you a separate log file
- #log file = /var/log/rsync.log
- #This will log every file transferred - up to 85,000+ per user, per sync
- #transfer logging = yes
- log format = %t %a %m %f %b
- syslog facility = local3
- timeout = 300
- [$username home]
- path = $rsyncpath
- list=yes
- ignore errors
- auth users = $username
- secrets file = /etc/rsyncd/rsyncd.secrets
- eof
- echo "$username:$password" > /etc/rsyncd/rsyncd.secrets
- chmod 600 /etc/rsyncd/rsyncd.secrets
- cat >/etc/rsyncd/rsyncd.motd<<eof
- hello gugoo...
- eof
- /usr/bin/rsync --daemon --config=/etc/rsyncd/rsyncd.conf
- echo "/usr/bin/rsync --daemon --config=/etc/rsyncd/rsyncd.conf" >>/etc/rc.d/rc.local
- echo "iptables -A INPUT -p tcp -m state --state NEW -m tcp --dport 873 -j ACCEPT" >>/etc/rc.d/rc.local
- ps -aux | grep rsync
客户端:
- touch rsync.password
- chmod 600 rsync.password
- echo "222222"> =/home/ehagames/pass14
修改了配置:
- pkill rsync
- /usr/bin/rsync --daemon --config=/etc/rsyncd/rsyncd.conf
本文出自 “峰云,就她了。” 博客,请务必保留此出处http://rfyiamcool.blog.51cto.com/1030776/876385
http://www.blogjava.net/baizhihui19870626/articles/379460.html
[root@Hammer home]# rpm -qa |grep rsync #检查系统是否安装了rsync软件包
rsync-2.6.8-3.1
[root@Hammer CentOS]# rpm -ivh rsync-2.6.8-3.1.i386.rpm # 如果没有安装则手动安装
[root@test rsync-3.0.4]# vim /etc/xinetd.d/rsync
1 配置rsync servervi /etc/xinetd.d/rsync
将disable=yes改为no
service rsync
{
disable = no
socket_type = stream
wait = no
user = root
server = /usr/bin/rsync
server_args = --daemon
log_on_failure += USERID
}
2 配置rsync自动启动
[root@test etc]# chkconfig rsync on
[root@test etc]# chkconfig rsync --list
rsync on
3 配置rsyncd.conf
[root@test etc]# vim rsyncd.conf
uid = root
gid = root
use chroot = no
max connections = 4
strict modes = yes
port = 873
pid file = /var/run/rsyncd.pid
lock file = /var/run/rsync.lock
log file = /var/log/rsyncd.log
[backup]
path = /srv
comment = This is test
auth users = scihoo
uid = root
gid = root
secrets file = /home/rsync.ps
read only = no
list = no
4 确保etc/services中rsync端口号正确
[root@test etc]# vim /etc/services
rsync 873/tcp # rsync
rsync 873/udp # rsync
5 配置rsync密码(在上边的配置文件中已经写好路径)/home/rsync.ps(名字随便写,只要和上边配置文件里的一致即可),格式(一行一个用户)
[root@test etc]# vi /home/rsync.ps
scihoo:scihoo
6 配置rsync密码文件权限
[root@test home]# chown root.root rsync.ps
[root@test home]# chmod 400 rsync.ps
7 启动配置
[root@test home]# /etc/init.d/xinetd restart
Stopping xinetd: [ OK ]
Starting xinetd: [ OK ]
8 如果xinetd没有的话,需要安装一下
[root@test home]# yum -y install xinetd
启动rsync server
RSYNC服务端启动的两种方法
9、启动rsync服务端(独立启动)
[root@test home]# /usr/bin/rsync --daemon
10、启动rsync服务端 (有xinetd超级进程启动)
[root@test home]# /etc/init.d/xinetd reload
11 加入rc.local
在各种操作系统中,rc文件存放位置不尽相同,可以修改使系统启动时把rsync --daemon加载进去。
[root@test home]# vi /etc/rc.local
/usr/local/rsync –daemon #加入一行
12 检查rsync是否启动
[root@test home]# lsof -i :873
COMMAND PID USER FD TYPE DEVICE SIZE NODE NAME
xinetd 4396 root 5u IPv4 633387 TCP *:rsync (LISTEN)
客户端配置
1 配置三个过程就可以了
1.1 设定密码文件
1.2 测试rsync执行指令
1.3 将rsync指令放入工作排程(crontab)
[root@aj1 home]# vi /etc/xinetd.d/rsync
# default: off
# description: The rsync server is a good addition to an ftp server, as it \
# allows crc checksumming etc.
service rsync
{
disable = yes
socket_type = stream
wait = no
user = root
server = /usr/bin/rsync
server_args = --daemon
log_on_failure += USERID
}
1.1 配置密码文件 (注:为了安全,设定密码档案的属性为:600。rsync.ps的密码一定要和Rsync Server密码设定案里的密码一样)
[root@aj1 home]# vi rsync.ps
sciooo
[root@aj1 home]# chown root.root .rsync.ps # 注意必须给权限
[root@aj1 home]# chmod 600 .rsync.ps # 必须修改权限
1.2 从服务器上下载文件
[root@aj1 rsync-3.0.4]# rsync -avz --password-file=/home/rsync.ps scihoo@192.168.0.206::backup /home/
从本地上传到服务器上去
[root@aj1 rsync-3.0.4]# rsync -avz --password-file=/home/rsync.ps /home scihoo@192.168.0.206::backup
小白
Rsync+Inotify所构成的系统,对于没有共享存储的童鞋们来说,无疑是一大优势。
所需软件:rsync http://pkgs.repoforge.org/rsync/
inotify http://jaist.dl.sourceforge.net/ ... y-tools-3.13.tar.gz
环境:
服务器端(代码更新服务器):192.168.10.15
客户端(web服务器):192.168.10.16 192.168.10.17 192.168.10.18
WEB目录:/data/httpd/wwwroot/
原理:
由192.168.10.15上inotify服务监测文件是否有更新,如果有更新(修改,删除,新建)inotify就会通过rsync命令将更新的文件推向三台web服务器
安装:
更新源服务器(192.168.10.15)需安装rsync+inotify即可;剩余三台WEB仅安装rsync即可。
# tar zxvf rsync-3.0.6pre1.tar.gz
# cd rsync-3.0.6pre1
# ./configure --prefix=/usr/local/rsync && make && make install
# tar zxvf inotify-tools-3.13.tar.gz
# cd inotify-tools-3.13
# ./configure --prefix=/usr/local/inotify && make && make install
配置:
更新源服务器:
1、设置脚本
#mkdir /root/bin
#cd /root/bin
#vim rsync.sh
#!/bin/bash
src=/data/httpd/wwwroot/
des=www
host="192.168.10.16 192.168.10.17 192.168.10.18"
/usr/local/bin/inotifywait -mrq --timefmt '%d/%m/%y %H:%M' --format '%T %w%f' -e modify,delete,create,attrib $src | while read files
do
for hostip in $host
do
rsync -vzrtopg --delete --progress --password-file=/etc/rsyncd.secrets $src root@$hostip::$des
done
echo "${files} was rsynced" >>/tmp/rsync.log 2>&1
done
#mkdir -p /data/httpd/wwwroot #创建web目录
#chmod +x rsync.sh
启动:nohup sh rsync.sh
2、创建rsync认证文件
#vim /etc/rsyncd.secrets
123456
三台WEB服务器的配置:
#vim /etc/rsyncd.conf #配置文件
uid = root
gid = root
use chroot = no
max connections = 5
pid file = /var/run/rsyncd.pid
lock file = /var/run/rsync.lock
log file = /var/log/rsyncd.log
[www]
path=/data/httpd/wwwroot/
comment = update
ignore errors
read only = no
list = no
hosts allow = 192.168.10.0/255.255.255.0
auth users = root
uid = root
gid = root
secrets file = /etc/rsyncd.secrets
#vim /etc/rsyncd.secrets #创建rsync证文件
123456
root:123456
#chmod 0600 /etc/rsyncd.secrets #设置权限
#rsync --daemon #启动服务
测试:
在源服务器/data/www/wwwroot/新建文件abc,三台WEB服务器即时更新过去了。。
rsync和inotify的说明:
脚本相关注解:
-m 是保持一直监听
-r 是递归查看目录
-q 是打印出事件~
-e create,move,delete,modify
监听 创建 移动 删除 写入 事件
rsync -aHqzt $SRC $DST
-a 存档模式
-H 保存硬连接
-q 制止非错误信息
-z 压缩文件数据在传输
-t 维护修改时间
-delete 删除于多余文件
当要排出同步某个目录时,为rsync添加--exculde=PATTERN参数,注意,路径是相对路径。详细查看man rsync
当要排除都某个目录的事件监控的处理时,为inotifywait添加--exclude或--excludei参数。详细查看man inotifywait
另:
/usr/local/bin/inotifywait -mrq --timefmt '%d/%m/%y %H:%M' --format '%T %w%f' \
-e modify,delete,create,attrib \
${src} \
上面的命令返回的值类似于:
10/03/09 15:31 /wwwpic/1
这3个返回值做为参数传给read,关于此处,有人是这样写的:
inotifywait -mrq -e create,move,delete,modify $SRC | while read D E F;do
细化了返回值。
说明: 当文件系统发现指定目录下有如上的条件的时候就触发相应的指令,是一种主动告之的而非我用循环比较目录下的文件的异动,该程序
在运行时,更改目录内的文件时系统内核会发送一个信号,这个信号会触发运行rsync命令,这时会同步源目录和目标目录。
--timefmt:指定输出时的输出格式
--format: '%T %w%f'指定输出的格式
二.关于inotify介绍
Inotify 是文件系统事件监控机制,作为 dnotify 的有效替代。dnotify 是较早内核支持的文件监控机制。Inotify 是一种强大的、细粒度的
、异步的机制,它满足各种各样的文件监控需要,不仅限于安全和性能。
inotify 可以监视的文件系统事件包括:
IN_ACCESS,即文件被访问
IN_MODIFY,文件被 write
IN_ATTRIB,文件属性被修改,如 chmod、chown、touch 等
IN_CLOSE_WRITE,可写文件被 close
IN_CLOSE_NOWRITE,不可写文件被 close
IN_OPEN,文件被 open
IN_MOVED_FROM,文件被移走,如 mv
IN_MOVED_TO,文件被移来,如 mv、cp
IN_CREATE,创建新文件
IN_DELETE,文件被删除,如 rm
IN_DELETE_SELF,自删除,即一个可执行文件在执行时删除自己
IN_MOVE_SELF,自移动,即一个可执行文件在执行时移动自己
IN_UNMOUNT,宿主文件系统被 umount
IN_CLOSE,文件被关闭,等同于(IN_CLOSE_WRITE | IN_CLOSE_NOWRITE)
IN_MOVE,文件被移动,等同于(IN_MOVED_FROM | IN_MOVED_TO)
注:上面所说的文件也包括目录。
root:123456
#chmod u+x /etc/rsyncd.secrets
3、启动
#nohup sh /root/bin/rsync.sh &
本文出自 “double.51.com” 博客,请务必保留此出处http://379162.blog.51cto.com/369162/896606