您的位置:首页 > 其它

RSync实现双机文件同步

2016-10-06 08:27 211 查看

双机同步任务操作步骤

注:红色为注释说明的内容,绿色为示例内容,请以项目实际情况修改操作

1、 安装或更新相关软件包#yum install rsync* xinetd* –y 2、 配置rsync服务端(提供文件同步的服务器),制定提供同步的应用条目信息A、 配置文件实例如下#vim /etc/rsyncd.conf -------------编辑rsync配置文件uid = root -------------设置进行数据传输时所使用的账户名或ID号,默认使用nobodygid = root -------------设置进行数据传输时所使用的组名称或GID号,默认使用nobodyuse chroot = no max connections = 100 -------------稍后重试的提示消息timeout = 600 -------------600s连接超时pid file = /var/run/rsyncd.pid ------------设置rsync进程号保存名称lock file = /var/run/rsyncd.lock ------------设置锁文件名称log file = /var/log/rsyncd.log ------------设置日志文件名称,可以通过log format 参数设置日志格式##以上为全局配置,下面为一个条目,可根据需求添加多个条目[tomcat_project]path = /opt/appl/ project/ ----------------同步目录的路径##应用用户组id,和用户iduid = 1501 gid = 1500read only = yes ----------------是否允许客户端上传数据,这里设置为只读list = yes ---------------客户端请求显示模块列表时,本模块名称是否显示,默认为ture##允许使用该条目进行同步的主机地址,加入备机和预生产主机地址hosts allow = 172.16.0.101,172.26.0.100 auth users = rsync ---------------设置允许连接服务器的账户,账户可以使系统中不存在的用户secrets file = /etc/rsyncd.pwd --------------设置密码验证文件名称,注意该文件的权限要求为只读,建议权限600 B、 增加Rsync同步认证文件#vim /etc/rsyncd.pwd ---------------设置验证文件密码rsync:123456 C、 配置服务启动管理项##vim /etc/xinetd.d/rsyncservice rsync{##把这里的yes 改为 nodisable = noflags = IPv6socket_type = streamwait = nouser = rootserver = /usr/bin/rsyncserver_args = --daemonlog_on_failure += USERID}

#启动rsync服务#service xinetd start#验证是否启动成功#netstat –anptl | grep 873
D、 防火墙放行rsync服务端口 #在vim /etc/sysconfig/iptables加入以上行-A INPUT -p tcp -m state --state NEW -m tcp --dport 873 -j ACCEPT #service iptables restart ------------重启防火墙设置
3、 系统执行过setup脚本,默认是限制远程访问,这里需要开放客户端(同步端)的地址#cat >> /etc/hosts.allow <<EOF##这里的地址是同步端的rsync:172.16.0.101:allowEOF服务端配置完毕!4、 客户端(同步端)配置A、 创建同步脚本放置目录#mkdir -p /opt/appl/assist/rsync B、 创建脚本生成日志文件的放置目录#mkdir -p /var/log/rsyncC、 在脚本放置目录下创建如下脚本#vim rsync_172.16.0.100_project.sh#!/bin/shLOG_FILE=/var/log/rsync/rsync_172.16.0.100_project_$(date +%Y-%m-%d)loginfo(){echo "$(date "+%Y-%m-%d_%H:%M") [ANIHC SAYS] $1" >>$LOG_FILE}##For projectloginfo "==========Synchronizing=========="loginfo "Source directory project..."loginfo "Destination directory is /opt/appl/project"/usr/bin/rsync -arP --delete --exclude-from=/opt/appl/assist/rsync/exclude_list_project --password-file=/etc/rsyncd.pwd_project rsync@172.16.0.100::tomcat_project /opt/appl/project >>$LOG_FILEloginfo "==========Synchronization done.=========="D、 创建rsync用户认证文件#cat /etc/rsyncd.pwd_project##这里的密码和服务端的/etc/rsyncd.pwd中的密码一样123456 E、 创建同步排除文件##cat /opt/appl/assist/rsync/exclude_list_project*.gz*.tgz*.zip*.log*.out*.bak*.tmplog/*logs/*tmp/*temp/*##以上均为常规性同步排除,可以添加指定行排除文件 F、 测试脚本是否可以成功同步#cd /opt/appl/assist/rsync##后台执行脚本,并重定向输出到nohup.out文件#nohup ./ rsync-172.16.0.100_project.sh &##执行后检查/opt/appl 目录下是否已经有project同步过来,如果没有查看nohup.out定位错误 G、 如果脚本执行成功,添加定时任务 #crontab –e#crontab –l00 01 * * * /opt/appl/assist/rsync/rsync-172.16.0.100_project.sh > /dev/null 2>&1常见的以下错误和相应的解决方法: Q:为什么我总会出现"Read-only file system"的错误呢?
   A:看看是否忘了设"read only = no"了   Q:为什么我会出现'@ERROR: invalid gid'的错误呢?   A:rsync使用时默认是用uid=nobody;gid=nobody来运行的,如果你的系统不存在nobody组的话,就会出现这样的错误,可以试试gid = ogroup或者其它   Q:绑定端口873失败是怎么回事?
   A:如果你不是以root权限运行这一守护进程的话,因为1024端口以下是特权端口,会出现这样的错误。你可以用--port参数来改变。   Q:为什么我认证失败?
   A:从你的命令行看来:你用的是   > bash$ rsync -a 144.16.251.213::test test
   > Password:
   > @ERROR: auth failed on module test
   >
   > I dont understand this. Can somebody explain as to how to acomplish this.
   > All suggestions are welcome.  应该是没有以你的用户名登陆导致的问题,试试rsync -a max@144.16.251.213::test test   Q: 出现以下这个讯息, 是怎么一回事?
   @ERROR: auth failed on module xxxxx
   rsync: connection unexpectedly closed (90 bytes read so far)
   rsync error: error in rsync protocol data stream (code 12) at io.c(150)   A: 这是因为密码设错了, 无法登入成功, 请再检查一下 rsyncd.secrets 中的密码设定, 二端是否一致?   Q: 出现以下这个讯息, 是怎么一回事?   password file must not be other-accessible
   continuing without password file
   Password:   A: 这表示 rsyncd.secrets 的档案权限属性不对, 应设为 600。请下 chmod 600 rsyncd.secrets   Q: 出现以下这个讯息, 是怎么一回事?   @ERROR: chroot failed
   rsync: connection unexpectedly closed (75 bytes read so far)
   rsync error: error in rsync protocol data stream (code 12) at io.c(150)   A: 这通常是您的 rsyncd.conf 中的 path 路径所设的那个目录并不存在所致.请先用 mkdir开设好备份目录. 完!
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息