rsync(远程同步文件)

1、rsync介绍

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

Rsync是一款开源的、快速的、多功能的、可实现全量及增量的本地或远程数据同步备份的优秀工具。并且可以不进行改变原有数据的属性信息,实现数据的备份迁移特性。Rsync软件适用于unix/linux/windows等多种操作系统平台。
Rsync是一个快速和非常通用的文件复制工具。它能本地复制,远程复制,或者远程守护进程方式复制。它提供了大量的参数来控制其行为的各个方面,并且允许非常灵活的方式来实现文件的传输复制。它以其delta-transfer算法闻名。
rsync监听端口:873
rsync运行模式:C/S

1.1 rsync特性

  • 可以镜像保存整个目录树和文件系统
  • 可以很容易做到保持原来文件的权限、时间、软硬链接等等
  • 传输效率高,使用同步算法,只比较变化的
  • 支持匿名传输,方便网站镜像;也可以做验证,加强安全
  • 支持匿名传输,以方便进行网站镜像

1.2 rsync命令

rsync命令常用的分为三种:
1、将本机的xx文件同步到另一台主机的xx位置

[root@original ~]# rsync -avz /root/wjm  root@192.168.164.132:/root

2、将远程主机的xx文件同步到本机的xx位置

[root@original ~]# rsync -avz   root@192.168.164.132:/root/111 .

3、将本机的xx文件拷贝到本机的xx目录下,同时也可以改名字

[root@original ~]# rsync -a wjm/  www   
//拷贝本机的wjm目录 改名为www

1.3 rsync常用的选项

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

2、部署rsync+inotify

Inotify是一种强大的、细粒度的、异步的文件系统事件监控机制,linux内核从2.6.13起,加入了Inotify支持,通过Inotify可以监控文件系统中添加、删除,修改、移动等各种细微事件,利用这个内核接口,第三方软件就可以监控文件系统下文件的各种变化情况,而inotify-tools就是这样的一个第三方软件。
在前面有讲到,rsync可以实现触发式的文件同步,但是通过crontab守护进程方式进行触发,同步的数据和实际数据会有差异,而inotify可以监控文件系统的各种变化,当文件有任何变动时,就触发rsync同步,这样刚好解决了同步数据的实时性问题。

服务器类型IP地址应用系统
源数据库192.168.164.133rsync、inotify-tools、脚本Redhat 8
目标数据库192.168.164.132rsyncRedhat 8

2.1关闭防火墙和selinux、并且安装rsync

这一步步骤是两个主机都需要操作的

[root@localhost ~]# systemctl stop firewalld.service 
[root@localhost ~]# setenforce 0
[root@yuam-133 ~]# vim /etc/selinux/config 
......
SELINUX=disabled

//安装rsync服务端软件
[root@localhost ~]# yum -y install rsync

3、目标服务器配置

一、添加rsync配置文件

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

[etc_from_client]     # 自定义同步名称
path = /tmp/          # 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 = wjm        # 执行数据同步的用户名,可以设置多个,用英文状态下逗号隔开
# hosts allow = 172.16.12.128   # 允许进行数据同步的客户端IP地址,可以设置多个,用英文状态下逗号隔开
# hosts deny = 192.168.1.1      # 禁止数据同步的客户端IP地址,可以设置多个,用英文状态下逗号隔开
最后两个本次实验用不到所有就不做设置

二、创建用户认证文件

[root@mubiao-132 ~]# echo 'wjm:wjmwjm123' > /etc/rsync.pass   //注意:这个wjm用户系统中是没有,只是一个虚拟用户,用户同步使用的
[root@mubiao-132 ~]# cat /etc/rsync.pass 
wjm:wjmwjm123

//修改文件的权限
[root@mubiao-132 ~]# chmod 660 /etc/rsync*
[root@mubiao-132 ~]# ll /etc/rsync*
-rw-rw----. 1 root root 423 1011 18:04 /etc/rsyncd.conf
-rw-rw----. 1 root root  14 1011 18:11 /etc/rsync.pass

三、设置开机自启
开机自启需要依赖一个软件包所以要先下载一个

[root@mubiao-132 ~]# yum -y install rsync-daemon

[root@mubiao-132 ~]# systemctl enable --now rsyncd
Created symlink /etc/systemd/system/multi-user.target.wants/rsyncd.service → /usr/lib/systemd/system/rsyncd.service.

//端口号873就是这个rsync服务
[root@mubiao-132 ~]# ss -antl
State   Recv-Q  Send-Q     Local Address:Port     Peer Address:Port  
LISTEN  0       128              0.0.0.0:22            0.0.0.0:*     
LISTEN  0       128            127.0.0.1:9000          0.0.0.0:*     
LISTEN  0       5                0.0.0.0:873           0.0.0.0:*     
LISTEN  0       128                 [::]:22               [::]:*     
LISTEN  0       5                   [::]:873              [::]:*     


4、源服务器配置

需要下载一个eple源

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

一、创建密码认证文件并且修改文件的权限
这个密码文件内容就是目标服务器中配置的密码

[root@yuam-133 ~]# echo 'wjmwjm123' > /etc/rsync.pass
[root@yuam-133 ~]# cat /etc/rsync.pass 
wjmwjm123

//修改密码认证文件的权限
[root@yuam-133 ~]# chmod 660 /etc/rsync.pass 
[root@yuam-133 ~]# ll /etc/rsync.pass 
-rw-rw----. 1 root root 10 1011 18:25 /etc/rsync.pass

二、测试
在源服务器上创建测试目录,然后在源服务器运行以下命令
目标配置文件中的监控地址是源服务器上的/tmp/

[root@yuam-133 ~]# mkdir etc/test -p
[root@yuam-133 ~]# tree /root
/root
├── anaconda-ks.cfg
└── etc
    └── test

//源服务器上执行同步的命令测试
[root@yuam-133 ~]#  rsync -avH --port 873 --progress --delete /root/etc/ wjm@192.168.164.132::etc_from_client --password-file=/etc/rsync.pass
# etc_from_client 目标服务器配置文件中的
# --password-file 源服务器上密码文件的位置
[root@yuan-133 test]# rsync -avH --port 873 --progress --delete /root/etc/ wjm@192.168.164.132::etc_from_client --password-file=/etc/rsync.pass
sending incremental file list
rsync: failed to set times on "." (in etc_from_client): Permission denied (13)
rsync: readlink_stat(".X11-unix" (in etc_from_client)) failed: Permission denied (13)
rsync: readlink_stat(".ICE-unix" (in etc_from_client)) failed: Permission denied (13)
rsync: readlink_stat(".font-unix" (in etc_from_client)) failed: Permission denied (13)
rsync: readlink_stat("vmware-root_976-2966103375" (in etc_from_client)) failed: Permission denied (13)
rsync: readlink_stat("vmware-root_984-2999526209" (in etc_from_client)) failed: Permission denied (13)
rsync: readlink_stat("pear" (in etc_from_client)) failed: Permission denied (13)
rsync: readlink_stat("vmware-root_974-2965579094" (in etc_from_client)) failed: Permission denied (13)
rsync: readlink_stat("zabbix_server_preprocessing.sock" (in etc_from_client)) failed: Permission denied (13)
rsync: readlink_stat("zabbix_server_lld.sock" (in etc_from_client)) failed: Permission denied (13)
rsync: readlink_stat("zabbix_server_alerter.sock" (in etc_from_client)) failed: Permission denied (13)
rsync: readlink_stat("zabbix_server_availability.sock" (in etc_from_client)) failed: Permission denied (13)
rsync: delete_file: rmdir(.XIM-unix) failed: Permission denied (13)
rsync: delete_file: rmdir(.Test-unix) failed: Permission denied (13)
rsync: delete_file: unlink(zabbix_server.log) failed: Permission denied (13)
rsync: delete_file: unlink(zabbix_agentd.log) failed: Permission denied (13)
IO error encountered -- skipping file deletion
./
test/

sent 77 bytes  received 1,580 bytes  3,314.00 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]


//目标服务器的/tmp/
[root@mubiao-132 tmp]# ll
总用量 84
drwxr-xr-x. 3 root   root      18 927 05:15 pear
drwxr-xr-x. 2 root   root       6 1011 18:51 test
......
......

三、安装inotify-tools工具,实时触发rsync进行同步

安装工具包

[root@yuan-133 test]# yum -y install inotify-tools
[root@yuan-133 ~]# mkdir /scripts
[root@yuan-133 ~]# cd /scripts/
[root@yuan-133 scripts]# vim  inotify.sh
[root@yuan-133 scripts]# chmod +x inotify.sh 

//执行脚本,此时会卡着因为没有触发 手动触发一下就会自动同步
[root@yuan-133 scripts]# ./inotify.sh 

//手动触发
[root@yuan-133 ~]# touch /etc/123

[root@yuan-133 scripts]# ./inotify.sh 
sending incremental file list
rsync: symlink "etc/favicon.png" (in etc_from_client) -> "/rsyncd-munged//usr/share/icons/hicolor/16x16/apps/fedora-logo-icon.png" failed: Permission denied (13)
rsync: symlink "etc/grub2.cfg" (in etc_from_client) -> "/rsyncd-munged/../boot/grub2/grub.cfg" failed: Permission denied (13)
rsync: symlink "etc/init.d" (in etc_from_client) -> "/rsyncd-munged/rc.d/init.d" failed: Permission denied (13)
rsync: symlink "etc/localtime" (in etc_from_client) -> "/rsyncd-munged/../usr/share/zoneinfo/Asia/Shanghai" failed: Permission denied (13)
......
......


#此时同步的是etc下的所有文件有很多所有弹出出了很多同步信息

//查看同步日志
[root@yuan-133 scripts]# tail /tmp/rsync.log 
20211011 19:15 /etc/123CREATE was rsynced
20211011 19:15 /etc/123ATTRIB was rsynced


此时去目标服务器查看有没有同步成功

[root@mubiao-132 etc]# cd /tmp/
[root@mubiao-132 tmp]# ll
总用量 96
drwxr-xr-x. 95 root   root    8192 1011 19:15 etc
drwxr-xr-x.  3 root   root      18 927 05:15 pear
......
......
同步成功

四、让脚本在后台开机自启运行
给系统的/etc/rc.d/rc.local文件赋予执行的权限,并配置脚本开机自启。这个文件是系统启动后最后一个执行的文件。这样脚本在系统启动的时候就可以启动了

[root@yuan-133 scripts]# chmod +x /etc/rc.d/rc.local 
[root@yuan-133 rc.d]# ll
总用量 4
drwxr-xr-x. 2 root root  37 81 14:05 init.d
drwxr-xr-x. 2 root root   6 910 2018 rc0.d
drwxr-xr-x. 2 root root   6 910 2018 rc1.d
drwxr-xr-x. 2 root root   6 910 2018 rc2.d
drwxr-xr-x. 2 root root   6 910 2018 rc3.d
drwxr-xr-x. 2 root root   6 910 2018 rc4.d
drwxr-xr-x. 2 root root   6 910 2018 rc5.d
drwxr-xr-x. 2 root root   6 910 2018 rc6.d
-rwxr-xr-x. 1 root root 474 324 2020 rc.local

//编辑配置文件,让脚本在系统启动的时候开机自启
[root@yuan-133 rc.d]# vim rc.local
nohup /scripts/inotify.sh &    //nohup是让脚本可以脱离终端时可以继续运行。

//重启后查看rsync是否在运行
[root@yuan-133 rc.d]# reboot    //重启
[root@yuan-133 ~]# ps -ef | grep inotify
root        1064       1  0 20:41 ?        00:00:00 /bin/sh /scripts/inotify.sh
root        1069    1064  0 20:41 ?        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        1070    1064  0 20:41 ?        00:00:00 /bin/sh /scripts/inotify.sh
root        2774    2373  0 20:42 pts/0    00:00:00 grep --color=auto inotify

5、作业

1.部署rsync+inotify同步/runtime目录至目标服务器的/NAME/下。这里的NAME是指你的名字,比如你叫tom,则要把/runtime目录同步至目标服务器的/tom/下。

更改目标服务器的配置文件

[root@localhost wjm]# cat /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 = /wjm/    //改成备份文件要放的位置          
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 = wjm       

改源服务器的脚本

host=192.168.164.132      # 目标服务器的ip(备份服务器)
src=/runtime      // 改成要监控的文件  # 在源服务器上所要监控的备份目录(此处可以自定义,
但是要保证存在)
des=etc_from_client     # 自定义的模块名,需要与目标服务器上定义的同>步名称一致
password=/etc/rsync.pass        # 执行数据同步的密码文件
user=wjm          # 执行数据同步的用户名
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


重启测试

//源数据库重启,查看脚本运行状态
[root@original runtime]# reboot
[root@original runtime]# ps -ef|grep inotify
root        1025       1  0 21:55 ?        00:00:00 /bin/sh /scripts/inotify.sh
root        1033    1025  0 21:55 ?        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        1034    1025  0 21:55 ?        00:00:00 /bin/sh /scripts/inotify.sh
root       18970   18347  0 21:59 pts/4    00:00:00 vim inotify.sh
root       54915   34660  0 22:06 pts/2    00:00:00 grep --color=auto inotify

//源数据库创建文件
[root@original runtime]# touch test22.03
[root@original runtime]# ll
总用量 0
-rw-r--r--. 1 root root 0 1011 22:03 test22.03

//目标数据库查看文件是否同步
[root@target runtime]# pwd
/wjm/runtime
[root@target runtime]# ll
总用量 0
-rw-r--r-- 1 root root 0 1011 22:03 test22.03

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值