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

一.scp

0.实验前的准备

在client上建立十个文件

[[email protected] Desktop]# ls
[[email protected] Desktop]# touch file{1..10}
[[email protected] Desktop]# ls
file1  file10  file2  file3  file4  file5  file6  file7  file8  file9
linux下不同主机之间的文件传输

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

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

##效果 
[[email protected] Desktop]# ls
file  file1  file10  file2  file3  file4  file5  file6  file7  file8  file9

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

2.下载

在fuwu上下载250主上的文件

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

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

##效果
[[email protected] Desktop]# ls
file1  file10  file2  file3  file4  file5  file6  file7  file8  file9

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




二.rsync


rsync [参数]    file    [email protected]:/dir

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



0.实验前的准备

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

[[email protected] mnt]# ls
file1  file2  file3  file4  file5
[[email protected] mnt]# ln -s /mnt/file5 /mnt/zl   ##给file5设置链接
[[email protected] mnt]# chmod 777 /mnt/*            ##改变所有文件权限
[[email protected] mnt]# chown student.student /mnt/*   ##修改所有文件所有人所有组
[[email protected] 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

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


1.

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

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

##效果(有目录)
[[email protected] mnt]# ls
mnt

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


2.

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

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

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

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


3.

[[email protected] mnt]# rsync -lr /mnt [email protected]:/mnt/  ##传输不忽略链接

##效果
[[email protected] mnt]# ls 
mnt
[[email protected] 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

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


4.

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


##效果
[[email protected] 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

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


5.

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


##效果

[[email protected] 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

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



三.文件归档tar

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

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


1.

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

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

2.

[[email protected] Desktop]# tar tf etc.tar   ##查看归档文件内容
etc/
etc/fstab

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

3.

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

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

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

4.

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


5.

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

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

6.

[[email protected] Desktop]# tar -f etc.tar --get boot  ##取出指定文件
[[email protected] Desktop]# ls     
boot  etc.tar

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

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

7.

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

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




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

1.

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

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

2.

[[email protected] Desktop]# unzip etc.tar.zip      ##解压zip文件 
Archive:  etc.tar.zip
  inflating: etc.tar                 
[[email protected] Desktop]# ls
etc.tar  etc.tar.zip

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

3.

[[email protected] Desktop]# gzip etc.tar    ##压缩成gzip格式
[[email protected] Desktop]# du -sh etc.tar.gz 
8.4M    etc.tar.gz

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

4.

[[email protected] Desktop]# gunzip etc.tar.gz   ##解压gzip文件 
[[email protected] Desktop]# ls
etc.tar

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

5.

[[email protected] Desktop]# bzip2 etc.tar   ##压缩成bz2格式
[[email protected] Desktop]# du -sh etc.tar.bz2 
7.0M    etc.tar.bz2

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

6.

[[email protected] Desktop]# bunzip2 etc.tar.bz2   ##解压bz2文件
[[email protected] Desktop]# ls
etc.tar

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

7.

[[email protected] Desktop]# xz etc.tar       ##压缩成xz格式
[[email protected] Desktop]# du -sh xz etc.tar.xz 
5.7M    etc.tar.xz

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

8.

[[email protected] Desktop]# unxz etc.tar.xz   ##解压xz文件 
[[email protected] Desktop]# ls
etc.tar
linux下不同主机之间的文件传输

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

1.

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

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

2.

[[email protected] Desktop]# tar -zxf etc.tar.gz   ##解压并解档gz格式文件
[[email protected] Desktop]# ls
etc  etc.tar.gz
linux下不同主机之间的文件传输

3.类似的









相关文章: