rsync实时同步

环境说明

服务器类型 IP地址 应用 操作系统 源服务器 192.168.169.139 rsync

inotify-tools

脚本 centos8 目标服务器 192.168.169.142 rsync centos8

rsync常用选项:
    -a, --archive       //归档
    -v, --verbose       //啰嗦模式
    -q, --quiet         //静默模式
    -r, --recursive     //递归
    -p, --perms         //保持原有的权限属性
    -z, --compress      //在传输时压缩,节省带宽,加快传输速度
    --delete            //在源服务器上做的删除操作也会在目标服务器上同步

需求:把源服务器上/etc目录实时同步到目标服务器的/tmp/下

在目标服务器上做以下操作

//关闭防火墙与selinux
[root@master ~]# systemctl disable --now firewalld.service
Removed /etc/systemd/system/multi-user.target.wants/firewalld.service.

Removed /etc/systemd/system/dbus-org.fedoraproject.FirewallD1.service.

[root@master ~]# setenforce 0

//安装rsync
[root@master ~]# dnf -y install rsync rsync-daemon

//配置rsyncd.conf配置文件

[root@master ~]# vim /etc/rsyncd.conf
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    # 用户认证配置文件,里面保存用户名称和密码,必须手动创建这个文件

[etc_from_client]     # 自定义同步名称
path = /tmp/          # rsync服务端数据存放路径,客户端的数据将同步至此目录
comment = sync etc from client
uid = root        # 设置rsync运行权限为root
gid = root        # 设置rsync运行权限为root
port = 873        # 默认端口
ignore errors     # 表示出现错误忽略错误,这个很重要
use chroot = no       # 默认为true,修改为no,增加对目录文件软连接的备份
read only = no    # 设置rsync服务端为读写权限
list = no     # 不显示rsync服务端资源列表
max connections = 200     # 最大连接数
timeout = 600     # 设置超时时间
auth users = admin        # 执行数据同步的用户名,可以设置多个,用英文状态下逗号隔开
#hosts allow = 172.16.12.128   # 允许进行数据同步的客户端IP地址,可以设置多个,用英文状态下逗号隔开
#hosts deny = 192.168.1.1      # 禁止数据同步的客户端IP地址,可以设置多个,用英文状态下逗号隔开
//以上注释在实际使用中需要删除掉,不然可能会出问题

//创建用户生成文件
[root@master ~]# echo 'admin:123456' > /etc/rsync.pass

//修改文件权限,只有管理员可读可写
[root@master ~]# chmod 600 /etc/rsync.pass
[root@master ~]# ll /etc/rsync.pass
-rw-------. 1 root root 13 Sep 22 18:07 /etc/rsync.pass

//设置rsync开机自启
[root@master ~]# systemctl enable --now rsyncd
Created symlink /etc/systemd/system/multi-user.target.wants/rsyncd.service → /usr/lib/systemd/system/rsyncd.service.

[root@master ~]# ss -antl
State           Recv-Q          Send-Q                   Local Address:Port                   Peer Address:Port          Process
LISTEN          0               5                              0.0.0.0:873                         0.0.0.0:*
LISTEN          0               128                            0.0.0.0:22                          0.0.0.0:*
LISTEN          0               5                                 [::]:873                            [::]:*
LISTEN          0               128                               [::]:22                             [::]:*

在源主机上做以下操作

//关闭防火墙与selinux
[root@localhost ~]# systemctl disable --now firewalld.service
Removed /etc/systemd/system/multi-user.target.wants/firewalld.service.

Removed /etc/systemd/system/dbus-org.fedoraproject.FirewallD1.service.

[root@localhost ~]# setenforce 0

//配置epel源
[root@localhost ~]# dnf -y install epel-release

//安装rsync,只需要安装,不要启动,不需要配置
[root@localhost ~]# dnf -y install rsync rsync-daemon

//创建用户认证密码文件
[root@localhost ~]# echo '123456' > /etc/rsync.pass

//修改文件权限,只有管理员可读可写
[root@localhost ~]# ll /etc/rsync.pass
-rw-------. 1 root root 7 Sep 22 18:15 /etc/rsync.pass

//在源服务器上创建测试目录,然后在源服务器运行以下命令
[root@localhost ~]# mkdir -pv /root/etc/test
mkdir: created directory '/root/etc'
mkdir: created directory '/root/etc/test'

[root@localhost ~]# rsync -avH --port 873 --progress --delete /root/etc/ admin@192.168.169.142::etc_from_client --password-file=/etc/rsync.pass
sending incremental file list
deleting vmware-root_879-4013723248/
deleting vmware-root_875-4022308853/
deleting vmware-root_873-4013854327/
deleting vmware-root_866-2722763301/
deleting vmware-root_860-2722763268/
deleting vmware-root_839-3979774022/
deleting .font-unix/
deleting .XIM-unix/
deleting .X11-unix/
deleting .Test-unix/
deleting .ICE-unix/
./
test/

sent 77 bytes  received 285 bytes  724.00 bytes/sec
total size is 0  speedup is 0.00

//运行完成后,在目标服务器上查看,在/tmp目录下有test目录,说明数据同步成功
[root@master ~]# ls /tmp/
test

//安装inotify-tools工具,实时触发rsync进行同步
[root@localhost ~]# ll /proc/sys/fs/inotify/
total 0
-rw-r--r--. 1 root root 0 Sep 22 18:42 max_queued_events
-rw-r--r--. 1 root root 0 Sep 22 18:42 max_user_instances
-rw-r--r--. 1 root root 0 Sep 22 18:42 max_user_watches
//如果有这三个max开头的文件则表示服务器内核支持inotify

//安装inotify-tools
[root@localhost ~]# dnf -y install inotify-tools

//写同步脚本
[root@localhost ~]# mkdir /scripts
[root@localhost ~]# cd /scripts/
[root@localhost scripts]# touch inotify.sh
[root@localhost scripts]# chmod +x inotify.sh
[root@localhost scripts]# ll
total 0
-rwxr-xr-x. 1 root root 0 Sep 22 18:45 inotify.sh
[root@localhost scripts]# vim inotify.sh

#!/bin/bash
host=192.168.169.142
src=/etc
des=etc_from_client
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 scripts]# nohup bash /scripts/inotify.sh &
[1] 11489
[root@localhost scripts]# nohup: ignoring input and appending output to 'nohup.out'

[root@localhost scripts]# ps -efl | grep inotify | grep -v grep
0 S root       11489   11107  0  80   0 -  3174 -      18:49 pts/0    00:00:00 bash /scripts/inotify.sh
4 S root       11490   11489  0  80   0 -  1676 core_s 18:49 pts/0    00:00:00 /usr/bin/inotifywait -mrq --timefmt %Y%m%d %H:%M --format %T %w%f%e -e modify,delete,create,attrib /etc
1 S root       11491   11489  0  80   0 -  3174 -      18:49 pts/0    00:00:00 bash /scripts/inotify.sh

//在目标服务器上查看
root@master ~]# ls /tmp/etc/
..................

//在源服务器上生成一个新文件
[root@localhost ~]# echo "hello world" > /etc/abc
[root@localhost ~]# cat /etc/abc
hello world

//在目标服务器上面进行查看
[root@master ~]# cat /tmp/etc/abc
hello world

设置脚本开机自动启动:

[root@localhost ~]# vim /etc/rc.local
追加以下内容
nohup bash /scripts/inotify.sh &

[root@localhost ~]# vim /etc/rc.local
[root@localhost ~]# chmod +x /etc/rc.d/rc.local

Original: https://www.cnblogs.com/zicnotes/p/16720572.html
Author: Zic师傅
Title: rsync实时同步

原创文章受到原创版权保护。转载请注明出处:https://www.johngo689.com/607004/

转载文章受原作者版权保护。转载请注明原作者出处!

(0)

大家都在看

  • 《闲扯Redis十一》Redis 有序集合对象底层实现

    一、前言 Redis 提供了5种数据类型:String(字符串)、Hash(哈希)、List(列表)、Set(集合)、Zset(有序集合),理解每种数据类型的特点对于redis的开…

    Linux 2023年5月28日
    0108
  • 2. 文件与I/O

    文件与I/o open 系统调用 close 系统调用 creat 系统调用 read 系统调用 write 系统调用 open系统&a…

    Linux 2023年6月6日
    098
  • OpenStack 命令行操作

    命令行删除 环境变量 OpenStack的九个组件必须熟记,命令不需要死记硬背,我们可以通过help来查询相关的命令和参数。如果你直接使用命令来查询或者做其他操作,那么会涉及到环境…

    Linux 2023年6月8日
    090
  • Unit 1 Computer hardware【石家庄铁道大学-专业英语课 】

    Unit 1 Computer hardware 1、Introduction of computer A computer is a machine that can be in…

    Linux 2023年6月14日
    0100
  • WEB自动化-05-Cypress-元素交互

    5 元素交互 元素识别和操作是UI自动化测试的基础,下面一起来学习一下在Cypress中的元素交互操作吧。 5.1 元素定位器选择 每一个测试用例都包含对元素的定位识别和操作等。因…

    Linux 2023年6月7日
    0104
  • [转帖]shell 学习之until语句

    404. 抱歉,您访问的资源不存在。 可能是网址有误,或者对应的内容被删除,或者处于私有状态。 代码改变世界,联系邮箱 contact@cnblogs.com 园子的商业化努力-困…

    Linux 2023年5月28日
    0111
  • Docker容器网络

    Docker容器网络 1、Docker容器网络 Docker在安装后自动提供3种网络,可以使用`docker network ls命令查看 [root@localhost ~]# …

    Linux 2023年6月7日
    0101
  • Lua集成Redis及Nginx

    1 Lua介绍 Lua是一门以其性能著称的脚本语言,被广泛应用在很多方面。Lua一般用于嵌入式应用,现在越来越多应用于游戏 当中,魔兽世界,愤怒的小鸟都有用到。优势 Lua极易嵌入…

    Linux 2023年6月13日
    080
  • 【证券从业】金融基础知识笔记与思维导图

    注1:金融基础知识一本书已整理成完整的思维导图和笔记,需要分享请留言 注2:接下来整理金融法律法规,待整理成完整的笔记后再合并 posted @2022-06-29 23:48 陈…

    Linux 2023年6月13日
    0126
  • 【Prometheus+Grafana系列】监控MySQL服务

    前言 前面的一篇文章已经介绍了 docker-compose 搭建 Prometheus + Grafana 服务。当时实现了监控服务器指标数据,是通过 node_exporter…

    Linux 2023年6月7日
    097
  • 截止2021年底,我国18个税种中已有12个税种完成立法

    截止2021年底,我国18个税种中已有12个税种完成立法: 1.中华人民共和国个人所得税法 (自1980年9月10日起施行)2.中华人民共和国企业所得税法 (自2008年1月1日起…

    Linux 2023年6月14日
    0411
  • 基于 OSPF 路由的邻居邻接关系发现实践

    1、实验目的 理解 OSPF 邻居关系和 OSPF 邻接关系的含义及差别 观察 OSPF 邻居邻接关系的建立过程 观察 OSPF 链路状态数据库的同步过程 2、实验原理 OSPF …

    Linux 2023年6月8日
    0134
  • 小文件、nginx、Redis、Moosefs

    现在有3KW的数据,单条数据都很小的,如果按key-value来看的话,key就是32位的MD5字符串,value按平均算大概是100字节左右。 现在需要将这些数据做缓存以在高并非…

    Linux 2023年5月28日
    0116
  • IDEA maven项目导包报红线

    原因:没有导入maven包 最后有设置一次,以后就无需设置 1、问题 2、打开Setting 3、将下载好的maven包导入 配置一次,就不用每次都需要配置 1、如果在这个界面先关…

    Linux 2023年6月14日
    0115
  • [20220811]奇怪的隐式转换问题(12c补充测试).txt

    [20220811]奇怪的隐式转换问题(12c补充测试).txt –//生产系统遇到一个奇怪的隐式转换问题,问题在于没有发生隐式转换,前面已经做了一些分析增加12c下的…

    Linux 2023年6月13日
    088
  • Android的调用C++代码报错解决办法汇总

    汇总Android通过JNI调用C++代码遇到的错误 IDE:Android Studio BumBlebee | 2021.1.1 dlopen failed: library …

    Linux 2023年6月13日
    0110
亲爱的 Coder【最近整理,可免费获取】👉 最新必读书单  | 👏 面试题下载  | 🌎 免费的AI知识星球