rsync概述:
rsync是类unix系统下的数据镜像备份工具——remote sync。一款快速增量备份工具 Remote Sync,远程同步 支持本地复制,或者与其他SSH、rsync主机同步
Rsync(remote sync)是UNIX及类UNIX平台下一款神奇的数据镜像备份软件,它不像FTP或其他文件传输服务那样需要进行全备份,Rsync可以根据数据的变化进行差异备份,从而减少数据流量,提高工作效率。你可以使用它进行本地数据或远程数据的复制,Rsync可以使用SSH安全隧道进行加密数据传输。Rsync服务器端定义源数据,Rsync客户端仅在源数据发生改变后才会从服务器上实际复制数据至本地,如果源数据在服务器端被删除,则客户端数据也会被删除,以确保主机之间的数据是同步的。Rsync使用TCP 873端口。
实验描述:
192.168.1.155 ###目标主机(主)
192.168.1.156 ###源主机(客户端)
通过rsync将源主机上的test1文件夹中的文件复制到目标主机test文件夹中。
然后使用cron定时任务定时执行
关闭两台服务器的防火墙( systemctl stop firewalld、setenforce 0)
rsync安装配置:
在源主机配置:
[iyunv@localhost ~]# yum -y install rsync
[iyunv@localhost ~]# mkdir /root/test1
[iyunv@localhost ~]# vim /etc/rsync_exclude.lst ###指定要排除复制的文件或目录
123123.txt ####指定排除123、123.txt文件或目录
在test1文件夹中创建一个test.123文件,便于同步时,看是否成功
在目标主机配置:
[iyunv@localhost /]# mkdir /root/test
[iyunv@localhost /]# yum -y install rsync
[iyunv@localhost /]# vim /etc/rsyncd.conf
uid = nobody
gid = nobody
use chroot = yes
max connections = 4
pid file = /var/run/rsyncd.pid
exclude = lost+found/
transfer logging = yes
timeout = 900
ignore nonreadable = yes
dont compress = *.gz *.tgz *.zip *.z *.Z *.rpm *.deb *.bz2
path = /root/test
hosts allow = 192.168.1.156
hosts deny = *
[iyunv@localhost /]# systemctl start rsyncd
[iyunv@localhost /]# systemctl enable rsyncd
配置完成后,在源主机上执行rsync,并设置cron定时任务
[iyunv@localhost ~]# rsync -avz --delete /root/test1/ 192.168.1.155:/test ###az参数是增量备份 aP是完整备份
[iyunv@localhost ~]# rsync -avz --delete /root/test1/ 192.168.1.155:/test
The authenticity of host '192.168.1.155 (192.168.1.155)' can't be established.
ECDSA key fingerprint is df:21:e3:6c:36:9a:9d:b5:7e:28:ec:ba:a2:a9:f8:fb.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added '192.168.1.155' (ECDSA) to the list of known hosts.
root@192.168.1.155 's password:
sending incremental file list
./
test.123
sent 90 bytes received 34 bytes 22.55 bytes/sec
total size is 0 speedup is 0.00
[iyunv@localhost ~]#
[iyunv@localhost ~]# crontab -l
* 02 * * * rsync -avz --delete /root/test1/ 192.168.1.155:/test
rsync中的参数:
-r 是递归 -l 是链接文件,意思是拷贝链接文件;-p 表示保持文件原有权限;-t 保持文件原有时间;-g 保持文件原有用户组;-o 保持文件原有属主;-D 相当于块设备文件; -z 传输时压缩; -P 传输进度; -v 传输时的进度等信息,和-P有点关系,自己试试。可以看文档; -e ssh的参数建立起加密的连接。 -u只进行更新,防止本地新文件被重写,注意两者机器的时钟的同时 --progress是指显示出详细的进度情况 --delete是指如果服务器端删除了这一文件,那么客户端也相应把文件删除,保持真正的一致 --password-file=/password/path/file来指定密码文件,这样就可以在脚本中使用而无需交互式地输入验证密码了,这里需要注意的是这份密码文件权限属性要设得只有属主可读。
--bwlimit rsync限速参数(限制速度很简单,添加个参数即可bwlimit,后面的值是多少k Bytes/s,有些机房会限制机器的流量,为了不触及底线,在使用scp和rsync的时候都要注意。
为了避免你的scp或者rsync因为无良&懒惰的OPS设置防火墙的偷懒而造成的断流现象,我们必须对自己的数据传输进行一定的限流措施,慢一点总比被断了的好)
Rsync+Inotify-tools实现数据实时同步
上面已经说过,使用rsync进行同步,就算是定时任务,也不是实时的,这时我们要用到一个软件进行辅助 inotify-tools
inotify-tools 是为 linux 下inotify文件监控工具提供的一套c的开发接口库函数,同时还提供了一系列的命令行工具,这些工具可以用来监控文件系统的事件。 inotify-tools是用c编写的,除了要求内核支持inotify外,不依赖于其他。inotify-tools提供两种工具,一是inotifywait,它是用来监控文件或目录的变化,二是inotifywatch,它是用来统计文件系统访问的次数
下载地址:https://jaist.dl.sourceforge.net/project/inotify-tools/inotify-tools/3.13/inotify-tools-3.13.tar.gz
由于rsync同步时,要输入对方的账号密码,每次同步时都要输入,这时可以用ssh免秘钥登陆,前面章节已经讲过,这里不做叙述。
1.inotify安装步骤:
2.编写脚本
vim /auto_inotify.sh (这里是从rsync服务器同步到客户端,推流)
#!/bin/sh
src=/root/test/
des=/root/test1/
ip=192.168.1.156
inotifywait -mrq --timefmt '%d/%m/%y-%H:%M' --format '%T %w%f' -e modify,delete,create,attrib ${src} | while read file
do
for i in $ip
do
/bin/rsync -a --delete $src root@$ip:$des
done
done
解释:
#!/bin/sh
src=/root/test/ ###inotify监控的目录 (服务端目录)
des=/root/test1/ ###客户端目录
ip=192.168.1.156 ###客户端IP
inotifywait -mrq --timefmt '%d/%m/%y-%H:%M' --format '%T %w%f' -e modify,delete,create,attrib ${src} ###检查服务器哪个文件在实时变化 | while read file ###读取文件
do
for i in $ip ###通过循环进行同步
do
/bin/rsync -a --delete $src root@$ip:$des
done
done
3. 运行脚本并在后台执行
[iyunv@localhost test]# 【nohup】sh /auto_inotify.sh &(最好加上nohup 如果只加上&那么关闭终端,运行程序也随之关闭加上nohup终端关闭后台也会运行)
[1] 8856
nohup sh /auto_inotify.sh &
root@localhost test]# ps -ef | grep auto_inotify.sh
root 16486 1 0 11:32 ? 00:00:00 sh /auto_inotify.sh
root 16488 16486 0 11:32 ? 00:00:00 sh /auto_inotify.sh
root 16573 16493 0 11:33 pts/1 00:00:00 grep --color=auto auto_inotify.sh
[iyunv@localhost test]# ps -ef | grep inotifywait
root 16487 16486 0 11:32 ? 00:00:00 inotifywait -mrq --timefmt %d/%m/%y-%H:%M --format %T %w%f -e modify,delete,create,attrib /root/test/
root 17545 16493 0 11:37 pts/1 00:00:00 grep --color=auto inotifywait
扩展:nohup
原程序的的标准输出被自动改向到当前目录下的nohup.out文件,起到了log的作用。(也就是原本输出在屏幕上的,现在输到一个文件中,相当于一个日志)
运维网声明
1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网 享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com