回顾:
对称加密和非对称加密
使用ssh远程管理
修改ssh服务端口,不允许root远程登录
- 密钥对
- 选择一个账户zhangsan。su zhangsan
- 生成公钥和私钥 ssh-keygen
- ~/.ssh/
Id_rsa 私钥
Id_rsa.pub 公钥
将公钥中字符串追加到对方主机的对应用户的家目录的~/.ssh/authorized_key
4.ssh-copy-id -p9999 用户@ip
Linux系统下数据同步服务RSYNC
一、RSYNC概述
1.什么是rsync
2.rsync特点
3.rsync与scp的区别
yum -y install rsync
二、RSYNC的使用
1.基本语法
三、RSYNC课程扩展
四、练习:
[root@y ~]#mkdir -p /app/studentweb/src/main/java/co/goho/yuanyu.studentweb/
监听+同步(自动化的同步)
将y上监听到的内容同步到x服务器上
在y虚拟机上写监听脚本,
[root@y ~]# vim inotifytest.sh
修改权限:
[root@y ~]# chmod +x inotifytest.sh
执行脚本:
[root@y ~]# ./inotifytest.sh
在另一个y终端创建目录
[root@y ~]# touch /app/studentweb/lalalala.txt
[root@y ~]# touch /app/studentweb/xxx.txt
您在 /var/spool/mail/root 中有新邮件
原来执行脚本的终端就会监听到:
[root@y ~]# ./inotifytest.sh
sending incremental file list
./
lalalala.txt
sent 626 bytes received 44 bytes 1,340.00 bytes/sec
total size is 0 speedup is 0.00
sending incremental file list
sent 580 bytes received 18 bytes 398.67 bytes/sec
total size is 0 speedup is 0.00
sending incremental file list
./
xxx.txt
sent 647 bytes received 44 bytes 1,382.00 bytes/sec
total size is 0 speedup is 0.00
sending incremental file list
sent 601 bytes received 18 bytes 412.67 bytes/sec
total size is 0 speedup is 0.00
而且会把在y上监听到的内容同步到x服务器上
[root@x ~]# ls /tmp/
1.txt hahaha src xxx.txt 俯卧撑1
2.txt lalalala.txt woshiqinshihuang.txt 俯卧撑 做俯卧撑
y服务器:
[root@y ~]# vim /etc/rsyncd.conf
[root@y ~]# systemctl restart rsyncd
在另一台上就找不到了
[root@x ~]# rsync -av root@192.168.2.8::app /tmp/
@ERROR: Unknown module 'app'
rsync error: error starting client-server protocol (code 5) at main.c(1656) [Receiver=3.1.2]
换掉配置文件[ ]里的内容,找到时候也要改
[root@x ~]# rsync -av root@192.168.2.8::efg /tmp/
receiving incremental file list
sent 26 bytes received 371 bytes 794.00 bytes/sec
total size is 0 speedup is 0.00
[root@y ~]# vim /etc/rsyncd.conf
[root@y ~]# vim /etc/rsync.secrets
您在 /var/spool/mail/root 中有新邮件
[root@y ~]# #以上是编辑rsync密码
[root@y ~]# cat /etc/rsync.secrets
tom:tom
Jerry:jerry
[root@y ~]# chmod 600 /etc/rsync.secrets
您在 /var/spool/mail/root 中有新邮件
[root@y ~]# systemctl restart rsyncd
在x虚拟机上操作:
[root@x ~]# rsync -av tom@192.168.2.8::efg /tmp/
Password:
receiving incremental file list
sent 26 bytes received 371 bytes 7.15 bytes/sec
total size is 0 speedup is 0.00
[root@x ~]#
[root@y ~]##安装监听工具
[root@y ~]# yum -y install inotify-tools
[root@y ~]# inotifywait -mr /app/
在另一个终端创建目录
[root@y studentweb]# touch /app/studentweb/woshiqinshihuang.txt
这个终端就会被监听到
[root@y ~]# cd /app/studentweb/
[root@y studentweb]# vim /etc/rsyncd.conf
[root@y studentweb]# systemctl restart rsyncd
[root@y studentweb]# #在y主机提供了一个针对app/下的项目rsync服务
[root@y studentweb]# #下次直接找服务
在另一台x上就可以找到
[root@x ~]# rsync -a root@192.168.2.8::
app
[root@x ~]#
将y服务器上的app传到 /tmp/
[root@x ~]# rsync -av root@192.168.2.8::app /tmp/
receiving incremental file list
sent 26 bytes received 371 bytes 37.81 bytes/sec
total size is 0 speedup is 0.00
[root@x ~]# ls -l /tmp/
总用量 307200
-rw-r--r--. 1 root root 314572800 7月 18 11:04 lajiwenjian
drwxr-xr-x. 3 root root 18 7月 18 11:28 src
drwx------. 3 root root 17 7月 11 16:47 systemd-private-7cb5d2e1e883490fbbe7a00fb8e02dcc-chronyd.service-wqYt5p
drwx------. 3 root root 17 7月 18 08:59 systemd-private-8eee7dca29cf4b1293598aa8fe8d1204-chronyd.service-d8mczV
drwx------. 3 root root 17 7月 17 21:52 systemd-private-c4f4a366efb3426cb039559604189a10-chronyd.service-i0Crts
drwx------. 3 root root 17 7月 17 17:53 systemd-private-f23ce88bb27c46af9c16c48053b6c562-chronyd.service-wZ6TpV
drwx------. 2 root root 6 7月 12 22:02 vmware-root
[root@x ~]# tree /tmp/src/
/tmp/src/
└── main
└── java
└── co
└── goho
└── yuanyu.studentweb
├── File0.java
├── File1.java
├── File2.java
├── File3.java
├── File4.java
├── File5.java
├── File6.java
├── File7.java
├── File8.java
└── File9.java
5 directories, 10 files
[root@x ~]#
[root@y ~]# mkdir folder
[root@y ~]# mkdir folder/f{1..3}
[root@y ~]# touch folder/f1/file{0..4}
[root@y ~]# tree folder/
folder/
├── f1
│ ├── file0
│ ├── file1
│ ├── file2
│ ├── file3
│ ├── file4
│ └── file5
├── f2
│ ├── file0
│ ├── file1
│ ├── file2
│ ├── file3
│ ├── file4
│ └── file5
└── f3
3 directories, 12 files
[root@y ~]# rsync -av folder/ /opt/
sending incremental file list
./
f1/
f1/file0
f1/file1
f1/file2
f1/file3
f1/file4
f1/file5
f2/
f2/file0
f2/file1
f2/file2
f2/file3
f2/file4
f2/file5
f3/
sent 833 bytes received 263 bytes 2,192.00 bytes/sec
total size is 26 speedup is 0.02
[root@y ~]# ls /opt/
f1 f2 f3 folder
[root@y ~]# rm -rf folder/f1/file5
[root@y ~]# rm -rf folder/f2/file5
[root@y ~]# touch folder/f1/file5
[root@y ~]# tree folder/
folder/
├── f1
│ ├── file0
│ ├── file1
│ ├── file2
│ ├── file3
│ ├── file4
│ └── file5
├── f2
│ ├── file0
│ ├── file1
│ ├── file2
│ ├── file3
│ └── file4
└── f3
3 directories, 11 files
[root@y ~]# rsync -av folder/f1/ folder/f2/
sending incremental file list
./
file0
file1
file2
file3
file4
file5
sent 399 bytes received 133 bytes 1,064.00 bytes/sec
total size is 13 speedup is 0.02
[root@y ~]# tree folder/
folder/
├── f1
│ ├── file0
│ ├── file1
│ ├── file2
│ ├── file3
│ ├── file4
│ └── file5
├── f2
│ ├── file0
│ ├── file1
│ ├── file2
│ ├── file3
│ ├── file4
│ └── file5
└── f3
3 directories, 12 files
您在 /var/spool/mail/root 中有新邮件
[root@y ~]# vim folder/f1/file1
[root@y ~]# vim folder/f1/file5
[root@y ~]# cat folder/f1/file5
大家好,haha!
[root@y ~]# rsync -av --delete folder/f1/ folder/f2/
sending incremental file list
./
file1
file5
sent 269 bytes received 57 bytes 652.00 bytes/sec
total size is 33 speedup is 0.10
[root@y ~]# cat folder/f2/file5
大家好,haha!
[root@y ~]# #文件的修改也会被rsync同步
[root@y ~]# touch folder/f1/file0 -m -d "2024-7-14 00:00"
您在 /var/spool/mail/root 中有新邮件
[root@y ~]# touch folder/f1/file5 -m -d "2024-7-14 00:00"
[root@y ~]# rsync -av --delete folder/f1/ folder/f2/
sending incremental file list
file0
file5
sent 242 bytes received 54 bytes 592.00 bytes/sec
total size is 33 speedup is 0.11
[root@y ~]# rsync -av folder/f1/ folder/f2/
sending incremental file list
sent 136 bytes received 12 bytes 296.00 bytes/sec
total size is 33 speedup is 0.22
[root@y ~]# ls -l folder/f1/file0
-rw-rw-r--. 1 root root 0 7月 14 00:00 folder/f1/file0
您在 /var/spool/mail/root 中有新邮件
[root@y ~]# ls -l folder/f1/file5
-rw-r--r--. 1 root root 20 7月 14 00:00 folder/f1/file5
[root@y ~]# ls -l folder/f2/file5
-rw-r--r--. 1 root root 20 7月 14 00:00 folder/f2/file5
[root@y ~]# ls -l folder/f2/file0
-rw-rw-r--. 1 root root 0 7月 14 00:00 folder/f2/file0
[root@y ~]# #同步文件内容的修改,文件的删除,以及文件的属性的修改
[root@y ~]# #向x主机 /tmp目录同步数据
您在 /var/spool/mail/root 中有新邮件
[root@x ~]# touch /tmp/eee
设置免密:
[root@y ~]#ssh-keygen
[root@y ~]#ssh-copy-id root@192.168.2.10
[root@y ~]# rsync -av root@192.168.2.10:/tmp/eee /tmp/
receiving incremental file list
eee
sent 43 bytes received 85 bytes 85.33 bytes/sec
total size is 0 speedup is 0.00
您在 /var/spool/mail/root 中有新邮件
[root@y ~]# ls /tmp/
eee
ks-script-8KMvxT
lajiwenjian
oooo
systemd-private-34cf21bb97ce4258b2ad6f261c29f842-chronyd.service-Jpxj7b
systemd-private-72d7a7be1f6746c08706d47c36815319-chronyd.service-MIA8k8
systemd-private-ee8aa827bec9402e9a9014258571b7a5-chronyd.service-p2nKEp
vmware-root
yum.log
yum_save_tx.2024-07-17.11-51.GxecEy.yumtx