1. 概述
rsync
命令是一个远程数据同步工具,可通过 LAN/WAN 快速同步多台主机间的文件。rsync
使用所谓的 “rsync算法” 来使本地和远程两个主机之间的文件达到同步,这个算法只传送两个文件的不同部分,而不是每次都整份传送,因此速度相当快。rsync
是一个功能非常强大的工具,其命令也有很多功能特色选项。
2. 安装与配置
请参考:
3. 语法
rsync [OPTION...] SRC... [DEST] |
3.1 本地模式
rsync [OPTION...] [USER@]HOST:SRC... [DEST] |
拷贝本地文件。当 SRC 和 DEST 路径信息都不包含有单个冒号 :
分隔符时就启动这种工作模式。例如:xiaosi@ying:~$ rsync -a adv_push adv_push_backup/
xiaosi@ying:~$ ll adv_push_backup/
总用量 20
drwxrwxr-x 3 xiaosi xiaosi 4096 3月 20 15:29 ./
drwxr-xr-x 83 xiaosi xiaosi 12288 3月 20 15:29 ../
drwxrwxr-x 2 xiaosi xiaosi 4096 3月 20 15:28 adv_push/
3.2 通过远程Shell访问-Pull
rsync [OPTION...] [USER@]HOST:SRC... [DEST] |
使用一个远程Shell程序(如rsh、ssh)来实现将远程机器的内容拷贝到本地机器。当 SRC 地址路径包含单个冒号 :
分隔符时启动该模式。例如:xiaosi@ying:~$ rsync -a ubuntu@xxx.xxx.xxx.xxx:test/remote_content.txt /home/xiaosi/data/share/
ubuntu@xxx.xxx.xxx.xxx's password:
在本地机器上查看从服务器拷贝到本地上的文件:xiaosi@ying:~$ cd /home/xiaosi/data/share/
xiaosi@ying:~/data/share$ ll
总用量 12
drwxrwxr-x 2 xiaosi xiaosi 4096 3月 20 19:41 ./
drwxr-xr-x 14 xiaosi xiaosi 4096 3月 20 19:24 ../
-rw-rw-r-- 1 xiaosi xiaosi 17 3月 20 15:57 remote_content.txt
ubuntu@xxx.xxx.xxx.xxx 中 ubuntu 是服务器登录名 xxx.xxx.xxx.xxx 是服务器IP地址 password 是服务器登录名 ubuntu 对应的密码
以 Shell 方式访问,首先在服务端启动 ssh 服务: service sshd start
3.3 通过远程Shell访问-Push
rsync [OPTION...] SRC... [USER@]HOST:DEST |
使用一个远程shell程序(如rsh、ssh)来实现将本地机器的内容拷贝到远程机器。当 DEST 路径地址包含单个冒号 :
分隔符时启动该模式。例如:xiaosi@ying:~$ rsync -a /home/xiaosi/data/exception.txt ubuntu@xxx.xxx.xxx.xxx:tmp
ubuntu@xxx.xxx.xxx.xxx's password:
在服务器上查看从本地拷贝到服务器上的文件:ubuntu@VM-0-7-ubuntu:~/tmp$ ll
total 24
drwxrwxr-x 5 ubuntu ubuntu 4096 Mar 20 19:34 ./
drwxr-xr-x 11 ubuntu ubuntu 4096 Mar 20 19:32 ../
...
-rw-rw-r-- 1 ubuntu ubuntu 80 Mar 20 11:03 exception.txt
...
ubuntu@xxx.xxx.xxx.xxx 中 ubuntu 是服务器登录名 xxx.xxx.xxx.xxx 是服务器IP地址 password 是服务器登录名 ubuntu 对应的密码
以 Shell 方式访问,首先在服务端启动 ssh 服务: service sshd start
3.4 通过rsync进程访问-Pull
rsync [OPTION...] [USER@]HOST::SRC... [DEST] |
从远程 rsync 服务器中拷贝文件到本地机。当 SRC 路径信息包含 ::
分隔符时启动该模式。例如:xiaosi@ying:~$ rsync -a test@xxx.xxx.xxx.xxx::share/remote_content.txt /home/xiaosi/data/share
在本地机器上查看从服务器拷贝到本地上的文件:xiaosi@ying:~/data/share$ ll
总用量 12
drwxrwxr-x 2 xiaosi xiaosi 4096 3月 20 19:06 ./
drwxr-xr-x 14 xiaosi xiaosi 4096 3月 20 19:06 ../
-rw-r--r-- 1 xiaosi xiaosi 17 3月 20 18:44 remote_content.txt
以后台服务方式访问 请参考:Linux rsync配置指南
test@xxx.xxx.xxx.xxx 中 test 是以后台方式访问配置的用户
3.5 通过rsync进程访问-Push
rsync [OPTION...] SRC... [USER@]HOST::DEST |
从本地机器拷贝文件到远程 rsync 服务器中。当 DEST 路径信息包含 ::
分隔符时启动该模式。例如:xiaosi@ying:~$ rsync -a /home/xiaosi/data/sources.list.backup test@xxx:xxx:xxx:xxx::share/config
在服务器上查看从本地拷贝到服务器上的文件:ubuntu@VM-0-7-ubuntu:/home/share/config$ ll
total 12
drwxr-xr-x 2 root root 4096 Mar 20 19:02 ./
drwxr-xr-x 3 root root 4096 Mar 20 19:02 ../
-rw-r--r-- 1 root root 2981 Mar 20 16:30 sources.list.backup
以后台服务方式访问 请参考:Linux rsync配置指南
test@xxx.xxx.xxx.xxx 中 test 是以后台方式访问配置的用户
3.6 查阅模式
只使用一个 SRC 参数,而不使用 DEST 参数将列出源文件而不是进行复制。例如:xiaosi@ying:~$ rsync -a adv_push
drwxrwxr-x 4,096 2018/03/20 15:28:15 adv_push
-rw-rw-r-- 353 2018/03/14 17:02:35 adv_push/adv_push_20180307.txt
-rw-rw-r-- 353 2018/03/14 17:02:33 adv_push/adv_push_20180308.txt
4. 选项
-v, --verbose 详细模式输出。 |