一、流程图
二、rsync的安装
1、卸载原来的rsync
当然在安装Linux操作系统时,rsync是会被默认安装好的,如果觉得版本太低的话,可以通过如下步骤将其卸载掉
- #rmp -qla | grep rsync //查看一下安装的rsync版本
- #rmp -e --nodeps rsync.2.6.. //卸载原来的rsync版本
2、安装新版本的rsync(服务器A和备份服务器B操作是一样的)
rsync下载地址如下
- #tar xf rsync-3.0.9.tar.gz
- #cd rsync-3.0.9
- #./configure --prefix=/usr/local/rsync
- #make
- #make install
3、在服务A(即rsync服务端)下创建配置文件
- #vim /etc/rsyncd.conf //默认是没有的
- 添加如下内容
- uid = nobody
- gid = nobody
- port = 873 //rsync的端口
- use chroot = no
- max connections = 3 //允许客户端的最大连接数
- strict modes = yes //是否需要密码验证文件
- 注:这里为yes时,密码文件权限要改为600
- pid file = /var/run/rsync.pid //pid文件
- lock file = /var/run/rsync.lock //锁文件
- log file = /var/log/rsync.log // 日志文件
- [web] //自定义的模块,用来客户端做验证的
- path = /data/ //要备份的目录
- ignore = errors //当有一些错误时,忽略
- read only = no //允许客户端上传文件
- hosts allow = 192.168.1.6 //允许该IP的客户端同步
- hosts deny = * //不允许
- list = false
- uid = root //用于同步的属主和属组
- gid = root
- auth users = centos //客户端用来验证的用户名
- secrets file = /etc/rsync.pas //密码文件
4、在客户端和服务器端建立密码文件
- 在服务器端(即服务器A上)
- #vim /etc/rsync.pas //这个密码文件是要和配置文件中的一致的。
- centos:rsync //其中centos为验证用户,rsync为密码
- #chmod 600 /etc/rsync.pas //修改权限为600
- 在客户端(即服务器B上)
- #vim /etc/rsync.pas //这个密码文件是要和配置文件中的一致的。
- rsync //此密码要和服务端相同
- #chmod 600 /etc/rsync.pas //修改权限为600
5、在服务器端启动rsync的守护进程
- #/usr/local/rsync/bin/rsync --daemon //启动守护进程
- #ps -ef | grep rsync //查看进程是否启动
6、同步数据到客户端
- 在客户端(即服务器B上)
- rsync -vzrtopg --progress --delete centos@192.168.1.5::web --password-file=/etc/rsyncd.secrets /data //把服务端文件同步到本地data目录下
- -v, --verbose 详细模式输出
- -q, --quiet 精简输出模式
- -r, --recursive 对子目录以递归模式处理
- -p, --perms 保持文件权限
- -o, --owner 保持文件属主信息
- -g, --group 保持文件属组信息
- --delete 删除那些DST中SRC没有的文件
- --progress 显示备份过程
- -t, --times 保持文件时间信息
7、编写脚本并建立定时任务让定时执行备份
- #vim /home/bakrsync.sh
- #!/bin/bash
- #
- RSYNC=/usr/local/rsync/bin/rsync
- $RSYNC -vzrtopg --progress --delete --password-file=/etc/rsync.pass centos@192.168.1.5::web /data
- #chmod +x /home/bakrsync.sh
- #contab -e
- 01 01 * * * /home/basrsync.sh & //每天凌晨放在后台自动执行
8、rsync中常见错误如下(转自http://blog.chinaunix.net/uid-13954085-id-158637.html)
配置一:
ignore errors
说明:这个选项最好加上,否则再很多crontab的时候往往发生错误你也未可知,因为你不可能天天去看每时每刻去看log,不加上这个出现错误的几率相对会很高,因为任何大点的项目和系统,磁盘IO都是一个瓶颈
Rsync error:
错误一:
@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)
说明:这是因为密码设置错了,无法登入成功,检查一下rsync.pwd,看客服是否匹配。还有服务器端没启动rsync 服务也会出现这种情况。
错误一:
@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)
说明:这是因为密码设置错了,无法登入成功,检查一下rsync.pwd,看客服是否匹配。还有服务器端没启动rsync 服务也会出现这种情况。
错误二:
password file must not be other-accessible
continuing without password file
Password:
说明:这是因为rsyncd.pwd rsyncd.sec的权限不对,应该设置为600。如:chmod 600 rsyncd.pwd
错误三:
@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)
说明:这是因为你在 rsync.conf 中设置的 path 路径不存在,要新建目录才能开启同步
错误四:
rsync: failed to connect to 218.107.243.2: No route to host (113)
rsync error: error in socket IO (code 10) at clientserver.c(104) [receiver=2.6.9]
说明:防火墙问题导致,这个最好先彻底关闭防火墙,排错的基本法就是这样,无论是S还是C,还有ignore errors选项问题也会导致
错误五:
@ERROR: access denied to www from unknown (192.168.1.123)
rsync: connection unexpectedly closed (0 bytes received so far) [receiver]
rsync error: error in rsync protocol data stream (code 12) at io.c(359)
rsync: connection unexpectedly closed (0 bytes received so far) [receiver]
rsync error: error in rsync protocol data stream (code 12) at io.c(359)
说明:此问题很明显,是配置选项host allow的问题,初学者喜欢一个允许段做成一个配置,然后模块又是同一个,致使导致
错误六:
rsync error: received SIGINT, SIGTERM, or SIGHUP (code 20) at rsync.c(244) [generator=2.6.9]
rsync error: received SIGUSR1 (code 19) at main.c(1182) [receiver=2.6.9]
rsync error: received SIGUSR1 (code 19) at main.c(1182) [receiver=2.6.9]
说明:导致此问题多半是服务端服务没有被正常启动,到服务器上去查查服务是否有启动,然后查看下 /var/run/rsync.pid 文件是否存在,最干脆的方法是杀死已经启动了服务,然后再次启动服务或者让脚本加入系统启动服务级别然后shutdown -r now服务器
错误七:
rsync: read error: Connection reset by peer (104)
rsync error: error in rsync protocol data stream (code 12) at io.c(604) [sender=2.6.9]
rsync error: error in rsync protocol data stream (code 12) at io.c(604) [sender=2.6.9]
说明:原数据目录里没有数据存在
转载于:https://blog.51cto.com/sxhxt/1130243