rsync

rsync是什么

rsync特性

1)可以镜像保存整个目录树和文件系统。

2)可以很容易做到保持原来文件的权限、时间、软硬连接等。

3)无需特殊权限即可安装。

4)快速:第一次同步时rsync复制全部内容,但在下一次值传输修改过的内容

5)压缩传输:rysnc在传输的过程中可以实行压缩及解压缩操作,可以使用更少的带宽

6)安全:可以使用scp、ssh等方式来进行文件传输

7)支持匿名传输,以方便进行网站镜像

8)rsync不仅可以远程同步数据(类似于scp),而且可以本地同步数据(类似于cp),做差异同步

9)openssh 8.0已经把scp标记为过时不建议使用了。建议用sftp或者rsync替代scp

rsync的ssh认证协议

rsync命令来同步系统文件之前要先登录 remote主机认证,认证过程中用到的协议有2种:

  • ssh协议
  • rsync协议
rsync server端不用启动进程,只要获取 host的用户名和密码就可以直接同步文件
 server端因为不用启动进程,所以也不用配置文件/etc/rsyncd.conf

ssh认证协议跟 scp的原理是一样的,如果在同步过程中不想输入密码就用 ssh-keygen -t rsa打通通道

rsync命令

Rsync的命令格式常用的有以下三种:
    rsync [OPTION]... SRC DEST
    rsync [OPTION]... SRC [USER@]HOST:DEST
    rsync [OPTION]... [USER@]HOST:SRC DEST
rsync常用选项:
    -a, --archive       //归档
    -v, --verbose       //详细模式输出
    -q, --quiet         //精简输出模式
    -r, --recursive     //递归
    -p, --perms         //保持原有的权限属性
    -z, --compress      //在传输时压缩,节省带宽,加快传输速度
    --delete            //在源服务器上做的删除操作也会在目标服务器上同步

环境说明:

服务器类型 IP地址 应用 操作系统 源服务器 192.168.111.135 rsync inotify-tools 脚本 centos8 目标服务器 192.168.111.137 rsync centos8

需求:

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

两台虚拟机都需要关闭防火墙

[root@localhost ~]# systemctl disable --now firewalld
[root@localhost ~]# setenforce 0
[root@localhost ~]# vim /etc/selinux/config
SELINUX=disabled

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

[root@localhost ~]# hostnamectl set-hostname 137
[root@localhost ~]# bash

安装rsync
[root@137 ~]# dnf -y install rsync
[root@137 ~]# yum -y install rsync-daemon

#修改配置文件
[root@137 ~]# vim /etc/rsyncd.conf
log file = /var/log/rsyncd.log      #添加内容
pidfile = /var/run/rsyncd.pid
lock file = /var/run/rsync.lock
secrets file = /etc/rsync.pass

[etc_from_client]
path = /tmp/
comment = sync etc from client
uid = root
gid = root
port = 873
ignore errors
use chroot = no
read only = no
list = no
max connections = 200
timeout = 600
auth users = admin

#创建用户认证文件
[root@137 ~]# echo 'admin:123456' > /etc/rsync.pass
[root@137 ~]# cat /etc/rsync.pass
admin:123456

#设置文件权限
[root@137 ~]# chmod 600 /etc/rsync*
[root@137 ~]# ll /etc/rsync*
-rw-------. 1 root root  13 Sep 22 18:41 /etc/rsync.pass
-rw-------. 1 root root 790 Sep 22 18:39 /etc/rsyncd.conf

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

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

在源服务器上做以下操作:

[root@localhost ~]# hostnamectl set-hostname 135
[root@localhost ~]# bash

#安装rsync服务端软件,只需要安装,不要启动,不需要配置
[root@135 ~]# yum -y install rsync

#创建认密码文件
[root@135 ~]# echo '123456' > /etc/rsync.pass
[root@135 ~]# cat /etc/rsync.pass
123456

#设置文件权限,只设置文件所有者具有读取、写入权限即可
[root@135 ~]# chmod 660 /etc/rsync.pass
[root@135 ~]# ll /etc/rsync.pass
-rw-rw----. 1 root root 7 Sep 22 18:50 /etc/rsync.pass

#在源服务器上创建测试目录,然后在源服务器运行以下命令
[root@135 ~]# mkdir -pv /root/etc/test
mkdir: created directory '/root/etc'
mkdir: created directory '/root/etc/test'
[root@135 ~]# rsync -avH --port 873 --progress --delete /root/etc/ admin@192.168.111.137::etc_from_client --password-file=/etc/rsync.pass
sending incremental file list
deleting vmware-root_933-3988752732/
./
test/

sent 77 bytes  received 58 bytes  270.00 bytes/sec
total size is 0  speedup is 0.00

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

#安装inotify-tools
[root@135 ~]# yum -y install make gcc gcc-c++ --allowerasing
[root@135 ~]# yum -y install inotify-tools

#编写脚本
[root@135 ~]# mkdir /scripts
[root@135 ~]# touch /scripts/inotify.sh
[root@135 ~]# chmod 755 /scripts/inotify.sh
[root@135 ~]# ll /scripts/inotify.sh
-rwxr-xr-x. 1 root root 0 Sep 22 19:03 /scripts/inotify.sh
[root@135 ~]# vim /scripts/inotify.sh
host=192.168.111.137
src=/etc
des=etc_from_clien
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@135 ~]# nohup bash /scripts/inotify.sh &
[1] 198988
[root@135 ~]# nohup: ignoring input and appending output to 'nohup.out'

[root@135 ~]# ps -ef|grep inotify
root      198988   24963  0 19:11 pts/0    00:00:00 bash /scripts/inotify.sh
root      198989  198988  0 19:11 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
root      198990  198988  0 19:11 pts/0    00:00:00 bash /scripts/inotify.sh
root      200024  199782  0 19:11 pts/2    00:00:00 grep --color=auto inotify

测试脚本

//在源服务器上生成一个新文件
[root@135 ~]# touch /etc/123

#查看inotify生成的日志
[root@135 ~]# tail /tmp/rsync.log
20220922 19:15 /etc/123CREATE was rsynced
20220922 19:15 /etc/123ATTRIB was rsynced

#在目标服务器中也可以看见创建的123文件
[root@137 ~]# ls
etc  test
[root@137 ~]# ls etc/123
etc/123

设置脚本开机自动启动:

[root@135 ~]# chmod +x /etc/rc.d/rc.local
[root@135 ~]# vim /etc/rc.d/rc.local
#
In contrast to previous versions due to parallel execution during boot
this script will NOT be run after all other services.

#
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 &     //添加此行

Original: https://www.cnblogs.com/Their-own/p/16720992.html
Author: 事愿人为
Title: rsync

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

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

(0)

大家都在看

  • MySQL隐式转换的坑

    MySQL以以下规则描述比较操作如何进行转换: 两个参数至少有一个是 NULL 时,比较的结果也是 NULL,例外是使用 实际使用中经研究发现字符串和数字比较时,优先把字符串转换为…

    数据库 2023年6月9日
    075
  • Java 全栈知识体系(2021 PDF 版本)

    40000 +字长文总结,民工哥已将此文整理成PDF文档了,需要的见文后下载获取方式。 全栈知识体系总览 Java入门与进阶 面向对象与Java基础 Java 基础 –…

    数据库 2023年6月9日
    076
  • MySQL数据库的创建、删除和备份

    总结MySQL数据库的创建、删除和备份操作 MySQL数据库的创建、删除和备份 数据库的创建 //使用指令创建数据库 CREATE DATABASE yjh_db01; //创建一…

    数据库 2023年6月16日
    088
  • 中文技术文档写作规范

    使用 markdown 格式书写文档 只使用一二三级标题,三级标题下面的并列性内容使用列表展示 二级标题前使用行分隔符表示分隔 段落之间使用一个空行隔开 一句话或者以逗号分隔的句子…

    数据库 2023年6月6日
    083
  • Mysql-5.7主从部署-yum方式

    一、环境准备 rpm -qa |grep mariadb |xargs yum remove -y setenforce 0(临时关闭),(selinux配置文件:SELINUX=…

    数据库 2023年5月24日
    066
  • NopCommerce学习:MSSQL 2005 排序规则导致中文编码错误

    这两天学习电子商务开源项目NopCommerce,她的确做的很好,在电子商务开源项目中应该算是很棒的. 官方站点: http://www.nopcommerce.com/ 中文社区…

    数据库 2023年6月11日
    090
  • 一段文字

    https://book.douban.com/review/13674387/现代人的困境,其实从出生开始每个人都有强烈的感受。我们会按户口管理,强调身份的是各种标签,各种统计数…

    数据库 2023年6月11日
    081
  • Spark学习(3) SparkSQL

    什么事sparkSQL Spark SQL是Spark用来处理结构化数据的一个模块,它提供了一个编程抽象叫做DataFrame并且作为分布式SQL查询引擎的作用, 它是将Spark…

    数据库 2023年6月16日
    090
  • Qingcloud_MySQL Plus(Xenon) 高可用搭建实验

    实验:Xenon on 5.7.30 Xenon (MySQL Plus) 是青云Qingcloud的一个开源项目,号称金融级别强一致性的高可用解决方案,项目地址为 https:/…

    数据库 2023年6月16日
    0119
  • Resilience4j 实践

    微服务设计模式 – circuit breaker circuit breaker 熔断器,在很多不同的领域都有这个定义,例如电路里面的熔断器,股票行业里面的熔断,当然…

    数据库 2023年6月11日
    0103
  • .NET nhibernate 添加新的表运行报is not mapped的问题

    最后在修改一个.NET nhibernate的项目,按照原来的表添加了一个实体和一个hbm.xml的配置文件,写好所有业务代码以后运行报以下错误 NoAuthorizationSi…

    数据库 2023年6月9日
    098
  • Mybatis的级联查询,分步查询,一对一,一对多和多对一

    配置和代码目录 ***util配置 ***log4j配置 –可以打印入日志,也可以使用系统自带的STDOUT_LOGGING个人喜欢log4j ***mybatis-c…

    数据库 2023年5月24日
    092
  • Java绘图基础

    Graphics 绘图类 Graphic是一个抽象的画笔对象,可以在组件上绘制丰富多彩的几何图形和位图。Graphics类封装了Java支持的基本绘图操作所需的属性,主要包括 颜色…

    数据库 2023年6月16日
    083
  • NO.5 MySQL-笔记

    404. 抱歉,您访问的资源不存在。 可能是URL不正确,或者对应的内容已经被删除,或者处于隐私状态。 [En] It may be that the URL is incorre…

    数据库 2023年5月24日
    088
  • MySQL学习笔记-day02

    select distinct job from emp;# distinct关键字去除重复记录。 结果: +———–+ | job | +———–+ …

    数据库 2023年6月11日
    093
  • 模板语法之继承

    什么是模板继承 模板继承就是指可以使父模板的内容重用,子模板直接继承父模板的全部内容,并可以覆盖父模板中相应的块 继承的语法 父模&am…

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