`

rsync远程文件同步部署(inotify)

 
阅读更多
  • rsync概述
  • rsync部署
  • 使用rsync文件同步工具
  • rsync常见用法
  • 结合inotify实时远程同步
  • sersync实时同步

 

一、rsync概述

 

rsync是一个快速的文件同步和传输工具,rsync程序类似于rcp命令的工作模式,但是拥有更强的特性,rsync使用rsync远程更新协议,根据效验和搜索的算法仅传输新增或更改过的文件,并且支持数据压缩,从而大大提高了文件同步的速度。

 

 

二、rsync部署

 

  1. 环境说明
  2. 配置网络通讯
  3. 建立配置文件
  4. 启动rsync服务

 

1.环境说明

 

rsync server 主机

主机名   :server

IP地址   :172.20.45.35

操作系统:centos 5.6

 

rsync client 主机

主机名   :client

IP地址   :172.20.45.38

操作系统:centos 5.6

 

 

2.配置网络通讯

 

关闭iptables防火墙

 

登录server

# service iptables stop
# chkconfig iptables off

 

登录client

# service iptables stop
# chkconfig iptables off

 

 

关闭SElinux

 

登录server

# vi /etc/selinux/config
SELINUX=enforcing
更改为
SELINUX=disabled

 

至此reboot重启serve主机。

 

 

登录client

# vi /etc/selinux/config
SELINUX=enforcing
更改为
SELINUX=disabled

 

至此reboot重启client主机。

 

 

更改hosts文件

 

登录server

# vi /etc/hosts
增加
172.20.45.35    server
172.20.45.38    client

 

登录client

# vi /etc/hosts
增加
172.20.45.38    client
172.20.45.35    master

 

 

3.建立配置文件

 

系统安装完毕后默认安装rsync软件包

 

登录server

# rpm -qa | grep rsync
rsync-3.0.6-4.el5_7.1

 

登录client

# rpm -qa | grep rsync
rsync-3.0.6-4.el5_7.1


但默认rsync程序配置文件rsyncd.conf文件是不存在的,需要手动创建。

 

 

登录server

# touch /etc/rsyncd.conf
# vi /etc/rsyncd.conf
增加
uid = root  #授权用户
gid = root  #授权组
log file = /var/log/rsyncd.log #log文件路径
pid file = /var/run/rsyncd.pid #pid文件路径

[rsync]  #同步名称
path = /rsync  #同步文件夹路径
read only = no #不使用只读模式
ignore errors = yes #忽略IO错误
hosts allow = 172.20.0.0/22  #只运行172.20.0.0网段访问
hosts deny = *  #拒绝所有网段访问

 

 

4.启动rsync服务

 

登录server

启动rsync服

# rsync --daemon
# ps -ef | grep [r]sync
root      2612     1  0 11:37 ?        00:00:00 rsync --daemon

 

开启启动rsync服务

# echo "/usr/bin/rsync --daemon &" >> /etc/rc.local

 

 

三、使用rsync文件同步工具

 

将远程主机的备份源目录同步到本地,称为下载同步,将本地的备份源目录同步到远程主机中,称为上传同步。

rsync也可用与本地目录直接的同步。

 

使用rsync文件同步时,命令格式如下

rsync [选项]  备份源 备份目标

 

常用选项

-a            使用归档模式,保留文件原有的权限、属性、属主等信息,等同于使用“-rlptgoD”等多个选项的组合

-l              符号链接文件仍然复制为符号链接

-H             保留硬链接文件

-r              递归模式,包含目录及子目录中所有文件

-v               显示同步过程的详细信息

-z               在传输过程中进行压缩

-o              保留文件的属主标记

-g              保留文件的属组标记

-t               保留文件的时间标记

-p               保留文件的权限标记

-D              保留设备文件以及其他的特殊文件

--delete       删除目录文件夹有而源文件夹没有的文件

--checksum  根据效验和来决定是否跳过文件

 

 

四、rsync常见用法

 

登录client

 

查看rsync server服务器可用的备份模块列表

# rsync rsync://172.20.45.35
rsync          

 

将rsync server服务器中rsync目录同步至本地

# rsync -vzrtopg root@172.20.45.35::rsync /rsync/
receiving incremental file list
./
1/
2/
3/

sent 42 bytes  received 99 bytes 

 

 

五、结合inotify实时远程同步

 

  1. 安装inotify
  2. 部署client rsync服务
  3. ssh验证
  4. 开启实时同步
  5. 验证实时同步

 

注意:linux内核2.6.13版本以后提供inotify。

 

inotify用于监控文件系统事件,如文件存取、删除、移动、修改等。

 

1.安装inotify

# tar zxvf inotify-tools-3.13.tar.gz
# cd inotify-tools-3.13
# ./configure –prefix=/usr/local/inotify
# make
# make install

 

 

2.登录client部署rsync

 

登录client

# touch /etc/rsyncd.conf
# vi /etc/rsyncd.conf
增加
uid = root  
gid = root  
log file = /var/log/rsyncd.log 
pid file = /var/run/rsyncd.pid 

[rsync]  
path = /rsync  
read only = no 
ignore errors = yes 
hosts allow = 172.20.0.0/22  
hosts deny = *  

 

开启rsync

# rsync --daemon
# ps -ef | grep [r]sync
root      2612     1  0 11:37 ?        00:00:00 rsync --daemon

 

 

3.ssh认证

 

登录server

# ssh-keygen -b 1024 -t rsa
Generating public/private rsa key pair.
Enter file in which to save the key (/root/.ssh/id_rsa): 
Enter passphrase (empty for no passphrase): 
Enter same passphrase again: 
Your identification has been saved in /root/.ssh/id_rsa.
Your public key has been saved in /root/.ssh/id_rsa.pub.
The key fingerprint is:
7c:1f:b4:8b:2e:9c:55:94:a8:f3:9c:1a:c8:0d:6b:54 root@server

 

将文件copy至client,以后再server上rsync client就不需要输入密码

# scp -p /root/.ssh/id_rsa.pub root@172.20.45.38:/root/.ssh/authorized_keys
root@172.20.45.35's password: 
id_rsa.pub       
[root@server opt]# ssh client
Last login: Fri Aug 23 13:36:12 2013 from master
[root@client ~]# exit
logout

 

验证rsync是否需要密码

# rsync -azH --delete /rsync/ root@172.20.45.38:/rsync
#

 

4.开启实时同步

 

登录server

使用inotify监控/rsync目录

# inotifywait -mrq -e modify,create,attrib,move,delete /rsync

 

新开窗口进行测试操作,新建三个目录a、b、c观察inotifywait显示

# mkdir a b c

inotify显示
/rsync/ CREATE,ISDIR a
/rsync/ CREATE,ISDIR b
/rsync/ CREATE,ISDIR c

 

证明inotify监控成功。

 

设定实时同步脚本

# vi /root/InotifyRsync.sh 
增加
#!/bin/bash

/usr/local/bin/inotifywait -mrq -e modify,delete,create,attrib /rsync | while read file
        do
                rsync -avzP --delete /rsync/ root@172.20.45.38::rsync  > /dev/null
         done
exit 0

 

开启实时同步

# /root/InotifyRsync.sh &

 

 

5.验证实时同步

 

登录server

# cd /rsync
# rm -rf *
# mkdir a b c
# rm -rf c

 

登录client

# cd /rsync
# ls
a b

 

实时同步成功。

 

 

六、sersync实时同步

 

sersync主要用于服务器同步,web镜像等功能。基于boost1.43.0,inotify api,rsync command.开发。

是一款非常好用的实时同步软体、配置简单,功能强大,具体请至sersync官网。

 

http://code.google.com/p/sersync/

 

至此、完成。

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics