首页 > 其他分享 >rsync

rsync

时间:2022-09-22 23:11:21浏览次数:59  
标签:rsync inotify -- etc 135 root

rsync

rsync是什么

rsync是linux系统下的数据镜像备份工具。使用快速增量备份工具Remote Sync可以远程同步,支持本地复制,或者与其他SSH、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`端不用启动`rsync`的`daemon`进程,只要获取`remote host`的用户名和密码就可以直接`rsync`同步文件
`rsync server`端因为不用启动`daemon`进程,所以也不用配置文件`/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            //在源服务器上做的删除操作也会在目标服务器上同步

rsync实时同步

环境说明:

服务器类型 IP地址 应用 操作系统
源服务器 192.168.142.130 rsync inotify-tools 脚本 centos8
目标服务器 192.168.142.134 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/ [email protected]::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 &     //添加此行

标签:rsync,inotify,--,etc,135,root
From: https://www.cnblogs.com/soap-bubble/p/16721176.html

相关文章

  • rsync
    rsync目录rsyncrsync是什么rsync特性rsync的ssh认证协议rsync命令rsync实时同步目标服务器上做以下操作:在目标服务器上做以下操作:测试脚本rsync是什么rsync是linux系统......
  • rsync实时同步
    rsync实时同步环境说明服务器类型IP地址应用操作系统源服务器192.168.169.139rsyncinotify-tools脚本centos8目标服务器192.168.169.142rsynccentos......
  • 19.rsync工具
    rsync用作数据镜像备份工具服务端安装部署(192.168.157.136)安装rsync和xinetdyum-yinstallrsync.x86_64yum-yinstallxinetdxinetd作为管理操作系统中不......
  • Rsync数据备份工具
    Rsync数据备份工具1、Rsync基本概述rsync是一款开源的备份工具,可以在不同主机之间进行同步(windows和Linux之间Mac和LinuxLinux和Linux),可实现全量备份与增量备份,因此......
  • rsync同步远程复制学习
    常用服务之rsync(1)Cloud研习社 Cloud研习社 2022-08-2307:38 发表于山东收录于合集#一站式教程128个#linux61个#服务器7个#云计算43个#rsync3个教程每周二......
  • rsync同步数据
    1.数据获取服务器安装rsyncyum-yinstallrsync2.查看安装情况psaux|greprsyncd  3.编辑密码操作vim/etc/rsyncd.secretsroot:123567 ......
  • rsync服务实时同步数据
    #selinux和防火墙关闭systemctlstopfirewalld.servicesystemctldisablefirewalld.service#安装xinetd和rsyncyum-yinstallxinetdrsync#启动rsync的进程......
  • 【云原生】rsync+inotify数据实时同步介绍与k8s实战应用
    目录一、rsync概述二、rsync优缺点1)优点2)缺点三、rsync+inotify配置与安装1)rsync安装2)rsync配置3)启动rsync服务4)测试验证5)编译安装inotify-tools1、inotify-tools概述2、......
  • Linux rsync使用
    rsync的优点1.它有效地将文件复制和同步到远程系统或从远程系统同步文件到本地。2.支持复制链接、设备、所有者、组和权限。3.它比scp(SecureCopy)更快,因为rsync使用......
  • rsync 命令
    linux上的rsync命令详解15个rsync命令实施-z:--compress使用压缩机制-v:--verbose打印详细信息-r:--recursive以递归模式同步子目录-a--archive归档模式选项.......