广告
返回顶部
首页 > 资讯 > 数据库 >rsync远程同步(定期同步,实时同步实战!)
  • 278
分享到

rsync远程同步(定期同步,实时同步实战!)

2024-04-02 19:04:59 278人浏览 独家记忆
摘要

本章结构 1.rsync同步简介2.配置rsync备份源3.rsync命令基本用法4.rsync备份操作示例5.rsync+inotify结合使用 关于rsync . 一款快速增量备份工具 1.Rem

本章结构

1.rsync同步简介
2.配置rsync备份源
3.rsync命令基本用法
4.rsync备份操作示例
5.rsync+inotify结合使用

关于rsync .

一款快速增量备份工具

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

rsync远程同步(定期同步,实时同步实战!)

配置rsync源服务器

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:根据对象的校验和来决定是否跳过文件

配置源的两种标识方法:

rsync远程同步(定期同步,实时同步实战!)

Demo:

环境准备:两台主机

CentOS 7-4作为同步源:192.168.18.148

CentOS 7-5作为客户机:192.168.18.145

Centos 7-4源端的操作:
[root@localhost ~]# hostnamectl set-hostname rsyncd
[root@localhost ~]# su
[root@rsyncd ~]# rpm -q rsync
rsync-3.0.9-18.el7.x86_64
[root@rsyncd ~]# vim /etc/rsyncd.conf
uid = nobody
gid = nobody
use chroot = yes
pid file = /var/run/rsyncd.pid
dont 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/html
comment = www.kGC.com
read only = yes
auth users = backuper
secrets file = /etc/rsyncd_users.db
#在dont下一行插入以上内容:共享模块
#修改完成后按Esc退出插入模式,输入:wq保存退出

#添加密码文件
[root@rsyncd ~]# vim /etc/rsyncd_users.db
backuper:abc123     #需要和共享模块中的auth_users名称一致
#添加完成后按Esc退出插入模式,输入:wq保存退出
[root@rsyncd ~]# chmod 600 /etc/rsyncd_users.db
[root@rsyncd ~]# rsync --daemon
[root@rsyncd ~]# netstat -ntap | grep rsync
tcp        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
总用量 0
drwxr-xr-x. 2 root root  6 8月   8 19:42 cgi-bin
drwxrwxrwx. 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 rsync
rsync-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
总用量 0
drwxr-xr-x. 2 root root 6 8月   8 19:42 cgi-bin
drwxrwxrwx. 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.html

sent 83 bytes  received 172 bytes  12.44 bytes/sec
total 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.html
this 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.html

sent 83 bytes  received 172 bytes  12.44 bytes/sec
total 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.html
this is test web
此时我们如果使用计划性任务,就需要考虑到免交互的问题:
[root@client html]# vim /etc/server.pass
abc123
#写入密码信息后按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.html

sent 83 bytes  received 172 bytes  510.00 bytes/sec
total size is 17  speedup is 0.07
[root@client html]# ls
index.html
[root@client html]# cat index.html
this is test web

#后面就可以在crontab -e中添加计划任务了

rsync实时同步

定期同步的不足

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

实时同步的优点

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

关于inotify

linux内核的inotify机制

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

rsync远程同步(定期同步,实时同步实战!)

rsync+inotify实时同步

调整inotify内核参数:

max_queue_events:监控队列大小
maxuser instances:最多监控实例数
max_ user_watches::每个实例最多监控文件数

rsync远程同步(定期同步,实时同步实战!)

安装inotify-tools辅助工具:

inotifywait:用于持续监控,实时输出结果
inotifywatch:用于短期监控,任务完成后再出结果

rsync远程同步(定期同步,实时同步实战!)

-m:持续进行监控

-r:递归监控所有子对象

-q:简化输出信息

-e:指定要监控哪些事件类型

Demo:

在client发起端中的操作:
[root@client html]# vim /etc/sysctl.conf
#需要在发起端开启监控
#在末行下插入以下内容
fs.inotify.max_queued_events = 16384
fs.inotify.max_user_instances = 1024
fs.inotify.max_user_watches = 1048576
#添加完成后按Esc退出插入模式,输入:wq保存退出

[root@client html]# sysctl -p       #刷新数据
fs.inotify.max_queued_events = 16384
fs.inotify.max_user_instances = 1024
fs.inotify.max_user_watches = 1048576

#加载inotofy管理工具
[root@client html]# mkdir /aaa
[root@client html]# mount.cifs //192.168.10.190/rpm /aaa
Password for root@//192.168.10.190/rpm:
[root@client html]# cd /aaa
[root@client aaa]# ls
Discuz_X3.4_SC_UTF8.zip           Nginx-1.12.2.tar.gz
error.png                         PHP
extundelete-0.2.4.tar.bz2         Redis-5.0.7.tar.gz
haproxy-1.5.19.tar.gz             ruby-2.4.1.tar.gz
httpd-2.4.29.tar.bz2              ruby.png
hzw.jpeg                          squid
inotify-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]# ls
aclocal.m4    config.h.in   COPYING     libinotifytools  man      src
AUTHORS       config.sub    depcomp     ltmain.sh        missing
ChangeLog     configure     INSTALL     Makefile.am      NEWS
config.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]# ls
index.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]# ls
inotify-tools-3.14  rh
[root@client opt]# vim inotify.sh
#!/bin/bash
INOTIFY_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 FILE
do
        if [ $(pgrep rsync | wc -l) -le 0 ] ; then
            $RSYNC_CMD
        fi
done
#添加完成后按Esc退出插入模式,输入:wq保存退出
[root@client opt]# chmod +x inotify.sh
[root@client opt]# ls -l /var/www/
总用量 0
drwxr-xr-x. 2 root root  6 8月   8 19:42 cgi-bin
drwxrwxrwx. 2 root root 24 12月 16 10:00 html
此时还需要注意到CentOS 7-4 rsync服务器端的配置文件:
[root@rsyncd www]# vim /etc/rsyncd.conf
read only = no      #关闭只读功能
#修改完成后按Esc退出插入模式,输入:wq保存退出

执行脚本
[root@rsyncd ~]# netstat -ntap | grep rsync
tcp        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]# ls
abrt          dhclient-ens33.pid  lock            radvd           syslogd.pid
alsactl.pid   dmeventd-client     log             rpcbind         systemd
atd.pid       dmeventd-server     lsm             rpcbind.sock    tmpfiles.d
auditd.pid    faillock            lvm             rsyncd.pid      tuned
avahi-daemon  firewalld           lvmetad.pid     samba           udev
certmonger    gdm                 mdadm           sepermit        udisks2
chrony        gssproxy.pid        media           setrans         user
chronyd.pid   gssproxy.sock       mount           setroubleshoot  utmp
console       httpd               named           sm-notify.pid   vmware
crond.pid     initramfs           netreport       spice-vdagentd  xtables.lock
cron.reboot   ksmtune.pid         NetworkManager  sshd.pid
cups          libvirt             plymouth        sudo
dbus          libvirtd.pid        ppp             sysconfig
[root@rsyncd run]# cat rsyncd.pid
2768
[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.pid
5416
#此时正常运转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]# ls
index.html  test.txt
[root@rsyncd html]# cat test.txt
this is test

以上就实现了实时同步!

您可能感兴趣的文档:

--结束END--

本文标题: rsync远程同步(定期同步,实时同步实战!)

本文链接: https://www.lsjlt.com/news/43698.html(转载时请注明来源链接)

有问题或投稿请发送至: 邮箱/279061341@qq.com    QQ/279061341

本篇文章演示代码以及资料文档资料下载

下载Word文档到电脑,方便收藏和打印~

下载Word文档
猜你喜欢
  • rsync远程同步(定期同步,实时同步实战!)
    本章结构 1.rsync同步简介2.配置rsync备份源3.rsync命令基本用法4.rsync备份操作示例5.rsync+inotify结合使用 关于rsync . 一款快速增量备份工具 1.Rem...
    99+
    2022-10-18
  • rsync实时同步
    与传统的cp、tar备份方式相比,rsync具有安全性高、备份迅速、支持增量备份等优点,通过rsync可以解决对实时性要求不高的数据备份需求,例如定期的备份文件服务器数据到远端服务器,对本地磁盘定期做数据镜像等。192.168.0.1 源服...
    99+
    2023-06-05
  • Rsync远程同步
    一、rsync远程同步 1.rsync简介   rsync是C/S架构的数据镜像备份工具,可以实现全量备份和快速增量备份支持本地复制或ssh、rsync主机同步。 rsync默认端口为 873 rsync特性:可以在不通主机之间镜像同步整个...
    99+
    2023-10-27
    linux 服务器 运维
  • centos7下rsync+crontab定期同步备份
    最近需求想定期备份内部重要的服务器数据到存储里面,顺便做个笔记 以前整过一个win下的cwrsync(客户端)+rsync(服务端:存储)的bat脚本 这次整一个linux下的脚本sh,执行定期自动备份数据 客户端:1...
    99+
    2022-06-04
    centos7 crontab同步备份 rsync crontab
  • Linux数据实时同步(sersync+rsync)
    需求:由于单台服务器存储着所有的文件,为了防止服务器故障导致文件丢失或者损坏,先将Master(175)/var/ftp/pub/的目录文件实时备份到Slave(176)/backup/目录下。环境:说明:Rsync可以远程同步,支持本地复...
    99+
    2023-05-16
    Linux 数据同步 Rsync
  • 怎么使用rsync实现远程同步
    这篇文章主要为大家展示了“怎么使用rsync实现远程同步”,内容简而易懂,条理清晰,希望能够帮助大家解决疑惑,下面让小编带领大家一起研究并学习一下“怎么使用rsync实现远程同步”这篇文章吧。rsync是可以实现增量备份的工具。配合任务计划...
    99+
    2023-06-28
  • Rsync如何实现文件同步
    这篇文章主要介绍“Rsync如何实现文件同步”的相关知识,小编通过实际案例向大家展示操作过程,操作方法简单快捷,实用性强,希望这篇“Rsync如何实现文件同步”文章能帮助大家解决问题。rsync的目的是实现本地主机和远程主机上的文件同步(包...
    99+
    2023-06-27
  • javascript实现同步服务器时间、同步倒计时
    这篇文章主要介绍“javascript实现同步服务器时间、同步倒计时”,在日常操作中,相信很多人在javascript实现同步服务器时间、同步倒计时问题上存在疑惑,小编查阅了各式资料,整理出简单好用的操作方...
    99+
    2022-10-19
  • Linux 通过Rsync+Inotify实现本、异地远程数据实时同步功能
    0x0 测试环境 总部生产服务器与分部备份服务器要求实现异地数据备份,环境如下 **centos 6.5** 生产服务器目录: /home/zytest/files 备份服务器目录: /home/zytest/fi...
    99+
    2022-06-03
    rsync+inotify实现远程实时同步 linux 远程数据同步
  • PHP Spring 文件同步:如何做到实时同步?
    文件同步是一项非常重要的任务,特别是在开发过程中,你需要不断地同步代码和文件以保证整个项目的正常运行。而实时同步则更为重要,它可以让你在修改文件之后立即看到效果,从而加快开发效率。本文将介绍如何使用 PHP Spring 实现实时文件同步...
    99+
    2023-08-06
    spring 文件 同步
  • 如何实现CentOS 5.4 rsync+inotify配置触发式实时文件远程同步
    这篇文章给大家介绍如何实现CentOS 5.4 rsync+inotify配置触发式实时文件远程同步,内容非常详细,感兴趣的小伙伴们可以参考借鉴,希望对大家能有所帮助。软件:rsync-2.6.8-3.1(一般系统默认安装)、inotify...
    99+
    2023-06-10
  • lsyncd实时同步工具
    首先介绍一下架构有3台app服务器,每次上线,是通过shell脚本将代码传输至app_1然后app_1自动同步到app_2和app_3代码路径为/www有一台code服务器,专门用来下载所有服务器代码的。路径为/data最开始的同步策略是r...
    99+
    2023-01-30
    实时 工具 lsyncd
  • Python 实时同步编程算法:如何实现高效的数据同步?
    在当今数据驱动的世界中,数据同步已经成为了一个非常重要的话题。如果你是一名开发人员或者系统管理员,你一定知道数据同步的重要性。数据同步可以确保我们的数据能够在不同的系统之间保持同步,从而确保我们的业务流程顺畅运行。 在本文中,我们将介绍 ...
    99+
    2023-10-21
    实时 同步 编程算法
  • Awaitility同步异步工具实战示例详解
    目录引言1. awaitility入门1.1 静态导入1.2 简单例子2. awaitility在RocketMQ中的实战3. 总结引言 在编写测试用例的时候遇到有异步或者队列处理的...
    99+
    2022-11-13
  • Mysql数据实时同步实践
    关于小米内部使用的数据库你知道多少?背景Mysql由于自身简单、高效、可靠的特点,成为小米内部使用最广泛的数据库,但是当数据量达到千万/亿级别的时候,mysql的相关操作会变的非常迟缓;如果这时还有实时BI...
    99+
    2022-10-18
  • Windowsserver2012NTP时间同步的实现
    NTP 服务【 Network Time Protocol ( NTP )】是用来使计算机时间同步化的一种协议,它可以使计算机 对其服务器或时钟源(如石英钟,GPS 等等 ) 做同步...
    99+
    2022-11-13
  • clickhouse实时同步MySQL数据
    两种方式         1、使用clickhouse表引擎,直接从MySQL中读取数据(针对表),如果业务需求不是很复杂,可以选择此方式,需要哪张表就配置哪张表,操作简单,数据实时同步;         2、使用clickhouse数据库...
    99+
    2023-09-02
    mysql clickhouse 数据库
  • CentOS平台实现搭建rsync远程同步服务器的方法
    本文实例讲述了CentOS平台实现搭建rsync远程同步服务器的方法。分享给大家供大家参考,具体如下: rsync(remote synchronize)是一个远程数据同步工具,可通过LAN/WAN快速同步多台主机间的文...
    99+
    2022-06-04
    CentOS rsync 远程同步服务器
  • Git 同步编程算法和 Java:如何同时实现?
    在软件开发中,Git 是一个广泛使用的版本控制工具,而 Java 则是一种常用的编程语言。但是,如何在 Git 中同步编程算法和 Java 代码呢?本文将介绍如何同时实现这两个方面的同步。 一、Git 同步编程算法 在 Git 中同步编程...
    99+
    2023-09-25
    git 同步 编程算法
  • 基于OGG实现MySQL实时同步
    📢📢📢📣📣📣 哈喽!大家好,我是【IT邦德】,江湖人称jeames007,10余年DBA及大数据工作经验 一位上进心十足的【大数据领域博主】!😜😜😜 中国DBA联盟(ACDU)成员,目前服务于工业互联网 擅长主流Oracle、MySQL...
    99+
    2023-12-22
    mysql 数据库
软考高级职称资格查询
编程网,编程工程师的家园,是目前国内优秀的开源技术社区之一,形成了由开源软件库、代码分享、资讯、协作翻译、讨论区和博客等几大频道内容,为IT开发者提供了一个发现、使用、并交流开源技术的平台。
  • 官方手机版

  • 微信公众号

  • 商务合作