9.带你玩转文件传输

1.实验环境
需要2台主机并且保证这两台主机是可以通信的
cxf.loacldomain  172.25.254.20
cxf.loacldomain  172.25.254.10
 
9.带你玩转文件传输9.带你玩转文件传输
 
9.带你玩转文件传输
 
scp命令
scp 本地文件 远程主机用户@远程主机ip:远程主机目录的绝对路径
scp 远程主机用户@远程主机ip:远程主机文件的绝对路径 本地文件
 
a)把本地文件复制到远程主机 (上传)
scp westos root@172.25.254.20:/root/Desktop
 
9.带你玩转文件传输
scp -r westosdir root@172.25.254.20:/root/Desktop ## -r 表示复制 目录
scp -q westos root@172.25.254.20:/root/ Desktop ## -q 传输文件时不显示进度
 
9.带你玩转文件传输
 
把远程文件复制到本地(下载)
scp root@172.25.254.20:/root/Desktop/westos_rhel8 /root/Desktop
 
9.带你玩转文件传输
 
9.带你玩转文件传输
 
2.rsync
1172.25.254.10:
dd if=/dev/zero of=/root/Desktop/westosfile1 bs=1M count=10
##dd=
取,if=inputfile
##of=outputfile
#bs=blocksize
#count=
快的个数
dd if=/dev/zero of=/root/Desktop/westosfile2 bs=1M count=20
dd if=/dev/zero of=/root/Desktop/westosfile3 bs=1M count=30

 

9.带你玩转文件传输

在主机之间建立免密登陆使远程文件传输可以直接执行
rhel7中:
ssh-****** ## 生成**
ssh-copy-id -i /root/.ssh/id_rsa.pub. root@172.25.254.209.带你玩转文件传输
 
3)创建测试脚本
vim check_scp.sh ##检测scp传输时间
time scp -qr /root/Desktop root@172.25.254.20:/root/Desktop
time scp -qr /root/Desktop root@172.25.254.20:/root/Desktop
time scp -qr /root/Desktop root@172.25.254.20:/root/Desktop
vim check_rsync.sh ##检测rsync的传输时间
time rsync -raCq /root/Desktop root@172.25.254.20:/root/Desktop
time rsync -raCq /root/Desktop root@172.25.254.20:/root/Desktop
time rsync -raCq /root/Desktop root@172.25.254.20:/root/Desktop

 

9.带你玩转文件传输

4)执行结果
#########scp ###################
sh check_scp.sh
real
0m1.334s
user 0m0.210s
sys 0m0.490s
第一次系统执行时间
real
0m1.642s
user 0m0.412s
sys 0m0.383s
第二次系统执行时间
real
0m1.586s
user 0m0.309s
sys 0m0.497s
第三次系统执行时间
以上执行效果我们可以看出scp三次执行时间几乎一致
###########rsync执行############
sh check_rsync.sh
real
0m1.603s
user 0m0.399s
sys 0m0.557s
第一次系统执行时间
real
0m0.329s
user 0m0.012s
sys 0m0.010s
第二次系统执行时间
real
0m0.348s
user 0m0.014s
sys 0m0.022s
第三次系统执行时间
以上执行效果我们可以看出rsync三次执行时间后两次远远小与第一次

 

rsync用法
rsync 文件
远程用户@远程主机ip:远程主机目录
rsync
远程用户@远程主机ip:远程主机目录 文件路径
rsync
-r ##复制目录
-l ##复制链接
-p ##复制权限
-t ##复制时间戳
-o ##复制拥有者
-g ##复制拥有组
-D ##复制设备文件
 
 
 
rhel8
watch -n 1 ls -lR /root/Desktop
rhel7
touch /root/Desktop/file{1..5}
chmod 777 /root/Desktop/*
useradd westos
chown westos /root/Desktop/*
ln -s /root/Desktop/file1 /root/Desktop/file

 

9.带你玩转文件传输

执行命令看效果:
rsync -r /root/Desktop root@172.25.254.20:/root/
Desktop ##同步目录本身其目录中的文件
rsync -r /root/Desktop/ root@172.25.254.20:/root/
Desktop ##只同步目录中的文件
rsync -rl /root/Desktop/ root@172.25.254.20:/root/
Desktop ##同步链接
 
9.带你玩转文件传输
rsync -rlp /root/Desktop/ root@172.25.254.20:/root/Desktop ##同步权
 
9.带你玩转文件传输
rsync -rlpog /root/Desktop/ root@172.25.254.20:/root/Desktop ##同步用
户组
 
9.带你玩转文件传输
rsync -rlpogt /root/Desktop/ root@172.25.254.20:/root/Desktop ##同步时
9.带你玩转文件传输
rsync -rD /dev/pts root@172.25.254.20:/root/Desktop
9.带你玩转文件传输
 
1.文件归档
tar
c ##创建
f ##指定文件名称
x ##解档
t ##查看
r ##向归档文件中添加文件
--get ##解档指定文件
--delete ##删除指定文件
-C ##指定解档路径
实验步骤:
tar cf etc.tar /etc/
9.带你玩转文件传输
tar tf etc.tar
 
9.带你玩转文件传输
tar rf etc.tar westos_rhel8
9.带你玩转文件传输
tar xf etc.tar
tar f etc.tar --get westos_rhel8
tar f etc.tar --delete westos_rhel8
tar xf etc.tar -C /root/Desktop
9.带你玩转文件传输
 
 
文件的压缩
zip
zip -r etc.tar.zip etc.tar #zip格式压缩
9.带你玩转文件传输
unzip etc.tar.zip #zip格式解压缩
9.带你玩转文件传输
 
 
gzip
gzip etc.tar
#gzip格式压缩
 
9.带你玩转文件传输
gunzip etc.tar.gz #gzip格式解压缩
bzip2 etc.tar
#bzip2格式压缩
bunzip2 etc.tar.bz2 #bzip2格式解压缩
xz etc.tar
#xz格式压缩
unxz etc.tar.xz
#xz格式解压缩
 
 
3.tar+压缩
gzip
tar zcf etc.tar.gz /etctar zxf etc.tar.gz
bzip2
tar jcf etc.tar.bz2 /etc
tar jxf etc.tar.bz2
xz
tar Jcf etc.tar.xz /etc
tar Jxf etc.tar.xz