linux下不同主机之间的文件传输

在运维过程中,我们经常会遇到需要两台机器的文件数据需要同步的情况。这里我们主要讲两种远程传输文件的命令:scp rsycn
scp就是secure copy,是用来进行远程文件拷贝的,验证时需要输入口令或密码
rsycn可以镜像保存整个目录树和文件系统,并且可以通过不同参数的使用可以保持原来文件的权限、时间、软硬链接等,优化的流程文件传输效率明显高于scp
在我们传输文件的过程中,可能会出现因为文件数目过多或文件内容过大 而造成的传输速度过慢的情况;我们可以通过对准备传输的文件进行归档·压缩
下面我们会对这些内容及其部分参数进行讲解

一.scp

0.实验前的准备

在client上建立十个文件

[root@client Desktop]# ls
[root@client Desktop]# touch file{1..10}
[root@client Desktop]# ls
file1  file10  file2  file3  file4  file5  file6  file7  file8  file9

1.上传
在client上建立多个文件并把其上传到fuwu主机(11)上
[root@client ~]# scp file{1..10} root@172.25.99.11:/root/Desktop ##上传文件到11的目录下


##效果 
[root@fuwu Desktop]# ls
file  file1  file10  file2  file3  file4  file5  file6  file7  file8  file9


2.下载

在fuwu上下载250主上的文件

[root@fuwu Desktop]# scp root@172.25.99.250:/root/Desktop/* . ##下载114主机内容到当前


##效果
[root@fuwu Desktop]# ls
file1  file10  file2  file3  file4  file5  file6  file7  file8  file9





二.rsync


rsync [参数]    file    username@ip:/dir

rsync    -r    ##同步目录
         -l    ##不忽略链接
         -p    ##不忽略文件权限
         -t    ##不忽文件时间戳
         -g    ##不忽文件所有组
         -o    ##不忽文件所有人
         -D    ##不忽略设备文件



0.实验前的准备

建立五个文件,并修改其所有人所有组以及文件权限,再给其中一个文件设置链接

[root@node1 mnt]# ls
file1  file2  file3  file4  file5
[root@node1 mnt]# ln -s /mnt/file5 /mnt/zl   ##给file5设置链接
[root@node1 mnt]# chmod 777 /mnt/*            ##改变所有文件权限
[root@node1 mnt]# chown student.student /mnt/*   ##修改所有文件所有人所有组
[root@node1 mnt]# ls -l /mnt/
total 0
-rwxrwxrwx. 1 student student  0 Oct 21 02:00 file1
-rwxrwxrwx. 1 student student  0 Oct 21 02:00 file2
-rwxrwxrwx. 1 student student  0 Oct 21 02:00 file3
-rwxrwxrwx. 1 student student  0 Oct 21 02:00 file4
-rwxrwxrwx. 1 student student  0 Oct 21 02:00 file5
lrwxrwxrwx. 1 root    root    10 Oct 21 02:01 zl -> /mnt/file5


1.

[root@node1 mnt]# rsync -r /mnt root@172.25.254.214:/mnt/  ##传输的文件后无‘/’时 传输包括文件目录本身
skipping non-regular file "mnt/zl"


##效果(有目录)
[root@node2 mnt]# ls
mnt


2.

[root@node1 mnt]# rsync -r /mnt/ root@172.25.254.214:/mnt/  ##传输文件内容不包括目录本身
skipping non-regular file "zl"


##效果(没有目录只有文件)
[root@node2 mnt]# ls 
file1  file2  file3  file4  file5



3.

[root@node1 mnt]# rsync -lr /mnt root@172.25.254.214:/mnt/  ##传输不忽略链接

##效果
[root@node2 mnt]# ls 
mnt
[root@node2 mnt]# ls -lR /mnt
/mnt:
total 0
drwx------ 2 root root 79 Oct 21 02:11 mnt
/mnt/mnt:
total 0
-rwx------ 1 root root  0 Oct 21 02:11 file1
-rwx------ 1 root root  0 Oct 21 02:11 file2
-rwx------ 1 root root  0 Oct 21 02:11 file3
-rwx------ 1 root root  0 Oct 21 02:11 file4
-rwx------ 1 root root  0 Oct 21 02:11 file5
lrwxrwxrwx 1 root root 10 Oct 21 02:11 zl -> /mnt/file5


4.

[root@node1 mnt]# rsync -lpogtr /mnt root@172.25.254.214:/mnt/  ##不忽略文件权限,时间,所有人所有组,链接


##效果
[root@node2 mnt]# ls -lR /mnt
/mnt:
total 0
drwxr-xr-x 2 root root 79 Oct 21 02:01 mnt
/mnt/mnt:
total 0
-rwxrwxrwx 1 student student  0 Oct 21 02:00 file1
-rwxrwxrwx 1 student student  0 Oct 21 02:00 file2
-rwxrwxrwx 1 student student  0 Oct 21 02:00 file3
-rwxrwxrwx 1 student student  0 Oct 21 02:00 file4
-rwxrwxrwx 1 student student  0 Oct 21 02:00 file5
lrwxrwxrwx 1 root    root    10 Oct 21 02:01 westos -> /mnt/file5


5.

[root@node1 mnt]# rsync -r /dev/pts root@172.25.254.214:/mnt/   ##同步设备文件所在目录 不成功
skipping non-regular file "pts/0"
skipping non-regular file "pts/1"
skipping non-regular file "pts/ptmx"
[root@node1 mnt]# rsync -Dr /dev/pts root@172.25.254.214:/mnt/  ##添加参数再次同步设备文件所在目录 成功

##效果

[root@node2 mnt]# ls -lR /mnt /mnt: total 0 drwx------ 2 root root 33 Oct 21 02:37 pts /mnt/pts: total 0 crw------- 1 root root 136, 0 Oct 21 02:37 0 crw------- 1 root root 136, 1 Oct 21 02:37 1 c--------- 1 root root   5, 2 Oct 21 02:37 ptmx




三.文件归档tar

归档文件(打包) 文件容量不变 只改变文件数目 加快传输速率

tar    c        ##创建
       f        ##指定归档文件名称
       t        ##显示归档文件中的内容
       r        ##向归档文件中添加文件
    --get       ##取出单个文件
    --delete    ##删除单个文件
       x        ##取出归档文件中的所有内容
      -C        ##指定解档目录
      -z        ##gz格式压缩
      -j        ##bz2格式压缩
      -J        ##xz格式压缩


1.

[root@node1 Desktop]# tar cf etc.tar /etc/   ##创建归档文件 将指定文件归档
tar: Removing leading `/' from member names  ##删除指定目录的‘/’


2.

[root@node1 Desktop]# tar tf etc.tar   ##查看归档文件内容
etc/
etc/fstab


3.

[root@node1 Desktop]# tar rf etc.tar /boot/   ##向已生成的归档文件中添加指定文件
tar: Removing leading `/' from member names


4.

[root@node1 Desktop]# tar -f etc.tar --delete boot  ##删除归档文件中的指定文件


5.

[root@node1 Desktop]# tar xf etc.tar   ##取出归档文件中的所有内容 
[root@node1 Desktop]# ls
boot  etc.tar  
etc   


6.

[root@node1 Desktop]# tar -f etc.tar --get boot  ##取出指定文件
[root@node1 Desktop]# ls     
boot  etc.tar


7.

[root@node1 Desktop]# tar xf etc.tar -C /mnt/   ##取出文件到指定目录
[root@node1 Desktop]# ls /mnt/
boot  etc





四.文件压缩
压缩文件只压缩纯文本文件 压缩高清图片或电影时解压后无法使用

1.

[root@node1 Desktop]# zip -r etc.tar.zip etc.tar   ##压缩成zip格式
  adding: etc.tar (deflated 72%)
[root@node1 Desktop]# du -sh etc.tar.zip   ##查看压缩文件的大小 
16M    etc.tar.zip


2.

[root@node1 Desktop]# unzip etc.tar.zip      ##解压zip文件 
Archive:  etc.tar.zip
  inflating: etc.tar                 
[root@node1 Desktop]# ls
etc.tar  etc.tar.zip


3.

[root@node1 Desktop]# gzip etc.tar    ##压缩成gzip格式
[root@node1 Desktop]# du -sh etc.tar.gz 
8.4M    etc.tar.gz


4.

[root@node1 Desktop]# gunzip etc.tar.gz   ##解压gzip文件 
[root@node1 Desktop]# ls
etc.tar


5.

[root@node1 Desktop]# bzip2 etc.tar   ##压缩成bz2格式
[root@node1 Desktop]# du -sh etc.tar.bz2 
7.0M    etc.tar.bz2


6.

[root@node1 Desktop]# bunzip2 etc.tar.bz2   ##解压bz2文件
[root@node1 Desktop]# ls
etc.tar


7.

[root@node1 Desktop]# xz etc.tar       ##压缩成xz格式
[root@node1 Desktop]# du -sh xz etc.tar.xz 
5.7M    etc.tar.xz


8.

[root@node1 Desktop]# unxz etc.tar.xz   ##解压xz文件 
[root@node1 Desktop]# ls
etc.tar


五.归档同时压缩(解压同时解档)

1.

[root@node1 Desktop]# tar -zcf etc.tar.gz /etc/  ##打包指定文件并压缩为gz格式
tar: Removing leading `/' from member names
[root@node1 Desktop]# du -sh etc.tar.gz 
8.4M    etc.tar.gz


2.

[root@node1 Desktop]# tar -zxf etc.tar.gz   ##解压并解档gz格式文件
[root@node1 Desktop]# ls
etc  etc.tar.gz


3.类似的

tar jcf etc.tar.bz2 /etc    ##把文件归档为tar并压缩成bz2
tar jxf etc.tar.bz2           ##解压并解档bz2格式压缩包
tar Jcf etc.tar.xz /etc      ##把文件归档为tar并压缩成zx
tar Jxf etc.tar.xz             ##解压并解档xz格式压缩包








  • 0
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值