SCP、NFS、SFTP、http、samba、rsync、drbd(基于文件系统同步,效率高)
rsync,英文全称是remote synchronize,是一款实现远程同步功能的免费软件,它在同步文件的同时,可以保持原来文件的权限、时间、软硬链接等附加信息。 rsync是用 “rsync 算法”提供了一个客户机和远程文件服务器的文件同步的快速方法,而且可以通过ssh方式来传输文件。甚至还可以实现只同步一个文件里有变化的内容部分,所以可以实现快速的同步备份数据。
同时,rsync还可以实现同步本地数据、删除文件和目录的功能。一个rsync相当于scp、cp、rm,并且还在性能上优于它们每一个命令。
能更新整个目录和树和文件系统;
有选择性的保持符号链链、硬链接、文件属于、权限、设备以及时间等;
对于安装来说,无任何特殊权限要求;
对于多个文件来说,内部流水线减少文件等待的延时;
能用rsh、ssh 或直接端口做为传输入端口(rsync本身不对数据加密);
支持匿名rsync 同步文件,是理想的镜像工具;
(1)服务器之间的数据同步(cron+rsync)
(2)把所有客户服务器数据同步到备份服务器(cron+rsync)
(3)与inotify或sersync配合,做实时的数据同步(rsync+inotify/sersync)
-v, --verbose 详细模式输出。
-z, --compress 对备份的文件在传输时进行压缩处理。
-a, --archive 归档模式,表示以递归方式传输文件,并保持所有文件属性,等于-rlptgoD。
-r, --recursive 对子目录以递归模式处理。
-t, --times 保持文件时间信息。
-o, --owner 保持文件属主信息。
-p, --perms 保持文件权限。
-g, --group 保持文件属组信息。
-D, --devices 保持设备文件信息。
-l, --links 保留软链结。
-e, --rsh=command 指定使用rsh、ssh方式进行数据同步。
--exclude=PATTERN 指定排除不需要传输的文件模式。
--exclude-from=FILE 排除FILE中指定模式的文件。
--bwlimit=KBPS 限制I/O带宽,KBytes per second。
一般运维中经常使用的参数是-avz(相当于-vzrtopgDl)
a、使用--bwlimit参数限制带宽
rsync -avz --bwlimit=100 -e 'ssh' a.log oldboy@192.168.58.85:/wddg/log/
b、使用-avz参数
rsync -avz -e 'ssh' a.logoldboy@192.168.58.85:/wddg/log/
一般来说,rsync大致使用3种主要的传输数据方式。
a、语法
rsync [选项] 源文件或目录 目标文件或目录
b、实例1:普通拷贝
rsync /etc/hosts /tmp/ 等价于 cp /etc/hosts /tmp/
c、实例2:保持文件属性
rsync –avz /etc/hosts /tmp/ 等价于cp -dpr /etc/hosts /tmp/ #-dpr=-a
d、实例3:删除文件
rsync删除文件或目录的实质还是拷贝,就是将源目录去替换目标目录。源目录中是什么,目标目录中就是什么,目标目录中原有的文件均被删除
(i)首先,创建一个空目录做为源目录
mkdir /srcnull
(ii)在目标目录中创建文件
touch /data/a{1,2,3}.txt
ll /data
-rw-r--r-- 1 root root 0 Feb 20 02:45 a1.txt
-rw-r--r-- 1 root root 0 Feb 20 02:45 a2.txt
-rw-r--r-- 1 root root 0 Feb 20 02:45 a3.txt
(iii)删除时,源目录后不带/
rsync –r -–delete /srcnull /data/ #srcnull后没有加/,删除不成功,没能删除/data下的文件
ll /data
-rw-r--r-- 1 root root 0 Feb 20 02:45 a1.txt
-rw-r--r-- 1 root root 0 Feb 20 02:45 a2.txt
-rw-r--r-- 1 root root 0 Feb 20 02:45 a3.txt
(iv)删除时,源目录后带/
rsync –r –-delete /srcnull/ /data/ #srcnull后加/,删除成功,删除了/data下的文件
ll data/
total 0
(v)原因:如果src目录后不带斜杠,那么是将src目录复制到dest中,包含了src目录,带斜杠是把src目录下的文件同步到dest中,不包含src目录,会把目标目录下所有文件删除,再同步。
(vi)源目录中存在文件
touch /srcnull/ddd.txt
touch /data/a{1,2,3}.txt
ll /data
-rw-r--r-- 1 root root 0 Feb 20 02:45 a1.txt
-rw-r--r-- 1 root root 0 Feb 20 02:45 a2.txt
-rw-r--r-- 1 root root 0 Feb 20 02:45 a3.txt
ll srcnull/
-rw-r--r-- 1 root root 0 Feb 20 02:49 ddd.txt
(vii)删除,源目录中的文件同步到目标目录中了,目标目录中的原有文件全部被删除
rsync –r –delete /srcnull/ /data/
ll /data
-rw-r--r-- 1 root root 0 Feb 20 02:50 ddd.txt
a、传统scp传输方式
(i)推送方式
scp -rp -P9080 /etc/hosts test@192.168.1.8:/tmp
(ii)拉取方式
scp -rp -P9080 test@192.168.1.8:/tmp /etc/hosts
b、rsync传输方式
(i)推送方式
rsync -avz -e 'ssh' a.log oldboy@192.168.58.85:/wddg/log/
(ii)拉取方式
rsync -avz -e 'ssh' oldboy@192.168.1.8:/tmp /etc/hosts
a、演示环境
主机名 |
IP |
用途 |
操作系统 |
linuxDelivery |
192.168.58.238 |
服务端A_备份服务器 |
CentOS6.8 |
StudyLinux |
192.168.58.85 |
客户端B_发送服务器 |
CentOS6.8 |
LS-B16175 |
192.168.16.75 |
客户端C_发送服务器 |
RedHat6.4 |
b、服务端A_生成rsync配置文件/etc/rsyncd.conf(需手动生成)
vi /etc/rsyncd.conf
#rsync-config------------------------start
#create by Study rsync 13:11 2017-02-21
##rsyncd.conf start##
uid = rsync #指定rsync服务进程的属主
gid = rsync #指定rsync服务进程的属组
use chroot = no #安全方面的参数,首先chroot到path参数指定的目录下。
max connections = 200 #允许的最大连接数
timeout = 300 #超时日间
pid file = /var/run/rsyncd.pid #指定rsync的pid文件
lock file = /var/run/rsync.lock #锁文件
log file = /var/log/rsyncd.log #日志文件
[StudyRsync] #[]表示模块,模块名就是rsync客户端看到的目录名
path = /wddg/rsync/ #rsync在服务端的路径
ignore errors #忽略错误
read only = false #指定目录可读可写(rw)
list = false #不允许客户端对共享目录进行列表操作
hosts allow = 192.168.0.0/16 #允许访问的主机。可以是IP,也可以是网段
hosts deny = 0.0.0.0/32 #拒绝访问的主机
auth users = rsync_backup #空格或逗号分隔的用户列表,为虚拟用户,列表中的用户才能连接该模块。
secrets file = /etc/rsync.password #虚拟用户的密码文件.格式为用户名:密码
#rsync-config------------------------end
c、服务端A_以守护进程方式启动rsync
rsync --daemon
d、服务端A_查看是否启动成功(rsync的监听端口为873)
netstat -lntup | grep rsync
tcp 0 0 0.0.0.0:873 0.0.0.0:* LISTEN 3747/rsync
tcp 0 0 :::873 :::* LISTEN 3747/rsync
cat /var/log/rsyncd.log
2017/02/21 14:01:39 [3747] rsyncd version 3.0.6 starting, listeningon port 873
e、服务端A_新建rsync用户
useradd rsync -s /sbin/nologin
id rsync
uid=503(rsync) gid=503(rsync) groups=503(rsync)
f、服务端A_新建rsync共享目录
mkdir /wddg/rsync/
g、服务端A_将rsync共享目录属主(组)改为rysnc
chown -R rsync.rsync /wddg/rsync
h、服务端A_新建rsync密码文件
echo "rsync_backup:mypassword" > /etc/rsync.password
i、服务端A_降低rsync密码文件的权限,保证安全
chmod 600 /etc/rsync.password
j、客户端B_创建rsync密码文件(只需密码,不需用户)
echo "mypassword" > /etc/rsync.password
k、客户端B _降低rsync密码文件的权限,保证安全
chmod 600 /etc/rsync.password
l、客户端C_创建rsync密码文件(只需密码,不需用户)
echo "mypassword" > /etc/rsync.password
m、客户端C _降低rsync密码文件的权限,保证安全
chmod 600 /etc/rsync.password
n、客户端B_拉取方式获取服务端A数据(需密碼)
rsync 参数 虚拟用户 IP 模块名 本地目录
rsync -avz rsync_backup@192.168.58.238::StudyRsync /wddg/share/ #需密碼
o、客户端B_拉取方式获取服务端A数据(指定密碼文件方式,無需密碼)
rsync -avz rsync_backup@192.168.58.238::StudyRsync/wddg/share/ --password-file=/etc/rsync.password #指定密碼文件,無需密碼,自動傳輸
p、客户端B_推送方式获取服务端A数据(指定密碼文件方式,無需密碼)
touch /wddg/share/{1..10}
rsync -avz /wddg/share/ rsync_backup@192.168.58.238::StudyRsync--password-file=/etc/rsync.password
说明:推送时,没有/时(/wddg/share)表示推送目录share,有/时(/wddg/share/)表示推送share下的所有文件.
q、客户端B_再推一次數据到服务端A(rsync不会推送同样的数据了)
rsync -avz /wddg/share/ rsync_backup@192.168.58.238::StudyRsync--password-file=/etc/rsync.password
显示结果为:total sizeis o speedup is 0.00
r、客户端C_拉取方式获取服务端A数据(指定rsync协议)
rsync -avz rsync://rsync_backup@192.168.58.238/StudyRsync/wddg/share/ --password-file=/etc/rsync.password
s、客户端C_推送方式获取服务端A数据(指定rsync协议)
rsync -avz /wddg/share/ rsync://rsync_backup@192.168.58.238/StudyRsync--password-file=/etc/rsync.password
a、配置文件/etc/rsyncd.conf
(i)文件名结尾有d,不要忘了。
(ii)文件内容要有:用户、目录、模块、虚拟用户、密码文件名和路径、日志文件名和路径
如果有多个模块,且共用一个虚拟用户、密码文件名和路径等信息的,可以把这些信息放在模块上面的公共区域。如果希望不同模块使用不同虚拟用户、密码文件名和路径等信息的,可以在每个模块下面保留相应信息。
示例:
uid = rsync #指定rsync服务进程的属主
gid = rsync #指定rsync服务进程的属组
use chroot = no #安全方面的参数,首先chroot到path参数指定的目录下。
max connections = 200 #允许的最大连接数
timeout = 300 #超时日间
pid file = /var/run/rsyncd.pid #指定rsync的pid文件
lock file = /var/run/rsync.lock #锁文件
log file = /var/log/rsyncd.log #日志文件
ignore errors #忽略错误
read only = false #指定目录可读可写(rw)
list = false #不允许客户端对共享目录进行列表操作
hosts allow = 192.168.0.0/16 #允许访问的主机。可以是IP,也可以是网段
hosts deny = 0.0.0.0/32 #拒绝访问的主机
auth users = rsync_backup #空格或逗号分隔的用户列表,为虚拟用户,列表中的用户才能连接该模块。
secrets file = /etc/rsync.password #虚拟用户的密码文件.格式为用户名:密码
[StudyRsync] #[]表示模块,模块名就是rsync客户端看到的目录名
path = /wddg/rsync/ #rsync在服务端的路径
[StudyTest]
path = /wddg/test/
b、创建共享目录。
根据配置文件中配置的目录,创建共享目录。
c、创建rsync用户,并授权访问共享目录
根据配置文件中配置的用户,创建相应的rsync用户,并授权访问共享目录
d、创建密码文件
(i)根据配置文件中配置的密码文件及路径,创建相应的密码文件。
一定要复制配置文件中的路径和文件名,防止出错。
(ii)内容格式:配置文件中的虚拟用户名:密码
(iii)密码文件的权限600
e、将rsync以守护进程方式自动启动
在/etc/rc.local中添加命令:/usr/bin/rsync --daemon
echo "/usr/bin/rsync --daemon">>/etc/rc.local
tail -1 /etc/rc.local
/usr/bin/rsync --daemon
f、查看rsync日志
根据配置文件中的日志路径和文件名,查看查看rsync日志
tail /var/log/rsyncd.log
a、密码文件
(i)客户端密码文件的文件名和路径与服务端没有任何关系。
之所以也命名为/etc/rsync.password是为了便于管理。
(ii)内容格式:密码
密码文件中的内容只有密码,并且必须要与服务端的密码文件中的密码相同。
(iii)密码文件的权限600
b、同步
(i)推
rsync -avz /wddg/share/ rsync://rsync_backup@192.168.58.238/StudyRsync--password-file=/etc/rsync.password
(ii)拉
rsync -avz rsync://rsync_backup@192.168.58.238/StudyRsync/wddg/share/ --password-file=/etc/rsync.password
a、防火墙和selinux要关闭
b、排错时,一定要看日志
c、排错时,一定从部署流程整体考虑。
如果是部署时就出错,一定是部署流程有问题。如果是运行很长一段时间后出错,要看看有没有被攻击或被修改。
d、要有良好的操作习惯。
一定要多拷贝。
主机名 |
IP |
用途 |
操作系统 |
linuxDelivery |
192.168.58.238 |
服务端A_备份服务器 |
CentOS6.8 |
StudyLinux |
192.168.58.85 |
客户端B_发送服务器 |
CentOS6.8 |
LS-B16175 |
192.168.16.75 |
客户端C_发送服务器 |
RedHat6.4 |
ps -ef | grep rsync
root 3747 1 0 Feb21 ? 00:00:00 rsync --daemon
lsof -i :873
COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME
rsync 3747 root 4u IPv4 18077 0t0 TCP *:rsync (LISTEN)
rsync 3747 root 5u IPv6 18078 0t0 TCP *:rsync (LISTEN)
rsync -avz /wddg/share/ rsync_backup@192.168.58.238::StudyRsync--password-file=/etc/rsync.password
--exclude=PATTERN #需排除的文件较少时用
--exclude-from=FILE #需排除的文件过多是,可以将文件名写入一个文件中,再指定排除
ls /wddg/share
10.txt 2.txt 4.txt 6.txt 8.txt a.txt c.txt e.txt g.txt
1.txt 3.txt 5.txt 7.txt 9.txt b.txt d.txt f.txt test
ll /wddg/rsync
total 0
rsync -avz --exclude=a.txt /wddg/share/rsync_backup@192.168.58.238::StudyRsync --password-file=/etc/rsync.password
sending incremental file list
./
1.txt
10.txt
2.txt
3.txt
4.txt
5.txt
6.txt
7.txt
8.txt
9.txt
b.txt
c.txt
d.txt
e.txt
f.txt
g.txt
test/
sent 872 bytes received 319bytes 794.00 bytes/sec
total size is 0 speedup is0.00
说明:传输的文件中没有a.txt
ls /wddg/rsync
10.txt 2.txt 4.txt 6.txt 8.txt b.txt d.txt f.txt test
1.txt 3.txt 5.txt 7.txt 9.txt c.txt e.txt g.txt
#在客户端B将文件同步到服务端A,并排除a.txt,b.txt,c.txt
rsync -avz --exclude={a.txt,b.txt,c.txt} /wddg/share/
rsync_backup@192.168.58.238::StudyRsync--password-file=/etc/rsync.password
sending incremental file list
./
1.txt
10.txt
2.txt
3.txt
4.txt
5.txt
6.txt
7.txt
8.txt
9.txt
d.txt
e.txt
f.txt
g.txt
test/
sent 768 bytes received 281bytes 2098.00 bytes/sec
total size is 0 speedup is0.00
#在客户端B将文件同步到服务端A,并排除a.txt,b.txt,c.txt,d.txt,e.txt,f.txt
rsync -avz --exclude={a..f}.txt /wddg/share/ rsync_backup@192.168.58.238::StudyRsync--password-file=/etc/rsync.password
sending incremental file list
./
1.txt
10.txt
2.txt
3.txt
4.txt
5.txt
6.txt
7.txt
8.txt
9.txt
g.txt
test/
sent 606 bytes received 224bytes 553.33 bytes/sec
total size is 0 speedup is0.00
vi /wddg/share/paichu.log
1.txt
2.txt
3.txt
4.txt
5.txt
6.txt
7.txt
8.txt
9.txt
10.txt
rsync -avz --exclude-from=paichu.log/wddg/share/ rsync_backup@192.168.58.238::StudyRsync--password-file=/etc/rsync.password
sending incremental file list
./
.paichu.log.swp
a.txt
b.txt
c.txt
d.txt
e.txt
f.txt
g.txt
paichu.log
test/
sent 797 bytes received 186bytes 655.33 bytes/sec
total size is 12349 speedupis 12.56
vi /etc/rsyncd.conf
uid = rsync
gid = rsync
use chroot = no
max connections = 200
timeout = 300
pid file = /var/run/rsyncd.pid
lock file = /var/run/rsync.lock
log file = /var/log/rsyncd.log
[StudyRsync]
path = /wddg/rsync/
ignore errors
read only = false
list = false
hosts allow = 192.168.0.0/16
hosts deny = 0.0.0.0/32
auth users = rsync_backup
secrets file = /etc/rsync.password
exclude=a.txt b.txt
a、方法一:kill `cat/var/run/rsyncd.pid`
b、方法二:pkill rsync
c、方法三:ps -ef | greprsync 查出pid,再kill
ps -ef | grep "rsync --daemon" | grep -v grep | awk'{print $2}'
rsync --daemon
rsync -avz /wddg/share/ rsync_backup@192.168.58.238::StudyRsync--password-file=/etc/rsync.password
sending incremental file list
./
1.txt
10.txt
2.txt
3.txt
4.txt
5.txt
6.txt
7.txt
8.txt
9.txt
skipping daemon-excluded file "a.txt"
skipping daemon-excluded file "b.txt"
c.txt
d.txt
e.txt
f.txt
g.txt
paichu.log
test/
sent 968 bytes received 319bytes 2574.00 bytes/sec
total size is 12349 speedupis 9.60
rsync error: some files/attrs were not transferred(see previous errors) (code 23) at main.c(1039) [sender=3.0.6]
a、排除单个文件
rsync -avz --exclude={a} /wddg/share/rsync_backup@192.168.58.238::StudyRsync--password-file=/etc/rsync.password
b、排除多个文件:方法一(不连续的文件名)
rsync -avz --exclude={a,c} /wddg/share/rsync_backup@192.168.58.238::StudyRsync--password-file=/etc/rsync.password
c、排除多个文件:方法二(连续文件名)
rsync -avz --exclude={a..f}/wddg/share/ rsync_backup@192.168.58.238::StudyRsync--password-file=/etc/rsync.password
d、排除多个文件:方法三
rsync -avz --exclude=a --exclude=d /wddg/share/ rsync_backup@192.168.58.238::StudyRsync--password-file=/etc/rsync.password
d、排除多个文件:方法四(指定文件,针对不规律文件名较多时)
rsync -avz --exclude-from=paichu.log/wddg/share/ rsync_backup@192.168.58.238::StudyRsync--password-file=/etc/rsync.password
a、在服务端上修改配置文件/etc/rsync.conf,加上exclude=xxx参数(多个文件用空格分隔)
b、格式示例:exclude=a b /wddg/c.txt
c、与客户端比较,服务端使用排除传输比较麻烦。
差异同步(普通同步)时,如果是拉取方式(PULL)同步,只能保证服务端目录有的,客户端目录一定会有,但客户端目录中原来有的,服务端目录不一定会有。如果是推送方式(PUSH)同步,只能保证客户端目录有的,服务端目录一定会有,但服务端目录中原来有的,客户端目录不一定会有。所以称为有差异同步。
无差异同步就是客户端和服务端目录的内容完全一样。
在某些场合下,要求保证生产服务器的内容与备份服务器内容完全一样。一般是2台服务器之间,必须要求数据一致,且实时性又不是很高的情况下使用。如2台负载均衡下面web服务器之间的同步,或者是高可用双机配置之间的同步等。但无差异同步的影响(风险)很大,而且有很多替代方案。因此,生产场景中没有特殊的需求,应避免使用。
风险是也就是本地服务器有什么,远端服务器就有什么,远端服务器原有数据均被删除
风险是也就是远端服务器有什么,本地服务器就有什么,本地服务器原有数据均被删除而丢失
rsync -avz --delete /wddg/share/ rsync_backup@192.168.58.238::StudyRsync
--password-file=/etc/rsync.password
rm –fr *
rsync -avz --delete /wddg/share/ rsync_backup@192.168.58.238::StudyRsync
--password-file=/etc/rsync.password
sending incremental file list
./
deleting g
deleting f
deleting e
deleting d
deleting c
deleting b
deleting a
deleting 9
deleting 8
deleting 7
deleting 6
deleting 5
deleting 4
deleting 3
deleting 2
deleting 10
deleting 1
sent 29 bytes received 11bytes 26.67 bytes/sec
total size is 0 speedup is0.00
ll /wddg/rsync
total 0
(1)部署流程要熟练
(2)原理要理解
(3)要学会看日志,包括命令行输出和日志/var/log/rsyncd.log
(1)先检查配置文件
(2)再检查的用户对不对
(3)检查密码文件对不对,复制配置文件中的密码文件名检查,不要用手敲,要复制。
(4)检查密码文件属性
(5)查看日志文件
如查密码文件不存在(含指定的密码文件名错误),会在输入密码后报在模块上认证失败的错误。错误内容如下:
@ERROR: auth failed on module
rsync error: error startingclient-server protocol (code 5) at main.c (1503) [receiver=3.0.6]
a、rsync要求密码文件不能让其它用户访问,如果文件权限过大,会报验证失败错误。
在日志文件中会出现如下内容:
secrets file must not be other-accessible(see strict modes optain)
continuing without secrets file
auth failed on module xxx from unknow (IP) missing secret for user"username"
b、检查密码文件权限
ll /etc/rsync.password
-rw-r--r-- 1 root root 24 Feb 21 14:16 /etc/rsync.password #644,过大
c、改为600
chmod 600 /etc/rsync.password
-rw------- 1 root root 24 Feb 21 14:16 /etc/rsync.password #600
rsync: failed to connect to xx.xx.xx.xx: No route to host(113)
@ERROR: chdir failed
rsync: recv_generator:mkdir xxx failed:Permission denied(13)
1、http://rsync.samba.org/
2、http://www.samba.org/ftp/rsync/rsync.html
3、http://www.samba.org/ftp/rsync/rsyncd.conf.html
4、man rsync
5、man rsyncd.conf
inotify是一个从2.6.13内核开始,对Linux文件系统进行高效率、细粒度、异步地监控机制,用于通知用户空间程序的文件系统变化。可利用它对用户空间进行安全、性能、以及其他方面的监控。Inotify 反应灵敏,用法非常简单,并且比 cron 任务的繁忙轮询高效得多。如果内核版本不低于 2.6.13,系统就支持 inotify。如果存在/usr/include/sys/inotify.h 文件,表明内核支持 inotify。从文件管理器到安全工具,文件系统监控对于的许多程序来说都是必不可少的。它允许监控程序打开一个独立文件描述符,并针对事件集监控一个或者多个文件,例如打开、关闭、移动/重命名、删除、创建或者改变属性。第三方程序可以通过这个内核接口监控文件系统的各种变化。inotify-tools就是一款这样的监控软件。
IN_ACCESS:文件被访问
IN_MODIFY:文件被修改
IN_ATTRIB,文件属性被修改
IN_CLOSE_WRITE,以可写方式打开的文件被关闭
IN_CLOSE_NOWRITE,以不可写方式打开的文件被关闭
IN_OPEN,文件被打开
IN_MOVED_FROM,文件被移出监控的目录
IN_MOVED_TO,文件被移入监控着的目录
IN_CREATE,在监控的目录中新建文件或子目录
IN_DELETE,文件或目录被删除
IN_DELETE_SELF,自删除,即一个可执行文件在执行时删除自己
IN_MOVE_SELF,自移动,即一个可执行文件在执行时移动自己
inotify可以监控文件,也可以监控目录。当监控目录时,它可以同时监控目录及目录中的各子目录及文件的。此外,inotify 使用文件描述符作为接口,因而可以使用通常的文件I/O操作select、poll和epoll来监视文件系统的变化。
inotify:功能简单,性能高
sersync:国产,c封装,功能较多,可过滤。
lsyncd:开源,轻量级的实时镜像解决方案。
无论是手动执行rsync还是把rsync客户端发出的数据同步请求命令做成周期性任务计划,每隔一段时间不管有没有数据变化都发出一次数据同步请求命令,同步一次数据。服务端和客户端都有时间差。
所以,使用内核提供的inotify机制,当数据发生改变时(删除、修改等)就触发rsync客户端发出数据 同步请求。从而实现数据的实时传输。
Rsync + inotify 机制实现的两台服务器数据同步如下图如示:
主机名 |
IP |
用途 |
操作系统 |
linuxDelivery |
192.168.58.238 |
服务端A_备份服务器 |
CentOS6.8 |
StudyLinux |
192.168.58.85 |
客户端B_发送服务器 |
CentOS6.8 |
(1)rysnc --daemon服务正常
(2)客户端可能正常推送和拉取数据
(3)是在客户端配置inotify
ps -ef | grep rsync
rsync -avz /wddg/share/ rsync_backup@192.168.58.238::StudyRsync--password-file=
/etc/rsync.password
uname -r
a、检查系统相关参数
ls -l /proc/sys/fs/inotify
-rw-r--r-- 1 root root 0 4月 22 14:56 max_queued_events
-rw-r--r-- 1 root root 0 4月 22 14:56 max_user_instances
-rw-r--r-- 1 root root 0 4月 22 14:56 max_user_watches
b、参数说明:
max_queued_evnets:表示调用inotify_init时分配给inotify instance中可排队的event的数目的最大值,超出这个值的事件被丢弃,但会触发IN_Q_OVERFLOW事件。所以需要调大一些。
例如: echo 327679 > /proc/sys/fs/inotify/max_queued_events
max_user_instances:表示每一个real user ID可创建的inotify instatnces的数量上限。
max_user_watches:表示每个inotify instatnces可监控的最大目录数量。如果监控的文件数目巨大,需要根据情况,适当增加此值的大小。
例如: echo 30000000 > /proc/sys/fs/inotify/max_user_watches
wget http://github.com/downloads/rvoicilas/inotify-tools/inotify-tools-3.14.tar.gz
cd /wddg/unzip
tar zxf inotify-tools-3.14.tar.gz
cd inotify-tools-3.14
./configure--prefix=/usr/local/inotify-3.14
make
make install
make && make install
echo $?
0
ln -s /usr/local/inotify-3.14//usr/local/inotify
cd /usr/local/inotify
ls –l
drwxr-xr-x 2 root root 4096 Feb 21 23:10 bin #inotify的二进制命令
drwxr-xr-x 3 root root 4096 Feb 21 23:10 include #inotify程序所需的头文件
drwxr-xr-x 2 root root 4096 Feb 21 23:10 lib #动态链接库文件
drwxr-xr-x 4 root root 4096 Feb 21 23:10 share #帮助文档
tree
.
|-- bin
| |-- inotifywait
| `-- inotifywatch
|-- include
| `-- inotifytools
| |-- inotify-nosys.h
| |-- inotify.h
| `-- inotifytools.h
|-- lib
| |-- libinotifytools.a
| |-- libinotifytools.la
| |-- libinotifytools.so -> libinotifytools.so.0.4.1
| |-- libinotifytools.so.0 -> libinotifytools.so.0.4.1
| `-- libinotifytools.so.0.4.1
`-- share
|-- doc
| `-- inotify-tools
| |-- doxygen.css
| |-- doxygen.png
| |-- files.html
| |-- globals.html
| |-- globals_func.html
| |-- index.html
| |--inotifytools_8c_source.html
| |-- inotifytools_8h.html
| |--inotifytools_8h_source.html
| |-- pages.html
| |-- tab_b.gif
| |-- tab_l.gif
| |-- tab_r.gif
| |-- tabs.css
| `-- todo.html
`-- man
`-- man1
|-- inotifywait.1
`-- inotifywatch.1
./bin/inotifywait –help
-r|--recursive Watchdirectories recursively.
-q|--quiet Print less (only print events).
-m|--monitor Keep listeningfor events forever. Without this option, inotifywait will exit afterone event is received.
--excludei
--timefmt
--format
a、%w:显示被监控文件的文件名;
b、%f:如果发生某事件的对象是目录,则显示被监控目录的名字;默认显示为空串;
c、%T:使用--timefmt选项中自定义的时间格式;
d、%e 表示发生的事件
e、%Xe 事件以“X”分隔
-e|--event
access file or directory contents were read
modify file or directory contents were written
attrib file or directory attributes changed
close file or directory closed, regardless of read/write mode
open file or directory opened
moved_to file or directory moved to watched directory
move file or directory movedto or from watched directory
create file or directory created within watcheddirectory
delete file or directory deleted within watched directory
unmount file system containing file or directory unmounted
在客户端B上监控/wddg/share目录的create事件,并打印出创建的文件名。打印格式为YYYY-MM-DD HH24:MI 文件名
/usr/local/inotify-3.14/bin/inotifywait-mrq --timefmt '%Y-%m-%d %H:%M' --format '%T %e %w%f' -e create /wddg/share
touch test.txt
mkdir aa
2017-02-22 00:15 /wddg/share/test.txt
2017-02-22 00:15 /wddg/share/aa
/usr/local/inotify-3.14/bin/inotifywait-mrq --timefmt '%Y-%m-%d %H:%M' --format '%T %e %w%f' -e create /wddg/share
mkdir a
touch b.txt
2017-02-22 00:36 CREATE,ISDIR/wddg/share/a
2017-02-22 00:36 CREATE/wddg/share/b.txt
/usr/local/inotify-3.14/bin/inotifywait-mrq --timefmt '%Y-%m-%d %H:%M' --format '%T %e %w%f' -e create,delete/wddg/share
rm -f b.txt
rm -fr aa
2017-02-22 00:43 DELETE/wddg/share/b.txt
2017-02-22 00:43 DELETE,ISDIR/wddg/share/aa
/usr/local/inotify-3.14/bin/inotifywait-mrq --timefmt '%Y-%m-%d %H:%M' --format '%T %e %w%f' -e create,delete,close_write/wddg/share
touch x.txt
echo 111>a.log
2017-02-22 00:47 CREATE/wddg/share/x.txt
2017-02-22 00:47 CLOSE_WRITE,CLOSE/wddg/share/x.txt
2017-02-22 00:48 CREATE /wddg/share/a.log
2017-02-22 00:48 CLOSE_WRITE,CLOSE/wddg/share/a.log
由于在生产环境中,需要把变化的文件推送到其他服务器,并不需要时间,只需要文件的全路径,调用rsync将其推送到服务端,所以事件的输出可以简化到只输出文件的全路径
/usr/local/inotify-3.14/bin/inotifywait-mrq --format ' %w%f' -e create /wddg/share
vim /server/scripts/inotify.sh
#!/bin/sh
cmd="/usr/local/inotify-3.14/bin/inotifywait"
$cmd -mrq --format '%w%f' -ecreate,delete,close_write,attrib /wddg/share |\
while read line
do
rsync -az --delete $line rsync_backup@192.168.158.198::StudyRsync--password-file=/etc/rsync.password
done
sh -x inotify.sh
+cmd=/usr/local/inotify-3.14/bin/inotifywait
+ read line
+ /usr/local/inotify-3.14/bin/inotifywait-mrq --format %w%f -e create,delete,close_write /wddg/share
ll /wddg/rsync
total 0
touch a
+ rsync -az --delete /wddg/share/arsync_backup@192.168.158.198::StudyRsync --password-file=/etc/rsync.password
+ read line
+ rsync -az --delete /wddg/share/arsync_backup@192.168.158.198::StudyRsync --password-file=/etc/rsync.password
+ read line
ll
-rw-r--r--. 1 rsync rsync 0 Feb 22 01:14 a
touch {1..1000}
ls
1 11 13 15 17 19 20 22 24 26 28 3 31 33 35 37 39 40 42 5 7 9
10 12 14 16 18 2 21 23 25 27 29 30 32 34 36 38 4 41 43 6 8 a
ls
1 11 13 15 17 19 20 22 24 26 28 3 31 33 35 37 39 40 42 44 5 7 9
10 12 14 16 18 2 21 23 25 27 29 30 32 34 36 38 4 41 43 45 6 8 a
ls
1 11 13 15 17 19 20 22 24 26 28 3 31 33 35 37 39 40 42 44 46 5 7 9
10 12 14 16 18 2 21 23 25 27 29 30 32 34 36 38 4 41 43 45 47 6 8 a
rm -f *
rsync: link_stat "/wddg/share/9" failed: No such file ordirectory (2)
rsync error: some files/attrs were not transferred (see previouserrors) (code 23) at main.c(1039) [sender=3.0.6]
vim /server/scripts/inotify1.sh
#!/bin/sh
cmd="/usr/local/inotify-3.14/bin/inotifywait"
$cmd -mrq --format '%w%f' -e create,delete,attrib /wddg/share |\
while read line
do
[ ! -e $line ] &&continue
rsync -arzu --delete $linersync_backup@192.168.58.238::StudyRsync --password-file=/etc/rsync.password>/dev/null 2>&1
done
vim /server/scripts/inotify2.sh
#!/bin/sh
cmd="/usr/local/inotify-3.14/bin/inotifywait"
$cmd -mrq --format '%w%f' -ecreate,delete,close_write,attrib /wddg/share |\
while read line
do
rsync -az --delete /wddg/share/rsync_backup@192.168.158.198::StudyRsync--password-file=/etc/rsync.password
done
vim /server/scripts/inotify2.sh
#!/bin/bash
#para
host01=192.168.1.160 #inotify-slave的ip地址
src=/backup/ #本地监控的目录
dst=backup #inotify-slave的rsync服务的模块名
user=rsync_backup #inotify-slave的rsync服务的虚拟用户
rsync_passfile=/etc/rsync.password #本地调用rsync服务的密码文件
inotify_home=/usr/local/inotify-3.14 #inotify的安装目录
#judge
if [ ! -e "$src" ] \
|| [ ! -e "${rsync_passfile}" ] \
|| [ ! -e "${inotify_home}/bin/inotifywait" ] \
|| [ ! -e "/usr/bin/rsync" ];
then
echo "Check File and Folder"
exit 9
fi
${inotify_home}/bin/inotifywait -mrq --timefmt '%d/%m/%y %H:%M'--format '%T %w%f' -e close_write,delete,create,attrib $src \
| while read file
do
# rsync -avzP --delete --timeout=100 --password-file=${rsync_passfile}$src $user@$host01::$dst >/dev/null 2>&1
cd $src && rsync -aruz -R --delete ./ --timeout=100$user@$host01::$dst --password-file=${rsync_passfile} >/dev/null 2>&1
done
exit 0
传文件:比较精准,但删除操作会报错,无法在服务端删除文件。可以另起一个删除脚本,监控同一个目录。
传目录:每次都是将目录下的文件全部上传,效率较低,但可以在服务端删除文件
sh inotify.sh &
a、查看服务器产品名称
dmidecode | grep "Product Name"
Product Name: VMware Virtual Platform
Product Name: 440BX Desktop Reference Platform
b、查看CPU型号
grep name /proc/cpuinfo
model name : Intel(R)Xeon(R) CPU E7-4809 v3 @ 2.00GHz
model name : Intel(R)Xeon(R) CPU E7-4809 v3 @ 2.00GHz
model name : Intel(R)Xeon(R) CPU E7-4809 v3 @ 2.00GHz
model name : Intel(R)Xeon(R) CPU E7-4809 v3 @ 2.00GHz
model name : Intel(R)Xeon(R) CPU E7-4809 v3 @ 2.00GHz
model name : Intel(R)Xeon(R) CPU E7-4809 v3 @ 2.00GHz
model name : Intel(R)Xeon(R) CPU E7-4809 v3 @ 2.00GHz
model name : Intel(R)Xeon(R) CPU E7-4809 v3 @ 2.00GHz
c、查看CPU个(核)数
grep "physical id" /proc/cpuinfo
physical id : 0
physical id : 2
physical id : 4
physical id : 6
physical id : 8
physical id : 10
physical id : 12
physical id : 14
d、查看内存信息
grep MemTotal /proc/meminfo
MemTotal: 264535076 kB
free -m
total used free shared buffers cached
Mem: 258335 2351 255983 1 187 338
-/+ buffers/cache: 1825 256509
Swap: 4031 0 4031
e、查看系统信息
uname -a
Linux linuxDelivery 2.6.32-642.el6.x86_64 #1 SMP Wed Apr 13 00:51:26EDT 2016 x86_64 x86_64 x86_64 GNU/Linux
a、查看服务器产品名称
dmidecode | grep "Product Name"
Product Name: VMware Virtual Platform
Product Name: 440BX Desktop Reference Platform
b、查看CPU型号
grep name /proc/cpuinfo
model name : Intel(R)Xeon(R) CPU E7-4809 v3 @ 2.00GHz
model name : Intel(R)Xeon(R) CPU E7-4809 v3 @ 2.00GHz
model name : Intel(R)Xeon(R) CPU E7-4809 v3 @ 2.00GHz
model name : Intel(R)Xeon(R) CPU E7-4809 v3 @ 2.00GHz
model name : Intel(R)Xeon(R) CPU E7-4809 v3 @ 2.00GHz
model name : Intel(R)Xeon(R) CPU E7-4809 v3 @ 2.00GHz
model name : Intel(R)Xeon(R) CPU E7-4809 v3 @ 2.00GHz
model name : Intel(R)Xeon(R) CPU E7-4809 v3 @ 2.00GHz
c、查看CPU个(核)数
grep "physical id" /proc/cpuinfo
physical id : 0
physical id : 2
physical id : 4
physical id : 6
physical id : 8
physical id : 10
physical id : 12
physical id : 14
d、查看内存信息
grep MemTotal /proc/meminfo
MemTotal: 8060636 kB
free -m
total used free shared buffers cached
Mem: 7871 1267 6604 1 178 636
-/+ buffers/cache: 451 7420
Swap: 4031 0 4031
e、查看系统信息
uname -a
Linux linuxDelivery 2.6.32-642.el6.x86_64 #1 SMP Wed Apr 13 00:51:26EDT 2016 x86_64 x86_64 x86_64 GNU/Linux
f、检查服务端rsync --daemon服务是否启动
ps -ef | grep rsync
root 19551 1 0Feb24 ? 00:00:00 rsync --daemon
vi /server/scripts/w.sh
#!/bin/sh
count=10
while true
do
for((i=1;i<=$count;i++))
do
/bin/cp/wddg/oldboy/10K.log /wddg/share/$i/10K_`echo $(date)$RANDOM|md5sum | cut -c1-8`.log
done
sleep 1
for((i=1;i<=$count;i++))
do
/bin/cp/wddg/oldboy/30K.log /wddg/share/$i/30K_`echo $(date)$RANDOM|md5sum | cut -c1-8`.log
done
sleep 1
for((i=1;i<=$count;i++))
do
/bin/cp/wddg/oldboy/50K.log /wddg/share/$i/50K_`echo $(date)$RANDOM|md5sum | cut -c1-8`.log
done
sleep 1
done
for n in `seq 100`;do mkdir $n ;done;
a、客户端
sh w.sh
b、服务端查看
ll | wc -l
c、结论
由于是虚拟机,目录是通过NFS挂载的NAS盘,每秒只能同步6个左右的文件,性能不尽人意。
并发200-300/秒,文件大小10K-500K左右,这是inotify的极限。数据同步几乎无延时(小于1秒),再多就会有延时。
(1)并发不以大于200个文件
(2)每次都是全部推送
scp、nfs、sftp、http、samba、rsync、csysn2、union
详细见注1参考博文8:2016年linux运维人员必会开源运维工具体系
文件同步的思想:
(1)可以利用mysql、mongodb等数据库软件实现文件同步(写库-同步-读库-文件)
(2)双写,同时向两台服务器写数据。
drbd:基于文件系统同步,又称网络raid1。几乎可以同步任何业务数据。
mysql数据库的官方推荐使用drbd同步数据。
所有单点服务,如NFS、MFS等都可以用drbd
mysql:replication(主从同步,基于SQL语句重写)
oracle:dataguard(物理的:基于磁盘块(block)复制;逻辑的:基于SQL语句重写)
详细见注1参考博文9:Heartbeat+DRBD+MySQL高可用架构方案与实施