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@loaclhost ~]# yum -y install rsync
[root@loaclhost ~]# which rsync
/usr/bin/rsync
在家目录中创建一些文件,将文件同步到opt下
如果源目录不以 / 结尾,整个目录同步包含目录文件,带 / 结尾,只同步目录下文件
[root@loaclhost ~]# mkdir folder
[root@loaclhost ~]# mkdir folder/f{1..3}
[root@loaclhost ~]# tree folder/
3 directories, 0 files
[root@loaclhost ~]# touch folder/f1/file{0..4}
[root@loaclhost ~]# tree folder/
folder/
├── f1
│ ├── file0
│ ├── file1
│ ├── file2
│ ├── file3
│ └── file4
├── f2
└── f3
3 directories, 5 files
[root@loaclhost ~]#
将folder 目录下的文件传到opt
[root@loaclhost ~]# 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 loaclhost0 bytes 1,054.00 bytes/sec
total size is 0 speedup is 0.00
[root@loaclhost ~]#
实现
[root@loaclhost ~]# 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@loaclhost ~]# tree /opt/
[root@loaclhost ~]# rsync -av folder/f1/ folder/f2/
sending incremental file list
./
file0
file1
file2
file3
file4
sent 3loaclhost bytes received 1loaclhost bytes 856.00 bytes/sec
total size is 0 speedup is 0.00
[root@loaclhost ~]#
文件修改也会被rsync 同步
[root@loaclhost ~]# vim folder/f1/file1
[root@loaclhost ~]# 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@loaclhost ~]#
rsync可以同步文件内容修改,文件的删除,以及文件的属性修改
向loaclhost主机 /tmp目录同步数据
[root@loaclhost ~]# rsync -av folder/ root@192.168.1.20:/tmp/
root@192.168.1.20'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@loaclhost ~]#
要实现远程同步,要求对面主机都安装rsync
[root@loaclhost ~]# rsync -av folder/ root@192.168.1.20:/tmp/
root@192.168.1.20'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@loaclhost ~]#
在loaclhost主机上下载300M的文件,用loaclhost去同步
[root@loaclhost ~]# dd if=/dev/zero of=/tmp/ljwj bs=300M count=1
记录了1+0 的读入
记录了1+0 的写出
3loaclhost572800字节(315 MB)已复制,3.92535 秒,80.1 MB/秒
[root@loaclhost ~]# 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@loaclhost ~]#
loaclhost主机:
[root@loaclhost ~]# rsync -av root@192.168.1.20:/tmp/ljwj /tmp/
root@192.168.1.20's password:
receiving incremental file list
ljwj
sent 43 bytes received 314,649,683 bytes 17,979,984.34 bytes/sec
total size is 3loaclhost,572,800 speedup is 1.00
[root@loaclhost ~]#
免密:
[root@loaclhost ~]# ssh-keygen
[root@loaclhost ~]# ssh-copy-id root@192.168.1.20
/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.1.20's password:
Number of key(s) added: 1
Now try logging into the machine, with: "ssh 'root@192.168.1.20'"
and check to make sure that only the key(s) you wanted were added.
[root@loaclhost ~]# rsync -av root@192.168.1.20:/tmp/ljwj /tmp/ //不用输密码直接登录
receiving incremental file list
sent 20 bytes received 44 bytes 5.57 bytes/sec
total size is 3loaclhost,572,800 speedup is 4,915,200.00
[root@loaclhost ~]#
查看服务:
[root@loaclhost ~]# 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@loaclhost ~]# systemctl start rsyncd
[root@loaclhost ~]# 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 loaclhost systemd[1]: Started fast remote file copy program daemon.
7月 18 11:18:49 loaclhost systemd[1]: Starting fast remote file copy program d.....
7月 18 11:18:49 loaclhost rsyncd[2417]: rsyncd version 3.1.2 starting, listeni...73
Hint: Some lines were ellipsized, use -l to show in full.
[root@loaclhost ~]# 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@loaclhost ~]#
查找服务配置文件:
[root@loaclhost ~]# find / -name "rsync*conf"
/etc/rsyncd.conf
[root@loaclhost ~]# 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@loaclhost ~]#
创建Java 目录:
[root@loaclhost ~]# mkdir -p /app/studentweb/src/main/java/co/goho/yuanyu.studentwed/
[root@loaclhost ~]# tree /app/
/app/
└── studentweb
└── src
└── main
└── java
└── co
└── goho
└── yuanyu.studentwed
7 directories, 0 files
[root@loaclhost ~]# touch /app/studentweb/src/main/java/co/goho/yuanyu.studentwed/Yulan{0..9}.java
[root@loaclhost ~]# 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@loaclhost ~]#
写入Java 文件:在loaclhost主机提供了一个针对 /app 的服务
修改配置文件、重启服务
[root@loaclhost ~]# cd /app/studentweb/
[root@loaclhost studentweb]# pwd
/app/studentweb
[root@loaclhost studentweb]# vim /etc/rsyncd.conf
[app]
path=/app/studentweb/
log file=/var/log/rsync.log
[root@loaclhost studentweb]# systemctl restart rsyncd
[root@loaclhost studentweb]#
实现 在loaclhost主机上拉取rsync 服务中的项目:
[root@loaclhost ~]# rsync -a root@192.168.1.20:: //检测配置的app 服务
app
[root@loaclhost ~]#
[root@loaclhost ~]# rsync -av root@192.168.2.loaclhost::app /tmp/
每30秒推送一次,编辑计划任务:
[root@loaclhost studentweb]# crontab -e
crontab: installing new crontab
*/1 * * * * /usr/bin/rsync -av /app/studentweb/ root@192.168.1.20:/tmp/
为rsyncd服务添加密码:
编辑配置文件 /vim /etc/rsyncd.conf 添加两个属性
[root@loaclhost ~]# vim /etc/rsyncd.conf
[app]
path=/app/studentweb/
log file=/var/log/rsync.log
auth users=111,222
secrets file=/etc/rsync.secrets
[root@loaclhost ~]# vim /etc/rsync.secrets
[root@loaclhost ~]# cat /etc/rsync.secrets
111:123
222:123
[root@loaclhost ~]#
赋予权限,重启:
[root@loaclhost ~]# chmod 600 /etc/rsync.secrets
[root@loaclhost ~]# ls -l /etc/rsync.secrets
-rw-r--r--. 1 root root 19 7月 18 15:12 /etc/rsync.secrets
[root@loaclhost ~]# systemctl restart rsyncd
安装监听工具:(监听):
[root@loaclhost ~]# yum -y install inotify-tools
[root@loaclhost ~]# inotifywait //等待
[root@loaclhost ~]# inotifywatch //看守
监听ap 目录:
[root@loaclhost ~]# 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@loaclhost ~]# touch /app/lll
[root@loaclhost ~]# ls /app/
lll studentweb
[root@loaclhost ~]# touch /app/studentweb/woshiheyulan
[root@loaclhost ~]#
自动检测:
[root@loaclhost ~]# vim inotify.sh
[root@loaclhost ~]# 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.loaclhost:/tmp/
done
[root@loaclhost ~]# 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@loaclhost ~]# chmod 700 inotify.sh
[root@loaclhost ~]# ./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.loaclhost 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
./
happy every day
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@loaclhost ~]# touch /app/studentweb/woshiheyulan
[root@loaclhost ~]# touch /app/studentweb/wuliao
[root@loaclhost ~]# touch /app/studentweb/happy every day
[root@loaclhost ~]#
在loaclhost 主机的 /tmp 目录下查看
[root@loaclhost ~]# rm -rf /tmp/*
[root@loaclhost ~]# ls /tmp/
src woshiheyulan wuliao
[root@loaclhost ~]# ls /tmp/
src woshiheyulan wuliao happy every day
[root@loaclhost ~]#
在同一个窗口实现自动同步:(在后台执行)
[root@loaclhost ~]# nohup ./inotify.sh &
[1] 19427
[root@loaclhost ~]# nohup: 忽略输入并把输出追加到"nohup.out"
[root@loaclhost ~]# touch /app/studentweb/mm
[root@loaclhost ~]#
loaclhost主机:
[root@loaclhost ~]# ls /tmp/
mm src woshiheyulan wuliao 天天好心情
[root@loaclhost ~]#
配置自动检测同步:
主机loaclhost:
[root@loaclhost ~]# mkdir -p /aaa/bbb/ccc/ddd.eee
[root@loaclhost ~]# tree /aaa/
/aaa/
└── bbb
└── ccc
└── ddd.eee
3 directories, 0 files
[root@loaclhost ~]# cd /aaa/bbb/
[root@loaclhost bbb]# pwd
/aaa/bbb
[root@loaclhost bbb]# vim /etc/rsyncd.conf
[root@loaclhost bbb]# systemctl restart rsyncd
[root@loaclhost bbb]# cd /root
[root@loaclhost ~]# vim inotify.sh
[root@loaclhost ~]# chmod +x inotify.sh
[root@loaclhost ~]# nohup ./inotify.sh &
[2] 22460
[root@loaclhost ~]# nohup: 忽略输入并把输出追加到"nohup.out"
[root@loaclhost ~]# touch /aaa/bbb/快乐
[root@loaclhost ~]# touch /aaa/bbb/开心
[root@loaclhost ~]# touch /aaa/bbb/积极
[root@loaclhost ~]#
主机loaclhost:
[root@loaclhost ~]# mkdir /aaaa
[root@loaclhost ~]# ls /
aaaa boot dvd home lib64 mnt proc run srv tmp usr
bin dev etc lib media opt root sbin sys tom var
[root@loaclhost ~]#
[root@loaclhost ~]# ls /aaaa/
ccc 快乐
[root@loaclhost ~]# ls /aaaa/
ccc 快乐
[root@loaclhost ~]# ls /aaaa/
ccc 快乐 开心 积极
[root@loaclhost ~]#