Linux rsync命令排除文件/目录示例

时间:2020-03-05 15:28:39  来源:igfitidea点击:

Rsync(远程同步)是在Linux/Unix系统中远程以及本地复制和同步文件和目录的最常用命令。
借助rsync命令,我们可以跨目录,磁盘和网络跨目录远程和本地复制和同步数据,在两台Linux机器之间执行数据备份和镜像。

Rsync是服务器管理员最有用的工具之一,但是默认情况下它会同步所有内容,如果应用程序创建了很多临时文件,这可能会很烦人。
以下是使用rsync时如何排除文件的方法。

rsync命令的重要功能

  • 速度:第一次,rsync在源目录和目标目录之间复制整个内容。下次,rsync仅将更改的块或者字节传输到目标位置,这使传输非常快。
  • 安全性:rsync允许在传输过程中使用ssh协议对数据进行加密。
  • 低带宽:rsync分别在发送和接收端逐块地对数据进行压缩和解压缩。因此,与其他文件传输协议(例如FTP)相比,rsync所使用的带宽将始终更少。
  • 特权:安装和执行rsync不需要特殊特权。

rsync的基本语法非常简单,并且以类似于ssh,scp和cp的方式运行。

$sudo rsync options source destination
  • -v:详细。
  • -r:递归复制数据(但在传输数据时不保留时间戳和权限)。
  • -a:存档模式,存档模式允许递归复制文件,并且还保留符号链接,文件许可权,用户和组所有权以及时间戳。
  • -z:压缩文件数据。
  • -h:人类可读的,以人类可读的格式输出数字。
  • -d:传输目录而无需递归-e:将ssh指定为远程shell。

首先,创建如下所示的示例目录结构(带有一些空文件),该目录结构可用于测试目的。

$cd ~
  $sudo mkdir -p source/dir1/dir2
  $sudo mkdir -p source/dir3
  $sudo touch source/file1.txt
  $sudo touch source/file2.txt
  $sudo touch source/dir1/dir2/file3.txt
  $sudo touch source/dir3/file4.txt

上面的命令将创建具有以下结构的源目录(在主目录下),要查看源目录的结构,请在下面的命令中键入。

$sudo find source/
source
  - file1.txt
  - file2.txt
  - dir1
    - dir2
      - file3.txt
  - dir3
    - file4.txt

1)排除特定目录

首先,如果键入以下命令,则删除“目标”目录(如果存在)。

$sudo rm -rf destination

如果我们不想将'dir3'及其子目录从源同步到目标文件夹,请使用rsync'--exclude'选项,如下所示。

$sudo rsync -avz --exclude 'dir3' source/destination/

对于远程同步,我们可以在下面键入命令。

$sudo rsync -avz --exclude 'dir3' source/[email protected]:destination/
output
sending incremental file list
created directory destination
./
file1.txt
file2.txt
dir1/
dir1/dir2/
dir1/dir2/file3.txt
sent 307 bytes  received 126 bytes  866.00 bytes/sec
total size is 0  speedup is 0.00

2)排除特定文件

如果我们不想将“ file3.txt”从源同步到目标文件夹,请在下面输入命令。

$sudo rm -rf destination
$sudo rsync -avz --exclude 'dir1/dir2/file3.txt' source/destination/

对于远程同步,我们可以在下面键入命令。

$sudo rsync -avz --exclude 'dir1/dir2/file3.txt' source/[email protected]:destination/
output
sending incremental file list
created directory destination
./
file1.txt
file2.txt
dir1/
dir1/dir2/
dir3/
dir3/file4.txt
sent 338 bytes  received 130 bytes  936.00 bytes/sec
total size is 0  speedup is 0.00

例如,在实时情况下,我们希望将/var同步到备份目录,但又不想包含通常在两次重启之间不保存重要内容的缓存和tmp文件夹,则可以使用以下命令:

$sudo rsync -aAXhv --exclude={"/var/cache","/var/tmp"} /var [email protected]:destination/

3)排除特定的文件类型

为了排除具有特定扩展名的特定字符,我们使用适当的模式。
例如,要排除所有包含“ .txt”作为扩展名的文件,请在下面键入命令。

$sudo rm -rf destination
$sudo rsync -avz --exclude '*.txt' source/destination/

对于远程同步,我们可以在下面键入命令。

$sudo rsync -avz --exclude '*.txt' source/[email protected]:destination/
output
sending incremental file list
created directory destination
./
dir1/
dir1/dir2/
dir3/
sent 136 bytes  received 65 bytes  402.00 bytes/sec
total size is 0  speedup is 0.00

4)排除与模式匹配的多个目录

我们将排除“ source /”下以“ dir”开头的任何目录(或者子目录)

$sudo rm -rf destination
$sudo rsync -avz --exclude 'dir*' source/destination/

对于远程同步,我们可以在下面键入命令。

$$sudo rsync -avz --exclude 'dir*' source/[email protected]:destination/
output
sending incremental file list
created directory destination
./
file1.txt
file2.txt
sent 168 bytes  received 91 bytes  518.00 bytes/sec
total size is 0  speedup is 0.00

5)同时排除多个文件和目录

首先,创建一个文本文件,其中包含我们不想备份的所有文件和目录的列表。
这是我们要从rsync中排除的文件和目录的列表。

$sudo nano exclude-list.txt
file2.txt
  dir3/file4.txt

接下来,使用'--exclude-from'选项和'exclude-list.txt'执行rsync,如下所示。

$sudo rm -rf destination
$sudo rsync -avz --exclude-from 'exclude-list.txt' source/destination/

对于远程同步,我们可以在下面键入命令。

$sudo rsync -avz --exclude-from 'exclude-list.txt' source/[email protected]:destination/
output
sending incremental file list
created directory destination
./
file1.txt
dir1/
dir1/dir2/
dir1/dir2/file3.txt
dir3/
sent 277 bytes  received 111 bytes  776.00 bytes/sec
total size is 0  speedup is 0.00

6)排除路径始终是相对的

exclude选项似乎具有完整路径(即/dir3/file4.txt)。
但是,从rsync的角度来看,排除路径始终是相对的,它将被视为dir3/file4.txt。
在下面的示例中,rsync将在源目录下查找dir3(而不是在/根目录下)。

$sudo rm -rf destination
$sudo rsync -avz --exclude '/dir3/file4.txt' source/destination/

对于远程同步,我们可以在下面键入命令。

$sudo rsync -avz --exclude '/dir3/file4.txt' source/[email protected]:destination/
output
sending incremental file list
created directory destination
./
file1.txt
file2.txt
dir1/
dir1/dir2/
dir1/dir2/file3.txt
dir3/
sent 334 bytes  received 130 bytes  928.00 bytes/sec
total size is 0  speedup is 0.00

它类似于下面的命令。

$sudo rsync -avz --exclude 'dir3/file4.txt' source/destination/

7)从命令行排除文件

也可以直接从命令行排除文件,当要排除的文件数量较少时,这很有用。
例如,如果我们希望将/source同步到目标目录,但又不想包含file3.txt和file4.txt文件,则可以使用以下命令。

$sudo rm -rf destination
$sudo rsync -avz --exclude={"/dir1/dir2/file3.txt","/dir3/file4.txt"} source/destinaton/

对于远程同步,我们可以在下面键入命令。

$sudo rsync -avz --exclude={"/dir1/dir2/file3.txt","/dir3/file4.txt"} source/[email protected]:destination/
output
sending incremental file list
./
file1.txt
file2.txt
dir1/
dir1/dir2/
dir3/
sent 268 bytes  received 73 bytes  682.00 bytes/sec
total size is 0  speedup is 0.00

8)排除多个文件或者文件夹

当我们要排除文件“ * .txt”和特定文件夹“ dir3”的类型时,可以在下面键入命令。

$sudo rm -rf destination
$sudo rsync -avz --exclude '*.txt' --exclude 'dir3' source/destination/

对于远程同步,我们可以在下面键入命令。

$sudo rsync -avz --exclude '*.txt' --exclude 'dir3' source/[email protected]:destination/
output
sending incremental file list
created directory destination
./
dir1/
dir1/dir2/
sent 109 bytes  received 61 bytes  340.00 bytes/sec
total size is 0  speedup is 0.00

9)排除具有特定模式的多个目录

我们还可以提供多个具有特定模式的排除目录。
我们将使用通配符'*'来完成'dir'目录名。

$sudo rm -rf destination
$sudo rsync -avz --exclude 'dir*' source/destination/

对于远程同步,我们可以在下面键入命令。

$sudo rsync -avz --exclude 'dir*' source/[email protected]:destination/
output
sending incremental file list
created directory destination
./
file1.txt
file2.txt
sent 168 bytes  received 91 bytes  518.00 bytes/sec
total size is 0  speedup is 0.00

10)排除某些大小的文件

在备份过程中,我们遇到了一些大型文件,这些文件确实花费了很长时间才能复制。
我们必须复制一些大小超过1GB的电影。
因此,我们认为最好是简单地排除大文件或者任何不重要的文件,以便尽快完成备份。

例如,我们要排除大于3 MB的文件。
我们要做的就是在rsync命令中使用'--max-size = SIZE'选项。
此选项将不传输任何大于指定大小的文件。

$sudo rm -rf destination
$sudo rsync -avz --max-size=3m source/destination/

对于远程同步,我们可以在下面键入命令。

$sudo rsync -avz --max-size=3m source/[email protected]:destination/
output
sending incremental file list
created directory destination
./
file1.txt
file2.txt
dir1/
dir1/dir2/
dir1/dir2/Bootstrap-tutorial.pdf
dir1/dir2/file3.txt
dir3/
dir3/file4.txt
sent 3,080,297 bytes  received 168 bytes  6,160,930.00 bytes/sec
total size is 8,101,480  speedup is 2.63

我们还可以使用'--min-size = SIZE'排除小于指定大小的任何文件。

例如,要排除小于7 MB的文件,请运行以下命令。

$sudo rsync -avz --min-size=7m source/destination/
output
sending incremental file list
created directory destination
./
dir1/
dir1/dir2/
dir3/
sent 313 bytes  received 65 bytes  756.00 bytes/sec
total size is 8,101,480  speedup is 21,432.49

大小的后缀如下:

  • “ K”(或者“ KiB”)是一个千字节(1024)
  • “ M”(或者“ MiB”)是一个兆字节(1024 * 1024)
  • “ G”(或者“ GiB”)是千兆字节(1024 * 1024 * 1024)

如果希望乘数为1000而不是1024,请使用“ KB”,“ MB”或者“ GB”(注意:所有值也接受小写字母)。