若是有一天去接受面试:面试官问你一个1t的文件须要进行传输,问题用何种方法。假如你答了scp或者ftp。那么你挂的概率是很大的html
inotify-tools有两个命令 //inotify-tools是用c编写的,除了要求内核支持inotify外,不依赖于其余。
inotifywait,是用来监控文件或目录的变化
inotifywatch,是用来统计文件系统访问的次数
inotify是一种强大的、细粒度的、异步的文件系统事件控制机制。linux内核从2.6.13起,加入了inotify支持,经过inotify能够监控文件系统中添加、删除、修改、移动等各类事件,利用这个内核接口,第三方软件就能够监控文件系统下文件的各类变化状况,而inotify-tools正是实施监控的软件。linux
[root@localhost nginx]# yum install inotify-tools -y
/usr/local/bin/inotifywait -mrq --format '%Xe %w%f' -e modify,create,delete,attrib /data/
让inotifywait监听/data/目录,当监听到有发生modify,create,delete,attrib等事件发生时,按%Xe %w%f的格式输出。在/data/目录touch几个文件nginx
-m :monitor -r :递归 -q :Print less (only print events) -qq :Print nothing (not even events). //除非是致命错误 -d :--daemon后台运行,须要指定–outfile把事情输出到一个文件。也意味着使用了–syslog。 -o|--outfile [file] :Print events to <file> rather than stdout. -s|--syslog Send errors to syslog rather than stderr. –fromfile :Read files to watch from <file> or `-' for stdin.从文件读取须要监视的文件或排除的文件,一个文件一行,排除的文件以@开头。 @ :排除不须要监视的文件,能够是相对路径,也能够是绝对路径。 –exclude 正则匹配须要排除的文件,大小写敏感。 –excludei 正则匹配须要排除的文件,忽略大小写。 -t , –timeout 设置超时时间,若是为0,则无限期地执行下去。 -e , –event 指定监视的事件。 -c, –csv 输出csv格式。 –timefmt 指定时间格式,如(“%”后面的大小写表明不一样的格式,如%y表示2位的年) %Y-%m-%d 日期:2012-10-13 %H:%M:%S 时间:15:45:05 %w 表示发生事件的目录 %f 表示发生事件的文件 %e 表示发生的事件 %Xe 事件以“X”分隔 %T 显示由–timefmt定义的时间格式
Exit status:
0 - An event you asked to watch for was received.
1 - An event you did not ask to watch for was received (usually delete_self or unmount), or some error occurred.
2 - The --timeout option was given and no events occurred in the specified interval of time.web
Events: access file or directory contents were read modify file or directory contents were written attrib file or directory attributes changed 文件属性更改,如权限,时间戳等。 close_write file or directory closed, after being opened in writeable mode 以可写模式打开的文件被关闭,不表明此文件必定已经写入数据。 close_nowrite file or directory closed, after being opened in read-only mode 以只读模式打开的文件被关闭。 close file or directory closed, regardless of read/write mode open file or directory opened moved_to file or directory moved to watched directory 一个文件或目录移动到监听的目录,即便是在同一目录内移动,此事件也触发。 moved_from file or directory moved from watched directory 一个文件或目录移出监听的目录,即便是在同一目录内移动,此事件也触发。 move file or directory moved to or from watched directory 包括moved_to和 moved_from create file or directory created within watched directory 文件或目录被移除,以后再也不监听此文件或目录。 delete file or directory deleted within watched directory delete_self file or directory was deleted 文件或目录移除,以后再也不监听此文件或目录 unmount file system containing file or directory unmounted 文件系统取消挂载,以后再也不监听此文件系统。
inotifywatch [-hvzrqf] [-e ] [-t ] [-a ] [-d ] [ ... ]
@ 排除不须要监视的文件,能够是相对路径,也能够是绝对路径。
–fromfile 从文件读取须要监视的文件或排除的文件,一个文件一行,排除的文件以@开头。
-z, –zero 输出表格的行和列,即便元素为空
–exclude 正则匹配须要排除的文件,大小写敏感。
例:要排除/home/mjb目录下的test1,test2,cc目录,可这样写--exclude="/home/mjb/(test1/|test2/|cc/)"。多个目录或文件必定要用“|”分开,不能在命令行中用两个--exclude,不然最后的--exclude会覆盖其它的。系统只是在文件路径中查找是否有上面参数指定的字符,若是有就排除。所以在test1后面加了“/”。不然/home/mjb/test123也会被排除。
–excludei 正则匹配须要排除的文件,忽略大小写。
-r,递归
-t,timeout
-e,event
-a , –ascending 以指定事件升序排列。
-d , –descending 以指定事件降序排列。面试
实时监控/home的全部事件(包括文件的访问,写入,修改,删除等)// inotifywait -rm /home
统计/home文件系统的事件 //inotifywatch -v -e access -e modify -t 60 -r /home
监控/home/www目录及其下子目录
inotifywait -m -r -d -o /var/log/change.log –timefmt '%F %T’ –format '%T %w%f %e’ -e close_write,create /home/www 算法
Linux 主机之间即时传送文件,scp命令你们都很熟悉
但当要传送的文件较大,过程当中若是网络中断了,就比较悲剧了。这时候能够考虑使用rsync命令替代scp,实现断点续传文件。
rsync在同步数据时,须要扫描全部文件后进行比对,进行差量传输。若是文件数量达到了百万甚至千万量级,扫描全部文件将是很是耗时的,而且正在发生变化的每每是其中不多的一部分,这是很是低效的方式。
rsync不能实时的去监测、同步数据,虽然它能够经过linux守护进程的方式进行触发同步,可是两次触发动做必定会有时间差,这样就致使了服务端和客户端数据可能出现不一致,没法在应用故障时彻底的恢复数据。shell
[root@web ~]# ll /proc/sys/fs/inotify/
max_user_watches #设置inotifywait或inotifywatch命令能够监视的文件数量(单进程)
max_user_instances #设置每一个用户能够运行的inotifywait或inotifywatch命令的进程数
max_queued_events #设置inotify实例事件(event)队列可容纳的事件数量
[root@web ~]# echo 50000000>/proc/sys/fs/inotify/max_user_watches -- 把他加入/etc/rc.local就能够实现每次重启都生效
[root@web ~]# echo 50000000>/proc/sys/fs/inotify/max_queued_eventsvim
1 rsync [OPTION]... SRC DEST
2 rsync [OPTION]... SRC [USER@]HOST:DEST
3 rsync [OPTION]... [USER@]HOST:SRC DEST
4 rsync [OPTION]... [USER@]HOST::SRC DEST
5 rsync [OPTION]... SRC [USER@]HOST::DEST
6 rsync [OPTION]... rsync://[USER@]HOST[:PORT]/SRC [DEST]centos
对应于以上六种命令格式,rsync有六种不一样的工做模式:
1)拷贝本地文件。当SRC和DES路径信息都不包含有单个冒号”:”分隔符时就启动这种工做模式。如:rsync -a /data /backup
2)使用一个远程shell程序(如rsh、ssh)来实现将本地机器的内容拷贝到远程机器。当DST路径地址包含单个冒号”:”分隔符时启动该模式。如:rsync -avz *.c foo:src
3)使用一个远程shell程序(如rsh、ssh)来实现将远程机器的内容拷贝到本地机器。当SRC地址路径包含单个冒号”:”分隔符时启动该模式。如:rsync -avz foo:src/bar /data
4)从远程rsync服务器中拷贝文件到本地机。当SRC路径信息包含”::”分隔符时启动该模式。如:rsync -av root@172.16.78.192::www /databack
5)从本地机器拷贝文件到远程rsync服务器中。当DST路径信息包含”::”分隔符时启动该模式。如:rsync -av /databack root@172.16.78.192::www
6)列远程机的文件列表。这相似于rsync传输,不过只要在命令中省略掉本地机信息便可。如:rsync -v rsync://172.16.78.192/www
rsync参数的具体解释以下:安全
-v, --verbose 详细模式输出 -q, --quiet 精简输出模式 -c, --checksum 打开校验开关,强制对文件传输进行校验 -a, --archive 归档模式,表示以递归方式传输文件,并保持全部文件属性,等于-rlptgoD -r, --recursive 对子目录以递归模式处理 -R, --relative 使用相对路径信息 -b, --backup 建立备份,也就是对于目的已经存在有一样的文件名时,将老的文件从新命名为~filename。可使用--suffix选项来指定不一样的备份文件前缀。 --backup-dir 将备份文件(如~filename)存放在在目录下。 -suffix=SUFFIX 定义备份文件前缀 -u, --update 仅仅进行更新,也就是跳过全部已经存在于DST,而且文件时间晚于要备份的文件。(不覆盖更新的文件) -l, --links 保留软链结 -L, --copy-links 想对待常规文件同样处理软链结 --copy-unsafe-links 仅仅拷贝指向SRC路径目录树之外的链结 --safe-links 忽略指向SRC路径目录树之外的链结 -H, --hard-links 保留硬链结 -p, --perms 保持文件权限 -o, --owner 保持文件属主信息 -g, --group 保持文件属组信息 -D, --devices 保持设备文件信息 -t, --times 保持文件时间信息 -S, --sparse 对稀疏文件进行特殊处理以节省DST的空间 -n, --dry-run现实哪些文件将被传输 -W, --whole-file 拷贝文件,不进行增量检测 -x, --one-file-system 不要跨越文件系统边界 -B, --block-size=SIZE 检验算法使用的块尺寸,默认是700字节 -e, --rsh=COMMAND 指定使用rsh、ssh方式进行数据同步 --rsync-path=PATH 指定远程服务器上的rsync命令所在路径信息 -C, --cvs-exclude 使用和CVS同样的方法自动忽略文件,用来排除那些不但愿传输的文件 --existing 仅仅更新那些已经存在于DST的文件,而不备份那些新建立的文件 --delete 删除那些DST中SRC没有的文件 --delete-excluded 一样删除接收端那些被该选项指定排除的文件 --delete-after 传输结束之后再删除 --ignore-errors 及时出现IO错误也进行删除 --max-delete=NUM 最多删除NUM个文件 --partial 保留那些因故没有彻底传输的文件,以是加快随后的再次传输 --force 强制删除目录,即便不为空 --numeric-ids 不将数字的用户和组ID匹配为用户名和组名 --timeout=TIME IP超时时间,单位为秒 -I, --ignore-times 不跳过那些有一样的时间和长度的文件 --size-only 当决定是否要备份文件时,仅仅察看文件大小而不考虑文件时间 --modify-window=NUM 决定文件是否时间相同时使用的时间戳窗口,默认为0 -T --temp-dir=DIR 在DIR中建立临时文件 --compare-dest=DIR 一样比较DIR中的文件来决定是否须要备份 -P 等同于 --partial {断点传输} --progress {显示备份过程} -z, --compress 对备份的文件在传输时进行压缩处理 --exclude=PATTERN 指定排除不须要传输的文件模式 --include=PATTERN 指定不排除而须要传输的文件模式 --exclude-from=FILE 排除FILE中指定模式的文件 --include-from=FILE 不排除FILE指定模式匹配的文件 --version 打印版本信息 --address 绑定到特定的地址 --config=FILE 指定其余的配置文件,不使用默认的rsyncd.conf文件 --port=PORT 指定其余的rsync服务端口 --blocking-io 对远程shell使用阻塞IO -stats 给出某些文件的传输状态 --progress 在传输时现实传输过程 --log-format=formAT 指定日志文件格式 --password-file=FILE 从FILE中获得密码 --bwlimit=KBPS 限制I/O带宽,KBytes per second -h, --help 显示帮助信息
测试客户端执行报错://远程server到本地
[root@localhost data]# rsync -rP --rsh=ssh 192.168.154.139:test/a.big ./ //这样访问的是192.168.154.139的/root/test/a.big
The authenticity of host '192.168.154.139 (192.168.154.139)' can't be established.
RSA key fingerprint is ed:0b:47:d4:20:2b:21:89:b6:69:b2:b4:42:07:0f:df.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added '192.168.154.139' (RSA) to the list of known hosts.
root@192.168.154.139's password:
receiving incremental file list
rsync: link_stat "/root/test/a.big" failed: No such file or directory (2)
正确的方法:
[root@localhost data]# rsync -rP --rsh=ssh 192.168.154.139::test/a.big ./ root@192.168.154.139's password: receiving incremental file list a.big 3604480 0% 3.08MB/s 0:05:39 17629184 1% 7.93MB/s 0:02:09 rsync error: received SIGINT, SIGTERM, or SIGHUP (code 20) at rsync.c(546) [generator=3.0.6] rsync error: received SIGUSR1 (code 19) at main.c(1285) [receiver=3.0.6] //中断了 [root@localhost data]# rsync -rP --rsh=ssh 192.168.154.139::test/a.big ./ //从新传输 [root@localhost ~]# rsync -rP --rsh=ssh /test/a.tmp 192.168.154.132:/home/admin //传输本地的a.tmp到远程机器上。以ssh的方式 [root@localhost ~]# rsync -avzrP /tmp/bigfile cnangel@10.1.6.160:/tmp/bigfile //a:归档,v:详细显示,z:压缩,P:显示过程和断点传输,r:递归,
#!/bin/bash
/usr/bin/inotifywait -mrq --format '%w%f' -e create,close_write,delete /backup |while read file
#把发生更改的文件列表都接收到file 而后循环,但有什么鬼用呢?下面的命令都没有引用这个$file 下面作的是全量rsync
do
cd /backup && rsync -az --delete /backup/ rsync_backup@192.168.24.101::backup/--password-file=/etc/rsync.password
done
这里的rsync 每次都是全量的同步(这就坑爹了),并且 file列表是循环形式触发rsync ,等于有10个文件发生更改,就触发10次rsync全量同步(简直就是噩梦),那还不如直接写个死循环的rsync全量同步得了。
#!/bin/bash src=/data/ # 须要同步的源路径 des=data # 目标服务器上 rsync --daemon 发布的名称,rsync --daemon这里就不作介绍了,网上搜一下,比较简单。 rsync_passwd_file=/etc/rsyncd.passwd # rsync验证的密码文件 ip1=192.168.0.18 # 目标服务器1 ip2=192.168.0.19 # 目标服务器2 user=root # rsync --daemon定义的验证用户名 cd ${src} # 此方法中,因为rsync同步的特性,这里必需要先cd到源目录,inotify再监听 ./ 才能rsync同步后目录结构一致,有兴趣的同窗能够进行各类尝试观看其效果 /usr/local/bin/inotifywait -mrq --format '%Xe %w%f' -e modify,create,delete,attrib,close_write,move ./ | while read file # 把监控到有发生更改的"文件路径列表"循环 do INO_EVENT=$(echo $file | awk '{print $1}') # 把inotify输出切割 把事件类型部分赋值给INO_EVENT INO_FILE=$(echo $file | awk '{print $2}') # 把inotify输出切割 把文件路径部分赋值给INO_FILE echo "-------------------------------$(date)------------------------------------" echo $file #增长、修改、写入完成、移动进事件 #增、改放在同一个判断,由于他们都确定是针对文件的操做,即便是新建目录,要同步的也只是一个空目录,不会影响速度。 if [[ $INO_EVENT =~ 'CREATE' ]] || [[ $INO_EVENT =~ 'MODIFY' ]] || [[ $INO_EVENT =~ 'CLOSE_WRITE' ]] || [[ $INO_EVENT =~ 'MOVED_TO' ]] # 判断事件类型 then echo 'CREATE or MODIFY or CLOSE_WRITE or MOVED_TO' rsync -avzcR --password-file=${rsync_passwd_file} $(dirname ${INO_FILE}) ${user}@${ip1}::${des} && # INO_FILE变量表明路径哦 -c校验文件内容 rsync -avzcR --password-file=${rsync_passwd_file} $(dirname ${INO_FILE}) ${user}@${ip2}::${des} #仔细看 上面的rsync同步命令 源是用了$(dirname ${INO_FILE})变量 即每次只针对性的同步发生改变的文件的目录(只同步目标文件的方法在生产环境的某些极端 #环境下会漏文件 如今能够在不漏文件下也有不错的速度 作到平衡) #而后用-R参数把源的目录结构递归到目标后面 保证目录结构一致性 fi #删除、移动出事件 if [[ $INO_EVENT =~ 'DELETE' ]] || [[ $INO_EVENT =~ 'MOVED_FROM' ]] then echo 'DELETE or MOVED_FROM' rsync -avzR --delete --password-file=${rsync_passwd_file} $(dirname ${INO_FILE}) ${user}@${ip1}::${des} && rsync -avzR --delete --password-file=${rsync_passwd_file} $(dirname ${INO_FILE}) ${user}@${ip2}::${des} #看rsync命令 若是直接同步已删除的路径${INO_FILE}会报no such or directory错误 因此这里同步的源是被删文件或目录的上一级路径 #并加上--delete来删除目标上有而源中没有的文件,这里不能作到指定文件删除,若是删除的路径越靠近根,则同步的目录月多,同步删除的操做就越花时间。 #这里有更好方法的同窗,欢迎交流。 fi #修改属性事件 指 touch chgrp chmod chown等操做 if [[ $INO_EVENT =~ 'ATTRIB' ]] then echo 'ATTRIB' if [ ! -d "$INO_FILE" ] # 若是修改属性的是目录 则不一样步,由于同步目录会发生递归扫描,等此目录下的文件发生同步时,rsync会顺带更新此目录。 then rsync -avzcR --password-file=${rsync_passwd_file} $(dirname ${INO_FILE}) ${user}@${ip1}::${des} && rsync -avzcR --password-file=${rsync_passwd_file} $(dirname ${INO_FILE}) ${user}@${ip2}::${des} fi fi done
###三、结合crontab
由于inotify只在启动时会监控目录,他没有启动期间的文件发生更改,他是不知道的,因此这里每2个小时作1次全量同步,防止各类意外遗漏,保证目录一致。
crontab -e
* */2 * * * rsync -avz --password-file=/etc/rsync-client.pass /data/ root@192.168.0.18::data && rsync -avz --password-file=/etc/rsync-client.pass /data/ root@192.168.0.19::data
server:192.168.154.139 //CentOS7
client;192.168.154.132 //CentOS6
[root@localhost data]# systemctl start rsyncd
[root@localhost data]# systemctl enable rsyncd
默认监听端口873
[root@localhost ~]# vim /etc/rsyncd.conf
[test]
path = /test
auth user = user1
secrets file = /etc/rsyncd.secrets
[root@localhost ~]#
[root@localhost ~]# systemctl restart rsyncd
[root@localhost ~]# vim /etc/rsyncd.secrets
wolf:wolf
[root@localhost ~]# chmod 600 /etc/rsyncd.secrets
[root@localhost ~]# touch /test/file{1..10}
[root@localhost test]# rsync -a 192.168.154.139::
test
[root@localhost test]# rsync -av wolf@192.168.154.139::test /tmp/test/mm/
receiving incremental file list
./
file1
file10
file2
file3
file4
file5
file6
file7
file8
file9
sent 219 bytes received 524 bytes 1486.00 bytes/sec
total size is 0 speedup is 0.00
[root@localhost data]# rsync -a 192.168.154.139::test/file1 ./ //传输单个文件
注意:此时假如在192.168.154.139上删除file1-10,从新执行上面的命令,不会在本地删除原有的file1-10
要求server主机上面的/data目录发生新的建立,删除,移动,以及文件属性信息改变时,自动同不到client主机的/tmp/test/mm目录
server端设置:
[root@localhost ~]# cat 1.sh
#!/bin/bash
inotifywait -mrq -e modify,create,move,delete,attrib /data |while read events
do
rsync -a --delete /data 192.168.154.132::test
echo "date +'%F %T'
出现事件 $events" >>/tmp/rsync.log 2>&1
done
[root@localhost ~]# ls /data
aa bb cc dd
Client端设置:
[root@localhost ~]# cat /etc/rsyncd.conf //CentOS6默认没有这个文件,须要手动建立
[test]
path = /tmp/test/mm
read only = false
uid = root
gid = root
[root@localhost ~]# mkdir /test/test/mm
[root@localhost ~]# service restart xinetd
测试:
[root@localhost ~]# nohup sh /tmp/1.sh & //server上执行
[root@localhost ~]# echo haha >>/data/aa
[root@localhost ~]# cat /tmp/rsync.log
[root@localhost ~]#
[root@localhost ~]# ls /tmp/test/mm //client上查看是否生成文件
[root@inotify-slave ~]# yum install rsync
[root@inotify-slave ~]# useradd -s /bin/nologin -M rsync
[root@inotify-slave ~]# mkdir /mydata/ ##建立rsync工做模式的模块目录
[root@inotify-slave ~]# chown rsync.rsync /mydata/ ##更改属主、主组;使rsync用户有权限更改数据
[root@inotify-slave ~]# vim /etc/rsync.password
[root@inotify-slave ~]# chmod 600 /etc/rsync.password ##为密码文件增长安全性
[root@inotify-slave ~]# vim /etc/rsyncd.conf
uid = rsync
gid = rsync
user chroot = no
max connections = 200
timeout = 300
read only = no
[rsync] ##定义模块名,名字可随意
path = /mydata ##指定rsync用户的模块目录
auth users = linuxidc ##指定虚拟用户名
secrets file = /etc/rsync.password ##指定虚拟用户的密码文件路径
ignore errors ##表示忽略错误
[root@inotify-slave ~]# rsync --daemon --config=/etc/rsyncd.conf ##rsync监听在tcp协议的873端口
[root@inotify-slave ~]# ps aux | grep rsync
root 1330 0.0 0.0 114640 328 ? Ss 21:13 0:00 rsync --daemon --config=/etc/rsyncd.conf
root 1338 0.0 0.1 112644 952 pts/0 R+ 21:13 0:00 grep --color=auto rsync
配置虚拟用户的密码文件
[root@inotify-master ~]# echo "linux" > /etc/rsync.password
[root@inotify-master ~]# cat /etc/rsync.password
linux ##注意:只须要写出虚拟用户的密码;不用写出用户名
[root@inotify-master ~]# chmod 600 /etc/rsync.password
[root@inotify-master ~]# ls -ld /etc/rsync.password
[root@inotify-master ~]# echo "Hello Word" > linuxidc.txt
[root@inotify-master ~]# rsync -avz linuxidc.txt lweim@172.18.42.200::rsync --password-file=/etc/rsync.password
sending incremental file list
linuxidc.txt
sent 81 bytes received 27 bytes 216.00 bytes/sec
total size is 11 speedup is 0.10
检查inotify-slave的工做模块目录
[root@inotify-slave ~]# ll /mydata/
total 4
-rw-r--r-- 1 rsync rsync 11 May 19 21:21 linuxidc.txt
[root@inotify-slave ~]# cat /mydata/linuxidc.txt
Hello Word ##推送成功
[root@inotify-master ~]# yum install inotify-tools-3.13.tar
[root@inotify-master ~]# vim inotify.sh
#!/bin/bash host=172.18.42.200 ##指明inotify-slave的ip地址 src=/www/linuxidc ##本机监控的目录,可随意定义 dst=rsync ##inotify-slave的rsync用户的模块目录 user=linuxidc ##虚拟用户的用户名 passfile=/etc/rsync.password ##调用本地的密码文件 inotify_home=/usr/local/inotify ##指明inotify的安装目录 if [ ! -e "$src" ] || [ ! -e "${inotify_home}/bin/inotifywait" ] || [ ! -e "/usr/bin/rsync" ] || [ ! -e "/etc/rsync.password" ]; then #if [ ! -e "${inotify_home}/bin/inotifywait" ] || [ ! -e "/usr/bin/rsync" ] || [ ! -e "/etc/rsync.password" ]; then echo "Check File and Folder" exit 1 fi ${inotify_home}/bin/inotifywait -mrq -e close_write,delete,create,attrib $src | while read file do cd $src && rsync -arvz -P ./ --timeout=100 $user@$host::$dst --password-file=$passfile &>/dev/null done exit 0
执行inotify-master上的inotify.sh脚本
[root@inotify-master ~]# bash inotify.sh ##运行脚本
在inotify-master本机监控的目录下建立文件
[root@inotify-master linuxidc]# pwd
/www/linuxidc
[root@inotify-master linuxidc]# touch a aa aaa aaaa
[root@inotify-master linuxidc]# ll
total 0
-rw-r--r-- 1 root root 0 May 19 22:54 a
-rw-r--r-- 1 root root 0 May 19 22:54 aa
-rw-r--r-- 1 root root 0 May 19 22:54 aaa
-rw-r--r-- 1 root root 0 May 19 22:54 aaaa
在inotify-slave的rsync工做模块目录下查看
[root@inotify-slave mydata]# ll
total 0
-rw-r--r-- 1 rsync rsync 0 May 19 22:54 a
-rw-r--r-- 1 rsync rsync 0 May 19 22:54 aa
-rw-r--r-- 1 rsync rsync 0 May 19 22:54 aaa
-rw-r--r-- 1 rsync rsync 0 May 19 22:54 aaaa
[root@inotify-slave mydata]# pwd
/mydata
如下是为配置rsync时的常见问题:
问题一:
@ERROR: chroot failed
rsync error: error starting client-server protocol (code 5) at main.c(1522) [receiver=3.0.3]
缘由:
服务器端的目录不存在或无权限,建立目录并修正权限可解决问题。
问题二:
@ERROR: auth failed on module tee
rsync error: error starting client-server protocol (code 5) at main.c(1522) [receiver=3.0.3]
缘由:
服务器端该模块(tee)须要验证用户名密码,但客户端没有提供正确的用户名密码,认证失败。
提供正确的用户名密码解决此问题。
问题三:
@ERROR: Unknown module ‘tee_nonexists'
rsync error: error starting client-server protocol (code 5) at main.c(1522) [receiver=3.0.3]
缘由:
服务器不存在指定模块。提供正确的模块名或在服务器端修改为你要的模块以解决问题。
问题1:
在client上遇到问题:
rsync -auzv --progress --password-file=/etc/rsync.pas root@192.168.133.128::backup /home/
rsync: could not open password file "/etc/rsync.pas": No such file or directory (2)
Password:
@ERROR: auth failed on module backup
rsync error: error starting client-server protocol (code 5) at main.c(1506) [Receiver=3.0.7]
遇到这个问题:client端没有设置/etc/rsync.pas这个文件,而在使用rsync命令的时候,加了这个参数--
password-file=/etc/rsync.pas
问题2:
rsync -auzv --progress --password-file=/etc/rsync.pas root@192.168.133.128::backup /home/
@ERROR: auth failed on module backup
rsync error: error starting client-server protocol (code 5) at main.c(1506) [Receiver=3.0.7]
遇到这个问题:client端已经设置/etc/rsync.pas这个文件,里面也设置了密码111111,和服务器一致,可是
服务器段设置有错误,服务器端应该设置/etc/rsync.pas ,里面内容root:111111 ,这里登录名不可缺乏
问题3:
rsync -auzv --progress --password-file=/etc/rsync.pas root@192.168.133.128::backup /home/
@ERROR: chdir failed
rsync error: error starting client-server protocol (code 5) at main.c(1506) [Receiver=3.0.7]
遇到这个问题,是由于服务器端的/home/backup 其中backup这个目录并无设置,因此提示:chdir failed
问题4:
rsync: write failed on "/home/backup2010/wensong": No space left on device (28)
rsync error: error in file IO (code 11) at receiver.c(302) [receiver=3.0.7]
rsync: connection unexpectedly closed (2721 bytes received so far) [generator]
rsync error: error in rsync protocol data stream (code 12) at io.c(601) [generator=3.0.7]
磁盘空间不够,因此没法操做。
能够经过df /home/backup2010 来查看可用空间和已用空间
问题5:网络收集问题
一、权限问题
相似以下的提示:rsync: opendir "/kexue" (in dtsChannel) failed: Permission denied (13)注意查看同步的目录权限是否为755
二、time out
rsync: failed to connect to 203.100.192.66: Connection timed out (110)
rsync error: error in socket IO (code 10) at clientserver.c(124) [receiver=3.0.5]
检查服务器的端口netstat –tunlp,远程telnet测试。
可能由于客户端或者服务端的防火墙开启 致使没法通讯,能够设置规则放行 rsync(873端口) 或者直接关闭防火墙。
还有一种在同步过程当中可能会提示没有权限 (将同步目录加上SvcwRsync所有权限便可,更简单的方法就是将SvcwRsync设为管理员便可)
三、服务未启动
rsync: failed to connect to 10.10.10.170: Connection refused (111)
rsync error: error in socket IO (code 10) at clientserver.c(124) [receiver=3.0.5]
启动服务:rsync --daemon --config=/etc/rsyncd.conf
四、磁盘空间满
rsync: recv_generator: mkdir "/teacherclubBackup/rsync……" failed: No space left on device (28)
Skipping any contents from this failed directory
五、Ctrl+C或者大量文件
rsync error: received SIGINT, SIGTERM, or SIGHUP (code 20) at rsync.c(544) [receiver=3.0.5]
rsync error: received SIGINT, SIGTERM, or SIGHUP (code 20) at rsync.c(544) [generator=3.0.5]
说明:致使此问题多半是服务端服务没有被正常启动,到服务器上去查查服务是否有启动,而后查看下 /var/run/rsync.pid 文件是否存在,最干脆的方法是杀死已经启动了服务,而后再次启动服务或者让脚本加入系统启动服务级别而后shutdown -r now服务器
六、xnetid启动
rsync: read error: Connection reset by peer (104)
rsync error: error in rsync protocol data stream (code 12) at io.c(759) [receiver=3.0.5]
查看rsync日志
rsync: unable to open configuration file "/etc/rsyncd.conf": No such file or directory
xnetid查找的配置文件位置默认是/etc下,根据具体状况建立软连接。例如:
ln -s /etc/rsyncd/rsyncd.conf /etc/rsyncd.conf
或者更改指定默认的配置文件路径,在/etc/xinetd.d/rsync配置文件中。
Rsync configure:
配置一:
ignore errors
说明:这个选项最好加上,不然再不少crontab的时候每每发生错误你也未可知,由于你不可能每天去看每时每刻去看log,不加上这个出现错误的概率相对会很高,由于任何大点的项目和系统,磁盘IO都是一个瓶颈
Rsync error:
错误一:
@ERROR: auth failed on module xxxxx
rsync: connection unexpectedly closed (90 bytes read so far)
rsync error: error in rsync protocol data stream (code 12) at io.c(150)
说明:这是由于密码设置错了,没法登入成功,检查一下rsync.pwd,看客服是否匹配。还有服务器端没启动rsync 服务也会出现这种状况。
错误二:
password file must not be other-accessible
continuing without password file
Password:
说明:这是由于rsyncd.pwd rsyncd.sec的权限不对,应该设置为600。如:chmod 600 rsyncd.pwd
错误三:
@ERROR: chroot failed
rsync: connection unexpectedly closed (75 bytes read so far)
rsync error: error in rsync protocol data stream (code 12) at io.c(150)
说明:这是由于你在 rsync.conf 中设置的 path 路径不存在,要新建目录才能开启同步
错误四:
rsync: failed to connect to 218.107.243.2: No route to host (113)
rsync error: error in socket IO (code 10) at clientserver.c(104) [receiver=2.6.9]
说明:防火墙问题致使,这个最好先完全关闭防火墙,排错的基本法就是这样,不管是S仍是C,还有ignore errors选项问题也会致使
错误五:
@ERROR: access denied to www from unknown (192.168.1.123)
rsync: connection unexpectedly closed (0 bytes received so far) [receiver]
rsync error: error in rsync protocol data stream (code 12) at io.c(359)
说明:此问题很明显,是配置选项host allow的问题,初学者喜欢一个容许段作成一个配置,而后模块又是同一个,导致致使
错误六:
rsync error: received SIGINT, SIGTERM, or SIGHUP (code 20) at rsync.c(244) [generator=2.6.9]
rsync error: received SIGUSR1 (code 19) at main.c(1182) [receiver=2.6.9]
说明:致使此问题多半是服务端服务没有被正常启动,到服务器上去查查服务是否有启动,而后查看下 /var/run/rsync.pid 文件是否存在,最干脆的方法是杀死已经启动了服务,而后再次启动服务或者让脚本加入系统启动服务级别而后shutdown -r now服务器
错误七:
rsync: read error: Connection reset by peer (104)
rsync error: error in rsync protocol data stream (code 12) at io.c(604) [sender=2.6.9]
说明:原数据目录里没有数据存在
参考博客:
https://www.centos.bz/2012/06/inotify-tools-introduction/
https://blog.csdn.net/birdie_l/article/details/78189064
http://www.javashuo.com/article/p-fqycycvc-ew.html
http://man.linuxde.net/inotifywait
https://www.linuxidc.com/Linux/2016-08/134027.htm