Linux服务-rsyncc++
rsync是linux系统下的数据镜像备份工具。使用快速增量备份工具Remote Sync能够远程同步,支持本地复制,或者与其余SSH、rsync主机同步。shell
rsync支持不少特性:数据库
rsync命令来同步系统文件以前要先登陆remote主机认证,认证过程当中用到的协议有2种vim
rsync协议centos
rsync server端不用启动rsync的daemon进程,只要获取remote host的用户名和密码就能够直接rsync同步文件
rsync server端由于不用启动daemon进程,因此也不用配置文件/etc/rsyncd.conf安全
ssh认证协议跟scp的原理是同样的,若是在同步过程当中不想输入密码就用ssh-keygen -t rsa打统统道bash
//这种方式默认是省略了 -e ssh 的,与下面等价: [root@cwh ~]# rsync -avz test -e 'ssh' root@192.168.112.149:/root -a //文件宿主变化,时间戳不变 -z //压缩数据传输 root@192.168.112.149's password: sending incremental file list sent 71 bytes received 17 bytes 25.14 bytes/sec total size is 0 speedup is 0.00 //在149上查看 [root@149 ~]# ls test //若是ssh协议端口号不是默认的须要添加-p选项指定端口
//Rsync的命令格式经常使用的有如下三种: rsync [OPTION]... SRC DEST rsync [OPTION]... SRC [USER@]HOST:DEST rsync [OPTION]... [USER@]HOST:SRC DEST //对应于以上三种命令格式,rsync有三种不一样的工做模式: 1.拷贝本地文件 [root@cwh ~]# rsync -avz test/ bbb/ sending incremental file list created directory bbb ./ aaa sent 101 bytes received 64 bytes 330.00 bytes/sec total size is 0 speedup is 0.00 [root@cwh ~]# ls bbb/ aaa 2.使用一个远程shell程序(如rsh、ssh)来实现将本地机器的内容拷贝到远程机器 [root@cwh ~]# rsync -avz bbb root@192.168.112.149:/root root@192.168.112.149's password: sending incremental file list bbb/ bbb/ccc sent 117 bytes received 39 bytes 104.00 bytes/sec total size is 0 speedup is 0.00 //在149上查看 [root@149 ~]# ls bbb/ ccc 3.使用一个远程shell程序(如rsh、ssh)来实现将远程机器的内容拷贝到本地机器 [root@cwh ~]# rm -rf bbb/ [root@cwh ~]# rsync -avz root@192.168.112.149:/root/bbb /root root@192.168.112.149's password: receiving incremental file list bbb/ bbb/ccc sent 47 bytes received 117 bytes 65.60 bytes/sec total size is 0 speedup is 0.00 [root@cwh ~]# ls anaconda-ks.cfg bbb //rsync经常使用选项: -a, --archive //归档 -v, --verbose //啰嗦模式 -q, --quiet //静默模式 -r, --recursive //递归 -p, --perms //保持原有的权限属性 -z, --compress //在传输时压缩,节省带宽,加快传输速度 --delete //在源服务器上作的删除操做也会在目标服务器上同步
rsync优势:服务器
rsync与传统的cp、tar备份方式相比,rsync具备安全性高、备份迅速、支持增量备份等优势,经过rsync能够解决对实时性要求不高的数据备份需求,例如按期的备份文件服务器数据到远端服务器,对本地磁盘按期作数据镜像等。网络
rsync缺点:
改进:
Inotify是一种强大的、细粒度的、异步的文件系统事件监控机制,linux内核从2.6.13起,加入了Inotify支持,经过Inotify能够监控文件系统中添加、删除,修改、移动等各类细微事件,利用这个内核接口,第三方软件就能够监控文件系统下文件的各类变化状况,而inotify-tools就是这样的一个第三方软件。
在前面有讲到,rsync能够实现触发式的文件同步,可是经过crontab守护进程方式进行触发,同步的数据和实际数据会有差别,而inotify能够监控文件系统的各类变化,当文件有任何变更时,就触发rsync同步,这样恰好解决了同步数据的实时性问题
rsync+inotify实验:
环境说明:
服务器类型 | IP地址 | 应用 | 操做系统 |
---|---|---|---|
源服务器 | 172.16.12.128 | rsync inotify-tools 脚本 |
centos7/redhat7 |
目标服务器 | 172.16.12.129 | rsync | centos7/redhat7 |
实验需求:
部署rsync+inotify同步/etc目录至目标服务器149的/cwh/下。
//第一步将服务端和客户端的防火墙和selinux关闭 //服务端 [root@cwhsever ~]# systemctl stop firewalld [root@cwhsever ~]# setenforce 0 //客户端 [root@cwhclient ~]# systemctl stop firewalld [root@cwhclient ~]# setenforce 0 //第二步安装rsync服务端软件 //服务端 [root@cwhsever ~]# yum -y install rsync //客户端 [root@cwhclient ~]# yum -y install rsync //第三步在客户机上设置rsyncd.confd额配置文件 #部署rsync+inotify同步/etc目录至目标服务器149的/cwh/下 log file = /var/log/rsyncd.log //日志文件位置,启动rsync后自动产生这个文件,无需提早建立 pidfile = /var/run/rsyncd.pid //pid文件的存放位置 lock file = /var/run/rsync.lock //支持max connections参数的锁文件 secrets file = /etc/rsync.pass //用户认证配置文件,里面保存用户名称和密码,必须手动建立这个文件 [test_from_149] //自定义同步名称,相似samba挂载目录 path = /cwh/ //rsync客户端数据存放路径,服务端的数据将同步至此目录 uid = root //设置rsync运行权限为root gid = root //设置rsync运行权限为root port = 873 //设置rsync端口 ignore errors //表示出现错误忽略错误 use chroot = no //默认为true,修改成no,增长对目录文件软链接的备份,不改成no没法备份软链接 read only = no //设置rsync服务端为读写权限,只读没法同步 list = no //不显示rsync服务端资源列表 max connections = 200 //对大链接数 timeout = 600 //设置超时时间 auth users = admin //执行数据同步的用户名,能够设置多个用逗号隔开,要与写入/etc/rsync.pass的用户名一致 hosts allow = 172.16.12.128 //(能够不设置)容许进行数据同步的客户端IP地址,能够设置多个,用英文状态下逗号隔开 hosts deny = 192.168.1.1 //(能够不设置)禁止数据同步的客户端IP地址,能够设置多个,用英文状态下逗号隔开 //第四步建立用户认证文件(也就是写在rsyncd.conf中的用户配置文件) [root@cwhclient ~]# echo 'admin:123456' > /etc/rsync.pass [root@cwhclient ~]# cat /etc/rsync.pass admin:123456 //第五步设置用户认证文件权限 [root@cwhclient ~]# chmod 600 /etc/rsync.pass //第六步在客户端上启动rsync服务并设置开机自启动 [root@cwhclient ~]# systemctl restart rsyncd [root@cwhclient ~]# systemctl enable rsyncd //第七步查看客户机上rsync的端口是否打开 [root@cwhclient ~]# ss -antl State Recv-Q Send-Q Local Address:Port Peer Address:Port LISTEN 0 5 *:873 *:* LISTEN 0 128 *:111 *:* LISTEN 0 128 *:22 *:* LISTEN 0 100 127.0.0.1:25 *:* LISTEN 0 5 :::873 :::* LISTEN 0 128 :::111 :::* LISTEN 0 128 :::22 :::* LISTEN 0 100 ::1:25 :::* //下面操做在源服务器端操做 //第八步关闭服务端防火墙与SELINUX [root@cwhsever ~]# systemctl stop firewalld [root@cwhsever ~]# setenforce 0 //第九步配置yum源(最好是163的网络源) [root@cwhserver ~]# cd /etc/yum.repos.d/ [root@cwhserver yum.repos.d]# wget http://mirrors.163.com/.help/CentOS7-Base-163.repo [root@cwhserver ~]# sed -i 's/\$releasever/7/g' /etc/yum.repos.d/CentOS7-Base-163.repo [root@cwhserver ~]# sed -i 's/^enabled=.*/enabled=1/g' /etc/yum.repos.d/CentOS7-Base-163.repo //第十步装好163网络源后安装epel源 [root@cwhserver ~]# yum -y install epel-release //第十一步安装rsync服务端软件,只须要安装,不要启动,不须要配置(若是以前已经安装则不须要再次安装) [root@cwhsever ~]# yum -y install rsync //第十二步建立认证密码文件,须要将密码写入/etc/rsyncd.pass(若是没有该目录则建立要与客户端一制)中密码要与客户端写入pass文件中的密码一致 [root@cwhsever ~]# echo '123456' > /etc/rsync.pass //注意若是已经建立了该pass目录不要使用重定向 [root@cwhsever ~]# chmod 600 /etc/rsync.pass //第十二步在源服务器上建立测试目录,而后在源服务器同步到客户端 [root@cwhsever ~]# mkdir aaa [root@cwhsever ~]# touch aaa/bbb [root@cwhsever ~]# rsync -avH --port 873 --progress --delete /root/aaa admin@192.168.112.149::cwh_from_149 --password-file=/etc/rsync.pass sending incremental file list aaa/ aaa/bbb 0 100% 0.00kB/s 0:00:00 (xfr#1, to-chk=0/2) sent 124 bytes received 47 bytes 342.00 bytes/sec total size is 0 speedup is 0.00 //在客户端查看 [root@cwhclient ~]# ls /cwh/ aaa [root@cwhclient ~]# ls /cwh/aaa/ bbb //说明测试成功 //第十三步安装inotify-tools工具,实时触发rsync进行同步(注意内核等级是否支持inotify) [root@cwhsever ~]# yum -y install make gcc gcc-c++ [root@cwhsever ~]# yum -y install inotify-tools //第十四步写同步脚本,此步乃最最重要的一步,请慎之又慎。让脚本自动去检测咱们制定的目录下,文件发生的变化,而后再执行rsync的命令把它同步到咱们的服务器端去 [root@localhost ~]# mkdir /scripts/ [root@localhost ~]# vim /scripts/inotify.sh #!/bin/bash host=192.168.112.149 //目标服务器的ip(备份服务器) src=/etc/ //在源服务器上所要监控的备份目录(此处能够自定义,可是要保证存在) des=cwh_from_149 //自定义的模块名,须要与目标服务器上定义的同步名称一致 password=/etc/rsync.pass //执行数据同步的密码文件 user=admin //执行数据同步的用户名 inotifywait=/usr/bin/inotifywait //命令 $inotifywait -mrq --timefmt '%Y%m%d %H:%M' --format '%T %w%f%e' -e modify,delete,create,attrib $src \ | while read files;do rsync -avzP --delete --timeout=100 --password-file=${password} $src $user@$host::$des echo "${files} was rsynced" >>/tmp/rsync.log 2>&1 done //第十五步 [root@localhost ~]# nohup bash /scripts/inotify.sh & //这条命是将脚本放入后台一直运行,不重启就一直运行,若是须要开机自动运行须要写入/etc/rc.local文件中 //第十六步在源服务器上生成一个新文件,并在客户端查看 [root@localhost ~]# touch /etc/lcr090 [root@cwhclient ~]# ls /cwh/ |grep lcr090 lcr090 //能够看出脚本运行成功 //第十七步设置脚本开机自动启动: [root@localhost ~]# chmod a+x /etc/rc.local [root@localhost ~]# echo 'nohup /bin/bash /scripts/inotify.sh' >> /etc/rc.d/rc.local [root@localhost ~]# tail -5 /etc/rc.d/rc.local # Please note that you must run 'chmod +x /etc/rc.d/rc.local' to ensure # that this script will be executed during boot. touch /var/lock/subsys/local nohup /bin/bash /scripts/inotify.sh //能够看出启动脚本的命令已经写入了配置文件中 //第十八步查看重启以后脚本运行结果 [root@localhost ~]# reboot [root@localhost ~]# ps -ef|grep inotify root 1002 995 0 18:22 ? 00:00:00 /bin/bash /scripts/inotify.sh root 1009 1002 0 18:22 ? 00:00:00 /usr/bin/inotifywait -mrq --timefmt %Y%m%d %H:%M --format %T %w%f%e -e modify,delete,create,attrib /etc/ root 1010 1002 0 18:22 ? 00:00:00 /bin/bash /scripts/inotify.sh root 1536 1518 0 18:23 pts/1 00:00:00 grep --color=auto inotify //能够看出重启后脚本已经在运行 [root@localhost ~]# touch /etc/hellocwh //在服务端建立一个文件 [root@cwhclient ~]# ls /cwh/ |grep hellocwh hellocwh //在客户端能够看出已经同步过去了