LINUX核心命令实战总结七——文件备份与压缩命令

1.1 tar:打包备份

【功能说明】

Linux系统里,tar是将多个文件打包在一起,并且可以实现解压打包的文件的命令。是系统管理员最常用的命令之一。

【语法格式】

tar [OPTION...] [FILE]...

tar [选项...] [文件]...

【选项说明】

命令tar参数选项及说明

参数选项

解释说明(带*的为重点)

             z

通过gzip压缩或解压(*

c

创建新的tar

v

显示详细的tar命令执行过程(*

f

指定压缩文件的名字

t

不解压查看tar包的内容(*

p

保持文件的原有属性

P(大写)

以绝对路径打包,危险参数

j

通过bzip2命令压缩或解压

x

解开压缩包(*

C

指定解压的目录路径(*

--exclude=PATTERN

打包时排除不需要处理的文件或目录(*

 

   -X文件名

从指定文件读取不需要处理的文件或目录列表

-N日期

仅打包比指定日期新的文件,可用于增量打包备份

-h

打包软链接文件指向的真实源文件(*

--hard-dereference

打包硬链接文件

【使用范例】

备份站点目录

[root@A ~]# mkdir -p /var/www/html/yhh/test
[root@A ~]# touch /var/www/html/{1..10}.html
[root@A ~]# ls /var/www/html
10.html  1.html  2.html  3.html  4.html  5.html  6.html  7.html  8.html  9.html  yhh
[root@A ~]# cd /var/html
-bash: cd: /var/html: 没有那个文件或目录
[root@A ~]# cd /var/www
[root@A www]# ls
html
[root@A www]# tar zcvf www.tar.gz ./html/
./html/
./html/10.html
./html/8.html
./html/5.html
./html/yhh/
./html/yhh/test/
./html/9.html
./html/1.html
./html/7.html
./html/2.html
./html/4.html
./html/3.html
./html/6.html
[root@A www]# ll -h www.tar.gz
-rw-r--r-- 1 root root 252 1月  24 23:36 www.tar.gz
View Code

查看压缩包内的内容

[root@A www]# tar ztvf www.tar.gz
drwxr-xr-x root/root         0 2018-01-24 23:19 ./html/
-rw-r--r-- root/root         0 2018-01-24 23:19 ./html/10.html
-rw-r--r-- root/root         0 2018-01-24 23:19 ./html/8.html
-rw-r--r-- root/root         0 2018-01-24 23:19 ./html/5.html
drwxr-xr-x root/root         0 2018-01-24 23:19 ./html/yhh/
drwxr-xr-x root/root         0 2018-01-24 23:19 ./html/yhh/test/
-rw-r--r-- root/root         0 2018-01-24 23:19 ./html/9.html
-rw-r--r-- root/root         0 2018-01-24 23:19 ./html/1.html
-rw-r--r-- root/root         0 2018-01-24 23:19 ./html/7.html
-rw-r--r-- root/root         0 2018-01-24 23:19 ./html/2.html
-rw-r--r-- root/root         0 2018-01-24 23:19 ./html/4.html
-rw-r--r-- root/root         0 2018-01-24 23:19 ./html/3.html
-rw-r--r-- root/root         0 2018-01-24 23:19 ./html/6.html
[root@A www]# tar ztf www.tar.gz 
./html/
./html/10.html
./html/8.html
./html/5.html
./html/yhh/
./html/yhh/test/
./html/9.html
./html/1.html
./html/7.html
./html/2.html
./html/4.html
./html/3.html
./html/6.html
[root@A www]# tar tf www.tar.gz 
./html/
./html/10.html
./html/8.html
./html/5.html
./html/yhh/
./html/yhh/test/
./html/9.html
./html/1.html
./html/7.html
./html/2.html
./html/4.html
./html/3.html
./html/6.html
View Code

解开压缩包

[root@A www]# tar zcvf www.tar.gz ./html/ --exclude=html/yhh/test
./html/
./html/10.html
./html/8.html
./html/5.html
./html/yhh/
./html/9.html
./html/1.html
./html/7.html
./html/2.html
./html/4.html
./html/3.html
./html/6.html
View Code

排除多个文件打包参数-X

[root@A www]# cat list.txt
10.html
8.html
1.html
7.html
5.html
[root@A www]# tar zcvfX paichu.tar.gz list.txt ./html/
./html/
./html/yhh/
./html/yhh/test/
./html/9.html
./html/2.html
./html/4.html
./html/3.html
./html/6.html
View Code

打包链接文件

[root@A www]# cd /etc/
[root@A etc]# tar zcf local.tar.gz ./rc.local
[root@A etc]# tar tfv local.tar.gz
lrwxrwxrwx root/root         0 2016-07-05 11:07 ./rc.local -> rc.d/rc.local
[root@A etc]# tar zcfh local_h.tar.gz ./rc.local
[root@A etc]# tar tfv local_h.tar.gz
-rwxr-xr-x root/root       263 2017-11-30 15:57 ./rc.local

【技巧性范例】

使用tar的时候,有时候需要排除要压缩的目录下的一个子目录,但此时可能会遇到一个问题,这和要压缩目录的相对路径和绝对路径的选择有关。

[root@A www]# tar zcvf www.tar.gz ./html/ --exclude=/var/www/html/yhh/test
./html/
./html/10.html
./html/8.html
./html/5.html
./html/yhh/
./html/yhh/test/
./html/9.html
./html/1.html
./html/7.html
./html/2.html
./html/4.html
./html/3.html
./html/6.html
[root@A www]# tar zcvf www.tar.gz ./html/ --exclude=html/yhh/test         
./html/
./html/10.html
./html/8.html
./html/5.html
./html/yhh/
./html/9.html
./html/1.html
./html/7.html
./html/2.html
./html/4.html
./html/3.html
./html/6.html
View Code

【经验技巧】

下面列出打包时的经验技巧以供大家参

1、在打包一个目录之前,先前进入到这个目录的上一级目录,然后执行打包命令,这是大部分情况下打包文件的规范操作流程。少数情况下打包需要完整的目录结构时,也可以使用绝对路径打包,但是需要注意的是解压tar包时压缩内的文件是否会覆盖原始文件。

2、打包模型为:tar  zcf  /路径/筐   .tar.gz    相对路径/苹果。打包其实就是把苹果放进筐里

1.2 gzip:压缩或解压

【功能说明】

命令gzip用于将一个大的文件通过压缩算法变成一个小的文件。Gzip命令不能直接压缩目录,因此目录需要先用tar打包成一个文件,然后tar再调用gzip进行压缩。

【语法格式】

gzip  [ option ]  [file]

gzip  [ 选项 ] [文件]

【选项说明】

参数选项

解释说明(带*的为重点)

-d

解开压缩文件(*

-v

显示指令执行的过程

-I

列出压缩文件的内容信息

-c

将内容输出到标准输出,不改变原始文件(*

-r

对目录下的所有文件递归进行压缩操作

-数字<1-9>

指定压缩率,默认为6,值越大压缩率越高

-t

测试,检查压缩文件是否完整

 【使用范例】

把目录下的每个文件都压缩成单独的.gz文件

[root@A html]# ls
10.html  1.html  2.html  3.html  4.html  5.html  6.html  7.html  8.html  9.html  yhh
[root@A html]# gzip *.html
[root@A html]# ls
10.html.gz  1.html.gz  2.html.gz  3.html.gz  4.html.gz  5.html.gz  6.html.gz  7.html.gz  8.html.gz  9.html.gz  yhh
View Code

比解压显示上一个例子中每个压缩文件的信息

[root@A html]# gzip -1 *.html
gzip: *.html: No such file or directory
[root@A html]# gzip -l *.gz
         compressed        uncompressed  ratio uncompressed_name
                 28                   0   0.0% 10.html
                 27                   0   0.0% 1.html
                 27                   0   0.0% 2.html
                 27                   0   0.0% 3.html
                 27                   0   0.0% 4.html
                 27                   0   0.0% 5.html
                 27                   0   0.0% 6.html
                 27                   0   0.0% 7.html
                 27                   0   0.0% 8.html
                 27                   0   0.0% 9.html
View Code

解压缩文件,并显示解压过程

[root@A html]# gzip -dv *.gz
10.html.gz:       0.0% -- replaced with 10.html
1.html.gz:        0.0% -- replaced with 1.html
2.html.gz:        0.0% -- replaced with 2.html
3.html.gz:        0.0% -- replaced with 3.html
4.html.gz:        0.0% -- replaced with 4.html
5.html.gz:        0.0% -- replaced with 5.html
6.html.gz:        0.0% -- replaced with 6.html
7.html.gz:        0.0% -- replaced with 7.html
8.html.gz:        0.0% -- replaced with 8.html
9.html.gz:        0.0% -- replaced with 9.html
View Code

压缩解压缩保留原文件

[root@A html]# cp /etc/services .
[root@A html]# ll -h /etc/services
-rw-r--r--. 1 root root 626K 10月  2 2013 /etc/services
[root@A html]# gzip -c services>services.gz
[root@A html]# ll -h services*
-rw-r--r-- 1 root root 626K 1月  25 15:30 services
-rw-r--r-- 1 root root 125K 1月  25 15:31 services.gz
[root@A html]# gzip -dc services.gz>services2
[root@A html]# diff services services2
[root@A html]# ll -h services*
-rw-r--r-- 1 root root 626K 1月  25 15:30 services
-rw-r--r-- 1 root root 626K 1月  25 15:32 services2
-rw-r--r-- 1 root root 125K 1月  25 15:31 services.gz
View Code

【经验技巧】

直接处理压缩文件zcatzgrep zless 等价于cat grepless

[root@A html]# zcat services.gz|head -5
# /etc/services:
# $Id: services,v 1.48 2009/11/11 14:32:31 ovasik Exp $
#
# Network services, Internet style
# IANA services version: last updated 2009-11-10
View Code

1.3 zip:打包和压缩文件

【功能说明】

命令zip格式是WindowsLinux等多平台通用的压缩格式,和gzip命令相比,zip命令压缩文件不仅不会删除源文件,而且还可以压缩目录。

【语法格式】

zip   [option]    [file]

zip   [选项]     [文件]

【选项说明】

参数选项

解释说明(带*的为重点)

-r

将指定目录下的所有文件和子目录一并压缩(*

-x

压缩文件时排除某个文件

-q

不显示压缩信息

命令zip参数及说明

【使用范例】

压缩文件

[root@A tmp]# ll -h
总用量 632K
drwxr-xr-x 3 root root 4.0K 1月  24 23:19 html
-rw-r--r-- 1 root root 626K 1月  25 15:46 services
[root@A tmp]# zip services.zip ./services
  adding: services (deflated 80%)
[root@A tmp]# ll -h
总用量 760K
drwxr-xr-x 3 root root 4.0K 1月  24 23:19 html
-rw-r--r-- 1 root root 626K 1月  25 15:46 services
-rw-r--r-- 1 root root 125K 1月  25 15:47 services.zip
View Code

压缩目录

[root@A tmp]# cd /
[root@A /]# zip tmp.zip ./tmp/
  adding: tmp/ (stored 0%)
[root@A /]# zip -r tmp.zip ./tmp/
updating: tmp/ (stored 0%)
  adding: tmp/services.zip (stored 0%)
  adding: tmp/services (deflated 80%)
  adding: tmp/html/ (stored 0%)
  adding: tmp/html/10.html (stored 0%)
  adding: tmp/html/8.html (stored 0%)
  adding: tmp/html/5.html (stored 0%)
  adding: tmp/html/yhh/ (stored 0%)
  adding: tmp/html/yhh/test/ (stored 0%)
  adding: tmp/html/9.html (stored 0%)
  adding: tmp/html/1.html (stored 0%)
  adding: tmp/html/7.html (stored 0%)
  adding: tmp/html/2.html (stored 0%)
  adding: tmp/html/4.html (stored 0%)
  adding: tmp/html/3.html (stored 0%)
  adding: tmp/html/6.html (stored 0%)
View Code

排除压缩

[root@A /]# zip -r tmpl.zip ./tmp/ -x tmp/services.zip
  adding: tmp/ (stored 0%)
  adding: tmp/services (deflated 80%)
  adding: tmp/html/ (stored 0%)
  adding: tmp/html/10.html (stored 0%)
  adding: tmp/html/8.html (stored 0%)
  adding: tmp/html/5.html (stored 0%)
  adding: tmp/html/yhh/ (stored 0%)
  adding: tmp/html/yhh/test/ (stored 0%)
  adding: tmp/html/9.html (stored 0%)
  adding: tmp/html/1.html (stored 0%)
  adding: tmp/html/7.html (stored 0%)
  adding: tmp/html/2.html (stored 0%)
  adding: tmp/html/4.html (stored 0%)
  adding: tmp/html/3.html (stored 0%)
  adding: tmp/html/6.html (stored 0%)
  adding: tmp/.ICE-unix/ (stored 0%)
View Code

1.4 unzip:解压zip文件

【功能说明】

命令unzip可以解压缩zip命令或其他压缩软件压缩的zip格式的文件

【语法格式】

unzip [option] file[.zip] [file(s) ...]

unzip [选项] 文件[压缩文件 ...]

【选项说明】

参数选项

解释说明(带*的为重点)

-l

不压缩显示压缩包

-d

指定解压目录

-o

解压缩不提示是否覆盖文件

-v

解压时显示详细信息

命令参数unzip选项及说明

【使用范例】

查看压缩文件

[root@A /]# unzip -l tmp.zip
Archive:  tmp.zip
  Length      Date    Time    Name
---------  ---------- -----   ----
        0  01-25-2018 15:47   tmp/
   127362  01-25-2018 15:47   tmp/services.zip
   641020  01-25-2018 15:46   tmp/services
        0  01-24-2018 23:19   tmp/html/
        0  01-24-2018 23:19   tmp/html/10.html
        0  01-24-2018 23:19   tmp/html/8.html
        0  01-24-2018 23:19   tmp/html/5.html
        0  01-24-2018 23:19   tmp/html/yhh/
        0  01-24-2018 23:19   tmp/html/yhh/test/
        0  01-24-2018 23:19   tmp/html/9.html
        0  01-24-2018 23:19   tmp/html/1.html
        0  01-24-2018 23:19   tmp/html/7.html
        0  01-24-2018 23:19   tmp/html/2.html
        0  01-24-2018 23:19   tmp/html/4.html
        0  01-24-2018 23:19   tmp/html/3.html
        0  01-24-2018 23:19   tmp/html/6.html
        0  01-17-2018 20:26   tmp/.ICE-unix/
---------                     -------
   768382                     17 files
View Code

常规解压缩文件

[root@A /]# unzip tmp.zip
Archive:  tmp.zip
replace tmp/services.zip? [y]es, [n]o, [A]ll, [N]one, [r]ename: y
 extracting: tmp/services.zip        
replace tmp/html/5.html? [y]es, [n]o, [A]ll, [N]one, [r]ename: N
[root@A /]# 
[root@A /]# unzip -v tmp.zip
Archive:  tmp.zip
 Length   Method    Size  Cmpr    Date    Time   CRC-32   Name
--------  ------  ------- ---- ---------- ----- --------  ----
       0  Stored        0   0% 01-25-2018 15:47 00000000  tmp/
  127362  Stored   127362   0% 01-25-2018 15:47 e7eecb4b  tmp/services.zip
  641020  Defl:N   127196  80% 01-25-2018 15:46 33bd3343  tmp/services
       0  Stored        0   0% 01-24-2018 23:19 00000000  tmp/html/
       0  Stored        0   0% 01-24-2018 23:19 00000000  tmp/html/10.html
       0  Stored        0   0% 01-24-2018 23:19 00000000  tmp/html/8.html
       0  Stored        0   0% 01-24-2018 23:19 00000000  tmp/html/5.html
       0  Stored        0   0% 01-24-2018 23:19 00000000  tmp/html/yhh/
       0  Stored        0   0% 01-24-2018 23:19 00000000  tmp/html/yhh/test/
       0  Stored        0   0% 01-24-2018 23:19 00000000  tmp/html/9.html
       0  Stored        0   0% 01-24-2018 23:19 00000000  tmp/html/1.html
       0  Stored        0   0% 01-24-2018 23:19 00000000  tmp/html/7.html
       0  Stored        0   0% 01-24-2018 23:19 00000000  tmp/html/2.html
       0  Stored        0   0% 01-24-2018 23:19 00000000  tmp/html/4.html
       0  Stored        0   0% 01-24-2018 23:19 00000000  tmp/html/3.html
       0  Stored        0   0% 01-24-2018 23:19 00000000  tmp/html/6.html
       0  Stored        0   0% 01-17-2018 20:26 00000000  tmp/.ICE-unix/
--------          -------  ---                            -------
  768382           254558  67%                            17 files
View Code

指定解压目录解压文件

[root@A /]# unzip -d /tmp tmp.zip
Archive:  tmp.zip
   creating: /tmp/tmp/
 extracting: /tmp/tmp/services.zip   
  inflating: /tmp/tmp/services       
   creating: /tmp/tmp/html/
 extracting: /tmp/tmp/html/10.html   
 extracting: /tmp/tmp/html/8.html    
 extracting: /tmp/tmp/html/5.html    
   creating: /tmp/tmp/html/yhh/
   creating: /tmp/tmp/html/yhh/test/
 extracting: /tmp/tmp/html/9.html    
 extracting: /tmp/tmp/html/1.html    
 extracting: /tmp/tmp/html/7.html    
 extracting: /tmp/tmp/html/2.html    
 extracting: /tmp/tmp/html/4.html    
 extracting: /tmp/tmp/html/3.html    
 extracting: /tmp/tmp/html/6.html    
   creating: /tmp/tmp/.ICE-unix/
View Code

1.5 scp:远程文件复制

【功能说明】

命令scp用于在不同的主机之间复制文件,它采用SSH协议来保证复制的安全性,scp命令每次都是全量完整复制,因此效率不高,适合第一次复制时使用,增量复制建议使用rsync命令代替。

【语法格式】

scp  [option]   [[user@]host1:]file1      [[user@]host2:]file2

scp  [选项]  [[用户@]主机名:]文件名1     [[用户@]主机2:]文件名2

【选项说明】

参数选项

解释说明(带*的为重点)

-C

压缩传输

-l

指定传输占用的带宽,单位Kbit/s

-P port

大写的P,指定传输的端口号(*

-p

小写的p,传输后保留文件原始属性(*

-q

不显示传输进度条

-r

递归复制整个目录(*

命令scp的参数选项及说明

【使用范例】

推送文件或目录

[root@A /]# ll -h /etc/services
-rw-r--r--. 1 root root 626K 10月  2 2013 /etc/services
[root@A /]# scp /etc/services 10.16.50.242:/tmp
The authenticity of host '10.16.50.242 (10.16.50.242)' can't be established.
RSA key fingerprint is 11:11:97:0e:f1:96:60:d5:96:6a:73:50:16:65:dc:5f.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added '10.16.50.242' (RSA) to the list of known hosts.
root@10.16.50.242's password: 
services                                                                               100%  626KB 626.0KB/s   00:00    
[root@A /]# scp /data/test0 -p 10.16.50.242:/tmp
root@10.16.50.242's password: 
test0                                                                                  100%  168     0.2KB/s   00:00    
-p: No such file or directory
[root@A /]# scp -rp /data/  10.16.50.242:/tmp       
root@10.16.50.242's password: 
passwd.txt                                                                             100%  113     0.1KB/s   00:00    
test6                                                                                  100%   77     0.1KB/s   00:00    
yhh04                                                                                  100%    0     0.0KB/s   00:00    
yhh02                                                                                  100%    0     0.0KB/s   00:00    
file5.txt                                                                              100%    0     0.0KB/s   00:00    
yhh03                                                                                  100%    0     0.0KB/s   00:00    
yhh01                                                                                  100%    0     0.0KB/s   00:00    
yhh05                                                                                  100%    0     0.0KB/s   00:00    
yhh00                                                                                  100%    0     0.0KB/s   00:00    
test1                                                                                  100%  147     0.1KB/s   00:00    
file1.txt                                                                              100%    0     0.0KB/s   00:00    
yhh04                                                                                  100%    0     0.0KB/s   00:00    
yhh02                                                                                  100%    0     0.0KB/s   00:00    
yhh03                                                                                  100%    0     0.0KB/s   00:00    
yhh01                                                                                  100%    0     0.0KB/s   00:00    
yhh05                                                                                  100%    0     0.0KB/s   00:00    
yhh00                                                                                  100%    0     0.0KB/s   00:00    
mac.txt                                                                                100%  442     0.4KB/s   00:00    
yhh                                                                                    100%    0     0.0KB/s   00:00    
ip.txt                                                                                 100%   90     0.1KB/s   00:00    
test.txt                                                                               100%    6     0.0KB/s   00:00    
test8                                                                                  100%   10     0.0KB/s   00:00    
yhh20180124                                                                            100%    0     0.0KB/s   00:00    
file5.txt                                                                              100%    0     0.0KB/s   00:00    
file1.txt                                                                              100%    0     0.0KB/s   00:00    
test                                                                                   100%    4     0.0KB/s   00:00    
ls.txt                                                                                 100%   22     0.0KB/s   00:00    
test2                                                                                  100%   42     0.0KB/s   00:00    
test2.txt                                                                              100%   29     0.0KB/s   00:00    
test4                                                                                  100%  113     0.1KB/s   00:00    
test3.txt                                                                              100%  490     0.5KB/s   00:00    
test7                                                                                  100%   48     0.1KB/s   00:00    
()                                                                                     100%   15     0.0KB/s   00:00    
inittab                                                                                100%  884     0.9KB/s   00:00    
test9                                                                                  100%   12     0.0KB/s   00:00    
test5                                                                                  100%  120     0.1KB/s   00:00    
test0                                                                                  100%  168     0.2KB/s   00:00    
[root@A /]# 
View Code
[root@client ~]# ll -h /tmp/services
-rw-r--r-- 1 root root 626K 1月  26 10:45 /tmp/services
[root@client ~]# cd /tmp
[root@client tmp]# tree /tmp
/tmp
├── data
│   ├── ()
│   ├── dir1
│   │   ├── file1.txt
│   │   ├── ls.txt
│   │   └── test
│   ├── dir2
│   │   ├── file5.txt
│   │   ├── yhh00
│   │   ├── yhh01
│   │   ├── yhh02
│   │   ├── yhh03
│   │   ├── yhh04
│   │   └── yhh05
│   ├── dir3
│   │   ├── yhh00
│   │   ├── yhh01
│   │   ├── yhh02
│   │   ├── yhh03
│   │   ├── yhh04
│   │   └── yhh05
│   ├── file1.txt
│   ├── file5.txt
│   ├── inittab
│   ├── ip.txt
│   ├── mac.txt
│   ├── passwd.txt
│   ├── test0
│   ├── test1
│   ├── test2
│   ├── test2.txt
│   ├── test3.txt
│   ├── test4
│   ├── test5
│   ├── test6
│   ├── test7
│   ├── test8
│   ├── test9
│   ├── test.txt
│   ├── yhh
│   └── yhh20180124
├── services
└── test0

4 directories, 39 files
View Code

从远程服务器将数据复制到本地服务器(拉取)

[root@client yhh]# scp 10.16.50.243:/data/test0 .
root@10.16.50.243's password: 
test0                                                                                  100%  168     0.2KB/s   00:00    
[root@client yhh]# ls
test0  zengsiqing

1.6  rsync:文件同步工具

【功能说明】

命令rsync是一款开源的、快捷的、多功能的、可实现全量及增加量的本地货远程数据镜像同步备份的优秀工具。Rsync适用于unix/linux/windows等多种操作系统平台。

【语法格式】

1)、本地模式

Local:   rsync [OPTION...] SRC... [DEST]

Local:   rsync [选项...] 源文件... [目标文件]

2) 、通过远程shell访问模式

 Pull:     rsync         [OPTION...]         [USER@]HOST:SRC...             [DEST]
rsync     [选项...]             [用户@]主机:源文件...         [目标文件]
 Push:     rsync        [OPTION...]     SRC...             [USER@]HOST:DEST
         rsync        [选项...]         源文件...         [用户@]主机:目标文件

 3)、rsync守护进程模式

Pull: rsync     [OPTION...]         [USER@]HOST::SRC...             [DEST]
rsync     [选项...]        [用户@]主机::源文件...         [目标文件]
rsync     [OPTION...]     rsync://[USER@]HOST[:PORT]/SRC...     [DEST]
rsync     [选项...]         rsync://[用户@]主机[:端口]/源文件...     [目标文件]
Push: rsync     [OPTION...]     SRC...     [USER@]HOST::DEST
rsync     [选项...]          源文件...     [用户@]主机::目标文件
rsync     [OPTION...]     SRC...     rsync://[USER@]HOST[:PORT]/DEST
rsync     [选项...]          源文件...     rsync://[用户@]主机[:端口]/目标文件

 【选项说明】

命令rsync的参数选项及说明

参数选项

解释说明(带*的为重点)

-v,--verbose

详细模式输出,传输时的进度等信息

-z,--compress

传输时进行压缩以提高传输效率,--compress-level=NUM可按级别压缩(*

-a,--archive

以递归方式传输文件,并保持所有文件的属性,相当于-rtopgD1*

-r,--recursive

对子目录以递归模式,即目录下的所有都可以同样的模式传输,注意是小写r

-t,--times

保持文件的时间信息

-o,--owner

保持文件的属主信息

-p,--perms

保持文件的权限

-g,--group

保持文件的属组信息

-P,--progress

显示同步的过程及传输时的进度等信息

-D,--devices

保持设备文件信息

-l,--links

保留软链接

-e,--rsh=COMMAND

使用的信道协议,指定替代rshshell程序,例如:ssh

-n

测试选项,模拟执行

--exclued-from=FILE

指定排除不需要传输的文本模式(和tar参数一样)

--bwlimit=KBPS

限制传输速度

--delete

使目标目录内容和源保持目录一致,删除不同的文件

 【使用范例】

源地址带与不带斜线(/)的区别的例子

[root@A ~]# mkdir -p /data1/{test1,test2}    /data2
[root@A ~]# rsync -av /data1 /data2
sending incremental file list
data1/
data1/test1/
data1/test2/

sent 82 bytes  received 24 bytes  212.00 bytes/sec
total size is 0  speedup is 0.00
[root@A ~]# ll -h /data2
总用量 4.0K
drwxr-xr-x 4 root root 4.0K 1月  26 15:13 data1

[root@A ~]# mkdir /data3
[root@A ~]# rsync -av /data1/  /data3
sending incremental file list
./
test1/
test2/

sent 72 bytes  received 23 bytes  190.00 bytes/sec
total size is 0  speedup is 0.00
[root@A ~]# ls /data3
test1  test2
View Code

删除文件的特殊例子

[root@A ~]# mkdir /null
[root@A ~]# rsync -av --delete /null/ /tmp/
sending incremental file list
./
deleting tmp/html/yhh/test/
deleting tmp/html/yhh/
deleting tmp/html/9.html
deleting tmp/html/8.html
deleting tmp/html/7.html
deleting tmp/html/6.html
deleting tmp/html/5.html
deleting tmp/html/4.html
deleting tmp/html/3.html
deleting tmp/html/2.html
deleting tmp/html/10.html
deleting tmp/html/1.html
deleting tmp/html/
deleting tmp/.ICE-unix/
deleting tmp/services.zip
deleting tmp/services
deleting tmp/
deleting null/
deleting html/yhh/test/
deleting html/yhh/
deleting html/9.html
deleting html/8.html
deleting html/7.html
deleting html/6.html
deleting html/5.html
deleting html/4.html
deleting html/3.html
deleting html/2.html
deleting html/10.html
deleting html/1.html
deleting html/
deleting .ICE-unix/
deleting services.zip
deleting services

sent 29 bytes  received 15 bytes  88.00 bytes/sec
total size is 0  speedup is 0.00
View Code

拉取推送文件及目录(类似scp命令)

[root@A ~]# rsync -av 10.16.50.241:/root/server/scripts /tmp
The authenticity of host '10.16.50.241 (10.16.50.241)' can't be established.
RSA key fingerprint is 11:11:97:0e:f1:96:60:d5:96:6a:73:50:16:65:dc:5f.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added '10.16.50.241' (RSA) to the list of known hosts.
root@10.16.50.241's password: 
receiving incremental file list
scripts/
scripts/python1.py
scripts/python2.py

sent 53 bytes  received 239 bytes  44.92 bytes/sec
total size is 66  speedup is 0.23
[root@A ~]# ls /tmp
scripts
[root@A ~]# ls
aa               data3  eth0   file2  hard_link    install.log.syslog  test       test.txt     yanhuihuang1
anaconda-ks.cfg  dir1   eth1   file3  hello.txt    md5.log             test1      yan          yanhuihuang2
data             dir2   file1  g      install.log  soft_link           test2.txt  yanhuihuang  yhh
[root@A ~]# rsync -av /data/ 10.16.50.241:/tmp/
root@10.16.50.241's password: 
sending incremental file list
./
()
file1.txt
file5.txt
inittab
ip.txt
mac.txt
passwd.txt
test.txt
test0
test1
test2
test2.txt
test3.txt
test4
test5
test6
test7
test8
test9
yhh
yhh20180124
dir1/
dir1/file1.txt
dir1/ls.txt
dir1/test
dir2/
dir2/file5.txt
dir2/yhh00
dir2/yhh01
dir2/yhh02
dir2/yhh03
dir2/yhh04
dir2/yhh05
dir3/
dir3/yhh00
dir3/yhh01
dir3/yhh02
dir3/yhh03
dir3/yhh04
dir3/yhh05
sent 5038 bytes  received 730 bytes  1281.78 bytes/sec
total size is 2832  speedup is 0.49
View Code

利用SSH隧道模式(-e)拉取推送文件及目录

[root@A ~]# rsync -av -e 'ssh -p 22' /tmp  10.16.50.241:/tmp/
root@10.16.50.241's password: 
sending incremental file list
tmp/
tmp/test.txt
tmp/scripts/
tmp/scripts/python1.py
tmp/scripts/python2.py

sent 320 bytes  received 77 bytes  88.22 bytes/sec
total size is 66  speedup is 0.17

【经验技巧】

下面列出rsync命令的经验技巧以供读者参考

1、生产场景常用选项-avz,相当于-vzrtopg,但是在此处建议使用-avz选项,更简单了,脚本中可以省略-v选项。

2、关于z压缩选项的使用建议,如果为内网环境,且没有其他业务占用带宽,可以不使用z选项,不压缩传输,几乎可以满带宽传输(千M网络),压缩传输则网络发送速度就会骤降,压缩的速度赶不上传输的速度。

3、选项n是一个提高安全性的选项,它可以结合-v选项输出模拟的传输过程,如果没有错误,则可以去除n选项真正的传输文件。

1.7新手如何高效的提问

1、问问题前要有充分的准备,努力让自己问问题的水平更专业

2、想好你要问的内容,确定是否能表达清楚,可以先和小伙伴提前练习一下表达能力

3、如果口头表达不清楚,就写下来,给小伙伴看,采用适合自己的表达式进行沟通很重要

4、问问题时,把自己尝试过的解决方法也一并说出来,避免别人解答时走弯路。

5、问问题应礼貌客气,但要学会开门见山,及时抛出问题少

6、Linux问题错误日志及输出报错类问题尽量截图,若使用QQ发文字,也要注意避免将文字自动转换为表情,也不要自行翻译后描述,就保持原样给出错误描述。解答的人可能需要搜索才能帮到你,如果你提供的是截图,那么解答人如果很忙就会很忙就会很容易放弃帮你

7、不要吊死在一棵树上

8、最终解决完问题后,将解决思路整理成文档、无论别人是否帮到你,都要把答案发给你问过问题的人,学会感恩,未来的路才会越来越宽。

9、通过赞美,凸显重要性,满足对方心理需求方式。

10、多问封闭性问题,少问开放性问题,多为解答问题的人着想,是否能让对方省事。

原文地址:https://www.cnblogs.com/huihuangyan/p/13689502.html