Linux运维学习笔记之十一:同步工具之rsync和inotify

第十九章 同步工具之rsync

一、rsync基础
1、常用同步方法

SCP、NFS、SFTP、http、samba、rsync、drbd(基于文件系统同步,效率高)

2、rsync介绍

rsync,英文全称是remote synchronize,是一款实现远程同步功能的免费软件,它在同步文件的同时,可以保持原来文件的权限、时间、软硬链接等附加信息。 rsync是用 “rsync 算法”提供了一个客户机和远程文件服务器的文件同步的快速方法,而且可以通过ssh方式来传输文件。甚至还可以实现只同步一个文件里有变化的内容部分,所以可以实现快速的同步备份数据。

同时,rsync还可以实现同步本地数据、删除文件和目录的功能。一个rsync相当于scp、cp、rm,并且还在性能上优于它们每一个命令。

3、rsync特性

能更新整个目录和树和文件系统;
有选择性的保持符号链链、硬链接、文件属于、权限、设备以及时间等;
对于安装来说,无任何特殊权限要求;
对于多个文件来说,内部流水线减少文件等待的延时;
能用rsh、ssh 或直接端口做为传输入端口(rsync本身不对数据加密);
支持匿名rsync 同步文件,是理想的镜像工具;

4、rsync在企业的工作场景

(1)服务器之间的数据同步(cron+rsync)

(2)把所有客户服务器数据同步到备份服务器(cron+rsync)

(3)与inotify或sersync配合,做实时的数据同步(rsync+inotify/sersync)

5、rsync客户端常用参数
(1)参数说明

-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。

(2)工作中常用参数

一般运维中经常使用的参数是-avz(相当于-vzrtopgDl)

(3)参数使用演示

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/

6、rsync的工作方式

一般来说,rsync大致使用3种主要的传输数据方式。

(1)单个主机本地之间的数据传输(此时类似于cp命令的功能)

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

(2)借助rcp、ssh等通道来传输数据(此时类似于scp命令的功能)

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

(3)以守护进程(socket)的方式传输数据(这是rsync本身的重要功能)

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 

7、rsync基础小结
(1)rsync服务端

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

(2)rsync客户端(可有多个)

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 

(3)、注意事项

a、防火墙和selinux要关闭

b、排错时,一定要看日志

c、排错时,一定从部署流程整体考虑。

如果是部署时就出错,一定是部署流程有问题。如果是运行很长一段时间后出错,要看看有没有被攻击或被修改。

d、要有良好的操作习惯。

一定要多拷贝。

二、rsync深度应用之排除打包
1、演示环境

主机名

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

2、检查环境
(1)检查服务端A是否启动rsync

ps -ef | grep rsync

root      3747    1  0 Feb21 ?        00:00:00 rsync --daemon

(2)检查服务端A的rsync服务端口873

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)

(3)测试rsync传输是否正常(由B->A)

rsync -avz /wddg/share/ rsync_backup@192.168.58.238::StudyRsync--password-file=/etc/rsync.password

3、rsync排除的相关参数

--exclude=PATTERN       #需排除的文件较少时用

--exclude-from=FILE     #需排除的文件过多是,可以将文件名写入一个文件中,再指定排除

4、示例1:在客户端排除一个文件
(1)查看客户端B上需要同步的文件夹

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

(2)查看服务端A上的共享目录

ll /wddg/rsync

total 0

(3)在客户端B将文件同步到服务端A,并排除a.txt

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

(4)查看服务端A上的共享目录(没有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

5、示例2:在客户端排除多个针对不连续的文件。方式一:{文件1,文件2..}

#在客户端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

6、示例3:在客户端排除多个连续的文件。方式二:{文件1..文件n}

#在客户端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

7、示例4:在客户端通过指定文件来排除多个连续的文件。
(1)创建内容是排除文件名的文件

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

(2)通过指定文件排除

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

8、示例5:在服务端排除文件(通过配置文件)。
(1)在服务端A上修改配置文件,加上exclude=xxx参数

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

(2)停止rsync服务

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}'

(3)启动rsync服务

rsync --daemon

(4)在客户端B上通过rsync传输数据

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]

6、排除小结
(1)客户端

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

(2)服务端

a、在服务端上修改配置文件/etc/rsync.conf,加上exclude=xxx参数(多个文件用空格分隔)

b、格式示例:exclude=a b /wddg/c.txt

c、与客户端比较,服务端使用排除传输比较麻烦。

三、rsync深度应用之无差异同步
1、概念

差异同步(普通同步)时,如果是拉取方式(PULL)同步,只能保证服务端目录有的,客户端目录一定会有,但客户端目录中原来有的,服务端目录不一定会有。如果是推送方式(PUSH)同步,只能保证客户端目录有的,服务端目录一定会有,但服务端目录中原来有的,客户端目录不一定会有。所以称为有差异同步。

无差异同步就是客户端和服务端目录的内容完全一样。

2、无差异同步的应用场景

在某些场合下,要求保证生产服务器的内容与备份服务器内容完全一样。一般是2台服务器之间,必须要求数据一致,且实时性又不是很高的情况下使用。如2台负载均衡下面web服务器之间的同步,或者是高可用双机配置之间的同步等。但无差异同步的影响(风险)很大,而且有很多替代方案。因此,生产场景中没有特殊的需求,应避免使用

(1)推送场景:备份

风险是也就是本地服务器有什么,远端服务器就有什么,远端服务器原有数据均被删除

(2)拉取场景:代码发布

风险是也就是远端服务器有什么,本地服务器就有什么,本地服务器原有数据均被删除而丢失

3、使用方法:客户端使用--delete参数

rsync -avz --delete /wddg/share/ rsync_backup@192.168.58.238::StudyRsync

--password-file=/etc/rsync.password

4、示例演示
(1)删除客户端目录中所有文件

rm –fr *

(2)使用—delete参数进行无差异同步

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

(3)检查服务端目录中文件(所有文件均被删除,高风险)

ll /wddg/rsync

total 0

四、rsync的排错
1、排错思想

(1)部署流程要熟练

(2)原理要理解

(3)要学会看日志,包括命令行输出和日志/var/log/rsyncd.log

2、排错步骤

(1)先检查配置文件

(2)再检查的用户对不对

(3)检查密码文件对不对,复制配置文件中的密码文件名检查,不要用手敲,要复制。

(4)检查密码文件属性

(5)查看日志文件

2、常见错误
(1)、密码文件不存在

如查密码文件不存在(含指定的密码文件名错误),会在输入密码后报在模块上认证失败的错误。错误内容如下:

@ERROR: auth failed on module <modulename>

rsync error: error startingclient-server protocol (code 5) at main.c (1503) [receiver=3.0.6]

(2)密码文件权限过大。

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

(3)防火墙或SELinux开启。报错如下:

rsync: failed to connect to xx.xx.xx.xx: No route to host(113)

(4)服务端共享目录不存在。报错如下:

@ERROR: chdir failed

(5)服务端共享目录权限不对,配置文件中的rsync用户无权限访问目录。报错如下:

rsync: recv_generator:mkdir xxx failed:Permission denied(13)

五、rsync相关参考资料

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(实时同步工具)

一、inotify简介
1、概念

inotify是一个从2.6.13内核开始,对Linux文件系统进行高效率、细粒度、异步地监控机制,用于通知用户空间程序的文件系统变化。可利用它对用户空间进行安全、性能、以及其他方面的监控。Inotify 反应灵敏,用法非常简单,并且比 cron 任务的繁忙轮询高效得多。如果内核版本不低于 2.6.13,系统就支持 inotify。如果存在/usr/include/sys/inotify.h 文件,表明内核支持 inotify。从文件管理器到安全工具,文件系统监控对于的许多程序来说都是必不可少的。它允许监控程序打开一个独立文件描述符,并针对事件集监控一个或者多个文件,例如打开、关闭、移动/重命名、删除、创建或者改变属性。第三方程序可以通过这个内核接口监控文件系统的各种变化。inotify-tools就是一款这样的监控软件。

2、inotify可以监视的文件系统常见事件

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,自移动,即一个可执行文件在执行时移动自己

3、inotify的作用

inotify可以监控文件,也可以监控目录。当监控目录时,它可以同时监控目录及目录中的各子目录及文件的。此外,inotify 使用文件描述符作为接口,因而可以使用通常的文件I/O操作select、poll和epoll来监视文件系统的变化。

4、实现inotify的的软件

inotify:功能简单,性能高

sersync:国产,c封装,功能较多,可过滤。

lsyncd:开源,轻量级的实时镜像解决方案。

二、inotify的实现数据实时同步的原理

无论是手动执行rsync还是把rsync客户端发出的数据同步请求命令做成周期性任务计划,每隔一段时间不管有没有数据变化都发出一次数据同步请求命令,同步一次数据。服务端和客户端都有时间差。

所以,使用内核提供的inotify机制,当数据发生改变时(删除、修改等)就触发rsync客户端发出数据  同步请求。从而实现数据的实时传输。

Rsync + inotify 机制实现的两台服务器数据同步如下图如示:

 

三、演示环境

主机名

IP

用途

操作系统

linuxDelivery

192.168.58.238

服务端A_备份服务器

CentOS6.8

StudyLinux

192.168.58.85

客户端B_发送服务器

CentOS6.8

四、安装inotify
1、配置inotify的前提条件

(1)rysnc --daemon服务正常

(2)客户端可能正常推送和拉取数据

(3)是在客户端配置inotify

2、检查rsync--daemon服务是否正常,且是否能正常推送数据。

ps -ef | grep rsync

rsync -avz /wddg/share/ rsync_backup@192.168.58.238::StudyRsync--password-file=

/etc/rsync.password 

3、检查客户端系统是否支持inotify
(1)检查系统版本

uname -r

(2)检查系统相关参数,查看当前系统是否支持inotify

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

4、下载inotify源码包

wget http://github.com/downloads/rvoicilas/inotify-tools/inotify-tools-3.14.tar.gz

5、编译安装inotify源码包
(1)解压inotify源码包

cd /wddg/unzip

tar zxf inotify-tools-3.14.tar.gz

(2)#配置inotify,并指定安装路径为/usr/local/inotify-3.14

cd inotify-tools-3.14

./configure--prefix=/usr/local/inotify-3.14

(3)编译inotify源码包

make

(4)安装inotify源码包

make install

(5)也可能编译时并安装

make && make install

(6)查看是否成功

echo $?

0

(7)创建一个软连接

ln -s /usr/local/inotify-3.14//usr/local/inotify

6、inotify文件结构
(1)进入inotify目录

cd /usr/local/inotify

(2)查看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     #帮助文档

(3)查看inotify目录结构

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

 

 

五、inotify之inotifywait命令常用参数详解
1、查看帮助

./bin/inotifywait –help

2、参数详解
(1)-r:递归查询目录

-r|--recursive   Watchdirectories recursively.

(2)-q:打印监控事件的信息

-q|--quiet     Print less (only print events).

(3)-m:始终保持事件监听状态

-m|--monitor   Keep listeningfor events forever.  Without this option, inotifywait will exit afterone  event is received.

(4)--excludei:排除文件或目录时,不区分大小写。      

--excludei <pattern>  Like--exclude but case insensitive.   

(5)--timefmt:指定时间输出的格式

 --timefmt <fmt> strftime-compatibleformat string for use with %T in --format string.

(6)--format:打印使用指定的输出类似格式字符串

--format <fmt>  Print using aspecified printf-like format string; read the man page for more details.

a、%w:显示被监控文件的文件名;

b、%f:如果发生某事件的对象是目录,则显示被监控目录的名字;默认显示为空串;

c、%T:使用--timefmt选项中自定义的时间格式;

d、%e 表示发生的事件
e、%Xe 事件以“X”分隔

(7)-e:通过此参数可以指定需要监控的事件

-e|--event <event1> [ -e|--event <event2> ... ] Listen for specific event(s).  If omitted, all eventsare  listened for.  

3、事件(Events)详解
(1)access:文件或目录被读取

access   file or directory contents were read 

(2)modify:文件或目录内容被修改

modify   file or directory contents were written  

(3)attrib:文件或目录属性被改变

attrib   file or directory attributes changed    

(4)close:文件或目录封闭,无论读/写模式

close    file or directory closed, regardless of read/write mode 

(5)open:文件或目录被打开

open     file or directory opened  

(6)moved_to:文件或目录被移动至另外一个目录

moved_to file or directory moved to watched directory  

(7)move:文件或目录被移动另一个目录或从另一个目录移动至当前目录

move       file or directory movedto or from watched directory

(8)create:文件或目录被创建在当前目录

create    file or directory created within watcheddirectory 

(9)delete :文件或目录被删除

delete          file or directory deleted within watched directory 

(10)unmount:文件系统被卸载

unmount        file system containing file or directory unmounted

六、inotifywait命令之测试
1、测试create
(1)要求:

在客户端B上监控/wddg/share目录的create事件,并打印出创建的文件名。打印格式为YYYY-MM-DD HH24:MI 文件名

(2)执行命令

/usr/local/inotify-3.14/bin/inotifywait-mrq --timefmt '%Y-%m-%d %H:%M' --format '%T %e %w%f' -e create /wddg/share

(3)新开一个窗口,在/wddg/share目录下执行ls、cat命令,原窗口无输出
(4)在新窗口中,在/wddg/share目录下执行创建文件和目录命令

touch test.txt

mkdir aa

(5)查看原窗口输出

2017-02-22 00:15 /wddg/share/test.txt

2017-02-22 00:15 /wddg/share/aa

(6)在—format参数中使用-e,打印出是哪个要监控的事件上的操作

/usr/local/inotify-3.14/bin/inotifywait-mrq --timefmt '%Y-%m-%d %H:%M' --format '%T %e %w%f' -e create /wddg/share

(7)在新窗口中,在/wddg/share目录下执行创建文件和目录命令

mkdir a

touch b.txt

(8)查看原窗口输出

2017-02-22 00:36 CREATE,ISDIR/wddg/share/a

2017-02-22 00:36 CREATE/wddg/share/b.txt

2、测试delete
(1)执行命令

/usr/local/inotify-3.14/bin/inotifywait-mrq --timefmt '%Y-%m-%d %H:%M' --format '%T %e %w%f' -e create,delete/wddg/share

(2)在新窗口中,在/wddg/share目录下执行删除文件和目录命令

rm -f b.txt

rm -fr aa

(3)查看原窗口输出

2017-02-22 00:43 DELETE/wddg/share/b.txt

2017-02-22 00:43 DELETE,ISDIR/wddg/share/aa

3、测试close_write(写文件后关闭事件)
(1)执行命令

/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

(2)在新窗口中,在/wddg/share目录下执行创建文件和目录命令

touch x.txt

echo 111>a.log

(3)查看原窗口输出

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

(4)说明:事件是可以并行监控的。CREATE和CLOSE_WRITE有部分是重合的
4、简化事件输出

由于在生产环境中,需要把变化的文件推送到其他服务器,并不需要时间,只需要文件的全路径,调用rsync将其推送到服务端,所以事件的输出可以简化到只输出文件的全路径

/usr/local/inotify-3.14/bin/inotifywait-mrq --format ' %w%f' -e create /wddg/share

七、inotifywait命令之脚本编写
1、编写脚本

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

2、监控模式执行inotify.sh

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

3、检查服务端目录

ll /wddg/rsync

total 0

4、在新窗口中,在客户端/wddg/share目录中创建文件

touch a

5、查看原窗口输出

+ 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

6、服务端查看文件是否同步成功

ll

-rw-r--r--. 1 rsync rsync 0 Feb 22 01:14 a

7、同时创建大量文件

touch {1..1000}

8、传输发现有延时

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

9、客户端删除文件

rm -f *

10、客户端报错,查看服务端文件,发现没有删除(但客户端文件已经删除)

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]

11、修改脚本,捕获错误(只是跳过错误,还是不能删除服务端文件)

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

12、修改脚本,改传文件为传目录方式(可以删除服务端文件)

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

12、标准脚本写法(也是传目录)

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

13、传文件和传目录的区别

传文件:比较精准,但删除操作会报错,无法在服务端删除文件。可以另起一个删除脚本,监控同一个目录。

传目录:每次都是将目录下的文件全部上传,效率较低,但可以在服务端删除文件

14、将inotify放在后台执行

sh inotify.sh & 

八、inotify压力测试
1、检查环境配置
(1)客户机(客户端B)

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

(2)服务端(服务端A)

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

2、测试脚本
(1)不停向指定目录写入文件脚本

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

(2)在/wddg/share/中创建文件夹

for n in `seq 100`;do mkdir $n ;done;

(3)测试

a、客户端

sh w.sh

b、服务端查看

ll | wc -l

c、结论

由于是虚拟机,目录是通过NFS挂载的NAS盘,每秒只能同步6个左右的文件,性能不尽人意。

3、Linux下压力测试工具:webbench
九、inotify性能、缺点
1、inotify的性能

并发200-300/秒,文件大小10K-500K左右,这是inotify的极限。数据同步几乎无延时(小于1秒),再多就会有延时。

2、inotify的缺点

(1)并发不以大于200个文件

(2)每次都是全部推送

十、企业Linux运维场景数据同步方案
1、文件级别同步方案

scp、nfs、sftp、http、samba、rsync、csysn2、union

详细见注1参考博文8:2016年linux运维人员必会开源运维工具体系

文件同步的思想:

(1)可以利用mysql、mongodb等数据库软件实现文件同步(写库-同步-读库-文件)

(2)双写,同时向两台服务器写数据。

2、文件系统级别同步方案

drbd:基于文件系统同步,又称网络raid1。几乎可以同步任何业务数据。

mysql数据库的官方推荐使用drbd同步数据。

所有单点服务,如NFS、MFS等都可以用drbd

3、数据库同步方案
(1)自身同步机制

mysql:replication(主从同步,基于SQL语句重写)

oracle:dataguard(物理的:基于磁盘块(block)复制;逻辑的:基于SQL语句重写)

(2)第三方drbd

详细见注1参考博文9:Heartbeat+DRBD+MySQL高可用架构方案与实施

转发博文      https://blog.csdn.net/rumengjian/article/details/78699331

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值