9_Linux系统中的文件传输


实验环境

需要2台主机并且保证这两台主机是可以通信的

westos_linux : 172.25.254.10
westos_node1 : 172.25.254.20

westos_linux&westos_node1:

systemctl disable firewalld 
systemctl stop firewalld

一、scp命令

Linux scp 命令用于 Linux 之间复制文件和目录。
scp 是 secure copy 的缩写, scp 是 linux 系统下基于 ssh 登陆进行安全的远程文件拷贝命令。
scp 是加密的,rcp 是不加密的,scp 是 rcp 的加强版。

scp   本地文件    远程主机用户@远程主机ip:远程主机目录的绝对路径
scp   远程主机用户@远程主机ip:远程主机文件的绝对路径  本地文件
  • 实验步骤:

  • 1.在node2建立实验素材

    touch westos
    mkdir westosdir
    
  • 2.测试

    • a)把本地文件复制到远程主机 (上传)
    scp  	westos   	root@172.25.254.20:/root/Desktop
    scp -r	westosdir	root@172.25.254.20:/root/Desktop ## -r 表示复制目录
    scp -q	westos		root@172.25.254.20:/root/Desktop ## -q 传输文件时不显示进度
    
    • b)把远程文件复制到本地(下载)
    scp root@172.25.254.20:/root/Desktop/westos_rhel8   /root/Desktop
    
    • c)
    time scp
    

二、rsync

rsync是linux系统下的数据镜像备份工具。使用快速增量备份工具Remote Sync可以远程同步,支持本地复制,或者与其他SSH、rsync主机同步。

  • a) rsyncscp命令的对比
    node2:

    ssh-keygen		## 生成密钥
    ssh-copy-id -i /root/.ssh/id_rsa.pub. root@172.25.254.20
    
  • 3)创建测试脚本

time scp -qr /boot root@172.25.254.20:/root/Desktop
time scp -qr /boot root@172.25.254.20:/root/Desktop
time scp -qr /boot root@172.25.254.20:/root/Desktop


vim check_rsync.sh		##检测rsync的传输时间
time rsync -raCq /boot root@172.25.254.20:/root/Desktop
time rsync -raCq /boot root@172.25.254.20:/root/Desktop
time rsync -raCq /boot root@172.25.254.20:/root/Desktop
  • 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三次执行时间后两次远远小与第一次
  • b)rsync用法
rsync  	文件			远程用户@远程主机ip:远程主机目录
rsync	远程用户@远程主机ip:远程主机目录	文件路径	


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

  • 实验环境
在westos_node1中
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

westos_linux执行:
执行命令看效果:
rsync  -r root@172.25.254.20:/root/Desktop	 /mnt		##同步目录本身其目录中的文件
rsync  -r root@172.25.254.20:/root/Desktop/	 /mnt		##只同步目录中的文件
rsync  -rl root@172.25.254.20:/root/Desktop/	 /mnt		##同步链接
rsync  -rlp root@172.25.254.20:/root/Desktop/	 /mnt		##同步权限
rsync  -rlpog root@172.25.254.20:/root/Desktop/	 /mnt		##同步用户组
rsync  -rlpogt root@172.25.254.20:/root/Desktop/ /mnt		##同步时间
rsync -rD root@172.25.254.20:/dev/pts		 /mnt		##同步设备文件

三、文件的归档压缩

打包与压缩的区别:
1)打包:是指将许多文件和目录集中存储在一个文件中。
2)压缩:是指利用算法对文件进行处理,从而达到压缩占用磁盘空间的目的。

压缩比:压缩率(Compression rate),描述压缩文件的效果名,是文件压缩后的大小与压缩前的大小之比,例如:把100m的文件压缩后是90m,压缩率为90/100*100%=90%,压缩率一般是越小越好,但是压得越小,解压时间越长。

压缩比:xz > bz2 > gz ,“.bz2” 格式的压缩算法更先进,压缩比更好,而“.gz” 格式相对来讲操作更快。

  • 1)tar:文件归档
tar
	c		##创建
	f		##指定文件名称
	x		##解档
	v		##现实过程
	t		##查看
	r		##向归档文件中添加文件
	--get		##解档指定文件
	--delete	##删除指定文件
	-C		##指定解档路径
	-P		##don't remove "/" 
  • 实验步骤:
tar cf etc.tar /etc/
tar tf etc.tar
tar rf etc.tar westos_rhel8
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
  • 2)文件的压缩

    • zip

      zip
      zip -r  mnt.tar.zip mnt.tar		#zip格式压缩
      unzip 	mnt.tar.zip				#zip格式解压缩
      
    • gzip

      gzip
      gzip	mnt.tar			#gzip格式压缩
      gunzip 	mnt.tar.gz		#gzip格式解压缩
      
      • bzip2
      bzip2	mnt.tar				#bzip2格式压缩
      bunzip2 etc.tar.bz2		#bzip2格式解压缩
      
      • xz格式
      xz	mnt.tar			#xz格式压缩
      unxz 	mnt.tar.xz		#xz格式解压缩
      
  • 3)tar+压缩

    • gzip
      gzip
      tar zcf etc.tar.gz /etc
      tar zxf etc.tar.gz 
      
    • bzip2
      bzip2
      tar jcf etc.tar.bz2 /etc
      tar jxf etc.tar.bz2 
      
    • xz
      xz
      tar Jcf etc.tar.xz /etc
      tar Jxf etc.tar.xz
      



  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值