一).简介

rsync,remote synchronize顾名思意就知道它是一款实现远程同步功能的软件,它在同步文件的同时,可以保持原来文件的权限、时间、软硬链接等附加信息。 rsync是用 “rsync 算法”提供了一个客户机和远程文件服务器的文件同步的快速方法,而且可以通过ssh方式来传输文件,这样其保密性也非常好,另外它还是免费的软件。

安装前准备,需要把防火墙关闭/selinux也关闭

二).服务器A(服务端)配置

rysnc的官方网站:http://rsync.samba.org/可以从上面得到最新的版本。

1,安装rsync

#yum install rsync

#yum install inotify-tools

2,配置密码文件并给600权限

#echo “bakuser:abc110” >/etc/rsync.password

#chmod  644 /etc/rsync.password

3,修改配置文件

#vim /etc/rsyncd.conf

uid =  root

gid =  root

use chroot = no

max connections = 10

strict modes =yes

port = 873

read only = no

list = true

auth users = backuser

secrets file = /etc/rsync.password

hosts allow = *

#hosts deny = *

#transfer logging = yes

pid file = /var/run/rsyncd.pid

#lock file = /var/run/rsyncd.lock

log file = /var/log/rsyncd.log

[Web]

path = /home/mrliangqi

4,启动rsync

#/usr/bin/rsync  –daemon –config=/etc/rsyncd.conf

5,查看日志

#tail -f /var/log/rsyncd.log

三).服务器B(客户端)

1,rsync安装

# yum install rsync

#yum install inotify-tools

2,密码文件并授权

#echo “abc110” >/etc/rsync.password

# chmod  600 /etc/rsync.password

3,同步文件

命令说明:rsync [参数]  –progress[显示传输进度] –password****=[密码文件路径]  backuser[服务器a上的用户名]@ip[服务器A的地址]::Web[模块名称] /home/mrliangqi[保存到本地的路径]

# rsync   -avx  –progress –password-file=/etc/rsync.password   [email protected]::Web  /home/mrliangqi/

receiving incremental file list

sent 2880 bytes  received 387053 bytes  155973.20 bytes/sec

total size is 1858387692  speedup is 4765.92

查看rsync提供的数据源,也就是的rsyncd.conf[Web]中定义的内容

# rsync  –list-only [email protected]::Web 

四)实时文件同步

[计划任务实现同步]

#vim rsync.sh

/usr/bin/rsync  -avx –password-file=/etc/rsync.password   [email protected]::Web1 /home/mrliangqi

/usr/bin/rsync  -avx –password-file=/etc/rsync.password   [email protected]::Db1  /home/dbback

/usr/bin/rsync  -avx –password-file=/etc/rsync.password   [email protected]::Soft1 /root/soft

# bash  /home/rsync.sh //执行测试脚本~

把脚本添加到计划任务:

# crontab  -e  

* * * * *  bash  bash  /home/rsync.sh

重启计划任务

# service crond restart

rsync参数:

-a, –archive 归档模式,表示以递归方式传输文件,并保持所有文件属性

-c, –checksum 打开校验开关,强制对文件传输进行校验

–delete 删除那些DST中SRC没有的文件

-H, –hard-links 保留硬链接

-b, –backup 创建备份,也就是对于目的已经存在有同样的文件名时,将老的文件重新命名为~filename。可以使用–suffix选项来指定不同的备份文件前缀

–backup-dir 将备份文件(如~filename)存放在在目录下

-z, –compress 对备份的文件在传输时进行压缩处理

[inotify实现同步]

1,创建脚本,实时触发rsync进行目录同步(如果需要多个目录需要设置多个脚本或者不同的参数)

# vim  inotify.sh

#!/bin/sh

#源模块名称

srcdir=Web1

#同步到本地的目录

dstdir=/home/mrliangqi

#同步用户

rsyncuser=backuser

#密码文件

rsyncpassdir=/etc/rsync.password

#源服务器ip

dstip=”121.42.27.192″

for ip in $dstip

do

rsync -avH –port=873 –progress –delete   [email protected]$ip::$srcdir   $dstdir –password-fi

le=$rsyncpassdir

done

/usr/bin/inotifywait -mrq –timefmt ‘%d/%m/%y %H:%M’ –format ‘%T %w%f%e’ -e close_write,mo

dify,delete,create,attrib,move $srcdir |  while read file

do

echo ”  ${file} was rsynced” >> /tmp/rsync.log 2>&1

done

测试执行:

# bash inotify.sh

2,设置脚本开机启动

#vi /etc/rc.d/rc.local

bash /home/inotify.sh

3,在源服务器新建目录或文件,查看该服务器是否同步过来文件。~

Inotify参数:

-m 是保持一直监听

-r 是递归查看目录

-q 是打印出事件

-e create,move,delete,modify,attrib 是指 “监听 创建 移动 删除 写入 权限” 事件

-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

【问题分享】

出现以下这个讯息, 是怎么一回事?

@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 中的密码设定, 二端是否一致?

出现以下这个讯息, 是怎么一回事?

password file must not be other-accessible

continuing without password file

Password:

A: 这表示 rsyncd.secrets 的档案权限属性不对, 应设为 600。请下 chmod 600 rsyncd.secrets

出现以下这个讯息, 是怎么一回事?

@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开设好备份目录.

A:max connections (10) reached

解决:rsync配置文件的参数设置大max connections = 50

发表评论

后才能评论