Linux系统中的文件传输优化

1、实验环境
2、scp命令
3、rsync和scp命令的对比
4、rsync用法
5、文档的归档压缩

1、实验环境

需要2台主机并且保证这两台主机是可以通信的
rhel7: 192.168.1.10
rhel8:192.168.1.11
两台虚拟机应急完成通信,在rhel7_node1中连接rhel8_node1

2、scp命令

scp 本地文件 远程主机用户@远程主机ip:远程主机目录
scp 远程主机用户@远程主机ip:远程主机目录 本地文件

实验步骤:
1、在rhel7中建立实验素材
touch westos
mkdir westosdir
2、测试
1)把本地文件复制到远程主机
scp westos [email protected]:/mnt
scp -r westosdir [email protected]:/mnt (-r表示复制目录)
scp -q westos [email protected]:/mnt (-q表示传输文件时不显示进度)
Linux系统中的文件传输优化
2)把远程文件复制到本地
scp [email protected]:/mnt/westos_rhel8 /root/Desktop
Linux系统中的文件传输优化

3、rsync和scp命令的对比

实验素材
1)在rhel7中建立文件
dd if=/dev/zero of =/mnt/file1 bs=1M count=10
dd if=/dev/zero of =/mnt/file2 bs=1M count=20
dd if=/dev/zero of =/mnt/file3 bs=1M count=30
dd=截取,if=inputfile,of=outputfile,bs=blocksize,count=块的个数
2)在主机之间建立免密登录使远程文件传输可以直接执行
rhel7中:
ssh-****** ##生成**
Linux系统中的文件传输优化
ssh-copy-id -i /root/.ssh/id_rsa.pub. [email protected]
3)创建测试脚本
vim check_scp.sh ##检测scp传输时间
Linux系统中的文件传输优化
vim check_rsync.sh ##检测rsync传输时间
Linux系统中的文件传输优化
)执行
a、scp执行
sh check_scp.sh
Linux系统中的文件传输优化
b、rsync执行
sh check_rsybc.sh
Linux系统中的文件传输优化
以上执行效果我们可以看出rsync三次执行时间后两次往往小于第一次

4、rsync用法

rsync 文件 远程用户@远程主机ip:远程主机目录
rsync 远程用户@远程主机ip:远程主机目录 文件路径

rsync 用法
-r 复制目录
-l 复制链接
-p 复制权限
-t 复制时间戳
-o 复制拥有者
-g 复制拥有组
-D 复制设备文件

Linux系统中的文件传输优化
实验环境:
在rhel8中
watch -n 1 ls -lR /mnt
在rhel7中
touch /mnt/file{1…5}
chmod 777 /mnt/*
useradd westos
chown westos /mnt/*
ln -s /mnt/file1 /mnt/file

执行命令看效果:

命令 效果
rsync -r /mnt [email protected]:/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 -rD /dev/pts [email protected]:/mnt 同步设备文件

5、文件的归档压缩

1、文件归档

tar 用法
c 创建
f 指定文件名称
x 解档
t 查看
r 向归档文件中添加文件
–get 解档指定文件
–delete 删除指定文件
-C 指定解档路径

实验步骤:

2、文件压缩
zip:

zip 用法
zip -r etc.tar.zip.etc.tar zip格式压缩
uzip etc.tar.zip zip格式解压缩

gzip:

gzip 用法
gzip etc.tar gzip格式压缩
gunzip etc.tar.gz gzip格式解压缩

bzip2:

bzip2 用法
bzip2 etc.tar bzip2格式压缩
bunzip2 etc.tar.gz bzip2格式解压缩

xz:

xz 用法
xz etc.tar xz格式压缩
unxz etc.tar.xz xz格式解压缩

3、tar+压缩
gzip
tar zcf etc.tar.gz/etc
tar zxf etc.tar.gz

bzip2
tar jcf etc.tar.bz2 /etc
tar jxf etc.tar.gz

xz
tar jcf etc.tar.xz /etc
tar jxf etc.tar.xz