千家信息网

rsync远程同步中的定期同步和实时同步怎么用

发表于:2025-02-04 作者:千家信息网编辑
千家信息网最后更新 2025年02月04日,本篇文章给大家分享的是有关rsync远程同步中的定期同步和实时同步怎么用 ,小编觉得挺实用的,因此分享给大家学习,希望大家阅读完这篇文章后可以有所收获,话不多说,跟着小编一起来看看吧。关于rsync
千家信息网最后更新 2025年02月04日rsync远程同步中的定期同步和实时同步怎么用

本篇文章给大家分享的是有关rsync远程同步中的定期同步和实时同步怎么用 ,小编觉得挺实用的,因此分享给大家学习,希望大家阅读完这篇文章后可以有所收获,话不多说,跟着小编一起来看看吧。

关于rsync .

一款快速增量备份工具

    1.Remote Sync,远程同步    2.支持本地复制,或者与其他SSH、rsync主机同步    3.官方网站: http://rsync.samba.org

配置rsync源服务器

rsync同步源:

    指备份操作的远程服务器,也称为备份源

配置rsync源

基本思路:

1.建立rsyncd.conf配置文件、独立的账号文件.启用rsync的--daemon模式

应用示例:

1.户backuper,允许下行同步2.操作的目录为/var/www/html/

配置文件rsyncd.conf

1.需手动建立,语法类似于Samba配置2.认证配置auth users、secrets file,不加则为匿名

rsync账号文件

1.采用"用户名:密码"的记录格式,每行一个用户记录2.独立的账号数据,不依赖于系统账号

启用rsync服务

1.通过--daemon独自提供服务 [执行kill $(catIvar/run/rsyncd.pid)关闭rsync服务]

使用rsync备份工具

rsync命令的用法:

rsync [选项] 原始位置 目标位置1.常用选项:-a:归档模式,递归并保留对象属性,等同于-rlptgoD-v:显示同步过程的详细(verbose)信息-z:在传输文件时进行压缩(compress)-H:保留硬连接文件-A:保留ACL属性信息--delete:删除目标位置有而原始位置没有的文件--checksum:根据对象的校验和来决定是否跳过文件

配置源的两种标识方法:格式1:用户名@主机地址::共享模块名
br/>格式1:用户名@主机地址::共享模块名
[root@localhost ~]# rsync -avz
backuper@ 192.168.4.200::wwwroot /root
[root@localhost ~]# rsync -avz
rsync://backuper@ 192.1 68.4.200/wwwroot /root

实验操作:

环境准备:两台主机

CentOS 7-4作为同步源:192.168.18.148CentOS 7-5作为客户机:192.168.18.145

CentOS 7-4源端的操作:

[root@localhost ~]# hostnamectl set-hostname rsyncd[root@localhost ~]# su[root@rsyncd ~]# rpm -q rsyncrsync-3.0.9-18.el7.x86_64[root@rsyncd ~]# vim /etc/rsyncd.confuid = nobodygid = nobodyuse chroot = yespid file = /var/run/rsyncd.piddont compress   = *.gz *.tgz *.zip *.z *.Z *.rpm *.deb *.bz2#以上内容去掉注释address = 192.168.18.148        #本机IP地址port 873        #开端口log file = /var/rsyncd.log      #指定日志文件 hosts allow = 192.168.18.0/24   #允许网段访问#在pid下行添加以上内容[wwwroot]path = /var/www/htmlcomment = www.kgc.comread only = yesauth users = backupersecrets file = /etc/rsyncd_users.db#在dont下一行插入以上内容:共享模块#修改完成后按Esc退出插入模式,输入:wq保存退出#添加密码文件[root@rsyncd ~]# vim /etc/rsyncd_users.dbbackuper:abc123     #需要和共享模块中的auth_users名称一致#添加完成后按Esc退出插入模式,输入:wq保存退出[root@rsyncd ~]# chmod 600 /etc/rsyncd_users.db[root@rsyncd ~]# rsync --daemon[root@rsyncd ~]# netstat -ntap | grep rsynctcp        0      0 192.168.18.148:873      0.0.0.0:*          LISTEN      6150/rsync#此时873端口开启[root@rsyncd ~]# systemctl stop firewalld.service[root@rsyncd ~]# setenforce 0[root@rsyncd ~]# yum install httpd -y[root@rsyncd html]# cd ..[root@rsyncd www]# chmod 777 html/[root@rsyncd www]# ls -l总用量 0drwxr-xr-x. 2 root root  6 8月   8 19:42 cgi-bindrwxrwxrwx. 2 root root 24 12月 16 08:41 html

CentOS 7-5客户机的操作:

[root@localhost ~]# hostnamectl set-hostname client[root@localhost ~]# su[root@client ~]# systemctl stop firewalld.service[root@client ~]# setenforce 0[root@client ~]# rpm -q rsyncrsync-3.0.9-18.el7.x86_64[root@client ~]# yum install httpd -y[root@client ~]# cd /var/www/html/#此时文件中是空的没有文件的[root@client html]# cd ..[root@client www]# chmod 777 html/[root@client www]# ls -l总用量 0drwxr-xr-x. 2 root root 6 8月   8 19:42 cgi-bindrwxrwxrwx. 2 root root 6 8月   8 19:42 html#同步方法一:[root@client www]#  rsync -avz backuper@192.168.18.148::wwwroot /var/www/html/Password:       #此时输入密码abc123,敲回车receiving incremental file list./index.htmlsent 83 bytes  received 172 bytes  12.44 bytes/sectotal size is 17  speedup is 0.07[root@client www]# cd html/[root@client html]# ls      #此时index.html文件被同步index.html[root@client html]# cat index.htmlthis is test web#同步方法二:[root@client www]#  rsync -avz rsync://backuper@192.168.18.148::wwwroot /var/www/html/Password:       #此时输入密码abc123,敲回车receiving incremental file list./index.htmlsent 83 bytes  received 172 bytes  12.44 bytes/sectotal size is 17  speedup is 0.07[root@client www]# cd html/[root@client html]# ls      #此时index.html文件被同步index.html[root@client html]# cat index.htmlthis is test web

使用计划性任务时,免交互的配置

[root@client html]# vim /etc/server.passabc123#写入密码信息后按Esc退出插入模式,输入:wq保存退出[root@client html]# chmod 600 /etc/server.pass[root@client html]# rsync -avz --delete --password-file=/etc/server.pass backuper@192.168.18.148::wwwroot /var/www/html/        #用此条命令可以直接进入receiving incremental file list./index.htmlsent 83 bytes  received 172 bytes  510.00 bytes/sectotal size is 17  speedup is 0.07[root@client html]# lsindex.html[root@client html]# cat index.htmlthis is test web#后面就可以在crontab -e中添加计划任务了

rsync实时同步

定期同步的不足

1.执行备份的时间固定,延迟明显、实时性差2.当同步源长期不变化时,密集的定期任务是不必要的

实时同步的优点

1.一旦同步源出现变化,立即启动备份2.只要同步源无变化,则不执行备份

关于inotify

Linux内核的inotify机制

1.从版本2.6.13开始提供2.可以监控文件系统的变动情况,并作出通知响应3.辅助软件: inotify-tools

rsync+inotify实时同步

调整inotify内核参数:

    max_queue_events:监控队列大小    maxuser instances:最多监控实例数    max_ user_watches::每个实例最多监控文件数
[root@localhost ~ ]# vi /etc/sysctl.conf......fs.inotify.max_ queued events = 16384fs.inotify.max user_ instances = 1024fs.inotify.max user watches = 1048576 [root@localhost ~]# sysctl -p

安装inotify-tools辅助工具

    inotifywait:用于持续监控,实时输出结果    inotifywatch:用于短期监控,任务完成后再出结果[root@localhost ~]# inotifywait -mrq -e modify,create,move,delete/var/www/htmlSetting up watches. Beware: since -r was given, this may take a while! Watches established./var/www/html/ CREATE index.php/var/www/html/ MODIFY index.php/var/www/html/ MOVED_FROM index.php/var/www/htmI/ MOVED_TO test.php
-m:持续进行监控-r:递归监控所有子对象-q:简化输出信息-e:指定要监控哪些事件类型

实验操作:

在client发起端中的操作:

[root@client html]# vim /etc/sysctl.conf#需要在发起端开启监控#在末行下插入以下内容fs.inotify.max_queued_events = 16384fs.inotify.max_user_instances = 1024fs.inotify.max_user_watches = 1048576#添加完成后按Esc退出插入模式,输入:wq保存退出[root@client html]# sysctl -p       #刷新数据fs.inotify.max_queued_events = 16384fs.inotify.max_user_instances = 1024fs.inotify.max_user_watches = 1048576#加载inotofy管理工具[root@client html]# mkdir /aaa[root@client html]# mount.cifs //192.168.10.190/rpm /aaaPassword for root@//192.168.10.190/rpm:[root@client html]# cd /aaa[root@client aaa]# lsDiscuz_X3.4_SC_UTF8.zip           nginx-1.12.2.tar.gzerror.png                         phpextundelete-0.2.4.tar.bz2         redis-5.0.7.tar.gzhaproxy-1.5.19.tar.gz             ruby-2.4.1.tar.gzhttpd-2.4.29.tar.bz2              ruby.pnghzw.jpeg                          squidinotify-tools-3.14.tar.gz         TC[root@client aaa]# tar zxvf inotify-tools-3.14.tar.gz -C /opt/[root@client opt]# cd /opt/inotify-tools-3.14/[root@client inotify-tools-3.14]# lsaclocal.m4    config.h.in   COPYING     libinotifytools  man      srcAUTHORS       config.sub    depcomp     ltmain.sh        missingChangeLog     configure     INSTALL     Makefile.am      NEWSconfig.guess  configure.ac  install-sh  Makefile.in      README[root@client inotify-tools-3.14]# yum install gcc gc-c++ make -y[root@client inotify-tools-3.14]# ./configure[root@client inotify-tools-3.14]# make && make install[root@client inotify-tools-3.14]# inotifywait -mrq -e modify,create,move,delete /var/www/html/#进入监控状态,监控本地的html文件中的更新,创建,移动,删除

此时无法进行操作,我们需要再开一个远程连接以进行操作

[root@client ~]# cd /var/www/html/[root@client html]# lsindex.html[root@client html]# touch abc       #创建新的abc文件[root@client html]# rm -rf abc      #删除abc

此时会在监控的操作界面显示同步到了此动作:

/var/www/html/ CREATE abc       #同步到创建动作/var/www/html/ DELETE abc       #同步到删除动作

我们可以使用:监控触发动作,然后调取rsync进行同步

在监控客户先使用Ctrl+c停止监控,然后创建脚本,操作如下:

[root@client inotify-tools-3.14]# cd /opt/[root@client opt]# lsinotify-tools-3.14  rh[root@client opt]# vim inotify.sh#!/bin/bashINOTIFY_CMD="inotifywait -mrq -e modify,create,attrib,move,delete /var/www/html/"RSYNC_CMD="rsync -azH --delete --password-file=/etc/server.pass /var/www/html/ backuper@192.168.18.148::wwwroot/"$INOTIFY_CMD | while read DIRECTORY EVEVT FILEdo        if [ $(pgrep rsync | wc -l) -le 0 ] ; then            $RSYNC_CMD        fidone#添加完成后按Esc退出插入模式,输入:wq保存退出[root@client opt]# chmod +x inotify.sh[root@client opt]# ls -l /var/www/总用量 0drwxr-xr-x. 2 root root  6 8月   8 19:42 cgi-bindrwxrwxrwx. 2 root root 24 12月 16 10:00 html

此时还需要注意到CentOS 7-4 rsync服务器端的配置文件:

[root@rsyncd www]# vim /etc/rsyncd.confread only = no      #关闭只读功能#修改完成后按Esc退出插入模式,输入:wq保存退出执行脚本[root@rsyncd ~]# netstat -ntap | grep rsynctcp        0      0 192.168.18.148:873      0.0.0.0:*          LISTEN      2768/rsync[root@rsyncd ~]# kill -9 2768       #杀死该进程[root@rsyncd ~]# rsync --daemon     #启动rsync[root@rsyncd ~]# failed to create pid file /var/run/rsyncd.pid: File exists#提示有pid文件存在[root@rsyncd ~]# cd /var/run/[root@rsyncd run]# lsabrt          dhclient-ens33.pid  lock            radvd           syslogd.pidalsactl.pid   dmeventd-client     log             rpcbind         systemdatd.pid       dmeventd-server     lsm             rpcbind.sock    tmpfiles.dauditd.pid    faillock            lvm             rsyncd.pid      tunedavahi-daemon  firewalld           lvmetad.pid     samba           udevcertmonger    gdm                 mdadm           sepermit        udisks2chrony        gssproxy.pid        media           setrans         userchronyd.pid   gssproxy.sock       mount           setroubleshoot  utmpconsole       httpd               named           sm-notify.pid   vmwarecrond.pid     initramfs           netreport       spice-vdagentd  xtables.lockcron.reboot   ksmtune.pid         NetworkManager  sshd.pidcups          libvirt             plymouth        sudodbus          libvirtd.pid        ppp             sysconfig[root@rsyncd run]# cat rsyncd.pid2768[root@rsyncd run]# rm -rf rsyncd.pid        #删除此pid文件[root@rsyncd run]# rsync --daemon       #再次启动[root@rsyncd run]# netstat -ntap | grep rsync       #此时会生成新的pid号tcp        0      0 192.168.18.148:873      0.0.0.0:*         LISTEN      5416/rsync[root@rsyncd run]# cat rsyncd.pid5416#此时正常运转rsync

在CentOS 7-5 client端开启监控:

[root@client opt]# ./inotify.sh#此时监控开启

再打开另一个7-5的远程连接开始写内容:

[root@client html]# echo "this is test" > test.txt

此时文件同步到CentOS 7-4 rsync服务器端,我们可以进行查询:

[root@rsyncd run]# cd /var/www/html/[root@rsyncd html]# lsindex.html  test.txt[root@rsyncd html]# cat test.txtthis is test

以上就是rsync远程同步中的定期同步和实时同步怎么用 ,小编相信有部分知识点可能是我们日常工作会见到或用到的。希望你能通过这篇文章学到更多知识。更多详情敬请关注行业资讯频道。

0