Linux系统中的文件传输优化

前言

文件的传输非常常用,是基本功

实验环境

需要两台主机并且保证这两台主机是可以通信的
rhel7:192.168.0.10
rhel8:192.168.0.11

用ifcofig进行查询主机的ip

在企业7这台主机中连接企业8,
ssh root@192.168.0.11

复制文件命令

scp命令

scp 本地文件 远程主机用户@远程主机ip:远程主机目录
scp westos root@192.168.0.11:/mnt/(需要密码且有进度条)
scp -q westos root@192.168.0.11:/mnt/(去掉进度条)
scp -r westosdir root@192.168.0.11:/mnt/(远程传输目录)
scp 远程主机用户@远程主机ip:远程主机目录 本地文件
scp root@192.168.0.11:/mnt/westos_rhel8 /root/Desktop
前面加time可以查看时间。

实验步骤(scp文件传输):

1.在rhel7里面建立实验素材
touch westos

[root@rhel7_node1 Desktop]# touch haizhihzu  #建立文件

mkdir westosdir

[root@rhel7_node1 Desktop]# mkdir haizhihzudir  #建立文件夹

2.测试
在7中传输文件:

[root@rhel7_node1 Desktop]# scp haizhihzu   root@192.168.0.11:/mnt   #传输文件到目标主机的某个文件夹
root@192.168.0.11's password: 
haizhihzu                                                                       100%    7     1.9KB/s   00:00    
不要进度条:
[root@rhel7_node1 Desktop]# scp -q haizhihzu root@192.168.0.11:/mnt
root@192.168.0.11's password: 

在7中传输文件夹:

[root@rhel7_node1 Desktop]# scp  haizhizhudir root@192.168.0.11:/mnt
root@192.168.0.11's password: 
haizhizhudir: not a regular file(不可以用传输文件的方式传输文件夹)
[root@rhel7_node1 Desktop]# scp -r haizhizhudir root@192.168.0.11:/mnt
root@192.168.0.11's password: 
[root@rhel7_node1 Desktop]# 

把远程文件复制到本地:

[root@rhel7_node1 Desktop]# scp -r haizhizhudir root@192.168.0.11:/mnt
root@192.168.0.11's password: 
[root@rhel7_node1 Desktop]# scp -r root@192.168.0.11:/mnt/hello /root/Desktop/
root@192.168.0.11's password: 
[root@rhel7_node1 Desktop]# 

rsync命令

  • rsync和scp命令的对比
    rsync命令中远程主机已经有的就不会再复制了,更加的智能。
实验步骤(两个命令的对比):

1.建立实验素材
在rhel中建立文件

dd if=/dev/zero of /mnt/file1 bs=1M count=10(dd=截取,if=inputfile,of=outputfile ,bs=blocksize,count=块的个数)
dd if=/dev/zero of /mnt/file1 bs=2M count=20
dd if=/dev/zero of /mnt/file1 bs=3M count=30
10+0 records in
10+0 records out
10485760 bytes (10 MB) copied, 0.0336416 s, 312 MB/s

2.在主机之间建立免密登陆使远程文件的传输可以直接执行
在rhel7中:

ssh-keygen(生成密钥)
ssh-copy-id -i /root/.ssh/id_rsa.pub root@192.168.0.11  

3.创建测试脚本并进行测试

vim check_scp.sh(scp支持加密,且不会造成磁盘负载加重)
time scp -qr /mnt root@192.168.0.11:/mnt(检测scp的传输时间)
time scp -qr /mnt root@192.168.0.11:/mnt
time scp -qr /mnt root@192.168.0.11:/mnt

vim check_rsync.sh
time rsync -raCq /mnt root@192.168.0.11:/mnt(检测rsync的传输时间)
time rsync -raCq /mnt root@192.168.0.11:/mnt
time rsync -raCq /mnt root@192.168.0.11:/mnt

4.执行结果:

scp命令:
[root@rhel7_node1 Desktop]# sh check_scp.sh 
real	0m2.068s
user	0m0.237s
sys	0m0.633s

real	0m2.050s
user	0m0.452s
sys	0m0.400s

real	0m2.006s
user	0m0.333s
sys	0m0.517s     #三次传送时间几乎相同

rsync命令:
[root@rhel7_node1 Desktop]# sh check_rsync.sh 
real	0m2.481s
user	0m0.422s
sys	0m0.603s    #第一次时间较长,后面两次时间大幅度减少

real	0m0.402s
user	0m0.012s
sys	0m0.016s

real	0m0.368s
user	0m0.013s
sys	0m0.016s

rsync的所耗费时间减少,因为之后制作检测,不进行复制传送的操作

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

     [root@rhel7_node1 Desktop]# rsync -r root@192.168.0.10:/mnt/ haizhizhudir   #将本地文件发送到远程主机
     [root@rhel7_node1 Desktop]# rsync -r root@192.168.0.11:/mnt/haizhizhudir .   #将远程主机里的内容复制到当前地址
    
  • rsync
    -r(复制目录)
    -l(复制连接)
    -p(复制权限)
    -t(复制时间戳)
    -o(复制拥有者)
    -g(复制拥有组)
    -D(复制设备文件)

实验步骤

1.建立实验环境
在rhel8中

[root@rhel8_node1 mnt]# watch -n 1 ls -l /mnt

在rhel7中

先建立5个文件:
[root@rhel7_node1 mnt]# touch /mnt/file{1..5}
[root@rhel7_node1 mnt]# ll
total 0
-rw-r--r--. 1 root root 0 Mar  9 05:24 file1
-rw-r--r--. 1 root root 0 Mar  9 05:24 file2
-rw-r--r--. 1 root root 0 Mar  9 05:24 file3
-rw-r--r--. 1 root root 0 Mar  9 05:24 file4
-rw-r--r--. 1 root root 0 Mar  9 05:24 file5
将文件全部赋予满权限:
[root@rhel7_node1 mnt]# chmod 777 /mnt/*
添加一个新用户:
[root@rhel7_node1 mnt]# useradd westos
将文件加到新用户的权限之下:
[root@rhel7_node1 mnt]# chown westos.westos /mnt/*
将file1链接到file上面:
[root@rhel7_node1 mnt]# ln -s /mnt/file1 /mnt/file
[root@rhel7_node1 mnt]# ll
total 0
lrwxrwxrwx. 1 root   root   10 Mar  9 05:26 file -> /mnt/file1
-rwxrwxrwx. 1 westos westos  0 Mar  9 05:24 file1
-rwxrwxrwx. 1 westos westos  0 Mar  9 05:24 file2
-rwxrwxrwx. 1 westos westos  0 Mar  9 05:24 file3
-rwxrwxrwx. 1 westos westos  0 Mar  9 05:24 file4
-rwxrwxrwx. 1 westos westos  0 Mar  9 05:24 file5

2.执行命令看效果

rsync -r /mnt root@192.168.0.11:/mnt(同步目录本身与其目录中的文件

[root@rhel7_node1 mnt]# rsync -r /mnt root@192.168.0.11:/mnt

监控台显示:

Every 10.0s: ls -lR /mnt    rhel8_node1.westos.com: Mon Mar  9 05:35:56 2020

/mnt:
total 0
drwxr-xr-x. 2 root root 71 3月   9 05:33 mnt

/mnt/mnt:
total 0
----r-xr-x. 1 root root 0 3月   9 05:33 file1
----r-xr-x. 1 root root 0 3月   9 05:33 file2
----r-xr-x. 1 root root 0 3月   9 05:33 file3
----r-xr-x. 1 root root 0 3月   9 05:33 file4
----r-xr-x. 1 root root 0 3月   9 05:33 file5

rsync -r /mnt/ root@192.168.0.11:/mnt(只同步目录中的文件

[root@rhel7_node1 mnt]# rsync -r /mnt/ root@192.168.0.11:/mnt
skipping non-regular file "file"

监控台显示:

Every 10.0s: ls -lR /mnt    rhel8_node1.westos.com: Mon Mar  9 05:39:47 2020

/mnt:
total 0
----r-xr-x. 1 root root 0 3月   9 05:38 file1
----r-xr-x. 1 root root 0 3月   9 05:38 file2
----r-xr-x. 1 root root 0 3月   9 05:38 file3
----r-xr-x. 1 root root 0 3月   9 05:38 file4
----r-xr-x. 1 root root 0 3月   9 05:38 file5

rsync -rl /mnt/ root@192.168.0.11:/mnt(同步链接

[root@rhel7_node1 mnt]# rsync -rl /mnt/ root@192.168.0.11:/mnt

监控台显示:

Every 10.0s: ls -lR /mnt    rhel8_node1.westos.com: Mon Mar  9 05:40:27 2020

/mnt:
total 0
lrwxrwxrwx. 1 root root 10 3月   9 05:40 file -> /mnt/file1
----r-xr-x. 1 root root  0 3月   9 05:40 file1
----r-xr-x. 1 root root  0 3月   9 05:40 file2
----r-xr-x. 1 root root  0 3月   9 05:40 file3
----r-xr-x. 1 root root  0 3月   9 05:40 file4
----r-xr-x. 1 root root  0 3月   9 05:40 file5

rsync -rlp /mnt/ root@192.168.0.11:/mnt(同步权限

[root@rhel7_node1 mnt]# rsync -rlp /mnt/ root@192.168.0.11:/mnt

监控台显示:

Every 10.0s: ls -lR /mnt    rhel8_node1.westos.com: Mon Mar  9 05:46:11 2020

/mnt:
total 0
lrwxrwxrwx. 1 root root 10 3月   9 05:43 file -> /mnt/file1
-rwxrwxrwx. 1 root root  0 3月   9 05:46 file1
-rwxrwxrwx. 1 root root  0 3月   9 05:46 file2
-rwxrwxrwx. 1 root root  0 3月   9 05:46 file3
-rwxrwxrwx. 1 root root  0 3月   9 05:46 file4
-rwxrwxrwx. 1 root root  0 3月   9 05:46 file5

rsync -rlpog /mnt/ root@192.168.0.11:/mnt(同步用户组

[root@rhel7_node1 mnt]# rsync -rlpog /mnt/ root@192.168.0.11:/mnt

监控台显示:

Every 10.0s: ls -lR /mnt    rhel8_node1.westos.com: Mon Mar  9 05:47:31 2020

/mnt:
total 0
lrwxrwxrwx. 1 root   root   10 3月   9 05:43 file -> /mnt/file1
-rwxrwxrwx. 1 westos westos  0 3月   9 05:47 file1
-rwxrwxrwx. 1 westos westos  0 3月   9 05:47 file2
-rwxrwxrwx. 1 westos westos  0 3月   9 05:47 file3
-rwxrwxrwx. 1 westos westos  0 3月   9 05:47 file4
-rwxrwxrwx. 1 westos westos  0 3月   9 05:47 file5

rsync -rlpogt /mnt/ root@192.168.0.11:/mnt(同步时间

[root@rhel7_node1 mnt]# rsync -rlpogt /mnt/ root@192.168.0.11:/mnt

监控台显示:

Every 10.0s: ls -lR /mnt    rhel8_node1.westos.com: Mon Mar  9 05:49:12 2020

/mnt:
total 0
lrwxrwxrwx. 1 root   root   10 3月   9 05:26 file -> /mnt/file1
-rwxrwxrwx. 1 westos westos  0 3月   9 05:24 file1
-rwxrwxrwx. 1 westos westos  0 3月   9 05:24 file2
-rwxrwxrwx. 1 westos westos  0 3月   9 05:24 file3
-rwxrwxrwx. 1 westos westos  0 3月   9 05:24 file4
-rwxrwxrwx. 1 westos westos  0 3月   9 05:24 file5

rsync -rD /dev/pts root@192.168.0.11:/mnt(同步设备文件

[root@rhel7_node1 ~]# rsync -rD /dev/pts root@192.168.0.11:/mnt

监控台显示:

/mnt:
total 0
drwxr-xr-x. 2 root root 36 3月   9 05:54 pts

/mnt/pts:
total 0
crw-------. 1 root root 136, 0 3月   9 05:54 0
crw-------. 1 root root 136, 1 3月   9 05:54 1
c---------. 1 root root   5, 2 3月   9 05:54 ptmx

文件的归档压缩

在文件多的情况下提高复制效率(归档或者压缩)

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

实验步骤

1.将实验文件夹复制到桌面:

[root@rhel7_node1 Desktop]# cp -r /etc/ .

查看此文件的大小

[root@rhel7_node1 Desktop]# du -sh /stc/
du: cannot access ‘/stc/’: No such file or directory
[root@rhel7_node1 Desktop]# du -sh /etc/
44M	/etc/

2.开始归档

[root@rhel7_node1 Desktop]# tar cf etc.tar /etc
tar: Removing leading `/' from member names
[root@rhel7_node1 Desktop]# du -sh etc.tar
39M	etc.tar   #此处看起来变小了,其实是没有计算重复的链接所链接的文件

3.查看归档文件内容

[root@rhel7_node1 Desktop]# tar tf etc.tar 

4.用命令行将外面的文件打包进已打包到压缩包中

[root@rhel7_node1 Desktop]# tar rf etc.tar hello 

5.将压缩文件都取出

[root@rhel7_node1 Desktop]# tar xf etc.tar 

6.单独解出某一个文件

[root@rhel7_node1 Desktop]# tar f etc.tar --get hello

7.删除压缩文件中的其中一个

[root@rhel7_node1 Desktop]# tar f etc.tar --delete hello

8.将压缩文件解绑到指定目录

[root@rhel7_node1 Desktop]# tar xf etc.tar -C /mnt
  • 文件的压缩(归档完才可以压缩,压缩程度越高,效率越低)
    zip(16M)
    zip -r etc.tar.zip etc.tar(zip格式压缩)
    unzip etc.tar.zip(解压)

      压缩:
      [root@rhel7_node1 Desktop]# zip -r etc.tar.zip etc.tar
        adding: etc.tar (deflated 70%)
      [root@rhel7_node1 Desktop]# du -sh etc.tar.zip 
      16M	etc.tar.zip
      [root@rhel7_node1 Desktop]# du -sh etc.tar
      39M	etc.tar
      解压缩:
      [root@rhel7_node1 Desktop]# unzip etc.tar.zip
      Archive:  etc.tar.zip
        inflating: etc.tar         
    

    gzip(12M)
    gzip etc.tar
    gunzip etc.tar.gz
    bzip2 (10M)
    bzip2 etc.tar
    bunzip2 etc.tar.bz2
    xz (8M)
    xz etc.tar
    unxz etc.tar.xz

  • tar+压缩(tar的压缩格式没有zip)
    gzip
    tar zcf etc.tar.gz /etc(压缩)
    tar 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

    可以看看tar的文档:

      Common options:
      		...
             -j, --bzip2
                    filter the archive through bzip2
      
              -J, --xz
                    filter the archive through xz
      		...
      
             -z, --gzip
                    filter the archive through gzip
    

后记

本节的命令比较多,记忆起来有一定困难,也容易混淆。

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值