rsync

rsync简介

rsync是linux系统下的数据镜像备份工具。使用快速增量备份工具remote Syns 可以远程同步,支持本地复制,或者与其他SSH、rsync主机同步。

rsync特性

rsync支持的特性:

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

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 打通通道

//这种方式默认是省略了 -e ssh 的,与下面等价:
rsync -avz /SRC -e ssh root@172.16.12.129:/DEST 
    -a  //文件宿主变化,时间戳不变
    -z  //压缩数据传输
 
//当遇到要修改端口的时候,我们可以:
rsync -avz /SRC -e "ssh -p2222" root@172.16.12.129:/DEST  
//修改了ssh 协议的端口,默认是22

rsync常用选项

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

rsync+inotify

案例:

环境

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

需求:

  • .部署rsync+inotify同步/runtime目录至目标服务器的/yh/下

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

//关闭防火墙
[root@localhost ~]# systemctl stop firewalld
[root@localhost ~]# setenforce 0

//安装rsync服务端软件
[root@localhost ~]# dnf -y install rsync
CentOS Stream 8 - AppStream                        3.5 kB/s | 4.4 kB     00:01    
CentOS Stream 8 - AppStream                        376 kB/s | 9.6 MB     00:25    
CentOS Stream 8 - BaseOS                           3.8 kB/s | 3.9 kB     00:01    
CentOS Stream 8 - BaseOS                           166 kB/s | 5.7 MB     00:35    
CentOS Stream 8 - Extras                           498  B/s | 3.0 kB     00:06    
软件包 rsync-3.1.3-11.el8.x86_64 已安装。
依赖关系解决。
===================================================================================
 软件包          架构             版本                      仓库              大小
===================================================================================
升级:
 rsync           x86_64           3.1.3-12.el8              baseos           405 k

事务概要
===================================================================================
升级  1 软件包

总下载:405 k
下载软件包:
rsync-3.1.3-12.el8.x86_64.rpm                       54 kB/s | 405 kB     00:07    
-----------------------------------------------------------------------------------
总计                                                50 kB/s | 405 kB     00:08     
运行事务检查
事务检查成功。
运行事务测试
事务测试成功。
运行事务
  准备中  :                                                                    1/1 
  升级    : rsync-3.1.3-12.el8.x86_64                                          1/2 
  清理    : rsync-3.1.3-11.el8.x86_64                                          2/2 
  运行脚本: rsync-3.1.3-11.el8.x86_64                                          2/2 
  验证    : rsync-3.1.3-12.el8.x86_64                                          1/2 
  验证    : rsync-3.1.3-11.el8.x86_64                                          2/2 
Installed products updated.

已升级:
  rsync-3.1.3-12.el8.x86_64                                                        

完毕!


//设置rsync.conf配置文件

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

[hahaha]     # 自定义同步名称
path = /yh/          # 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        # 执行数据同步的用户名,可以设置多个,用英文状态下逗号隔开

//创建用户认证文件
[root@localhost ~]# vim /etc/rsync.pass
[root@localhost ~]# cat /etc/rsync.pass
admin:123456

//设置文件权限
[root@localhost ~]# chmod 600 /etc/rsync*
[root@localhost ~]# ll /etc/rsync*
-rw-------. 1 root root 375 67 07:01 /etc/rsyncd.conf
-rw-------. 1 root root  13 67 07:04 /etc/rsync.pass


//启动rsync服务并设置开机自启动
[root@localhost ~]# vim /etc/sysconfig/rsyncd
[root@localhost ~]# cat /etc/sysconfig/rsyncd
OPTIONS=""

[root@localhost ~]# vim /usr/lib/systemd/system/rsyncd.service
[Unit]
Description=fast remote file copy program daemon

[Service]
User=root
Group=root
EnvironmentFile=/etc/sysconfig/rsyncd
ExecStart=/usr/bin/rsync --daemon --config=/etc/rsyncd.conf --no-detach
ExecReload=/bin/kill -HUP $MAINPID
KillMode=process
Restart=on-failure
RestartSec=30s

[Install]
WantedBy=multi-user.target

[root@localhost ~]# systemctl daemon-reload   //刷新

[root@localhost ~]# systemctl start rsyncd   //启动
[root@localhost ~]# systemctl enable rsyncd  //开机自启
Created symlink /etc/systemd/system/multi-user.target.wants/rsyncd.service → /usr/lib/systemd/system/rsyncd.service.
[root@localhost ~]# ss -antl    //已有873端口
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:111            0.0.0.0:*                
LISTEN   0        32         192.168.122.1:53             0.0.0.0:*                
LISTEN   0        128              0.0.0.0:22             0.0.0.0:*                
LISTEN   0        5              127.0.0.1:631            0.0.0.0:*                
LISTEN   0        100            127.0.0.1:25             0.0.0.0:*                
LISTEN   0        5                   [::]:873               [::]:*                
LISTEN   0        128                 [::]:111               [::]:*                
LISTEN   0        128                 [::]:22                [::]:*                
LISTEN   0        5                  [::1]:631               [::]:*                
LISTEN   0        100                [::1]:25                [::]:*                



在源服务器上的操作:

//关闭防火墙
[root@localhost ~]# systemctl stop firewalld
[root@localhost ~]# setenforce 0

//安装rsync服务端软件
[root@localhost ~]# dnf -y install rsync
CentOS Stream 8 - AppStream                        3.5 kB/s | 4.4 kB     00:01    
CentOS Stream 8 - AppStream                        376 kB/s | 9.6 MB     00:25    
CentOS Stream 8 - BaseOS                           3.8 kB/s | 3.9 kB     00:01    
CentOS Stream 8 - BaseOS                           166 kB/s | 5.7 MB     00:35    
CentOS Stream 8 - Extras                           498  B/s | 3.0 kB     00:06    
软件包 rsync-3.1.3-11.el8.x86_64 已安装。
依赖关系解决。
===================================================================================
 软件包          架构             版本                      仓库              大小
===================================================================================
升级:
 rsync           x86_64           3.1.3-12.el8              baseos           405 k

事务概要
===================================================================================
升级  1 软件包

总下载:405 k
下载软件包:
rsync-3.1.3-12.el8.x86_64.rpm                       54 kB/s | 405 kB     00:07    
-----------------------------------------------------------------------------------
总计                                                50 kB/s | 405 kB     00:08     
运行事务检查
事务检查成功。
运行事务测试
事务测试成功。
运行事务
  准备中  :                                                                    1/1 
  升级    : rsync-3.1.3-12.el8.x86_64                                          1/2 
  清理    : rsync-3.1.3-11.el8.x86_64                                          2/2 
  运行脚本: rsync-3.1.3-11.el8.x86_64                                          2/2 
  验证    : rsync-3.1.3-12.el8.x86_64                                          1/2 
  验证    : rsync-3.1.3-11.el8.x86_64                                          2/2 
Installed products updated.

已升级:
  rsync-3.1.3-12.el8.x86_64                                                        

完毕!


[root@localhost ~]# yum -y install epel-release


//创建认证密码文件
[root@localhost ~]# vim /etc/rsync.pass
[root@localhost ~]# cat /etc/rsync.pass
123456

//设置文件权限,只设置文件所有者具有读取、写入权限即可
[root@localhost ~]# chmod 600 /etc/rsync.pass 
[root@localhost ~]# ll /etc/rsync.pass
-rw-------. 1 root root 7 67 09:07 /etc/rsync.pass

//在源服务器上创建runtime目录,然后再源服务器运行以下命令
[root@localhost ~]# mkdir runtime
[root@localhost ~]# ls
anaconda-ks.cfg   runtime
[root@localhost ~]# cd runtime
[root@localhost runtime]# touch qq ww eerr

[root@localhost runtime]# rsync -avH --port 873 --progress --delete /root/runtime/ admin@192.168.147.66::hahahaha --password-file=/etc/rsync.pass
sending incremental file list
deleting runtime/ww
deleting runtime/qq
deleting runtime/
./
eerr
              0 100%    0.00kB/s    0:00:00 (xfr#1, to-chk=2/4)
qq
              0 100%    0.00kB/s    0:00:00 (xfr#2, to-chk=1/4)
ww
              0 100%    0.00kB/s    0:00:00 (xfr#3, to-chk=0/4)

sent 225 bytes  received 124 bytes  33.24 bytes/sec
total size is 0  speedup is 0.00

//在目标服务器上查看,在/yh目录下有runtime目录,说明数据同步成功
[root@localhost /]# ls /yh
eerr  qq  ww

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

//编写同步脚本,让脚本自动去检测我们制定的目录下 \
//文件发生的变化,然后再执行rsync的命令把它同步到我们的服务器端
[root@localhost ~]# mkdir /scripts
[root@localhost ~]# touch /scripts/inotify.sh
[root@localhost ~]# chmod 755 /scripts/inotify.sh
[root@localhost ~]# ll /scripts/inotify.sh
-rwxr-xr-x. 1 root root 0 67 10:11 /scripts/inotify.sh

[root@localhost ~]# vim /scripts/inotify.sh
[root@localhost ~]# cat /scripts/inotify.sh
host=192.168.147.66     # 目标服务器的ip(备份服务器)
src=/runtime       # 在源服务器上所要监控的备份目录(此处可以自定义,但是要保证存在)
des=hahahaha     # 自定义的模块名,需要与目标服务器上定义的同步名称一致
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 ~]# nohup /bin/bash /scripts/inotify.sh &
[1] 439213
[root@localhost ~]# nohup: 忽略输入并把输出追加到'nohup.out'

[root@localhost ~]# ps -ef|grep inotify
root      439213  271418  0 10:18 pts/0    00:00:00 /bin/bash /scripts/inotify.sh
root      439215  439213  0 10:18 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 /runtime
root      439216  439213  0 10:18 pts/0    00:00:00 /bin/bash /scripts/inotify.sh
root      440942  271418  0 10:19 pts/0    00:00:00 grep --color=auto inotify


[root@localhost ~]# ls /runtime/
qq  ww
[root@localhost ~]# touch /runtime/oooooo
[root@localhost ~]# tail /tmp/rsync.log   //查看inotify生成的日志
20210607 10:21 /runtime/ooooooCREATE was rsynced

//设置脚本开机自启
[root@localhost ~]# chmod +x /etc/rc.d/rc.local
[root@localhost ~]# ll /etc/rc.d/rc.local
-rwxr-xr-x. 1 root root 474 121 2020 /etc/rc.d/rc.local
[root@localhost ~]# echo 'nohup /bin/bash /scripts/inotify.sh' >> /etc/rc.d/rc.local
[root@localhost ~]#  tail /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

//到目标服务器上去查看是否把新生成的文件自动传上去了
[root@localhost /]# ls yh
eerr  qq  runtime  ww
[root@localhost /]# ls /yh/runtime
oooooo  qq  ww
//可以看到已将源服务器的/runtime目录整个同步到了目标服务器,并且新增的oooooo文件也自动同步了

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值