和ssh一起使用rsync

到目前为止,在所有前面的示例中,每次连接到远程suse计算机时,我们都必须提供凭据(密码)。可以创建所谓的公共密钥,然后将其复制到远程服务器。然后,这将使我们无需提供密码即可登录。为此,我们必须执行以下操作:

在本地计算机(Ubuntu)上定义ssh密钥:

这是通过执行ssh-keygen命令来实现的。

在下面的示例中,我们没有提供任何密码,我们只按了enter

john@john-desktop:~/.ssh$ ssh-keygen -t rsa
Generating public/private rsa key pair.
Enter file in which to save the key (/home/john/.ssh/id_rsa): 
Enter passphrase (empty for no passphrase): 
Enter same passphrase again: 
Your identification has been saved in /home/john/.ssh/id_rsa.
Your public key has been saved in /home/john/.ssh/id_rsa.pub.
The key fingerprint is:
d7:f5:6e:20:9b:fe:29:51:f3:d4:2f:bb:6c:c5:9a:31 john@john-desktop
The keys randomart image is:
+--[ RSA 2048]----+
|                 |
|                 |
|              . .|
|           . .o.o|
|        S . o..=o|
|         .  .+Eo=|
|            o. Oo|
|           ...=o |
|            .+=. |
+-----------------+

从上面可以看到,我们在以下位置生成了称为id_rsa.pub公钥:/home/john/.ssh

接下来,我们需要复制密钥id_rsa的内容.pub`到远程计算机。

将公钥复制到远程位置

然后,我们将本地服务器/home/john/.ssh/id_rsa.pub中公钥的内容复制到远程suse计算机上的authorized_keys`文件中,要实现此目的,我们只需执行以下命令。命令你会被提示输入密码授权远程服务器上此活动

john@john-desktop:~/ubuntu$ cat $HOME/.ssh/id_rsa.pub | ssh 192.168.0.11 'cat >> .ssh/authorized_keys && echo "Contents Copied Successfully"'
Password: 
Contents Copied Successfully

现在让我们确认文件已复制到远程suse计算机:

john@linux-pd5y:~/.ssh> ls -l
total 4
-rw-r--r-- 1 john users 399 Apr 25 09:34 authorized_keys
john@linux-pd5y:~/.ssh> cat authorized_keys
ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQCpqMCtzPvRZD+pfnO8gHoJrtXF+JjOji/GXTK5FeioFHyZMzvT4CTXqAQIFRIkq1fIoZkMz8LYmVqBPP/vfS7SbTL81dR3Mpri6YzngjGka1ejH6+34PVgZT47eBi65kZddGWOpfpBYj/1x+7aNwvZTRwx6TLZcq0NQ5Ot8ZTJ/yTXJfRAaYKlLRN4cu6JZ4HYqVC0kBoVSGzeBdCmA1Yoa3ecWqGuAKXskb8sseRH2es6/7G+C3Wa205TUYf42DoUyD6X3BXhs0WvvsqXKrKOx+ZvUBgq0EsH58lL4tKu2aaKamg5mR8B6WUJZzlAkksaBukAW9sJqL6RxG4zJpNx john@john-desktop

现在,在远程计算机上,我们需要使用chmod命令修改新创建的authorized_keys文件的权限,以便只有文件所有者才能查看其内容:

john@linux-pd5y:~/.ssh> chmod 600 authorized_keys
john@linux-pd5y:~/.ssh> ls -l authorized_keys 
-rw------- 1 john users 399 Apr 25 09:34 authorized_keys

测试SSH密钥

要测试我们的密钥是否正常工作,我们可以从本地ubuntu计算机执行命令:ssh 192.168.0.11。

john@john-desktop:~/ubuntu$ ssh 192.168.0.11
Last login: Thu Apr 25 09:39:34 2013 from 192.168.0.14
Have a lot of fun...

成功,现在我们不必每次在远程suse计算机上使用rsync命令时都提供密码。

示例

对于我们的新测试,我创建了一个名为itr.com的附加文件:

john@john-desktop:~/ubuntu$ ls -rtl
total 76
-rw-r--r-- 1 john john  2866 Apr 25 08:59 file1.txt
-rw-r--r-- 1 john john  5732 Apr 25 08:59 file2.txt
-rw-r--r-- 1 john john  8598 Apr 25 08:59 file3.txt
-rw-r--r-- 1 john john 14330 Apr 25 09:00 file4.txt
-rw-rw-r-- 1 john john 31526 Apr 25 09:18 ubuntu_file.txt
-rw-rw-r-- 1 john john   333 Apr 25 09:42 itr.com

现在让我们执行rsync命令:rsync -avz /home/john/ubuntu/ john@192.168.0.11:/home/john/suse/

john@john-desktop:~/ubuntu$ rsync -avz /home/john/ubuntu/ john@192.168.0.11:/home/john/suse/
sending incremental file list
./
itr.com

sent 333 bytes  received 34 bytes  244.67 bytes/sec
total size is 63385  speedup is 172.71

这次我们没有要求输入密码!唯一被传输的文件是itr.com文件。我们可以通过查看远程suse服务器来确认这一点:

john@john-desktop:~/ubuntu$ ssh 192.168.0.11 ls -l /home/john/suse
total 76
-rw-r--r-- 1 john  1000  2866 Apr 25 08:59 file1.txt
-rw-r--r-- 1 john  1000  5732 Apr 25 08:59 file2.txt
-rw-r--r-- 1 john  1000  8598 Apr 25 08:59 file3.txt
-rw-r--r-- 1 john  1000 14330 Apr 25 09:00 file4.txt
-rw-rw-r-- 1 john users   333 Apr 25 09:42 itr.com
-rw-rw-r-- 1 john  1000 31526 Apr 25 09:18 ubuntu_file.txt

现在注意,我们可以直接从本地计算机在远程服务器上远程执行命令!在以上示例中,我们仅看到了rsync功能的一小部分。要获得rsync命令的完整概述,我建议阅读rsync的手册页。下面是可以与rsync命令一起使用的某些选项的概述。要查看此选项,您可以执行命令rsync --helpman rsync

LINUX RSYNC命令

使用rsync创建备份

rsync复制工具

rsync是一种快速而强大的文件复制工具。Rsync可以通过任何远程Shell在本地与另一个主机之间进行本地复制,也可以在远程rsync守护程序之间进行本地复制。Rsync具有大量选项,使您几乎可以控制其行为的各个方面以及复制文件的方式。Rsync以其增量传输算法而闻名,该算法通过仅发送源文件和目标中现有文件之间的差异来减少通过网络传输的数据量。这使得rsync作为备份工具非常有用。

rsync工具使用快速检查算法查找需要传输的文件,该算法查找大小已更改或上次修改时间已更改的任何文件。

Rsync通常是从命令行或脚本运行的,因此非常适合进行备份。gui(图形用户界面)中还提供了许多工具,可让您快速配置备份。GUI rsync工具的一个示例是Grsync。Grsync基本上是rsync命令的前端。

以下是rsync命令用于复制文件的一些基本用法示例:

使用rsync从远程主机检索文件

以下rsync命令将文件从远程位置检索到指定目录。在以下示例中,我们将使用本地计算机Ubuntu和远程计算机suse

Ubuntu当前在以下目录中没有文件:

john@john-desktop:~/ubuntu$ ls -l
total 0

suse在其目录中包含以下文件:

john@linux-pd5y:~/suse> pwd
/home/john/suse
john@linux-pd5y:~/suse> ls -l
total 40
-rw-r--r-- 1 john users  2866 Apr 25 08:59 file1.txt
-rw-r--r-- 1 john users  5732 Apr 25 08:59 file2.txt
-rw-r--r-- 1 john users  8598 Apr 25 08:59 file3.txt
-rw-r--r-- 1 john users 14330 Apr 25 09:00 file4.txt

在我们的本地机器Ubuntu上,我们将执行命令:

rsync -v -e ssh root@192.168.0.11:/home/john/suse/* .

此命令会将文件从远程suse计算机复制到我们的本地目录:

john@john-desktop:~/ubuntu$ rsync -v -e ssh root@192.168.0.11:/home/john/suse/* .
Password: 
file1.txt
file2.txt
file3.txt
file4.txt

sent 87 bytes  received 31778 bytes  4902.31 bytes/sec
total size is 31526  speedup is 0.99

首先,将要求我们提供远程计算机的密码。身份验证后,我们的文件应复制到当前目录中的本地计算机上。如果现在在本地计算机上执行ls -l命令,则应该看到文件已成功传输。

john@john-desktop:~/ubuntu$ ls -l
total 40
-rw-r--r-- 1 john john  2866 Apr 25 09:09 file1.txt
-rw-r--r-- 1 john john  5732 Apr 25 09:09 file2.txt
-rw-r--r-- 1 john john  8598 Apr 25 09:09 file3.txt
-rw-r--r-- 1 john john 14330 Apr 25 09:09 file4.txt

请注意,已复制的文件与远程suse计算机上的原始文件具有不同的时间戳。

如果要保留此文件戳,则可以修改命令以包括-t选项以保留时间戳信息:

rsync -vt -e ssh root@192.168.0.11:/home/john/suse/* .

john@john-desktop:~/ubuntu$ rsync -vt -e ssh root@192.168.0.11:/home/john/suse/* .
Password: 
file1.txt
file2.txt
file3.txt
file4.txt

sent 375 bytes  received 428 bytes  146.00 bytes/sec
total size is 31526  speedup is 39.26
john@john-desktop:~/ubuntu$ ls -l
total 40
-rw-r--r-- 1 john john  2866 Apr 25 08:59 file1.txt
-rw-r--r-- 1 john john  5732 Apr 25 08:59 file2.txt
-rw-r--r-- 1 john john  8598 Apr 25 08:59 file3.txt
-rw-r--r-- 1 john john 14330 Apr 25 09:00 file4.txt

这次,时间戳记信息已经保留,应该与远程suse计算机上原始文件的信息匹配。

使用rsync将本地文件复制到远程服务器

在下一个示例中,我们将本地文件复制到远程suse计算机。

对于此示例,我们在当前目录中添加了一个名为ubuntu_file.txt的新文件:

john@john-desktop:~/ubuntu$ ls -l
total 72
-rw-r--r-- 1 john john  2866 Apr 25 08:59 file1.txt
-rw-r--r-- 1 john john  5732 Apr 25 08:59 file2.txt
-rw-r--r-- 1 john john  8598 Apr 25 08:59 file3.txt
-rw-r--r-- 1 john john 14330 Apr 25 09:00 file4.txt
-rw-rw-r-- 1 john john 31526 Apr 25 09:18 ubuntu_file.txt

现在,在本地计算机(Ubuntu)上,执行以下命令:

rsync -avz /home/john/ubuntu/ john@192.168.0.11:/home/john/suse/

john@john-desktop:~/ubuntu$ rsync -avz /home/john/ubuntu/ john@192.168.0.11:/home/john/suse/
Password: 
sending incremental file list
./
ubuntu_file.txt

sent 1066 bytes  received 46 bytes  202.18 bytes/sec
total size is 63052  speedup is 56.70

现在,让我们看一下远程suse计算机:

john@linux-pd5y:~/suse> ls -l
total 72
-rw-r--r-- 1 john 1000  2866 Apr 25 08:59 file1.txt
-rw-r--r-- 1 john 1000  5732 Apr 25 08:59 file2.txt
-rw-r--r-- 1 john 1000  8598 Apr 25 08:59 file3.txt
-rw-r--r-- 1 john 1000 14330 Apr 25 09:00 file4.txt
-rw-rw-r-- 1 john 1000 31526 Apr 25 09:18 ubuntu_file.txt

我们可以看到只有新文件被传输了。

日期:2019-04-29 03:17:37 来源:oir作者:oir