Rsync

rsync 简介

rsync(remote synchronize)是一个远程数据同步工具,可通过 LAN/WAN 快速同步多台主机之间的文件。也可以使用 rsync 同步本地硬盘中的不同目录。
rsync 是用于替代 rcp 的一个工具,rsync 使用所谓的 rsync算法 进行数据同步,这种算法只传送两个文件的不同部分,而不是每次都整份传送,因此速度相当快。 您可以参考 How Rsync Works A Practical Overview 进一步了解 rsync 的运作机制。
rsync 的初始作者是 Andrew Tridgell 和 Paul Mackerras,目前由 http://rsync.samba.org 维护。
rsync 支持大多数的类 Unix 系统,无论是 Linux、Solaris 还是 BSD上 都经过了良好的测试。 CentOS系统默认就安装了 rsync 软件包。 此外,在 windows 平台下也有相应的版本,如 cwrsync 和DeltaCopy 等。

rsync基本特性:

  • 可以镜像保存整个目录树和文件系统
  • 可以很容易做到保持原来文件的权限、时间、软硬链接等
  • 无须特殊权限即可安装
  • 优化的流程,文件传输效率高
  • 可以使用 rsh、ssh 方式来传输文件,当然也可以通过直接的 socket 连接
  • 支持匿名传输,以方便进行网站镜象

rsync的ssh认证协议

在使用 rsync 进行远程同步时,可以使用两种方式:远程 Shell 方式(建议使用 ssh,用户验证由 ssh 负责)和 C/S 方式(即客户连接远程 rsync 服务器,用户验证由 rsync 服务器负责)。
无论本地同步目录还是远程同步数据,首次运行时将会把全部文件拷贝一次,以后再运行时将只拷贝有变化的文件(对于新文件)或文件的变化部分(对于原有文件)。
rsync 在首次复制时没有速度优势,速度不如 tar,因此当数据量很大时您可以考虑先使用 tar 进行首次复制,然后再使用 rsync 进行数据同步。

镜像、备份和归档

实施备份的两种情况:

需保留备份历史归档:在备份时保留历史的备份归档,是为了在系统出现错误后能恢复到从前正确的状态。这可以使用完全备份和增量备份来完成。
可以使用 tar 命令保存归档文件。
为了提高备份效率,也可以使用 rsync 结合 tar 来完成。
无需保留备份历史归档:若无需从历史备份恢复到正确状态,则只备份系统最“新鲜”的状态即可。这可以简单地使用 rsync 同步来完成。此时通常称为镜像。镜像可以分为两种:
被镜像的目录在各个主机上保持相同的位置。此时一般是为了实施负载均衡而对多个主机进行同步镜像。例如:将主机 A 的 /srv/www 目录同步到主机 B 的 /srv/www 目录等。
被镜像的目录在各个主机上不保持相同的位置。例如:主机 A 和主机 B 都运行着各自的业务,同时又互为镜像备份。此时主机 A 的 /srv/www 目录同步到主机 B 的 /backups/hosta/www 目录;主机 B 的 /srv/www 目录同步到主机 A 的 /backups/hostb/www 目录等。

rsync命令

使用一个远程shell程序(如rsh、ssh)来实现将本地机器的内容拷贝到远程机。

[root@localhost hgfs]# rsync -avz /hehe root@192.168.163.137:/

## The authenticity of host '192.168.163.137 (192.168.163.137)' can't be established.

ECDSA key fingerprint is SHA256:EU3yI7YAmPdO4oalUMGPiSM3eWPZF2QulqSX0bYnp+Y.
Are you sure you want to continue connecting (yes/no/[fingerprint])? yes
Warning: Permanently added '192.168.163.137' (ECDSA) to the list of known hosts.
root@192.168.163.137's password: 
sending incremental file list
rsync: link_stat "/hehe" failed: No such file or directory (2)

sent 18 bytes  received 12 bytes  3.16 bytes/sec
total size is 0  speedup is 0.00
rsync error: some files/attrs were not transferred (see previous errors) (code 23) at main.c(1189) [sender=3.1.3]

使用一个远程shell程序(如rsh、ssh)来实现将远程机器的内容拷贝到本地机器。

[root@client ~]# rsync -avz root@192.168.163.136:/root/haha /root
root@192.168.163.136's password: 
receiving incremental file list
haha

sent 43 bytes  received 83 bytes  36.00 bytes/sec
total size is 0  speedup is 0.00
[root@client ~]# ls
anaconda-ks.cfg  haha  nohup.out  runtime

实验:

部署rsync+inotify同步/runtime目录至目标服务器的/NAME/下。
这里的NAME是指你的名字.比如你叫tom,则要把/runtime目录同步至目标服务器的/tom/下。
环境说明:
实验先配置的目标服务器,后配置的源服务器.

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

源服务器上:

安装rsync以及安装inotify-tools工具,实时触发rsync进行同步
[root@client ~]# yum -y install rsync
[root@client ~]# yum -y install inotify-tools

创建认证密码文件
[root@client ~]# echo '123456' > /etc/rsync.pass
[root@client ~]# cat /etc/rsync.pass 
123456
设置文件权限,只设置文件所有者具有读取、写入权限即可
[root@client ~]# chmod 600 /etc/rsync.pass 
创建需要被同步的目录
[root@client ~]# mkdir /runtime
手动验证命令(发现客户端的文件已经同步到服务端)
[root@client ~]# rsync -avH --port 873 --progress --delete /runtime admin@192.168.163.136::share --password-file=/etc/rsync.pass
编写脚本
[root@client ~]# mkdir /scripts
[root@client ~]# touch /scripts/inotify.sh
为脚本增加执行权限
[root@localhost ~]# chmod 755 /scripts/inotify.sh
[root@client scripts]# cat inotify.sh 
#!/bin/bash
host=192.168.163.136
src=/root/runtime        # 在源服务器上所要监控的备份目录(此处可以自定义,但是要保证存在)
des=share    # 自定义的模块名,需要与目标服务器上定义的同步名称一致
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@clie[root@client ~]# nohup bash /root/scripts/inotify.sh &
[1] 2411
[root@client ~]# nohup: ignoring input and appending output to 'nohup.out'
nt scripts]# nohup bash inotify.sh &

查看脚本进程
[root@client ~]# ps -ef |grep inotify
root        1590       1  0 Oct18 ?        00:00:00 bash inotify.sh
root        1591    1590  0 Oct18 ?        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        1592    1590  0 Oct18 ?        00:00:00 bash inotify.sh
root        2420    2366  0 02:24 pts/0    00:00:00 grep --color=auto inotify
[1]+  Done                    nohup bash /root/scripts/inotify.sh[root@client scripts]# ps -ef | grep -inotify
验证,客户端的runtime目录已同步到服务端tangjunpeng目录中
[root@server tangjunpeng]# ls
runtime

设置开机自动启动脚本
[root@client ~]# vim /etc/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.

touch /var/lock/subsys/local
nohup /bin/bash /scripts/inotify.sh &
~                                            
重启后查看进程
[root@client ~]# ps -ef|grep inotify
root         993     983  0 03:46 ?        00:00:00 /bin/bash /scripts/inotify.sh
root         996     993  0 03:46 ?        00:00:00 /usr/bin/inotifywait -mrq --timefmt %Y%m%d %H:%M --format %T %w%f%e -e modify,delete,create,attrib /root/runtime
root         997     993  0 03:46 ?        00:00:00 /bin/bash /scripts/inotify.sh
root        1483    1456  0 03:46 pts/1    00:00:00 grep --color=auto inotify
[root@client ~]# cd runtime/
[root@client runtime]# ls
aaa  aaaaaaaaaaaaa  ab  ac  bbb  bd
[root@client runtime]# rm -f aaa
aaa            aaaaaaaaaaaaa  
[root@client runtime]# rm -f aaa
aaa            aaaaaaaaaaaaa  
[root@client runtime]# rm -f aaaaaaaaaaaaa  aaa
[root@client runtime]# ls
ab  ac  bbb  bd
源服务器上验证
[root@server runtime]# ls
aaa  aaaaaaaaaaaaa  ab  ac  bbb  bd
[root@server runtime]# ls
ab  ac  bbb  bd

源服务器上:

安装.源服务器上需安装daemon
[root@server ~]#yum -y install rsync
[root@server ~]# yum -y install rsync-daemon
设置rsyncd.conf配置文件
[root@server ~]# 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    # 用户认证配置文件,里面保存用户名称和密码,必须手动创建这个文件

[share]     # 自定义同步名称
path = /tangjunpeng/          # rsync服务端数据存放路径,客户端的数据将同步至此目录
comment = sync runtime 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 = 192.168.163.137   # 允许进行数据同步的客户端IP地址,可以设置多个,用英文状态下逗号隔开
hosts deny = 192.168.1.1      # 禁止数据同步的客户端IP地址,可以设置多个,用英文状态下逗号隔开
EOF
创建用户admin认证文件.设置文件权限,只设置文件所有者具有读取、写入权限即可
[root@server ~]echo 'admin:123456' >/etc/rsync.pass
[root@server ~]chmod 600 /etc/rsync.pass
启动rsync服务并设置开机自启动
[root@server ~]# systemctl start rsyncd
[root@server ~]# systemctl enable rsyncd
Created symlink from /etc/systemd/system/multi-user.target.wants/rsyncd.service to /usr/lib/systemd/system/rsyncd.service.
[root@server ~]# 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@server ~]# mkdir tangjunpeng

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值