一.实验环境
两台可以通信的主机
rhel7: 192.168.1.20
rhel8: 192.168.1.10
二.scp命令
scp 本地文件 远程主机用户@远程主机ip:远程主机目录
scp 远程主机用户@远程主机ip:远程主机目录 本地文件
实验步骤:
1.在rhel7.6建立文件和目录
[root@rhel7 mnt]# touch file
[root@rhel7 mnt]# mkdir filedir
2.测试
a)把本地主机复制到远程主机
[root@rhel7 mnt]# scp file [email protected]:/mnt
file 100% 0 0.0KB/s 00:00
[root@rhel7 mnt]# scp -r filedir [email protected]:/mnt
[root@rhel7 mnt]# scp -q file [email protected]:/mnt
b) 把远程文件复制到本地
[root@rhel7 mnt]# scp [email protected]:/mnt/rhel8.0_file .
rhel8.0_file 100% 0 0.0KB/s 00:00
三.rsync命令
1.rsync和scp命令的对比
1)在rhel7中建立文件
[root@rhel7 mnt]# dd if=/dev/zero of=/mnt/file1 bs=1M count=10
##dd=截取,if=inputfile of=outputfile bs=blocksize count=块的个数
10+0 records in
10+0 records out
10485760 bytes (10 MB) copied, 0.00716192 s, 1.5 GB/s
[root@rhel7 mnt]# dd if=/dev/zero of=/mnt/file2 bs=1M count=20
20+0 records in
20+0 records out
20971520 bytes (21 MB) copied, 0.0151408 s, 1.4 GB/s
[root@rhel7 mnt]# dd if=/dev/zero of=/mnt/file3 bs=1M count=30
30+0 records in
30+0 records out
31457280 bytes (31 MB) copied, 0.132892 s, 237 MB/s
2)在两个主机之间建立免密连接,使得scp远程传输可以直接执行而不用输入密码
ssh-keygen ##生成密钥
ssh-copy-id -i /root/.ssh/id_rsa.pub [email protected] ##上锁
3)写测试脚本
vim check_scp.sh
time scp -rq /mnt/ [email protected]:/mnt
time scp -rq /mnt/ [email protected]:/mnt
time scp -rq /mnt/ [email protected]:/mnt
vim check_rsync.sh
time rsync -raCq /mnt [email protected]:/mnt
time rsync -raCq /mnt [email protected]:/mnt
time rsync -raCq /mnt [email protected]:/mnt
4)执行
- real时间是指挂钟时间,也就是命令开始执行到结束的时间。这个短时间包括其他进程所占用的时间片,和进程被阻塞时所花费的时间。
- user时间是指进程在用户模式中的CPU执行时间,其他进程花费的时间和阻塞状态的时间没有计算在内。
- sys时间是进程在内核模式中的CPU执行时间,代表进程使用系统调用所花费的CPU执行时间,这也是由进程执行而消耗的CPU执行时间。
[root@rhel7 ~]# sh check_scp.sh
real 0m1.257s
user 0m0.233s
sys 0m0.408s ##第一次系统执行时间
real 0m1.208s
user 0m0.275s
sys 0m0.387s ##第二次系统执行时间
real 0m1.181s
user 0m0.274s
sys 0m0.363s ##第三次系统执行时间
执行结果看出scp三次执行时间基本一致
[root@rhel7 ~]# sh check_rsync.sh
real 0m1.013s
user 0m0.274s
sys 0m0.027s ##第一次系统执行时间
real 0m0.315s
user 0m0.013s
sys 0m0.013s ##第二次系统执行时间
real 0m0.277s
user 0m0.011s
sys 0m0.009s ##第三次系统执行时间
以上执行效果看出rsync后两次执行时间远小于第一次
扫描二维码关注公众号,回复:
9190521 查看本文章
【scp 和 rsync 的区别】
- scp是相当于复制,黏贴,如果有的话是覆盖,比较耗时间,不智能。
- rsync是复制,如果有重复的文件,会直接跳过,而且他自己的算法优化。
- scp是把文件全部复制过去,当文件修改后还是把所有文件复制过去
- rsync 第一次是把所有文件同步过去,当文件修改后,只把修改的文件同步过去
2)rsync用法
rsync 文件 远程用户@远程主机ip:远程主机目录
rsync 远程用户@远程主机ip:远程主机目录 文件路径
rsync
-r ##复制目录
-l ##复制链接
-p ##复制权限
-t ##复制时间戳
-o ##复制拥有者
-g ##复制拥有组
-D ##复制设备文件
在rhel7
[root@rhel7 mnt]# touch file{1..5}
[root@rhel7 mnt]# chmod 777 /mnt/*
[root@rhel7 mnt]# useradd student
[root@rhel7 mnt]# chown student.student /mnt/*
[root@rhel7 mnt]# ln -s /mnt/file1 /mnt/file
在rhel8中
[root@RHEL8 mnt]# watch -n 1 ls -lR /mnt
执行命令看效果
rsync -r /mnt [email protected]:/mnt ##同步目录本身及目录中的文件
rsync -r /mnt/ [email protected]:/mnt ##同步目录中的文件
rsync -rl /mnt/ [email protected]:/mnt ##同步链接
rsync -rlp /mnt/ [email protected]:/mnt ##同步权限
rsync -rlpog /mnt/ [email protected]:/mnt ##同步用户组
rsync -rlpogt /mnt/ [email protected]:/mnt ##同步时间
rsync -rD /dev/pts [email protected]:/mnt/ ##同步设备