Chinaunix首页 | 论坛 | 博客
  • 博客访问: 266186
  • 博文数量: 53
  • 博客积分: 2580
  • 博客等级: 少校
  • 技术积分: 509
  • 用 户 组: 普通用户
  • 注册时间: 2009-12-29 10:02
文章分类

全部博文(53)

文章存档

2014年(1)

2013年(1)

2011年(14)

2010年(37)

我的朋友

分类: LINUX

2010-06-18 15:00:46

     rsync是一个很强大的文件同步工具,基本特点如下:

  1.可以镜像保存整个目录树和文件系统;

  2.可以很容易做到保持原来文件的权限、时间、软硬链接等;

  3.无须特殊权限即可安装;

  4.优化的流程,文件传输效率高;

  5.可以使用rsh、ssh等方式来传输文件,当然也可以通过直接的socket连接;

  6.支持匿名传输。

   由于服务器数量较多,并且会经常有更新等需求,在目前还没有完全应用cfengine的情况下,我主要使用rsync保证服务器处于便于管理的状态

   简单来说,我会在每台linux下新建一个tool目录(这个在系统初始化时就会完成,并且初始化时会自动更新为最新版),里面有大量系统日志分析等各种脚本,有一个脚本,名为rsync.sh,再在后面加不同的参数,从不同的rsync源站来同步各个不同的需求。当我有了新的需求,只需要在每台机器上运行rsync.sh tool  就可以更新tool内内容了,包括rsync.sh这个文件。

    同步其实很简单:就一句话,但结合着shell就可以做很多事情:            rsync -vzrtogp --progress  IP::test /home/test/tool

如果写到crond就可以实现自动更新了,当然前提要保证源站的准确性!

部署rsync很简单,在/etc下创建rsyncd.conf文件,内容如下:(最简单的)  

uid = nobody
gid = nobody
use chroot = no
max connections = 200
port = 873
pid file = /var/run/rsyncd.pid
lock file = /var/run/rsync.lock
log file = /var/log/rsyncd.log
[tool]
path = /home/test/tool
read only = yes
uid=0  //如果你要同步crond之类的东西,就需要使用了,不然会提示权限拒绝
gid=0  //如果你要同步crond之类的东西,就需要使用了,不然会提示权限拒绝         另外这个还可以做权限认证,比喻需要用户名和密码来保证安全等

 

然后修改/etc/xinetd.d/rsync,把disable = yes  改为disable = no,重启服务:service xinetd restart  (这个是以守护模式运行,当然你也可以用别的方式)

最后请记得检查防火墙是否容许873端口通讯,和selinux是否启用           [root@test ~]# getenforce
Permissive   //这样就OK,如果是Enforcing就会有问题,请执行  setenforce 0

rsync参数的具体解释如下:

-v, --verbose 详细模式输出
-q, --quiet 精简输出模式
-c, --checksum 打开校验开关,强制对文件传输进行校验
-a, --archive 归档模式,表示以递归方式传输文件,并保持所有文件属性,等于-rlptgoD
-r, --recursive 对子目录以递归模式处理
-R, --relative 使用相对路径信息
-b, --backup 创建备份,也就是对于目的已经存在有同样的文件名时,将老的文件重新命名为~filename。可以使用--suffix选项来指定不同的备份文件前缀。
--backup-dir 将备份文件(如~filename)存放在在目录下。
-suffix=SUFFIX 定义备份文件前缀
-u, --update 仅仅进行更新,也就是跳过所有已经存在于DST,并且文件时间晚于要备份的文件。(不覆盖更新的文件)
-l, --links 保留软链结
-L, --copy-links 想对待常规文件一样处理软链结
--copy-unsafe-links 仅仅拷贝指向SRC路径目录树以外的链结
--safe-links 忽略指向SRC路径目录树以外的链结
-H, --hard-links 保留硬链结
-p, --perms 保持文件权限
-o, --owner 保持文件属主信息
-g, --group 保持文件属组信息
-D, --devices 保持设备文件信息
-t, --times 保持文件时间信息
-S, --sparse 对稀疏文件进行特殊处理以节省DST的空间
-n, --dry-run现实哪些文件将被传输
-W, --whole-file 拷贝文件,不进行增量检测
-x, --one-file-system 不要跨越文件系统边界
-B, --block-size=SIZE 检验算法使用的块尺寸,默认是700字节
-e, --rsh=COMMAND 指定使用rsh、ssh方式进行数据同步
--rsync-path=PATH 指定远程服务器上的rsync命令所在路径信息
-C, --cvs-exclude 使用和CVS一样的方法自动忽略文件,用来排除那些不希望传输的文件
--existing 仅仅更新那些已经存在于DST的文件,而不备份那些新创建的文件
--delete 删除那些DST中SRC没有的文件
--delete-excluded 同样删除接收端那些被该选项指定排除的文件
--delete-after 传输结束以后再删除
--ignore-errors 及时出现IO错误也进行删除
--max-delete=NUM 最多删除NUM个文件
--partial 保留那些因故没有完全传输的文件,以是加快随后的再次传输
--force 强制删除目录,即使不为空
--numeric-ids 不将数字的用户和组ID匹配为用户名和组名
--timeout=TIME IP超时时间,单位为秒
-I, --ignore-times 不跳过那些有同样的时间和长度的文件
--size-only 当决定是否要备份文件时,仅仅察看文件大小而不考虑文件时间
--modify-window=NUM 决定文件是否时间相同时使用的时间戳窗口,默认为0
-T --temp-dir=DIR 在DIR中创建临时文件
--compare-dest=DIR 同样比较DIR中的文件来决定是否需要备份
-P 等同于 --partial
--progress 显示备份过程
-z, --compress 对备份的文件在传输时进行压缩处理
--exclude=PATTERN 指定排除不需要传输的文件模式
--include=PATTERN 指定不排除而需要传输的文件模式
--exclude-from=FILE 排除FILE中指定模式的文件
--include-from=FILE 不排除FILE指定模式匹配的文件
--version 打印版本信息
--address 绑定到特定的地址
--config=FILE 指定其他的配置文件,不使用默认的rsyncd.conf文件
--port=PORT 指定其他的rsync服务端口
--blocking-io 对远程shell使用阻塞IO
-stats 给出某些文件的传输状态
--progress 在传输时现实传输过程
--log-format=formAT 指定日志文件格式
--password-file=FILE  从FILE中得到密码
--bwlimit=KBPS 限制I/O带宽,KBytes per second
-h, --help 显示帮助信息
另附rsync.sh

#!/bin/bash
#init-openservice.sh
#version 2.0
#update 20100310 BY Serwei Xu

#Author: Serwei Xu()
case $1 in
'tool' )
echo "Starting update tool...."
rsync -vzrtogp --progress --delete 192.168.1.11::tools /home/test/tool/
;;
'monitor' )
echo "Starting update monitor...."
rsync -vzrtogp --progress --delete 192.168.1.11::monitor /home/test/monitor/
;;
'crond' )
echo "Starting update cron...."
rsync -vzrtogp --progress --delete 192.168.1.11::cron /var/spool/cron/
;;
'scanner' )
echo "Starting update scanner...."
rsync -vzrtogp --progress --delete 192.168.1.11::scanner /usr/local/scanner/
;;
'dbxz' )
echo "Starting update nginx dbxz...."
rsync -vzrtogp --progress 192.168.12.11::dbxz /home/test/dbxz
;;
'nginx_logrotate' )
echo "Starting update nginx logrotate...."
rsync -vzrtogp --progress --delete 192.168.1.11::nginx_logrotate/nginx /etc/logrotate.d/
;;
'nginx_0' )
echo "Starting update nginx...."
rsync -vzrtogp --progress --delete 192.168.1.11::nginx /usr/local/nginx/
sh /etc/init.d/nginx stop
sh /etc/init.d/nginx start
;;
'nginx_l' )
echo "Starting update nginx...."
rsync -vzrtogp --progress --delete 192.168.3.4::nginx /usr/local/nginx/
nginx_script=/usr/local/nginx
nginx_perl=/usr/lib/perl5/site_perl/5.8.8/i386-linux-thread-multi
if [ ! -d $nginx_perl/auto/nginx ];then
     mkdir -p $nginx_perl/auto/nginx
     mv $nginx_script/perl/nginx.so $nginx_perl/auto/nginx/
     mv $nginx_script/perl/nginx.bs $nginx_perl/auto/nginx/
     mv $nginx_script/perl/nginx.pm $nginx_perl/
     mv $nginx_script/perl/nginx.3pm /usr/share/man/man3/
     mv $nginx_script/perl/.packlist $nginx_perl/auto/nginx/
     mv $nginx_script/perl/perllocal.pod $nginx_perl/
fi
sh /etc/init.d/nginx stop
sh /etc/init.d/nginx start
;;
'all' )
echo "Starting update all......(have no nginx)"
rsync -vzrtogp --progress --delete 192.168.1.11::tools /home/pplive/tools/
rsync -vzrtogp --progress --delete 192.168.1.11::monitor /home/pplive/monitor/
rsync -vzrtogp --progress --delete 192.168.1.11::cron /var/spool/cron/
rsync -vzrtogp --progress --delete 192.168.1.11::scanner /usr/local/scanner/
rsync -vzrtogp --progress --delete 192.168.1.11::nginx_logrotate/nginx /etc/logrotate.d/
;;
*)
echo "usage: `basename $0` {tools|monitor|crond|scanner|nginx_logrotate|nginx_0|dbxz|nginx_1|all}"
esac


阅读(1212) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~