Linux10-归档、系统间复制文件

目录

一、tar命令

二、scp、sftp命令

三、rsync命令


一、tar命令

tar命令可以归档文件、目录,提取创建的归档文件,同时进行压缩解压缩。使用tar选项时不需要加-,下面是常用的tar选项。

  • c,创建一个存档。
  • x,提取一个存档。
  • t,列出存档内容,但是不提取(解压缩)存档。
  • v,显示创建、提取的详细信息,文件有哪些。
  • f,指定创建、提取的存档文件名,后面紧跟文件名,通常放在最后一个选项。
  • z,使用gzip压缩(.tar.gz)。
  • j,使用bzip2压缩(.tar.bz2),压缩率通常比gzip高。
  • J,使用xz压缩(.tar.xz),压缩率通常比bzip2高。

在创建tar归档之前,必须先验证目录中没有其他存档与要创建的存档名称相同,tar命令将覆盖现有的存档,没有警告提示。使用绝对路径名归档文件时,将默认从文件名中删除该路径前面的/符号。这样有助于边可能造成重要文件被覆盖的错误。分别创建/etc目录的存档文件etc_backup.tar.gz、etc_backup.tar.bz2、etc_backup.tar.xz,比较一下三个方式的压缩率。

[root@server0 ~]# ll
total 12
-rw-------. 1 root root 8619 May  7  2014 anaconda-ks.cfg

[root@server0 ~]# tar czf etc_backup.tar.gz /etc/
tar: Removing leading `/' from member names
[root@server0 ~]# tar cjf etc_backup.tar.bz2 /etc/
tar: Removing leading `/' from member names
[root@server0 ~]# tar cJf etc_backup.tar.xz /etc/
tar: Removing leading `/' from member names

[root@server0 ~]# ll -h
total 21M
-rw-------. 1 root root 8.5K May  7  2014 anaconda-ks.cfg
-rw-r--r--. 1 root root 7.0M Feb 15 13:26 etc_backup.tar.bz2
-rw-r--r--. 1 root root 8.4M Feb 15 13:26 etc_backup.tar.gz
-rw-r--r--. 1 root root 5.7M Feb 15 13:27 etc_backup.tar.xz

再分别将三个归档文件提取到/tmp/gz、/tmp/bz2、/tmp/xz目录。

[root@server0 ~]# cd /tmp/
[root@server0 tmp]# mkdir gz bz2 xz

[root@server0 tmp]# cd gz/
[root@server0 gz]# tar xzf /root/etc_backup.tar.gz
[root@server0 gz]# ll
total 12
drwxr-xr-x. 133 root root 8192 Feb 15 13:24 etc


[root@server0 gz]# cd ../bz2
[root@server0 bz2]# tar xjf /root/etc_backup.tar.bz2
[root@server0 bz2]# ll
total 12
drwxr-xr-x. 133 root root 8192 Feb 15 13:24 etc

[root@server0 bz2]# cd ../xz/
[root@server0 xz]# tar xJf /root/etc_backup.tar.xz
[root@server0 xz]# ll
total 12
drwxr-xr-x. 133 root root 8192 Feb 15 13:24 etc

二、scp、sftp命令

scp命令的格式是 scp <local_path>  [user@]<remote_host>:<remote_path> 和 scp [user@]<remote_host>:<remote_path> <local_path>,分别是从本地向远程主机拷贝文件、从远程主机向本地拷贝文件。其中[user@]是可选的,以[user]身份访问远程主机,如果没有指定那么就以本地登录用户访问。

我们将server上/root/etc_backup.tar.gz、/root/etc_backup.tar.bz2、/root/etc_backup.tar.xz三个文件拷贝到desktop上student账户的/home/student目录中.

[root@server0 ~]# ll
total 21468
-rw-------. 1 root root    8619 May  7  2014 anaconda-ks.cfg
-rw-r--r--. 1 root root 7309341 Feb 15 13:26 etc_backup.tar.bz2
-rw-r--r--. 1 root root 8742008 Feb 15 13:26 etc_backup.tar.gz
-rw-r--r--. 1 root root 5911136 Feb 15 13:27 etc_backup.tar.xz
[root@server0 ~]#
[root@server0 ~]# scp etc_backup.tar.bz2 etc_backup.tar.gz etc_backup.tar.xz desktop0:/home/student/
The authenticity of host 'desktop0 (172.25.0.10)' can't be established.
ECDSA key fingerprint is 65:4d:ac:8a:c9:58:82:b5:0c:91:c4:ef:a5:e6:f6:65.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added 'desktop0,172.25.0.10' (ECDSA) to the list of known hosts.
root@desktop0's password:
etc_backup.tar.bz2                                                                                                100% 7138KB   7.0MB/s   00:01
etc_backup.tar.gz                                                                                                 100% 8537KB   8.3MB/s   00:00
etc_backup.tar.xz                                                                                                 100% 5773KB   5.6MB/s   00:00
[root@server0 ~]#
[root@server0 ~]# ssh student@desktop0 ls
student@desktop0's password:
etc_backup.tar.bz2
etc_backup.tar.gz
etc_backup.tar.xz

我们将desktop上/var/log目录以递归方式-r拷贝到本地/tmp目录中。

[root@server0 ~]# scp -r desktop0:/var/log /tmp
root@desktop0's password:
tallylog                                                                                                          100%    0     0.0KB/s   00:00
lastlog                                                                                                           100%  285KB 285.4KB/s   00:00
wtmp                                                                                                              100% 9216     9.0KB/s   00:00
btmp                                                                                                              100% 1152     1.1KB/s   00:00
tuned.log                                                                                                         100% 4403     4.3KB/s   00:00
messages                                                                                                          100%  311KB 311.1KB/s   00:00
secure                                                                                                            100%   15KB  15.3KB/s   00:00
maillog                                                                                                           100%  592     0.6KB/s   00:00
spooler                                                                                                           100%    0     0.0KB/s   00:00
ovirt-guest-agent.log                                                                                             100% 3234     3.2KB/s   00:00
rhsm.log                                                                                                          100%  892     0.9KB/s   00:00
audit.log                                                                                                         100%  379KB 379.4KB/s   00:00
cron                                                                                                              100%  632     0.6KB/s   00:00
anaconda.log                                                                                                      100% 9289     9.1KB/s   00:00
syslog                                                                                                            100%   65KB  65.0KB/s   00:00
anaconda.xlog                                                                                                     100%   16KB  15.5KB/s   00:00
anaconda.program.log                                                                                              100%   29KB  28.6KB/s   00:00
anaconda.packaging.log                                                                                            100%   98KB  98.5KB/s   00:00
anaconda.storage.log                                                                                              100%   47KB  47.0KB/s   00:00
anaconda.ifcfg.log                                                                                                100% 1028     1.0KB/s   00:00
ks-script-V3Y2pV.log                                                                                              100% 4460     4.4KB/s   00:00
ks-script-bYGfjD.log                                                                                              100%    0     0.0KB/s   00:00
ks-script-l8BPxv.log                                                                                              100%    0     0.0KB/s   00:00
cloud-init.log                                                                                                    100%  451KB 451.4KB/s   00:00
yum.log                                                                                                           100%   53KB  52.6KB/s   00:00
:0.log.1                                                                                                          100%   12KB  12.0KB/s   00:00
:0.log                                                                                                            100%   12KB  11.7KB/s   00:00
sa04                                                                                                              100%  656     0.6KB/s   00:00
sa15                                                                                                              100% 3056     3.0KB/s   00:00
dmesg.old                                                                                                         100%   32KB  32.3KB/s   00:00
boot.log                                                                                                          100% 5468     5.3KB/s   00:00
dmesg                                                                                                             100%   33KB  33.1KB/s   00:00
Xorg.0.log.old                                                                                                    100%   17KB  17.4KB/s   00:00
Xorg.0.log                                                                                                        100%   17KB  17.0KB/s   00:00
pm-powersave.log                                                                                                  100%    0     0.0KB/s   00:00
[root@server0 ~]# ll /tmp/
total 4
drwxr-xr-x.  3 root root   16 Feb 15 13:35 bz2
drwxr-xr-x.  3 root root   16 Feb 15 13:35 gz
drwxr-xr-x. 17 root root 4096 Feb 15 14:23 log
drwx------.  3 root root   16 Feb 15 13:24 systemd-private-kgiu1u
drwx------.  3 root root   16 Feb 15 13:24 systemd-private-lFP1bX
drwxr-xr-x.  3 root root   16 Feb 15 13:36 xz

sftp命令,通过ssh方式登录到远程主机 直接操作文件上传下载。登录以后,用ls、mkdir、cd等操作远程主机,用lls、lmkdir、lcd等操作本地主机,用put、get上传下载文件。

三、rsync命令

rsync命令是系统间安全复制文件的一种方式。和scp全复制不同,rsync只需要增量复制(复制系统之间差异),rsync更适合于两个相似目录之间进行同步。也就是说,rsync第一次是全备份,以后是增量备份。如果源头有文件被删除了,也可以指定--delete选项来进行差异备份。

rsync命令的选项

  • -n,空运行。空运行是对真正执行命令时发生情况的模拟。在任何rsync操作前都应先执行空运行,确保重要文件不会被覆盖或删除。
  • -v,在同步文件时向输出中添加详细信息,可以查看rsync模拟的具体情况。
  • -a,存档模式。一次性启用以下选项。
  • -P,断点续传。
  • --delete,差异备份,源文件删除,那么同步时目的文件也删除。

  • -r,地柜方式同步整个目录树。
  • -l,同步符号链接。
  • -p,保留权限。
  • -t,保留时间戳。
  • -g,保留组所有权。
  • -o,保留文件所有者。
  • -D,同步设备文件。

因此,同步文件常用的方式有这样几种。

  1. rsync -avPn <src> <dst> 同步src到dst,空运行,模拟运行。
  2. rsync -avP <src> <dst> 同步src到dst,只增量同步。
  3. rsync -avP --delete <src> <dst> 同步src到dst,差异同步。有增量会同步,src有文件删除那么dst也删除相应文件。

尝试一下,看看效果。

[root@server0 ~]# tree src
src
├── link -> /root/src/wp/
├── t1
├── t2
├── t3
├── t4
└── wp
    ├── wa
    ├── wb
    ├── wc
    └── wd

2 directories, 8 files

[root@server0 ~]# tree dst/
dst/

0 directories, 0 files

[root@server0 ~]#

我们先模拟同步一次,看到这个过程会发生什么,但是dst目录并没有什么变化。

[root@server0 ~]# rsync -avPn src/ dst/
sending incremental file list
./
link -> /root/src/wp/
t1
t2
t3
t4
wp/
wp/wa
wp/wb
wp/wc
wp/wd

sent 184 bytes  received 46 bytes  460.00 bytes/sec
total size is 13  speedup is 0.06 (DRY RUN)

[root@server0 ~]# tree dst/
dst/

0 directories, 0 files

同步一下。

[root@server0 ~]# rsync -avP src/ dst/
sending incremental file list
./
link -> /root/src/wp/
t1
           0 100%    0.00kB/s    0:00:00 (xfer#1, to-check=8/11)
t2
           0 100%    0.00kB/s    0:00:00 (xfer#2, to-check=7/11)
t3
           0 100%    0.00kB/s    0:00:00 (xfer#3, to-check=6/11)
t4
           0 100%    0.00kB/s    0:00:00 (xfer#4, to-check=5/11)
wp/
wp/wa
           0 100%    0.00kB/s    0:00:00 (xfer#5, to-check=3/11)
wp/wb
           0 100%    0.00kB/s    0:00:00 (xfer#6, to-check=2/11)
wp/wc
           0 100%    0.00kB/s    0:00:00 (xfer#7, to-check=1/11)
wp/wd
           0 100%    0.00kB/s    0:00:00 (xfer#8, to-check=0/11)

sent 472 bytes  received 174 bytes  1292.00 bytes/sec
total size is 13  speedup is 0.02
[root@server0 ~]# tree dst/
dst/
├── link -> /root/src/wp/
├── t1
├── t2
├── t3
├── t4
└── wp
    ├── wa
    ├── wb
    ├── wc
    └── wd

2 directories, 8 files

增量同步一下。

[root@server0 ~]# touch src/t{11..14}
[root@server0 ~]# tree src/
src/
├── link -> /root/src/wp/
├── t1
├── t11
├── t12
├── t13
├── t14
├── t2
├── t3
├── t4
└── wp
    ├── wa
    ├── wb
    ├── wc
    └── wd

2 directories, 12 files
[root@server0 ~]# tree dst/
dst/
├── link -> /root/src/wp/
├── t1
├── t2
├── t3
├── t4
└── wp
    ├── wa
    ├── wb
    ├── wc
    └── wd

2 directories, 8 files
[root@server0 ~]# rsync -avP src/ dst/
sending incremental file list
./
t11
           0 100%    0.00kB/s    0:00:00 (xfer#1, to-check=11/15)
t12
           0 100%    0.00kB/s    0:00:00 (xfer#2, to-check=10/15)
t13
           0 100%    0.00kB/s    0:00:00 (xfer#3, to-check=9/15)
t14
           0 100%    0.00kB/s    0:00:00 (xfer#4, to-check=8/15)

sent 343 bytes  received 92 bytes  870.00 bytes/sec
total size is 13  speedup is 0.03
[root@server0 ~]# tree dst/
dst/
├── link -> /root/src/wp/
├── t1
├── t11
├── t12
├── t13
├── t14
├── t2
├── t3
├── t4
└── wp
    ├── wa
    ├── wb
    ├── wc
    └── wd

2 directories, 12 files

差异同步一下。 我们看到,被删除的t2、t11、t13也被同步删除,新增的g1、g2、g3也同步到dst目录。

[root@server0 ~]# rm src/t2 src/t{11,13} -f
[root@server0 ~]# touch src/g{1..3}

[root@server0 ~]# tree src/
src/
├── g1
├── g2
├── g3
├── link -> /root/src/wp/
├── t1
├── t12
├── t14
├── t3
├── t4
└── wp
    ├── wa
    ├── wb
    ├── wc
    └── wd

2 directories, 12 files

[root@server0 ~]# tree dst/
dst/
├── link -> /root/src/wp/
├── t1
├── t11
├── t12
├── t13
├── t14
├── t2
├── t3
├── t4
└── wp
    ├── wa
    ├── wb
    ├── wc
    └── wd

2 directories, 12 files

[root@server0 ~]# rsync -avP --delete src/ dst/
sending incremental file list
./
deleting t2
deleting t13
deleting t11
g1
           0 100%    0.00kB/s    0:00:00 (xfer#1, to-check=13/15)
g2
           0 100%    0.00kB/s    0:00:00 (xfer#2, to-check=12/15)
g3
           0 100%    0.00kB/s    0:00:00 (xfer#3, to-check=11/15)

sent 308 bytes  received 73 bytes  762.00 bytes/sec
total size is 13  speedup is 0.03

[root@server0 ~]# tree dst/
dst/
├── g1
├── g2
├── g3
├── link -> /root/src/wp/
├── t1
├── t12
├── t14
├── t3
├── t4
└── wp
    ├── wa
    ├── wb
    ├── wc
    └── wd

2 directories, 12 files

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

苦行僧(csdn)

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值