天天看点

rsync同步服务配置手记

rsync在推送或抓取数据时,在rsync客户端会出现性能问题, 负载比较大且数据量比较大的生产环境要注意这一点。 本文只是个人平时配置示例,可以简单修改后用于实际环境使用,rsync不仅可以实现服务器间的复制,对于本地目录的同步也相当方便, 其它功能如实时同步等功能待补充。

#!/bin/bash   

# author: koumm    

# desc: backup for /data/ to 10    

# date: 20xx-xx-xx    

# version: v1.0    

# modify:

#rsync bak   

end_date=`date +%y/%m/%d_%h%m%s`

/usr/bin/rsync -vzrtopg --delete /data/ 192.168.200.10::ip6/ --exclude "tmp/" --port=1865

echo ""   

echo "############################################"    

echo "rsync completed time is :$end_date"    

echo "############################################"

# crontab -e   

40 12,23  * * *   /root/system/rsync_bak_to_10.sh

# vi /etc/rsyncd.conf   

uid=root     

gid=root     

max connections=36000     

use chroot=no     

log file=/var/log/rsyncd.log     

pid file=/var/run/rsyncd.pid     

lock file=/var/run/rsyncd.lock     

[ip6]    

path=/data/project_bak/ip6    

comment = ip6    

ignore errors    

read only = no    

write only = no     

list = no  

hosts allow = 192.168.200.0/24    

#auth users = www    

#secrets file = /etc/rsyncd.secrets

服务端配置开机自启动,以及手动执行启动命令。

# vi /etc/rc.local

/usr/bin/rsync --daemon --port=1865

重启rsync服务采用的杀进程方式。先ps –ef |grep rsync 找到进程号,再kill –9 xxxx杀之,再启动。

客户端: cwrsync_4.1.0_installer.zip   

服务端: cwrsyncserver_4.1.0_installer.zip    

默认安装即可,配置文件如下:

use chroot = false   

strict modes = false    

hosts allow = *    

log file = rsyncd.log    

gid = 0       

uid = 0

[gzbak]   

path = /cygdrive/d/data    

read only = false    

transfer logging = yes

修改windows服务,将rsyncserver配置为启动,自动启动。

rsync.bat   

@echo off    

cls    

"c:\program files\cwrsync\bin\rsync.exe" -avr --delete /cygdrive/c/opt 192.168.233.14::gzbak/ --port=1865

# rsync -avz -e ssh 192.168.0.25:/data/ /data01/

[email protected]'s password:  输入密码

receiving incremental file list

./

a.txt

abc.txt

注:如果两服务器之前配置密钥直接登录的话,可以直接传文件与scp方式类似。

4. inotify实时同步配置

(1) inotify-tools安装,只在客户端安装即可。

下载地址:http://github.com/downloads/rvoicilas/inotify-tools/inotify-tools-3.14.tar.gz

# tar zxvf inotify-tools-3.14.tar.gz

# cd inotify-tools-3.13

# ./configure --prefix=/usr/local/inotify

# make && make install

调整inotify队列参数及参数说明:

max_queued_events: inotify队列最大长度,如果值太小,会出现"** event queue overflow **"错误,导致监控文件不准确

max_user_watches:  要同步的文件包含多少目录

max_user_instances:每个用户创建inotify实例最大值

sysctl -w fs.inotify.max_queued_events="99999999"

sysctl -w fs.inotify.max_user_watches="99999999"

sysctl -w fs.inotify.max_user_instances="65535"

(2) 通过配置文件准备

# vi inosync.sh

#!/bin/sh

src=/data01/

dst=192.168.1.125::data

inwt=/usr/local/inotify/bin/inotifywait

rsync=/usr/bin/rsync

$inwt -mrq --timefmt '%d/%m/%y %h:%m' --format '%t %w%f' -e create,move,delete,modify,attrib $src \

| while read line

do

        $rsync -az --delete /data01/ 192.168.1.125::data

done

# chmod +x inosync.sh

(3) 手动运行测试

运行的时候日志会自动输出到终端,可以观察运行的情况。

# /usr/local/inotify/bin/inotifywait -mrq --timefmt '%d/%m/%y %h:%m' --format  '%t %w%f' -e modify,delete,create,attrib /data01 &

说明:

--timefmt: 指定输出时的输出格式

--format : '%t %w%f'指定输出的格式

(4) 启动,或将如下启动脚本加入/etc/rc.local中。

# ./inosync.sh &

[1] 23254

说明:inotify有一个问题就是如果文件数据量比较大,或同步的文件比较大,或变更比较频繁,效率不高。

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            显示帮助信息

继续阅读