Rsync(Remote sync)
是数据镜像备份软件,可以实现对于数据的差异进行差异备份,用于减少数据备份的负担,数据的传输可以使用加密机制,Rsync
使用的端口是873
端口;Rsync
也是C/S
架构的服务模型,一个Rsync
服务器,但是允许有多个Rsync
客户端,这里使用一台Rsync server
,两台Rsync Client
来配置服务的规划
Rsync server: 172.25.23.8 [server7.com]
Rsync Clinet: 172.25.23.9[server8.com] 172.25.23.10[server9.com]
OS:Rhel6.5
首先安装服务
[root@server8 ~]# yum install rsync -y
- 对于
rsync server
来说需要提供三个配置文件,都是在/etc/
目录里面,这些文件默认是不存在的
[root@server8 etc]# cat /etc/rsyncd.conf
添加:
motd file = /etc/rsyncd.motd //欢迎
transfer logging = yes
log file = /var/log/rsyncd.log
pid file = /var/run/rsyncd.pid
lock file = /var/run/rsync.lock
port = 873
address = 172.25.23.8
uid = nobody
gid = nobody
use chroot = no
read only = yes
max connections = 10
[common]
comment = Test connect
path = /shared
ignore errors
#exclude=noshared/
auth users = tom,jerry
secrets file = /etc/rsyncd.secrets
hosts allow = 172.25.23.0/255.255.255.0
hosts deny=*
list = false
- 这里新建立目录
/shared
来进行共享
[root@server8 ~]# mkdir /shared
[root@server8 ~]# mkdir /shared/noshared
- 添加允许的用户帐号和密码文件,并且需要更改密码文件的权限
[root@server8 ~]# echo "tom:pass" > /etc/rsyncd.secrets
[root@server8 ~]# echo "jerry:jerry" >> /etc/rsyncd.secrets
[root@server8 etc]# chmod 600 rsyncd.secrets
- 接下来复制一些文件到共享的目录里面
[root@server8 ~]# cp -r /etc/* /shared/
- 使用命令启动服务
[root@server8 ~]# rsync --daemon
[root@server8 ~]# echo "/usr/bin/rsync --daemon" >> /etc/rc.local
- 接下来在两个
rsync client
上面安装rsync
服务,
[root@server9 ~]# yum install rsync -y
[root@server10 ~]# yum install rsync -y
- 在
rsync client
上面使用命令来同步rsync server
上面的数据
[root@server9 ~]# rsync -vzrtopg --progress tom@172.25.23.8::shared /shared
[root@server10 ~]# rsync -vzrtopg --progress tom@172.25.23.8::shared /shared
Password: //总金额里需要输入设置好的密码
receiving incremental file list
created directory /shared
./
passwd
1220 100% 1.16MB/s 0:00:00 (xfer#1, to-check=1/3)
noshared/
sent 81 bytes received 704 bytes 224.29 bytes/sec
total size is 1220 speedup is 1.55
- 对比三台主机上面的数据
Rsync server:
[root@server8 shared]# ll
total 8
drwxr-xr-x. 2 nobody nobody 4096 3月 19 10:44 noshared
-rw-r--r--. 1 root root 1220 3月 19 11:03 passwd
Rsync client
[root@server9 ~]# ll /shared/
total 8
drwxr-xr-x. 2 nobody nobody 4096 3月 19 10:44 noshared
-rw-r--r--. 1 root root 1220 3月 19 11:03 passwd
[root@server10 ~]# ll /shared/
total 8
drwxr-xr-x. 2 nobody nobody 4096 3月 19 10:44 noshared
-rw-r--r--. 1 root root 1220 3月 19 11:03 passwd
- 关于rsync命令的使用
rsync
:用于实现数据远程复制的工具
-v
:用于显示详细信息-q
:表示静默模式,不输出错误信息;-a
:表示归档模式,表示保留文件的属性信息;-r
:表示进行递归;-b
:如果存在同名文件,那么修改旧的文件名称为~filename
;--back-dir
:用于将备份文件保留在指定文件下面;--suffix
:用于指定备份文件前缀;-u
:表示仅仅进行更新操作,不使用下载的文件覆盖旧文件;-l
:保留符号链接;-p
:表示保留文件属性;-H
:表示保留硬链接;-A
:表示保留ACL
权限;-o
:保留所有者信息;-g
:保留文件所属组的信息;--devices
:保留设备文件;--special
:表示保留设备文件;-t
:表示保留修改时间属性;-W
:表示不进行增量检查,直接复制所有的文件;--existing
:表示仅仅同步目标路径中已经存在的文件,不下载源路径下新的文件;--include=
:匹配不需要排除的文件;--exclude=
:匹配需要配出的文件;--progress
:表示显示数据传输的进度信息;--partial
:表示保留因为故障传输没有完成的文件;
- 对于文件的同步,如果每次都需要使用手动命令来进行同步,是不能够进行实时同步的,即使将数据的同步做成计划任务,这里使用的一种方式是结合内核提供的
Inotify
机制来实现文件的差异的自动同步; - 内核提供的
Inotify
通常可以对下列的事件进行通知;
IN_ACCESS
:文件访问事件;IN_MODIFY
:文件修改事件;IN_ATTRIB
:文件属性修改事件;IN_CLOSE_WRITE
:表示可写文件关闭;IN_CLOSR_NOWRITE
:表示不可写文件关闭;IN_MOVED_FROM
:文件位置或者名称改变;IN_MODED_TO
:同上;IN_DELETE
:文件删除;IN_CREATE
:文件创建;IN_DELETE_SELF
:文件的自删除, 一个可执行文件在执行时,删除自己;
- 接下来需要编译安装
inotify-tools-master.zip
- 这个软件时需要安装在
rsync server
上面的,首先解决依赖关系
[root@server8 inotify-tools-master]# yum install automake gcc libtool
- 其次进行编译安装
[root@server8 inotify-tools-master]# ./autogen.sh
[root@server8 inotify-tools-master]# ./configure
[root@server8 inotify-tools-master]# make && make install
- 编译安装生成两个命令
inotifywait
以及inotifywatch
inotifywait
:
- 用于等待文件系统事件,多用于实时监控文件系统变化;
@<file>
:用于指定监控路径之外的文件,也就是不进行监控的文件;--fromfile <file>
:表示需要监控的以及不需要监控的文件在一个文件里面进行说明;-m
:表示在接收到事件之后不进行退出;-d
:表示程序进入后台执行,可以通过--outfile
指定事件信息的输出文件;-o
:用于将事件输出的位置,默认是标准输出;-s
:将错误信息输出至系统日志,默认输出到标准错误输出;-r
:表示进行递归监控;-q
:表示静默模式,也就是不输出任何信息;-t
:表示用于说明监控的时间,也就是超出时间没有事件发生,那么就退出;-e
:用于指定监控的事件类型;
- 首先在
server8.com
上面进行一个简单的测试 - 创建测试目录以及测试文件
[root@server8 inotify-tools-master]# mkdir /inotifytest -p
[root@server8 inotify-tools-master]# echo "hello" > /inotifytest/foo
[root@server8 inotify-tools-master]# inotifywait /inotifytest/
- 在一个
tty
上面执行这个命令,在另一个tty
查看文件
[root@server8 inotify-tools-master]# inotifywait /inotifytest/
Setting up watches.
Watches established.
[root@server8 ~]# cat /inotifytest/foo
hello
然后查看得到的通知信息
/inotifytest/ OPEN foo
- 上面信息表示的含义是
/inotifytest/ 的foo
执行了OPEN
操作; - 结合上面的两个服务,接下来配置
rsync+inotify
来实现文件的实时同步 这里来重新进行一个规划
server8.com: 用于配置Rsync+Inotify,这个节点用于发布和修改数据;
server9.com: 用于配置Rsync服务器,并且实时同步server8.com上面的数据;
server10.com:配置和server10.com上面的配置是相同的;为了方便查看各个节点数据的同步情况,这里可以使用配置
Web
服务来查看页面;- 首先来配置两台
Web
服务器
- 首先来配置两台
[root@server9 ~]# yum install httpd -y
[root@server9 ~]# mkdir /var/www/server9.com -p
[root@server9 ~]# chmod 600 /var/www/server9.com/
[root@server9 ~]# chown nobody.nobody /var/www/server9.com/
[root@server9 ~]# yum install rsync -y
[root@server9 ~]# vim /etc/rsyncd.conf
transfer logging = yes
log file = /var/log/rsyncd.log
pid file= /var/run/rsynvd.pid
lock file= /var/run/rsynvd.lock
uid = nobody
gid = nobody
use chroot = no
ignore errors
read only = no
[server9.com] 名字和脚本里面的名称保持一致
comment = Web content
path = /var/www/server8.com
auth users = tom
secrets file = /etc/rsyncd.secrets
hosts allow = 172.25.23.8
hosts deny=*
list = false
[root@server9 ~]# echo "tom:tom" > /etc/rsyncd.secrets
[root@server9 ~]# rsync --daemon
[root@server9 ~]# echo "rsync -daemon" >> /etc/rc.local
- 在另一个节点上面进行相同的操作
[root@server10 ~]# yum install httpd -y
[root@server10 ~]# yum install rsync -y
[root@server10 ~]# mkdir /var/www/server10.com -p
[root@server10 ~]# chmod 600 /var/www/server10.com/
[root@server10 ~]# chown nobody.nobody /var/www/server10.com/
[root@server10 ~]# vim /etc/rsyncd.conf
transfer logging = yes
log file = /var/log/rsyncd.log
pid file= /var/run/rsynvd.pid
lock file= /var/run/rsynvd.lock
uid = nobody
gid = nobody
use chroot = no
ignore errors
read only = no
[server10.com] 这个名字和脚本里面的名字需要保持一致
comment = Web content
path = /var/www/server10.com
auth users = tom
secrets file = /etc/rsyncd.secrets
hosts allow = 172.25.23.8
hosts deny=*
list = false
[root@server10 ~]# vim /etc/rsyncd.secrets
tom:tom
[root@server10 ~]# chmod 600 /etc/rsyncd.secrets
[root@server10 ~]# rsync --daemon
[root@server10 ~]# echo "rsync --daemon" >> /etc/rc.local
- 接下来配置
server8.com 也就是数据发布服务器
root@server8 ~]# yum install rsync -y
[root@server8 ~]# yum install automake libtool -y
- 第一次使用的软件包在
server8.com
上面执行make
命令进入了无限循环,这里换了软件包来进行安装
[root@server8 mnt]# tar -zxf inotify-tools-3.14.tar.gz
[root@server8 mnt]# cd inotify-tools-3.14
[root@server8 mnt]# ./configure
[root@server8 mnt]# make
[root@server8 mnt]# make install
- 添加密码文件,一定修改密码文件的权限为
600
[root@server8 mnt]# echo "tom" > /root/rsync.pass
[root@server8 mnt]# chmod 600 /root/rsync.pass
- 这里需要手动编辑一个脚本
[root@server8 mnt]# vim notify_rsync.sh
#! /bin/bash
export PATH=/bin:/usr/bin:/usr/local/bin
SRC=/web_data/ //需要事先进行创建,并且权限可读可写
DEST1=server9.com
DEST2=server10.com
Client1=172.25.23.9
Client2=172.25.23.10
User=tom
Passfile=/root/rsync.pass
[ ! -e $Passfile ] && exit 2
inotifywait -mrq --timefmt '%y-%m-%d %H:%M' --format '%T %w%f %e' --event modify,create,move,delete,attrib $SRC|while read line
do
echo "$line" > /var/log/inotify_web 2>&1
/usr/bin/rsync -avz --delete --progress --password-file=$Passfile $SRC ${User}@$Client1::$DEST1 >> /var/log/sync_web1 2>&1
/usr/bin/rsync -avz --delete --progress --password-file=$Passfile $SRC ${User}@$Client2::$DEST2 >> /var/log/sync_web2 2>&1
done &
- 将这个脚本交给
rc.local
进行托管
[root@server8 mnt]# echo "/mnt/notify_rsync.sh" >> /etc/rc.local
- 然后执行这个脚本
[root@server8 ~]# /mnt/notify_rsync.sh
- 同时查看以下里面的日志信息
[root@server8 ~]# tail -f /var/log/sync_web1
sent 48 bytes received 11 bytes 118.00 bytes/sec
total size is 36 speedup is 0.61
rsync error: some files/attrs were not transferred (see previous errors) (code 23) at main.c(1039) [sender=3.0.6]
sending incremental file list
./
rsync: failed to set times on "." (in server9.com): Operation not permitted (1)
sent 48 bytes received 11 bytes 118.00 bytes/sec
total size is 36 speedup is 0.61
rsync error: some files/attrs were not transferred (see previous errors) (code 23) at main.c(1039) [sender=3.0.6]
这个里面就是可能出现的错误信息;
这里来修改
server9.com
以及server10.com
上面的默认发布目录
[root@server9 www]# vim /etc/httpd/conf/httpd.conf
DocumentRoot "/var/www/server9.com"
[root@server9 www]# /etc/init.d/httpd reload
Reloading httpd:
[root@server10 www]# vim /etc/httpd/conf/httpd.conf
DocumentRoot "/var/www/server10.com"
[root@server10 www]# /etc/init.d/httpd reload
Reloading httpd:
- 然后添加默认的发布页面,在
server8.com
的/web_data
目录里面
[root@server8 ~]# cat /web_data/index.html
<h1>hello.inotify.success.com</h1>
然后通过浏览器访问两个页面
对于在
inotify
过程中见出现的错误问题,这里转载别人写的很不错的一片博客,我遇到的错误就是在别人这里看到的
[错误总结 转载别人的] (http://blog.youkuaiyun.com/syaving_________/article/details/65437534%20%20%E9%94%99%E8%AF%AF%E6%80%BB%E7%BB%93)