rsync , rsync + ssh, rsync + lsyncd 多种同步方案与比较

来源:互联网 发布:淘宝论文降重靠谱吗 编辑:程序博客网 时间:2024/05/22 20:21

1. 利用 ssh+rsync
 只需要在服务器端打开 ssh 服务, 并启动 xinetd 中 rsync 服务则可使用
 a. 增量同步
  rsync -av /src-dir/.  user@ipadress:/dest-dir/.
 b. 镜像同步
  rsync -av --delete /src-dir/.  user@ipadress:/dest-dir/.
 
 特点: 配置简单, 不需要配置 rsyncd.conf 配置文件, 但需要在同步过程中进行用户认证
 注: 利用 expact + gzipexe 可以创建自动同步加密脚本
 使用: 可手动同步或每天晚上定制时间任务同步
 缺点:无法实现实时同步, 不可以成为双机热备份方案么

 

2. 启用 rsync 服务
 需要配置 /etc/rsyncd.conf 配置文件, 需要启动 xinetd 中 rsync 服务, 可以不通过 sshd 协调
 a. 配置文件 (参考  man 5 rsyncd.conf , 目录共享管理参考 smb.conf)

rsyncd.conf 参考
motd file = true     # 时间记录
pid file = /var/run/rsyncd.pid
port = 873
address = 0.0.0.0
log file = /var/log/rsyncd.log  # rsync 独立日志, 记录每个文件更新信息
syslog facility = syslog   # 增加同步启动与结束信息到 /var/log/messages 仲
uid = nobody      # 注: 定义同步时 rsync 进程用户 id
gid = nobody
use chroot = no

更多参数参考配置文件说明 man rsyncd.conf

[share]
path=/tmp/test
read only=false


rsyncd.conf 配置安全信息:
charset 用于设定字符集, 可用字符集参考 smb.conf 标准
max connections 并发连接数量
read only  读写控制
write only 读写控制
list  是否允许客户端利用  rsync --list-only rsync://192.168.1.11:873 查询共享目录信息
 注: rsync --list-only rsync://192.168.1.11:873/share/. 能够列出文件信息, 与 list 参数无关
exclude 同步过程中忽略某个文件或目录 ex: = new/ old/ kdump.conf (注:只需要相对路径)
exclude from = /etc/rsyncd.list 以文件记录同步过程忽略信息
incoming chmod 文件目录权限定义
outgoing chmod 文件目录权限定义
auth users 配合 secrets file 使用, 定义用户认证(明文) = user1 user2 user3
secrets file = filepath 验证文件 语法 user:password (必须包含上述user123) 文件 root 600 属性
hosts allow 主机 IP 定义
hosts deny 主机 IP 定义
log format 默认格式 %o %h [%a] %m (%u) %f %l 参考 rsyncd.conf


 常见同步语法
 rsync --list-only rsync://192.168.1.11:873/share/. 能够列出文件信息
 rsync --list-only rsync://192.168.1.11:873 查询共享目录信息
 rsync --exclude-from=file.txt rsync://192.168.1.11:873/share/.  /tmp/test/. 不同步指定信息

 

 优点: 避免显示主机文件信息绝对路径, 多种安全设定, 不需要利用ssh 进行用户验证, 支持匿名同步
 缺点: 无法主动推送文件

rsync执行一次就退出了。所以若不间断执行,可以添加到crontab中定时执行,也可以写成服务在死循环中执行,如下:

[root@TiYanPlat-Node1 resource.d]# cat sync_client
#!/bin/bash
#rsync start and stop script by Jet

# chkconfig: - 30 60
# description: this script is for sync the files from 192.168.200.2  and work with lsyncd at 192.168.200.2
# config: no config
# Provides:Jet design at Cndatacom

# Source function library.
. /etc/rc.d/init.d/functions

 


#check the pid file exists
des_ip=192.168.200.2
source_dirs=/opt/TiYanPlatProject/
source_dirs1=/opt/app/
des_dirs=TiYanPlatProject
des_dirs1=apps
sync_user=bak
sync_pass_file=/etc/pass_file_for_sync.txt
syn=/usr/bin/rsync
intervel=5 #5秒执行一次

function r_sync(){
 $syn -av -W  --ignore-errors  --stats --progress --ipv4  --log-file-format='%F %H:%M:%S '   --log-file=/var/log/sync_client.log --password-file=$sync_pass_file  $source_dirs$sync_user@$des_ip::$des_dirs
[ $? -eq 0 ] && echo ' sync files    .......................................... [ OK ]' ||
 echo ' sync files   .......................................... [ Faild ]'


# $syn -av -W  --ignore-errors  --stats --progress --ipv4  --log-file-format='%F %H:%M:%S '   --log-file=/var/log/sync_client.log --password-file=$sync_pass_file  $source_dirs1$sync_user@$des_ip::$des_dirs1
#[ $? -eq 0 ] && echo ' sync files    .......................................... [ OK ]' ||
# echo ' sync files   .......................................... [ Faild ]'
}

function r_start(){
 pids=$(ps -ef | grep '$syn -av -W  --ignore-errors'| grep -v 'grep'| awk -F ' ' '{print $2}')
 if [ "$pids" == "" ]; then
    while [ 1 ]
     do
         r_sync

      sleep $intervel
      done &
fi


}

function r_stop(){
pids=$(ps -ef | grep 'sync_client start' | grep -v 'grep'|awk -F ' ' '{print $2}')
if [ "$pids" != "" ]; then

 for i in ${pids[@]}
   do
     [ "$i" != "" ] && kill -9 $i >> /dev/null
   done
fi
pids2=$(ps -ef | grep 'sync_client start' | grep -v 'grep' | awk -F ' ' '{print $2}')
[ "$pids2" == "" ] && echo ' stoping  sync client programs   .......................................... [ OK ]' || echo ' stoping  sync client programs   .......................................... [ Faild ]'
}

function r_restart(){
   r_stop
   r_start &

}

case $1 in
 start)
     r_start &
;;
 stop)
           r_stop
;;
 restart)
    r_restart
;;
 *)
echo 'Useage service sync_client start|stop|restart'
;;
esac

 

上面的密码文件格式为:

用户名:密码

如:

 bak:XXXXXXXXXXXXXXXXXXXXXXXXX

 

----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
3. rsync+lsyncd 数据同步

 

       [rsync server ]  <------     [ lsyncd ]    --->  [ rsync server ]

 

rsync 客户端需要编译 lsyncd 软件,安装 rsync 工具
rsync 服务器需要配置并启动 rsync 进程, 如上例子, 匿名共享 share 目录

 

下载最新版
http://lsyncd.googlecode.com/files/lsyncd-2.0.5.tar.gz

REDHAT系的,如果懒得编译,可以到http://download.fedoraproject.org/pub/epel下载
依赖: lua >= 5.1.3, rhel6 补丁
ftp://ftp.pbone.net/mirror/ftp.scientificlinux.org/linux/scientific/6rolling/i386/os/Packages/lua-devel-5.1.4-4.1.el6.i686.rpm

语法需调用 LUA 格式

 


创建配置文件 share.lua

settings = {
.. 略
 nodaemon   = true,    # false 成为前台进程, 方便测试
 statusInterval = 3,    # lsyncd.status 状态更新时间间隔
 ...略
}

sync{
 ..  略
}

bash = {    
 ... 略
}

settings 为主配置, sync 为同步配置, bash 为同步方案

启动方法
lsyncd  share.lua

初次启动则会自动同步文件内容, 后发生文件修改, 增加, 删除都会进行自动同步
如果需要对多台电脑进行同步, 可以利用 lsyncd  backup.lua 等脚本多次进行进程启动

也可以新建个配置文件 /etc/lsyncd.conf

[root@TiYanPlat-Node1 resource.d]# cat /etc/lsyncd.conf
----
-- User configuration file for lsyncd.
--
-- Simple example for default rsync, but executing moves through on the target.
--
-- For more examples, see /usr/share/doc/lsyncd*/examples/
--
-- sync{default.rsyncssh, source="/var/www/html", host="localhost", targetdir="/tmp/htmlcopy/"}


settings  {
        logfile = "/var/log/lsyncd.log",
 statusFile = "/tmp/lsyncd.stat",
 statusInterval = 1,
}

sync{
 default.rsync,
 source="/opt/TiYanPlatProject",
 target="192.168.200.2::TiYanPlatProject",
       
}

 

 方案优点:
  1. 自动对目录中文件向 rsync 服务器进行推送
  2. 能够支持多台 rsync 服务器
  3. 避免人工参与修改
 缺点:
  同步时间非实时, 约莫具有30秒延时
  不建议采用大文件进行同步

 

原创粉丝点击