linux系统下数据同步服务RSYNC
1.rsync概述
sync同步:刷新文件系统缓存,强制将修改过的数据块写入磁盘,并且更新超级快
asycn异步:将数据先放到缓冲区,再周期性(一般是30s)的去同步磁盘
rsync远程同步:==remote synchronous==
2.rsync特点
可以镜像保存整个目录树和文件系统
可以保留原有的权限(permission,mode),owner,group,时间(修改时间,modify time),软硬链接,文件acl,文件属性信息等
传输效率高,使用同步算法,只比较变化的(增量备份)
支持匿名传输,方便网站镜像;也可以做验证,加强安全
3.rsync与scp的区别
两者都可以实现远程同步,但是想对比而言,rsync能力更强
(1)支持增量备份
(2)数据同步时保持原有属性不变
[root@y ~]# yum list | grep rsync
[root@y ~]# yum -y install rsync
选项说明
-v 详细模式输出
-a 归档模式,递归的方式传输文件,并保持文件的属性
-r 递归拷贝目录
-l 保留软链接
-p 保留原有权限
-t 保留原有时间(修改)
-g 保留属组权限
-o 保留属主权限
-D 等于--devices --specials 表示⽀持b,c,s,p类型的⽂件
-R 保留相对路径
-H 保留硬链接
-A 保留ACL策略
-e 指定要执⾏的远程shell命令,ssh更改端⼝常⽤选项
-E 保留可执行权限
-X 保留扩展属性信息 a属性
4.本地文件同步、远程文件同步
[root@y ~]# mkdir folder
[root@y ~]# mkdir folder/f{1..3}
[root@y ~]# tree folder/
[root@y ~]# touch folder/f1/{0..4}
[root@y ~]# tree folder/
– rsync [选项...] 本地目录1 本地目录2 //同步整个文件夹
– rsync [选项...] 本地目录1/ 本地目录2 //只同步目录下的数据
[root@y ~]# rsync -av folder/ /opt/ #只同步目录下面的数据
[root@y ~]# ls /opt/
f1 f2 f3 skel test test0
[root@y ~]# rsync -av folder /opt/ #会直接同步整个目录
[root@y ~]# ls /opt/
f1 f2 f3 folder skel test test0
[root@y ~]# rsync -avR folder/ /opt/ #保留目录的相对路径,也会携带目录
[root@y ~]# rsync -av folder/f1/ folder/f2/
[root@y ~]# touch folder/f1/file5
[root@y ~]# tree folder/
folder/
├── f1
│ ├── 0
│ ├── 1
│ ├── 2
│ ├── 3
│ ├── 4
│ └── file5
├── f2
│ ├── 0
│ ├── 1
│ ├── 2
│ ├── 3
│ └── 4
└── f3
[root@y ~]# rsync -av folder/f1/ folder/f2/
[root@y ~]# tree folder/
folder/
├── f1
│ ├── 0
│ ├── 1
│ ├── 2
│ ├── 3
│ ├── 4
│ └── file5
├── f2
│ ├── 0
│ ├── 1
│ ├── 2
│ ├── 3
│ ├── 4
│ └── file5
└── f3
[root@y ~]# rm -rf folder/f1/0
[root@y ~]# tree folder/
folder/
├── f1
│ ├── 1
│ ├── 2
│ ├── 3
│ ├── 4
│ └── file5
├── f2
│ ├── 0
│ ├── 1
│ ├── 2
│ ├── 3
│ ├── 4
│ └── file5
└── f3
[root@y ~]# rsync -av --delete folder/f1/ folder/f2/ #使源和目标目录的数据保持一致
[root@y ~]# tree folder/
folder/
├── f1
│ ├── 1
│ ├── 2
│ ├── 3
│ ├── 4
│ └── file5
├── f2
│ ├── 1
│ ├── 2
│ ├── 3
│ ├── 4
│ └── file5
└── f3
修改文件
[root@y ~]# vim folder/f1/1
[root@y ~]# rsync -av folder/f1/ folder/f2/
sending incremental file list
./
1
[root@y ~]# cat folder/f2/1
ya si de lai
修改时间
[root@y ~]# touch folder/f1/1 -m -d "2024-07-14 00:00"
[root@y ~]# rsync -av folder/f1/ folder/f2/
修改权限
[root@y ~]# chmod g+w folder/f1/1
[root@y ~]# ls -l folder/f1/1
-rw-rw-r--. 1 root root 13 7月 14 00:00 folder/f1/1
[root@y ~]# rsync -av folder/f1/ folder/f2/
[root@y ~]# ls -l folder/f2/1
-rw-rw-r--. 1 root root 13 7月 14 00:00 folder/f2/1
远程同步
两台主机都需安装rsync
– rsync [选项...] 本地目录/ user@host:远程目录 //将本地文件同步到远程主机
– rsync [选项...] user@host:远程目录 本地目录/ //将远程文件同步到本地目录
[root@y ~]# rsync -av folder/ root@192.168.1.20:/tmp
[root@x ~]# ls /tmp/
f1
f2
f3
从远程主机拉取数据
[root@x ~]# dd if=/dev/zero of=/tmp/lajiwenjian bs=300M count=1
[root@y ~]# rsync -av root@root192.168.1.20:/tmp/lajiwenjian /tmp/
[root@y ~]# ls /tmp/
lajiwenjian
免密传输
[root@y ~]# ssh-keygen
[root@y ~]# ssh-copy-id root@192.168.1.20
5.rsync作为系统服务(关闭防火墙,关闭selinux)
[root@y ~]# systemctl start rsyncd
[root@y ~]# systemctl status rsyncd
[root@y ~]# vim /etc/rsyncd.conf
# configuration example:
# uid = nobody #用户编号
# gid = nobody #所属组编号
# use chroot = yes #是否禁锢目录
# max connections = 4 #最大连接数
# pid file = /var/run/rsyncd.pid #进程文件,每个服务都有一个PID编号,其来源就是PID文件
# exclude = lost+found/ #传输文件时,忽略lost+found/目录
# transfer logging = yes #传输日志是否需要写入日志文件,yes代表写入
# timeout = 900 #传输超时时间,默认15分钟
# ignore nonreadable = yes #是否忽略不可读文件
# dont compress = *.gz *.tgz *.zip *.z *.Z *.rpm *.deb *.bz2 #以下格式文件不压缩
# [ftp]
# path = /home/ftp
# comment = ftp export area
[root@y ~]# mkdir -p /app/studentweb/src/main/java/co/goho/yuanyu.studentweb/
[root@y ~]# touch /app/studentweb/src/main/java/co/goho/yuanyu.studentweb/File{0..9}.java
[root@y studentweb]# vim /etc/rsyncd.conf
[app]
path=/app/studentweb/
log file=/var/log/rsync.log
另一台主机
[root@x ~]# rsync -a root@192.168.1.11::
app
[root@x ~]# rsync -av root@192.168.1.11::app /tmp/
[root@x ~]# ls -l /tmp/
drwxr-xr-x. 3 root root 18 7月 18 11:28 src
自动同步(使用计划任务)
[root@y ~]# which rsync
/usr/bin/rsync
[root@y ~]# crontab -e
*/1 * * * * /usr/bin/rsync -av /app/studentweb/ root@192.168.1.20:/tmp/
另一台主机
[root@x ~]# ls /tmp/
src
rsync加密
[root@y ~]# vim /etc/rsyncd.conf
[app]
path=/app/studentweb/
log file=/var/log/rsync.log
auth users=user0,user1
secrets file=/etc/rsync.secrets
[root@y ~]# vim /etc/rsync.secrets //设置密码(用户名:密码)
user0:11
user1:22
[root@y ~]# chmod 600 /etc/rsync.secrets //更改密码文件权限
[root@y ~]# systemctl restart rsyncd
另一台主机
[root@x ~]# rsync -av user0@192.168.1.11::app /tmp/
Password: 11
6.rsync集合inotify工具实现代码实时同步
安装监听工具
[root@y ~]# yum -y install inotify-tools
– -m:持续监控 (捕获一个事件后不退出)
– -r:递归监控、包括子目录及文件
– -q:减少屏幕输出信息
– -e:指定监视的 modify、move、create、delete、attrib 等事件类别,不写-e全部监控
modify: ⽂件被修改
delete: ⽂件被删除
create: ⽂件被创建
attrib: ⽂件属性被修改
move: ⽂件被移动
[root@y ~]# inotifywait -mr /app/
[root@y ~]# touch /app/s.txt
/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 s.txt
/app/ OPEN s.txt
/app/ ATTRIB s.txt
/app/ CLOSE_WRITE,CLOSE s.txt
[root@y ~]# vim inotifytest
#!/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.1.20:/tmp/
done
[root@y ~]# mv inotifytest inotifytest.sh
[root@y ~]# chmod 700 inotifytest.sh
[root@y ~]# touch /app/studentweb/ww.txt
另一台主机
[root@x ~]# ls /tmp/
ww.txt
[root@y ~]# nohup ./inotifytest.sh & //后台运行
[root@y ~]# jobs //查看
[1]+ 运行中 nohup ./inotifytest.sh &
[root@y ~]# kill % //结束
[root@y ~]# jobs
[1]+ 已终止 nohup ./inotifytest.sh