栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 系统运维 > 运维 > Linux

rsync

Linux 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

rsync

rsync

rsync

简介
rsync 是一个常用的 Linux 应用程序,用于文件同步。

它可以在本地计算机与远程计算机之间,或者两个本地目录之间同步文件(但不支持两台远程计算机之间的同步)。它也可以当作文件复制工具,替代cp和mv命令。里面的r指的是remote,rsync 其实就是"远程同步"(remote sync)的意思。与其他文件传输工具(如 FTP 或 scp)不同,rsync 的最大特点是会检查发送方和接收方已有的文件,仅传输有变动的部分(默认规则是文件大小或修改时间有变动)。

rsync特性

可以镜像保存整个目录树和文件系统
可以很容易做到保持原来文件的权限、时间、软硬链接等等
无须特殊权限即可安装
快速:第一次同步时rsync会复制全部内容,但在下一次只传输修改过的文件。rsync在传输数据的过程中可以实行压缩及解压缩操作,因此可以使用更少的带宽
安全:可以使用scp、ssh等方式来传输文件,当然也可以通过直接的socket连接
支持匿名传输,以方便进行网站镜像

//Rsync的命令格式常用的有以下三种:
rsync [OPTION]… SRC DEST
rsync [OPTION]… SRC [USER@]HOST:DEST
rsync [OPTION]… [USER@]HOST:SRC DEST

//对应于以上三种命令格式,rsync有三种不同的工作模式:
环境

主机名ip
server192.168.58.10
client192.168.58.20
  • 拷贝本地文件。当SRC和DES路径信息都不包含有单个冒号":"分隔符时就启动这种工作模式。如:
//关闭防火墙
##传输的双方都需要关闭防火墙
[root@server ]# systemctl stop firewalld
[root@server ]# systemctl disable firewalld
Removed /etc/systemd/system/multi-user.target.wants/firewalld.service.
Removed /etc/systemd/system/dbus-org.fedoraproject.FirewallD1.service.
[root@server ]# setenforce 0

[root@server ]# yun -y install rsync
##传输的双方都需要安装rsync
.........
传输
[root@server ~]# rsync -avz xu/ root@192.168.58.20:/tmp/
The authenticity of host '192.168.58.20 (192.168.58.20)' can't be established.
ECDSA key fingerprint is SHA256:WWNUfnxMY6JpaYtzbdkxTIZSANvOUmXQl0hTULKwikw.
Are you sure you want to continue connecting (yes/no/[fingerprint])? yes
root@192.168.58.20's password: 
sending incremental file list
xu/

sent 52 bytes  received 20 bytes  28.80 bytes/sec
total size is 0  speedup is 0.00

验证
[root@client ~]# cd /tmp/
[root@client tmp]# ls
ks-script-agf_49hx  vmware-root_970-2956993747  vmware-root_975-4281646728  xu
[root@client tmp]# 

使用一个远程shell程序(如rsh、ssh)来实现将本地机器的内容拷贝到远程机器。当DST路径地址包
含单个冒号":"分隔符时启动该模式。如:

模拟生成文件
[root@client ~]# cd /opt/
[root@client opt]# touch meng
[root@client opt]# ls
meng
[root@client opt]# 

传输
[root@server ~]# rsync -avz root@192.168.58.20:/opt/meng /root/
root@192.168.58.20's password: 
receiving incremental file list
meng

验证
sent 43 bytes  received 82 bytes  27.78 bytes/sec
total size is 0  speedup is 0.00
[root@server ~]# ls
anaconda-ks.cfg  meng  xu
[root@server ~]# 

使用一个远程shell程序(如rsh、ssh)来实现将远程机器的内容拷贝到本地机器。当SRC地址路径
包含单个冒号":"分隔符时启动该模式。如:

[root@client opt]# touch stp.sh
[root@client opt]# ls
meng  stp.sh
[root@client opt]# chmod +x stp.sh 
[root@client opt]# ls
meng  stp.sh
[root@client opt]# 

传输
[root@server ~]# rsync -avz root@192.168.58.20:/opt/stp.sh /root/
root@192.168.58.20's password: 
receiving incremental file list
stp.sh

sent 43 bytes  received 85 bytes  51.20 bytes/sec
total size is 0  speedup is 0.00

验证
total size is 0  speedup is 0.00
[root@server ~]# ls
anaconda-ks.cfg  meng  stp.sh  xu

rsync常用选项:

参数注释
-a归档
-v啰嗦模式
-q静默模式
-r递归
-p保持原有的权限属性
-z,–compress //在传输时压缩,节省带宽,加快传输速度
-z–delete //在源服务器上做的删除操作也会在目标服务器上同步
inotify+rsync

Inotify是一种强大的,细粒度的。异步的文件系统事件监控机制,linux内核从2.6.13起,加入了 Inotify支持,通过Inotify可以监控文件系统中添加、删除,修改、移动等各种事件,利用这个内核接口,第三方软件就可以监控文件系统下文件的各种变化情况,而 inotify-tools 正是实施这样监控的软件。国人周洋在金山公司也开发了类似的实时同步软件sersync。

如何实现同步

  • 利用监控服务(inotify),监控同步数据服务器目录中信息的变化
  • 发现目录中数据产生变化,就利用rsync服务推送到备份服务器上

实时同步原理

inotify+rsync使用方式

  • inotify 对同步数据目录信息的监控

  • rsync 完成对数据信息的实时同步

  • 利用脚本进行结合

实战环境

服务器类型主机名ip应用
源服务器server192.168.58.10rsync inotify-tools 脚本
目标服务器client192.168.58.20rsync

需求:

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

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

[root@client ~]# yum -y install rsync
安装过程略.....
yum -y install rsync-daemon
安装过程略.....

配置rsyncd.conf配置文件
[root@client ]# cat >> /etc/rsyncd.conf < /etc/rsync.pass

设置文件权限
[root@client ~]# chmod 600 /etc/rsync*
[root@client ~]# ll /etc/rsync*
-rw-------. 1 root root 789 10月 12 03:24 /etc/rsyncd.conf
-rw-------. 1 root root  10 10月 12 03:24 /etc/rsync.pass

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

查看端口号
[root@client ~]# ss -antl
State         Recv-Q        Send-Q               Local Address:Port                Peer Address:Port        
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                          [::]:*           
[root@client ~]# 

源服务器上操作:

配置elerl源
yum -y install epel-release
安装过程略.....

安装inotify-tools
[root@server ~]# yum -y install inotify-tools
安装过程略......

创建认证密码文件
[root@server ~]# echo '123' > /etc/rsync.pass
[root@server ~]# chmod 600 /etc/rsync.pass

创建测试目录
[root@server ~]# mkdir /opt/test
[root@server ~]# rsync -avH --port 873 --progress --delete /opt/test/ admin@192.168.58.20::etc_from_client --password-file=/etc/rsync.pass
sending incremental file list
deleting xu/
deleting vmware-root_970-2956993747/
./

sent 51 bytes  received 61 bytes  224.00 bytes/sec
total size is 0  speedup is 0.00
[root@server ~]# 

创建脚本存放目录及编写同步脚本

[root@server /]# mkdir scripts
[root@server /]# ls
bin   dev  home  lib64  mnt  proc  run   scripts  sys  usr
boot  etc  lib   media  opt  root  sbin  srv      tmp  var
[root@server /]# cd scripts/
[root@server scripts]# ls
[root@server scripts]# vim inotify.sh

host=192.168.58.20      # 目标服务器的ip(备份服务器)
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@server scripts]# chmod +x inotify.sh 
[root@server scripts]# ls
inotify.sh

启动脚本并挂在后台
[root@server scripts]# nohup bash /scripts/inotify.sh &
[2] 140039

查看进程
[root@server scripts]# ps -ef|grep inotify
root      141852    5139  0 19:47 pts/0    00:00:00 bash /scripts/inotify.sh
root      141853  141852  0 19:47 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      141854  141852  0 19:47 pts/0    00:00:00 bash /scripts/inotify.sh
root      144190    5139  0 19:48 pts/0    00:00:00 grep --color=auto inotify

在源服务器上生成一个新文件
[root@server etc]# touch testing.txt
[root@server etc]# echo 'hell world' > testing.txt 
[root@server etc]# cat /etc/testing.txt 
hell world

设置开机自启
[root@server~ ]# chmod +x /etc/rc.d/rc.local
[root@server~ # cat /etc/rc.d/rc.local 
#!/bin/bash
# THIS FILE IS ADDED FOR COMPATIBILITY PURPOSES
#
# It is highly advisable to create own systemd services or udev rules
# to run scripts during boot instead of using this file.
#
# 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.

nohup /scripts/inotify.sh &
touch /var/lock/subsys/local

到目标服务器上去查看是否把新生成的文件自动传上去了:

[root@client etc]# ls
adjtime                  GREP_COLORS               motd.d                  sasl2
aliases                  groff                     mtab                    security
alternatives             group                     nanorc                  selinux
anacrontab               group-                    netconfig               services
dconf                    libreport                 protocols            //这里   testing.txt


转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/320227.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号