同步stands for remote sync.
同步is used to perform the backup operation in UNIX / Linux.
同步实用程序用于有效地将文件和目录从一个位置同步到另一个位置。备份位置可以在本地服务器上,也可以在远程服务器上。
Important features of 同步
- 速度:第一次,rsync在源目录和目标目录之间复制整个内容。下次,rsync仅将更改的块或字节传输到目标位置,这使传输非常快。
- 安全: 同步allows encryption of data using ssh protocol during transfer.
- 较少带宽:rsync分别在发送和接收端逐块使用数据压缩和解压缩。因此,与其他文件传输协议相比,rsync使用的带宽将始终更少。
- 礼遇: No special privileges are required to install and execute 同步
句法
$ 同步options source destination
源和目标可以是本地或远程的。如果是远程服务器,请指定登录名,远程服务器名称和位置。
示例1.同步本地服务器中的两个目录
To sync two directories in a local computer, use the following 同步-zvr command.
$ 同步-zvr /var/opt/installation/inventory/ /root/temp building file list ... done sva.xml svB.xml . sent 26385 通过tes received 1098 通过tes 54966.00 通过tes/sec total size is 44867 speedup is 1.63 $
In the above 同步example:
- -z用于启用压缩
- -v详细
- -r表示递归
现在,让我们看一下从源复制到目标的文件之一的时间戳。如下所示,rsync没有’t在同步期间保留时间戳。
$ ls -l /var/opt/installation/inventory/sva.xml /root/temp/sva.xml -r--r--r-- 1 bin bin 949 Jun 18 2009 /var/opt/installation/inventory/sva.xml -r--r--r-- 1 root bin 949 Sep 2 2009 /root/temp/sva.xml
Example 2. Preserve timestamps during Sync using 同步-a
同步option -a indicates archive mode. -a option does the following,
- 递归模式
- 保留符号链接
- 保留权限
- 保留时间戳
- 保留所有者和组
现在,执行示例1中提供的相同命令(但带有rsync选项-a),如下所示:
$ 同步-azv /var/opt/installation/inventory/ /root/temp/ building file list ... done ./ sva.xml svB.xml . sent 26499 通过tes received 1104 通过tes 55206.00 通过tes/sec total size is 44867 speedup is 1.63 $
As you see below, 同步preserved timestamps during sync.
$ ls -l /var/opt/installation/inventory/sva.xml /root/temp/sva.xml -r--r--r-- 1 root bin 949 Jun 18 2009 /var/opt/installation/inventory/sva.xml -r--r--r-- 1 root bin 949 Jun 18 2009 /root/temp/sva.xml
例子3.只同步一个文件
To copy 上ly 上e file, specify the file name to 同步command, as shown below.
$ 同步-v /var/lib/rpm/Pubkeys /root/temp/ Pubkeys sent 42 通过tes received 12380 通过tes 3549.14 通过tes/sec total size is 12288 speedup is 0.99
例子4.从本地到远程同步文件
同步允许您在本地和远程系统之间同步文件/目录。
$ 同步-avz /root/temp/ [email protected]:/home/thegeekstuff/temp/ Password: building file list ... done ./ rpm/ rpm/Basenames rpm/Conflictname sent 15810261 通过tes received 412 通过tes 2432411.23 通过tes/sec total size is 45305958 speedup is 2.87
与远程服务器同步时,需要指定远程服务器的用户名和ip地址。您还应该在远程服务器上指定目标目录。格式为username @ machinename:path
如您在上面看到的,它在执行从本地到远程服务器的rsync时要求输入密码。
有时候你不’要在将文件从本地服务器备份到远程服务器时输入密码。例如,如果您有一个备份Shell脚本,该脚本使用rsync将文件从本地服务器复制到远程服务器,则您需要能够进行rsync,而无需输入密码。
为此,请设置 SSH密码少登录 正如我们之前所解释的。
例子5.从远程到本地同步文件
要将文件从远程同步到本地时,如下所示,在源中指定远程路径,在目标中指定本地路径。
$ 同步-avz [email protected]:/var/lib/rpm /root/temp Password: receiving file list ... done rpm/ rpm/Basenames . sent 406 通过tes received 15810230 通过tes 2432405.54 通过tes/sec total size is 45305958 speedup is 2.87
例子6.同步的远程shell
同步允许您指定要使用的远程shell。您可以使用rsync ssh启用安全的远程连接。
使用rsync -e ssh指定要使用的远程Shell。在这种情况下,rsync将使用ssh。
$ 同步-avz -e ssh [email protected]:/var/lib/rpm /root/temp Password: receiving file list ... done rpm/ rpm/Basenames sent 406 通过tes received 15810230 通过tes 2432405.54 通过tes/sec total size is 45305958 speedup is 2.87
例子7.不要覆盖目的地的修改文件
在典型的同步情况下,如果在目标位置修改了文件,我们可能不想用源中的旧文件覆盖文件。
使用rsync -u选项可以做到这一点。 (即,如果文件被修改,请不要覆盖目标文件)。在下面的示例中,名为Basenames的文件已在目标位置被修改。因此,它不会被rsync -u覆盖。
$ ls -l /root/temp/Basenames total 39088 -rwxr-xr-x 1 root root 4096 Sep 2 11:35 Basenames $ 同步-avzu [email protected]:/var/lib/rpm /root/temp Password: receiving file list ... done rpm/ sent 122 通过tes received 505 通过tes 114.00 通过tes/sec total size is 45305958 speedup is 72258.31 $ ls -lrt total 39088 -rwxr-xr-x 1 root root 4096 Sep 2 11:35 Basenames
示例8.仅同步目录树结构(不同步文件)
使用rsync -d选项仅将目录树从源同步到目标。下面的示例以递归方式仅同步目录树,而不同步目录中的文件。
$ 同步-v -d [email protected]:/var/lib/ . Password: receiving file list ... done logrotate.status CAM/ YaST2/ acpi/ sent 240 通过tes received 1830 通过tes 318.46 通过tes/sec total size is 956 speedup is 0.46
Example 9. View the 同步Progress during Transfer
当使用rsync进行备份时,您可能想知道备份的进度。即要复制多少个文件,以什么速率复制文件等。
同步–progress option displays detailed progress of 同步execution as shown below.
$ 同步-avz --progress [email protected]:/var/lib/rpm/ /root/temp/ Password: receiving file list ... 19 files to consider ./ Basenames 5357568 100% 14.98MB/s 0:00:00 (xfer#1, to-check=17/19) Conflictname 12288 100% 35.09kB/s 0:00:00 (xfer#2, to-check=16/19) . . . sent 406 通过tes received 15810211 通过tes 2108082.27 通过tes/sec total size is 45305958 speedup is 2.87
You can also use rsnapshot utility (that uses 同步) to 备份本地linux服务器, 要么 备份远程Linux服务器.
例子10.删除在目标上创建的文件
如果文件不在源中而是在目标中存在,则您可能希望在rsync期间删除目标上的文件。
在这种情况下,请使用–删除选项,如下所示。 同步delete选项删除源目录中不存在的文件。
# Source and target are in sync. Now creating new file at the target. $ > new-file.txt $ 同步-avz --delete [email protected]:/var/lib/rpm/ . Password: receiving file list ... done deleting new-file.txt ./ sent 26 通过tes received 390 通过tes 48.94 通过tes/sec total size is 45305958 speedup is 108908.55
与目标同步时,目标具有名为new-file.txt的新文件。–删除选项,它删除了文件new-file.txt
例子11.不要在目标上创建新文件
如果愿意,您可以仅更新(同步)目标上的现有文件。如果源中有新文件,而目标中没有这些文件,则可以避免在目标上创建这些新文件。如果需要此功能,请使用–rsync命令的现有选项。
首先,在源代码处添加一个new-file.txt。
[/var/lib/rpm ]$ > new-file.txt
Next, execute the 同步from the target.
$ 同步-avz --existing [email protected]:/var/lib/rpm/ . [email protected]'s password: receiving file list ... done ./ sent 26 通过tes received 419 通过tes 46.84 通过tes/sec total size is 88551424 speedup is 198991.96
如果看到上面的输出,它没有’收到新文件new-file.txt
例子12.查看源和目的地之间的变化
此选项对于查看源和目标之间文件或目录的差异很有用。
从源头上:
$ ls -l /var/lib/rpm -rw-r--r-- 1 root root 5357568 2010-06-24 08:57 Basenames -rw-r--r-- 1 root root 12288 2008-05-28 22:03 Conflictname -rw-r--r-- 1 root root 1179648 2010-06-24 08:57 Dirnames
在目的地:
$ ls -l /root/temp -rw-r--r-- 1 root root 12288 May 28 2008年 Conflictname -rw-r--r-- 1 bin bin 1179648 Jun 24 05:27 Dirnames -rw-r--r-- 1 root root 0 Sep 3 06:39 Basenames
在上面的示例中,源和目标之间存在两个差异。首先,文件Dirname的所有者和组不同。接下来,文件Basenames的大小不同。
现在让我们看看rsync如何显示这种差异。 -i选项显示项目更改。
$ 同步-avzi [email protected]:/var/lib/rpm/ /root/temp/ Password: receiving file list ... done >f.st.... Basenames .f....og. Dirnames sent 48 通过tes received 2182544 通过tes 291012.27 通过tes/sec total size is 45305958 speedup is 20.76
在输出中,它在文件名或目录名之前显示大约9个字母,指示更改。
在我们的示例中,基本名称(和别名)前面的字母表示以下内容:
> specifies that a file is being transferred to the local host. f represents that it is a file. s represents size changes are there. t represents timestamp changes are there. o owner changed g group changed.
例子13.文件传输期间的包含和排除模式
同步允许您在进行同步时提供要包括和排除文件或目录的模式。
$ 同步-avz --include 'P*' --exclude '*' [email protected]:/var/lib/rpm/ /root/temp/ Password: receiving file list ... done ./ Packages Providename Provideversion Pubkeys sent 129 通过tes received 10286798 通过tes 2285983.78 通过tes/sec total size is 32768000 speedup is 3.19
在上面的示例中,它仅包含以开头的文件或目录‘P’(使用rsync include),并排除所有其他文件。 (使用rsync排除‘*’ )
例子14.不传输大文件
您可以使用rsync告诉rsync不要传输大于特定大小的文件–max-size option.
$ 同步-avz --max-size='100K' [email protected]:/var/lib/rpm/ /root/temp/ Password: receiving file list ... done ./ Conflictname Group Installtid Name Sha1header Sigmd5 Triggername sent 252 通过tes received 123081 通过tes 18974.31 通过tes/sec total size is 45305958 speedup is 367.35
max-size = 100K使rsync仅传输小于或等于100K的文件。您可以将M表示为兆字节,将G表示为千兆字节。
例子15.传输整个文件
同步的主要功能之一是它仅将更改的块传输到目标,而不是发送整个文件。
如果网络带宽不是您要考虑的问题(而CPU是),则可以使用rsync -W选项传输整个文件。因为这样做可以加快rsync的进程’不必在源和目标上执行校验和。
# 同步-avzW [email protected]:/var/lib/rpm/ /root/temp Password: receiving file list ... done ./ Basenames Conflictname Dirnames Filemd5s Group Installtid Name sent 406 通过tes received 15810211 通过tes 2874657.64 通过tes/sec total size is 45305958 speedup is 2.87
Additional 同步Tutorials
- 如何在UNIX / Linux上使用SSH设置Rsync(不带密码的rsync)
- 6 同步Examples to Exclude Multiple Files and Directories using exclude-from
如果您喜欢这篇文章,您可能还会喜欢..
![]() |
![]() |
![]() |
![]() |
我最近发现在其中包含许多自定义程序包的系统中进行备份(全新安装)的一个不错的选择,我们可以使用“Ghost for linux”。这可用于对整个系统(包括定制的软件包和OS)进行冷备份,该备份可用于在不同服务器中重新部署相同的设置,或者在系统崩溃时在同一服务器中使用。
这免除了从一开始就重新安装OS和软件包的麻烦。