Linux系统下数据同步服务rsync
1、什么是rsync
(1)rsync的好姐妹
1)sync 同步:刷新⽂件系统缓存,强制将修改过的数据块写⼊磁盘,并且更新超级块。
2)async 异步:将数据先放到缓冲区,再周期性(⼀般是30s)的去同步到磁盘。
3)rsync 远程同步:==remote synchronous==
(2)数据同步过程
1)sync数据同步 => 保存⽂件(⽬标)=> 强制把缓存中的数据写⼊磁盘(⽴即保存),实时性要求⽐较⾼的场景
2)asyn数据异步 => 保存⽂件(⽬标)=> 将数据先放到缓冲区,再周期性(⼀般是30s)的去同步到磁盘,适合⼤批量数据同步的场景
2、rsync特点
1)可以镜像保存整个⽬录树和⽂件系统
2)可以保留原有的权限(permission,mode),owner,group,时间(修改时间,modify time),软硬链接,⽂件acl,⽂件属性(attributes)信息等
3)传输==效率⾼==,使⽤同步算法,只⽐较变化的(增量备份)file1.txt file2.txt file3.txt(A服务器)
rsync实现数据同步 => 只同步file3.txt => 增量备份
file1.txt file2.txt(B服务器)
4)⽀持匿名传输,⽅便⽹站镜像;也可以做验证,加强安全
3、rsync与scp的区别
两者都可以实现远程同步,但是相对⽐⽽⾔,rsync能⼒更强
① ⽀持增量备份
② 数据同步时保持⽂件的原有属性
rsync :linux系统下数据同步服务
[root@13 ~]# yum -y install rsync [root@13 ~]# which rsync /usr/bin/rsync
在家目录中创建一些文件,将文件同步到opt下
如果源目录不以 / 结尾,整个目录同步包含目录文件,带 / 结尾,只同步目录下文件
[root@13 ~]# mkdir folder [root@13 ~]# mkdir folder/f{1..3} [root@13 ~]# tree folder/ folder/ ├── f1 ├── f2 └── f3 3 directories, 0 files [root@13 ~]# touch folder/f1/file{0..4} [root@13 ~]# tree folder/ folder/ ├── f1 │ ├── file0 │ ├── file1 │ ├── file2 │ ├── file3 │ └── file4 ├── f2 └── f3 3 directories, 5 files [root@13 ~]#
将folder 目录下的文件传到opt
[root@13 ~]# rsync -av folder/ /opt/ sending incremental file list ./ f1/ f1/file0 f1/file1 f1/file2 f1/file3 f1/file4 f2/ f3/ sent 397 bytes received 130 bytes 1,054.00 bytes/sec total size is 0 speedup is 0.00 [root@13 ~]#
实现
[root@13 ~]# rsync -avR folder/ /opt/ sending incremental file list folder/ folder/f1/ folder/f1/file0 folder/f1/file1 folder/f1/file2 folder/f1/file3 folder/f1/file4 folder/f2/ folder/f2/file0 folder/f2/file1 folder/f2/file2 folder/f2/file3 folder/f2/file4 folder/f3/ sent 686 bytes received 226 bytes 1,824.00 bytes/sec total size is 0 speedup is 0.00 [root@13 ~]# tree /opt/ /opt/ ├── f1 │ ├── file0 │ ├── file1 │ ├── file2 │ ├── file3 │ └── file4 ├── f2 ├── f3 ├── folder │ ├── f1 │ │ ├── file0 │ │ ├── file1 │ │ ├── file2 │ │ ├── file3 │ │ └── file4 │ ├── f2 │ │ ├── file0 │ │ ├── file1 │ │ ├── file2 │ │ ├── file3 │ │ └── file4 │ └── f3 └── test ├── a │ ├── abc.txt │ ├── def.txt │ └── ghi.txt ├── abc.txt ├── def.txt └── ghi.txt 9 directories, 21 files [root@13 ~]# rsync -av folder/f1/ folder/f2/ sending incremental file list ./ file0 file1 file2 file3 file4 sent 314 bytes received 114 bytes 856.00 bytes/sec total size is 0 speedup is 0.00 [root@13 ~]#
文件修改也会被rsync 同步
[root@13 ~]# vim folder/f1/file1 [root@13 ~]# rsync -av --delete folder/f1/ folder/f2/ sending incremental file list ./ file1 sent 182 bytes received 38 bytes 440.00 bytes/sec total size is 12 speedup is 0.05 [root@13 ~]#
rsync可以同步文件内容修改,文件的删除,以及文件的属性修改
向14主机 /tmp目录同步数据
[root@13 ~]# rsync -av folder/ root@192.168.2.14:/tmp/ root@192.168.2.14's password: sending incremental file list ./ f1/ f1/file0 f1/file1 f1/file2 f1/file3 f1/file4 f2/ f2/file0 f2/file1 f2/file2 f2/file3 f2/file4 f3/ sent 703 bytes received 225 bytes 68.74 bytes/sec total size is 24 speedup is 0.03 [root@13 ~]#
要实现远程同步,要求对面主机都安装rsync
[root@13 ~]# rsync -av folder/ root@192.168.2.14:/tmp/ root@192.168.2.14's password: sending incremental file list ./ f1/ f1/file0 f1/file1 f1/file2 f1/file3 f1/file4 f2/ f2/file0 f2/file1 f2/file2 f2/file3 f2/file4 f3/ sent 703 bytes received 225 bytes 68.74 bytes/sec total size is 24 speedup is 0.03 [root@13 ~]#
在14主机上下载300M的文件,用13去同步
[root@14 ~]# dd if=/dev/zero of=/tmp/ljwj bs=300M count=1 记录了1+0 的读入 记录了1+0 的写出 314572800字节(315 MB)已复制,3.92535 秒,80.1 MB/秒 [root@14 ~]# ls -lh /tmp/ 总用量 442M -rw-r--r--. 1 root root 9.5M 7月 15 14:00 etc -rw-r--r--. 1 root root 300M 7月 18 11:04 ljwj [root@14 ~]# 13主机: [root@13 ~]# rsync -av root@192.168.2.14:/tmp/ljwj /tmp/ root@192.168.2.14's password: receiving incremental file list ljwj sent 43 bytes received 314,649,683 bytes 17,979,984.34 bytes/sec total size is 314,572,800 speedup is 1.00 [root@13 ~]#
免密:
[root@13 ~]# ssh-keygen Generating public/private rsa key pair. Enter file in which to save the key (/root/.ssh/id_rsa): Enter passphrase (empty for no passphrase): Enter same passphrase again: Your identification has been saved in /root/.ssh/id_rsa. Your public key has been saved in /root/.ssh/id_rsa.pub. The key fingerprint is: SHA256:WZyBPqwUeTsnhxJJKBarEaRGbV0z0YfYrpHBfrtgDcU root@13 The key's randomart image is: +---[RSA 2048]----+ |ooo..o+B*.o | |o.o+..=+=E + | |ooo. .*=o= | |.o o=O=o | |. . oS*. | | .+ o | | . . . | | . | | | +----[SHA256]-----+ [root@13 ~]# ssh-copy-id root@192.168.2.14 /usr/bin/ssh-copy-id: INFO: Source of key(s) to be installed: "/root/.ssh/id_rsa.pub" /usr/bin/ssh-copy-id: INFO: attempting to log in with the new key(s), to filter out any that are already installed /usr/bin/ssh-copy-id: INFO: 1 key(s) remain to be installed -- if you are prompted now it is to install the new keys root@192.168.2.14's password: Number of key(s) added: 1 Now try logging into the machine, with: "ssh 'root@192.168.2.14'" and check to make sure that only the key(s) you wanted were added. [root@13 ~]# rsync -av root@192.168.2.14:/tmp/ljwj /tmp/ //不用输密码直接登录 receiving incremental file list sent 20 bytes received 44 bytes 5.57 bytes/sec total size is 314,572,800 speedup is 4,915,200.00 [root@13 ~]#
查看服务:
[root@13 ~]# systemctl status rsyncd ● rsyncd.service - fast remote file copy program daemon Loaded: loaded (/usr/lib/systemd/system/rsyncd.service; disabled; vendor preset: disabled) Active: inactive (dead) [root@13 ~]# systemctl start rsyncd [root@13 ~]# systemctl status rsyncd ● rsyncd.service - fast remote file copy program daemon Loaded: loaded (/usr/lib/systemd/system/rsyncd.service; disabled; vendor preset: disabled) Active: active (running) since 四 2024-07-18 11:18:49 CST; 3s ago Main PID: 2417 (rsync) Tasks: 1 CGroup: /system.slice/rsyncd.service └─2417 /usr/bin/rsync --daemon --no-detach 7月 18 11:18:49 13 systemd[1]: Started fast remote file copy program daemon. 7月 18 11:18:49 13 systemd[1]: Starting fast remote file copy program d..... 7月 18 11:18:49 13 rsyncd[2417]: rsyncd version 3.1.2 starting, listeni...73 Hint: Some lines were ellipsized, use -l to show in full. [root@13 ~]# netstat -lntup | grep rsync tcp 0 0 0.0.0.0:873 0.0.0.0:* LISTEN 2417/rsync tcp6 0 0 :::873 :::* LISTEN 2417/rsync [root@13 ~]#
查找服务配置文件:
[root@13 ~]# find / -name "rsync*conf" /etc/rsyncd.conf [root@13 ~]# cat /etc/rsyncd.conf # /etc/rsyncd: configuration file for rsync daemon mode # See rsyncd.conf man page for more options. # configuration example: # uid = nobody # gid = nobody # use chroot = yes # max connections = 4 # pid file = /var/run/rsyncd.pid # exclude = lost+found/ # transfer logging = yes # timeout = 900 # ignore nonreadable = yes # dont compress = *.gz *.tgz *.zip *.z *.Z *.rpm *.deb *.bz2 # [ftp] # path = /home/ftp # comment = ftp export area [root@13 ~]#
创建Java 目录:
[root@13 ~]# mkdir -p /app/studentweb/src/main/java/co/goho/yuanyu.studentwed/ [root@13 ~]# tree /app/ /app/ └── studentweb └── src └── main └── java └── co └── goho └── yuanyu.studentwed 7 directories, 0 files [root@13 ~]# touch /app/studentweb/src/main/java/co/goho/yuanyu.studentwed/Yulan{0..9}.java [root@13 ~]# tree /app/ /app/ └── studentweb └── src └── main └── java └── co └── goho └── yuanyu.studentwed ├── Yulan0.java ├── Yulan1.java ├── Yulan2.java ├── Yulan3.java ├── Yulan4.java ├── Yulan5.java ├── Yulan6.java ├── Yulan7.java ├── Yulan8.java └── Yulan9.java 7 directories, 10 files [root@13 ~]#
写入Java 文件:在13主机提供了一个针对 /app 的服务
修改配置文件、重启服务
[root@13 ~]# cd /app/studentweb/ [root@13 studentweb]# pwd /app/studentweb [root@13 studentweb]# vim /etc/rsyncd.conf [app] path=/app/studentweb/ log file=/var/log/rsync.log [root@13 studentweb]# systemctl restart rsyncd [root@13 studentweb]#
实现 在14主机上拉取rsync 服务中的项目:
[root@14 ~]# rsync -a root@192.168.2.13:: //检测配置的app 服务 app [root@14 ~]# [root@14 ~]# rsync -av root@192.168.2.13::app /tmp/
每30秒推送一次,编辑计划任务:
[root@13 studentweb]# crontab -e crontab: installing new crontab */1 * * * * /usr/bin/rsync -av /app/studentweb/ root@192.168.2.14:/tmp/
为rsyncd服务添加密码:
编辑配置文件 /vim /etc/rsyncd.conf 添加两个属性
[root@13 ~]# vim /etc/rsyncd.conf [app] path=/app/studentweb/ log file=/var/log/rsync.log auth users=111,222 secrets file=/etc/rsync.secrets [root@13 ~]# vim /etc/rsync.secrets [root@13 ~]# cat /etc/rsync.secrets 111:123 222:123 [root@13 ~]#
赋予权限,重启:
[root@13 ~]# chmod 600 /etc/rsync.secrets [root@13 ~]# ls -l /etc/rsync.secrets -rw-r--r--. 1 root root 19 7月 18 15:12 /etc/rsync.secrets [root@13 ~]# systemctl restart rsyncd
安装监听工具:(监听)
[root@13 ~]# yum -y install inotify-tools 已加载插件:fastestmirror Loading mirror speeds from cached hostfile * base: mirrors.aliyun.com * epel: mirrors.huaweicloud.com * extras: mirrors.aliyun.com * updates: mirrors.aliyun.com 正在解决依赖关系 --> 正在检查事务 ---> 软件包 inotify-tools.x86_64.0.3.14-9.el7 将被 安装 --> 解决依赖关系完成 依赖关系解决 ============================================================================= Package 架构 版本 源 大小 ============================================================================= 正在安装: inotify-tools x86_64 3.14-9.el7 epel 51 k 事务概要 ============================================================================= 安装 1 软件包 总下载量:51 k 安装大小:111 k Downloading packages: inotify-tools-3.14-9.el7.x86_64.rpm | 51 kB 00:06 Running transaction check Running transaction test Transaction test succeeded Running transaction 正在安装 : inotify-tools-3.14-9.el7.x86_64 1/1 验证中 : inotify-tools-3.14-9.el7.x86_64 1/1 已安装: inotify-tools.x86_64 0:3.14-9.el7 完毕! [root@13 ~]# inotifywait //等待 [root@13 ~]# inotifywatch //看守
监听app 目录:
[root@13 ~]# inotifywait -mr /app/ Setting up watches. Beware: since -r was given, this may take a while! Watches established. /app/ OPEN,ISDIR /app/ CLOSE_NOWRITE,CLOSE,ISDIR /app/ OPEN,ISDIR /app/ CLOSE_NOWRITE,CLOSE,ISDIR /app/ OPEN,ISDIR /app/ CLOSE_NOWRITE,CLOSE,ISDIR /app/ OPEN,ISDIR /app/ CLOSE_NOWRITE,CLOSE,ISDIR /app/ CREATE lll /app/ OPEN lll /app/ ATTRIB lll /app/ CLOSE_WRITE,CLOSE lll /app/ OPEN,ISDIR /app/ CLOSE_NOWRITE,CLOSE,ISDIR /app/ OPEN,ISDIR /app/ CLOSE_NOWRITE,CLOSE,ISDIR /app/ OPEN,ISDIR /app/ CLOSE_NOWRITE,CLOSE,ISDIR /app/studentweb/ CREATE woshiheyulan /app/studentweb/ OPEN woshiheyulan /app/studentweb/ ATTRIB woshiheyulan /app/studentweb/ CLOSE_WRITE,CLOSE woshiheyulan
同时在另一个窗口:
[root@13 ~]# touch /app/lll [root@13 ~]# ls /app/ lll studentweb [root@13 ~]# touch /app/studentweb/woshiheyulan [root@13 ~]#
自动检测:
[root@13 ~]# vim inotify.sh [root@13 ~]# cat inotify.sh #!/bin/bash /usr/bin/inotifywait -mrq -e modify,delete,create,attrib,move /app/studentweb |while read events do rsync -av /app/studentweb/ root@192.168.2.14:/tmp/ done [root@13 ~]# ls 7.17history.txt mongodb-linux-x86_64-rhel70-3.6.3 anaconda-ks.cfg mongodb-linux-x86_64-rhel70-3.6.3.tgz apache-tomcat-10.1.25.tar.gz perl5 apache-tomcat-9.0.91.tar.gz Python-3.12.4 a.txt Python-3.12.4.tgz b.txt redis-7.2.5.tar.gz code soft def.txt src echo.txt Start.java folder stu.jar inotify.sh stus jdk-17.0.9_linux-x64_bin.tar.gz todolist.jar minute.txt vuehtml000 [root@13 ~]# chmod 700 inotify.sh [root@13 ~]# ./inotify.sh sending incremental file list ./ woshiheyulan wuliao src/ src/main/ src/main/java/ src/main/java/co/ src/main/java/co/goho/ src/main/java/co/goho/yuanyu.studentwed/ src/main/java/co/goho/yuanyu.studentwed/Yulan0.java src/main/java/co/goho/yuanyu.studentwed/Yulan1.java src/main/java/co/goho/yuanyu.studentwed/Yulan2.java src/main/java/co/goho/yuanyu.studentwed/Yulan3.java src/main/java/co/goho/yuanyu.studentwed/Yulan4.java src/main/java/co/goho/yuanyu.studentwed/Yulan5.java src/main/java/co/goho/yuanyu.studentwed/Yulan6.java src/main/java/co/goho/yuanyu.studentwed/Yulan7.java src/main/java/co/goho/yuanyu.studentwed/Yulan8.java src/main/java/co/goho/yuanyu.studentwed/Yulan9.java sent 930 bytes received 279 bytes 115.14 bytes/sec total size is 0 speedup is 0.00 sending incremental file list sent 429 bytes received 18 bytes 42.57 bytes/sec total size is 0 speedup is 0.00 sending incremental file list ./ 天天好心情 sent 504 bytes received 44 bytes 52.19 bytes/sec total size is 0 speedup is 0.00 sending incremental file list sent 458 bytes received 18 bytes 45.33 bytes/sec total size is 0 speedup is 0.00
同时在另一个窗口创建文件:
[root@13 ~]# touch /app/studentweb/woshiheyulan [root@13 ~]# touch /app/studentweb/wuliao [root@13 ~]# touch /app/studentweb/天天好心情 [root@13 ~]#
在14 主机的 /tmp 目录下查看
[root@14 ~]# rm -rf /tmp/* [root@14 ~]# ls /tmp/ src woshiheyulan wuliao [root@14 ~]# ls /tmp/ src woshiheyulan wuliao 天天好心情 [root@14 ~]#
在同一个窗口实现自动同步:(在后台执行)
[root@13 ~]# nohup ./inotify.sh & [1] 19427 [root@13 ~]# nohup: 忽略输入并把输出追加到"nohup.out" [root@13 ~]# touch /app/studentweb/mm [root@13 ~]#
14主机:
[root@14 ~]# ls /tmp/ mm src woshiheyulan wuliao 天天好心情 [root@14 ~]#
配置自动检测同步:
主机13:
[root@13 ~]# mkdir -p /aaa/bbb/ccc/ddd.eee
[root@13 ~]# tree /aaa/
/aaa/
└── bbb
└── ccc
└── ddd.eee
3 directories, 0 files
[root@13 ~]# cd /aaa/bbb/
[root@13 bbb]# pwd
/aaa/bbb
[root@13 bbb]# vim /etc/rsyncd.conf
[root@13 bbb]# systemctl restart rsyncd
[root@13 bbb]# cd /root
[root@13 ~]# vim inotify.sh
[root@13 ~]# chmod +x inotify.sh
[root@13 ~]# nohup ./inotify.sh &
[2] 22460
[root@13 ~]# nohup: 忽略输入并把输出追加到"nohup.out"
[root@13 ~]# touch /aaa/bbb/我是玉兰
[root@13 ~]# touch /aaa/bbb/我是玉兰1
[root@13 ~]# touch /aaa/bbb/我是玉兰2
[root@13 ~]#
主机14:
[root@14 ~]# mkdir /aaaa
[root@14 ~]# ls /
aaaa boot dvd home lib64 mnt proc run srv tmp usr
bin dev etc lib media opt root sbin sys tom var
[root@14 ~]#
[root@14 ~]# ls /aaaa/
ccc 我是玉兰
[root@14 ~]# ls /aaaa/
ccc 我是玉兰
[root@14 ~]# ls /aaaa/
ccc 我是玉兰 我是玉兰1 我是玉兰2
[root@14 ~]#