转载请注明出处:http://forever.blog.chinaunix.net
最近一次内部方案讨论,同事说需要开发一个实时的文件同步功能,哥想到了rsync,于是就有了这篇文章
1. What’s rsync
Rsync is a fast and extraordinarily versatile file copying tool. It can copy locally, to/from another host over any remote shell, or to/from a remote rsync daemon. It offers a large number of options that control every aspect of its behavior and permit very flexible specification of the set of files to be copied. It is famous for its delta-transfer algorithm, which reduces the amount of data sent over the network by sending only the differences between the source files and the existing files in the destination. Rsync is widely used for backups and mirroring and as an improved copy command for everyday use.
Rsync finds files that need to be transferred using a "quick check" algorithm (by default) that looks for files that have changed in size or in last-modified time. Any changes in the other preserved attributes (as requested by options) are made on the destination file directly when the quick check indicates that the file's data does not need to be updated.
2. How to use rsync
使用rsync需要在同步的两台服务器上同时安装rsync安装包。
2.1 Rsync同步目录
rsync --compress --recursive --delete --links --times --perms --owner --group --verbose --progress --stats --rsh="ssh" /root/testdir/ hadoop_name:/root/tonytest
--compress 开启压缩模式
--recursive 对子目录中的文件进行同步
--delete 同步本地的删除信息(本地删除文件后,也会删除对端的文件)
--links 对链接文件进行同步
--times 同步文件的修改时间
--perms 同步文件的权限
--owner 同步文件的属主
--group 同步文件的属组
--verbose 打印更多的同步信息
--progress 打印出同步的进度信息
--rsh 指定对端shell
--stats 打印出同步的统计信息
/root/testdir/ 同步的本地源目录
hadoop_name:/root/tonytest 同步的目的主机和目录
2.2 Rsync的问题
Rsync同步目录时会比对所有的文件,再目录非常大的时候,性能低下。
Rsync不能实时同步,通过cron来触发的机制,会有延迟。
3. What’s Inotify and inotify-tools?
Inotify 是一个 Linux特性,它监控文件系统操作,比如读取、写入和创建。Inotify 反应灵敏,用法非常简单,并且比 cron 任务的繁忙轮询高效得多。
Inotify-tools 是对linux特性进行封装的开源工具包。
4. RSYNC&Inotify-tools
利用Inotify-tools的实时监测文件改动和rsync的同步,可以实现一个实时的目录文件同步系统
5. How to use on SUSE Linux?
5.1 源站安装相关工具
安装rsync
#zypper install rsync
安装inotify-tools
#tar zxf inotify-tools-3.14.tar.gz【inotify-tools-3.14.tar.gz见附件】
#cd inotify-tools-3.14
#./configure
#make;make install;ldconfig
设置无密码ssh登录到目标站
# ssh-keygen -t rsa
将~/.ssh/ id_dsa.pub内容追加到目标站~/.ssh/ authorized_keys文件中
5.2 目标站安装相关工具
#zypper install rsync
5.3 使用实例
# nohup realsync.sh “sourcedir” “destip[desthostname]” “destdir” &
6. realsync.sh代码
-
#!/bin/sh
-
#
-
# Copyright (C) Tony!
-
#
-
# Date: 2014-12-05 22:51:09 CST
-
# Version: 0.1
-
# Author:
-
# Tony
-
# vim set nu; set cindent; set tabstop = 4 ;set softtabstop=4*/
-
-
-
bluecolor()
-
{
-
echo -ne "\033[34m"
-
}
-
redcolor()
-
{
-
echo -ne "\033[31m"
-
}
-
greencolor()
-
{
-
echo -ne "\033[32m"
-
}
-
yellocolor()
-
{
-
echo -ne "\033[33m"
-
}
-
cleanstyle()
-
{
-
echo -ne "\033[0m"
-
}
-
-
help()
-
{
-
redcolor
-
echo -n "Usage: "
-
yellocolor
-
echo "sourcedir destip[desthostname] destdir"
-
cleanstyle
-
exit 0
-
}
-
-
if [ $# -ne 3 ];then
-
help
-
fi
-
srcdir=$1
-
dest=$2
-
destdir=$3
-
-
#inotifywait -m -r -q --timefmt '%F %T' --format '%T %w%f %e' -e close_write /root/rsynctestdir//|while read file
-
-
inotifywait -m -r -q --timefmt '%F %T' --format '%w%f' -e close_write ${srcdir}/|while read file
-
do
-
time=`date +'%F %T %Z'`
-
echo "$time RSYNC $file from \"$srcdir\" to \"${dest}:${destdir}\""
-
rsync --compress --recursive --links --times --perms --owner --group --verbose --progress --stats --rsh="ssh" $file ${dest}:${destdir}/
-
done
阅读(792) | 评论(0) | 转发(0) |