在Linux下使用rsync示范大全

ff530c84511562db6b4105f41f9d406c.png

Rsync是一个在类Unix和Window系统上通过网络在系统间同步文件夹和文件的网络协议。Rsync可以复制或者显示目录并复制文件。Rsync默认监听TCP 873端口,通过远程shell如rsh和ssh复制文件。Rsync必须在远程和本地系统上都安装。

rsync的主要好处是:

速度:最初会在本地和远程之间拷贝所有内容。下次,只会传输发生改变的块或者字节。

安全:传输可以通过ssh协议加密数据。

低带宽:rsync可以在两端压缩和解压数据块。

语法: #rsysnc [options] source path destination path

0e033009be10cfe2127a815b6ab85eba.jpeg

示例: 1 - 启用压缩

[root@localhost /]# rsync -zvr /home/aloft/ /backuphomedir
building file list ... done
bash_logout
bash_profile
bashrc
sent 472 bytes received 86 bytes 1116.00 bytes/sec
total size is 324 speedup is 0.58

上面的rsync命令使用了-z来启用压缩,-v是可视化,-r是递归。上面在本地的/home/aloft/和/backuphomedir之间同步。

示例: 2 - 保留文件和文件夹的属性

[root@localhost /]# rsync -azvr /home/aloft/ /backuphomedir
building file list ... done
/
bash_logout
bash_profile
bashrc
 
sent 514 bytes received 92 bytes 1212.00 bytes/sec
total size is 324 speedup is 0.53

上面我们使用了-a选项,它保留了所有人和所属组、时间戳、软链接、权限,并以递归模式运行。

示例: 3 - 同步本地到远程主机

root@localhost /]# rsync -avz /home/aloft/ azmath@192.168.1.4:192.168.1.4:/share/rsysnctest/
Password:
 
building file list ... done
/
bash_logout
bash_profile
bashrc
sent 514 bytes received 92 bytes 1212.00 bytes/sec

total size is 324 speedup is 0.53

上面的命令允许你在本地和远程机器之间同步。你可以看到,在同步文件到另一个系统时提示你输入密码。在做远程同步时,你需要指定远程系统的用户名和IP或者主机名。

示例: 4 - 远程同步到本地

[root@localhost /]# rsync -avz azmath@192.168.1.4:192.168.1.4:/share/rsysnctest/ /home/aloft/
Password:
building file list ... done
/
bash_logout
bash_profile
bashrc
sent 514 bytes received 92 bytes 1212.00 bytes/sec
total size is 324 speedup is 0.53

上面的命令同步远程文件到本地。

示例: 5 - 找出文件间的不同

[root@localhost backuphomedir]# rsync -avzi /backuphomedir /home/aloft/
building file list ... done
cd+++++++ backuphomedir/
>f+++++++ backuphomedir/.bash_logout
>f+++++++ backuphomedir/.bash_profile
>f+++++++ backuphomedir/.bashrc
>f+++++++ backuphomedir/abc
>f+++++++ backuphomedir/xyz
 
sent 650 bytes received 136 bytes 1572.00 bytes/sec
total size is 324 speedup is 0.41

示例: 6 - 备份

rsync命令可以用来备份linux。

你可以在cron中使用rsync安排备份。

0 0 * * * /usr/local/sbin/bkpscript &> /dev/null
vi /usr/local/sbin/bkpscript
 
rsync -avz -e ‘ssh -p2093′ /home/test/ root@192.168.1.150:/oracle/data/


rsync 语法如下

$  rsync [OPTION]... SRC [SRC]... DEST
  or   rsync [OPTION]... SRC [SRC]... [USER@]HOST:DEST
  or   rsync [OPTION]... SRC [SRC]... [USER@]HOST::DEST
  or   rsync [OPTION]... SRC [SRC]... rsync://[USER@]HOST[:PORT]/DEST
  or   rsync [OPTION]... [USER@]HOST:SRC [DEST]
  or   rsync [OPTION]... [USER@]HOST::SRC [DEST]
  or   rsync [OPTION]... rsync://[USER@]HOST[:PORT]/SRC [DEST]

从语法结构我们可以看出, 源和目标即可以在本地也可以在远端. 如果是远端的话,需要指明登录用户名, 远端服务器名, 和远端文件或目录. 同时源可以是多个, 目标位置只能是一个。

290e47cb269d8776e48328a7ac5f78cb.jpeg

更多示例

示例 1. 同步同一台机上的两个目录

$ rsync -zvr /var/opt/installation/inventory/ /root/temp
building file list ... done
sva.xml
svB.xml
.
sent 26385 bytes  received 1098 bytes  54966.00 bytes/sec
total size is 44867  speedup is 1.63
$

说明:

-z: --compress 使用压缩机制

-v: --verbose 打印详细信息

-r: --recursive 以递归模式同步子目录

注意: 同步完成后, 我们会发现文件的时间戳timestamps发生了改变.

$ 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
示例 2: 保留文件的时间戳

有时我们希望拷贝或同步时, 时间戳不要发生变化, 源文件是什么时间戳,目标文件就是什么时间戳, 这时我们需要使用 -a --archive 归档模式选项. -a 选项相当于7个选项的组合 -rlptgoD

-r, --recursive: 递归模式Recursive mode
-l, --links: 将符号链接当作符号链接文件拷贝, 不拷贝符合链接指向的文件内容.
-p, --perms: 保留文件权限
-t, --times: 保留修改时间戳
-g, --group: 保留用户组信息
-o, --owner: 保留用户信息(需要超级用户权限)
-D, 相当于 --devices --specials 的组合, 保留设备文件, 保留特殊文件.

$ rsync -azv /var/opt/installation/inventory/ /root/temp/
building file list ... done
./
sva.xml
svB.xml
.
sent 26499 bytes  received 1104 bytes  55206.00 bytes/sec
total size is 44867  speedup is 1.63
$

同步完成后, 我们再来看文件属性, 时间戳信息得到了保留, 不仅如此文件的所有者 和所在组也得到保留.

$ 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: 拷贝单个文件
$ rsync -v /var/lib/rpm/Pubkeys /root/temp/
Pubkeys


sent 42 bytes  received 12380 bytes  3549.14 bytes/sec
total size is 12288  speedup is 0.99

说明: Pubkeys 是一个普通文件

示例 4. 从本地拷贝多个文件到远端

使用rsync, 也可以从本地拷贝多个文件或目录到远端, 以下即为示例:

$ rsync -avz /root/temp/ thegeekstuff@192.168.200.10:/home/thegeekstuff/temp/
Password:
building file list ... done
./
rpm/
rpm/Basenames
rpm/Conflictname


sent 15810261 bytes  received 412 bytes  2432411.23 bytes/sec
total size is 45305958  speedup is 2.87

注意:
与本地文件拷贝不同的地方在于, 当拷贝文件到远程服务器时, 我们指定远程主机上的用户名, 服务器地址, 路径等信息, 类是于使用scp命令拷贝, 如果没有设置ssh免密码登录我们还需要提供远程用户的密码等信息.有时你不想频繁输入密码, 或者rsync运行在一个无人执守的脚本里面, 这是需要预先设置ssh免密登录, 或者使用结合expect命令,自动输入密码, 但是出于安全考虑, 密码需要加密. 所以在条件许可的情况下, 还是推荐设置ssh免密登录.

示例 5. 从远程服务器拷贝文件到本地

与示例 4 稍有不同, 这时远端目录或文件作为源位置, 本地目录或文件作为目标位置, 示例如下:

$ rsync -avz thegeekstuff@192.168.200.10:/var/lib/rpm /root/temp
Password:
receiving file list ... done
rpm/
rpm/Basenames
.
sent 406 bytes  received 15810230 bytes  2432405.54 bytes/sec
total size is 45305958  speedup is 2.87

示例 6. Remote shell for Synchronization

rsync 允许指定远程主机上运行shell命令.
这时需要使用 -e 选项:
-e, --rsh=COMMAND 指定远端使用的shell命令

Use rsync -e ssh to specify which remote shell to use. In this case, rsync will use ssh.

$ rsync -avz -e ssh thegeekstuff@192.168.200.10:/var/lib/rpm /root/temp
Password:
receiving file list ... done
rpm/
rpm/Basenames


sent 406 bytes  received 15810230 bytes  2432405.54 bytes/sec
total size is 45305958  speedup is 2.87

示例 7. 拷贝时不覆盖目标位置已修改过的文件

在一下特殊的使用场景中, 我们不希望拷贝文件时, 我们不希望拷贝过程覆盖掉目标位置中用户做出的修改. 这时我们需要使用 -u 选项明确的告诉rsync命令保留用户在目标文件中作出的修改. 在下面的例子中, 文件Basenames是用户基于上次的拷贝, 修改过的文件, 当我们使用了-u 选项后, 该文件中的修改将不会被覆盖掉.

$ ls -l /root/temp/Basenames
total 39088
-rwxr-xr-x 1 root root        4096 Sep  2 11:35 Basenames


$ rsync -avzu thegeekstuff@192.168.200.10:/var/lib/rpm /root/temp
Password:
receiving file list ... done
rpm/


sent 122 bytes  received 505 bytes  114.00 bytes/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. 仅拷贝目录结构, 不拷贝文件

在某些特殊场景中, 我们只需要远程服务器上的目录结构, 而不希望花大量时间, 带宽拷贝文件内容, 这时我们可以使用 -d, --dirs选项来达到目的.

$ rsync -v -d thegeekstuff@192.168.200.10:/var/lib/ .
Password:
receiving file list ... done
logrotate.status
CAM/
YaST2/
acpi/


sent 240 bytes  received 1830 bytes  318.46 bytes/sec
total size is 956  speedup is 0.46

示例 9. 文件传输时显示进度

有时我们希望拷贝文件时, 能实时的显示拷贝进度, 以及传输速率等信息. 尤其是拷贝大文件时, 程序不输出信息, 用户往往无法区分程序是在响应中, 还是已经挂起, 在这种情况下如果使用 –progress 就会非常有帮助.
rsync –progress option displays detailed progress of rsync execution as shown below.

$ rsync -avz --progress thegeekstuff@192.168.200.10:/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 bytes  received 15810211 bytes  2108082.27 bytes/sec
total size is 45305958  speedup is 2.87

你也可以使用rsnapshot工具 (rsnapshot会调用rsync)来备份本地linux服务器, 或者备份远程linux服务器.

示例 10. 同步时删除目标位置多余的文件或目录

有时我们希望目标文件和源文件保持严格一致, 不要多文件也不要少文件, 这是我们可能需要使用 -delete 选项来达到目的. 如果使用 -delete 选项, rsync将删除目标位置多余的文件或文件夹. 此选项还可以结合--delete-excluded 选项一起使用, 添加一些例外的文件.

# 现在Source and target 文件是一致的. 现在让我们在目标位置创建一个新文件.
$ touch new-file.txt


$ rsync -avz --delete thegeekstuff@192.168.200.10:/var/lib/rpm/ .
Password:
receiving file list ... done
deleting new-file.txt
./


sent 26 bytes  received 390 bytes  48.94 bytes/sec
total size is 45305958  speedup is 108908.55

上述示例中, new-file.txt 是源文件中没有的文件, 其将会在拷贝时被删除掉.

示例 11. Do not Create New File at the Target

在某些特殊的场景下, 我们只想更新, 目标位置已经存在的文件或目录, 而不关心源位置的新文件, 这时我们可以使用-existing 选项仅仅更新已经存在的文件.
让我们来验证一下这个选项的功能, 首先在源端添加一个新文件 new-file.txt.

[/var/lib/rpm ]$ > new-file.txt
Next, execute the rsync from the target.


$ rsync -avz --existing root@192.168.1.2:/var/lib/rpm/ .
root@192.168.1.2s password:
receiving file list..  done


sent 26 bytes  received 419 bytes  46.84 bytes/sec
total size is 88551424  speedup is 198991.96

从上面的例子可以看到, 由于加了--existing选项新文件new-file.txt没有被拷贝到目标位置

示例 12. 查看目标位置和源位置之间的差异

选项-i, --itemize-changes 非常有用, 当我们想了解目标位置和源位置的文件差异时.

在源端:

$ 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


注意: 在上面的例子中, 源位置和目标位置有两处差异. 第一, 源文件Basenames的所有者和组 与 目标文件不同, 第一Dirnames文件大小也不一样.
现在让我们来看看rsync会怎样显示这些差异
$ rsync -avzi thegeekstuff@192.168.200.10:/var/lib/rpm/ /root/temp/
Password:
receiving file list ... done
>f.st.... Basenames
.f....og. Dirnames


sent 48 bytes  received 2182544 bytes  291012.27 bytes/sec
total size is 45305958  speedup is 20.76

输出信息只在相应文件前面显示了9个字母来标识改变, 这些字母具体是什么意思呢? 请参考以下详细说明

表示文件已经被拷贝到了本地
f 代表该项目是一个文件.
s 代表文件大小发生了变化.
t 代表时间戳有差异.
o 所有者有差异
g 所属组有差异.

示例 13. 使用通配符过滤文件

rsync 可以使用--include 和 --exclude 选项结合通配符进行文件或文件夹过滤

$ rsync -avz --include 'P*' --exclude '*' thegeekstuff@192.168.200.10:/var/lib/rpm/ /root/temp/
Password:
receiving file list ... done
./
Packages
Providename
Provideversion
Pubkeys


sent 129 bytes  received 10286798 bytes  2285983.78 bytes/sec
total size is 32768000  speedup is 3.19
在上面的示例中, 仅仅以P打头的文件和文件夹被包含了进来, 其他的文件都被过滤在拷贝的过程中被排除在外了.

示例 14. 不拷贝大文件

可以使用--max-size 告诉rsync 不要拷贝大小超过某个值的文件, 可以使用K, M, G指定文件大小, M for megabytes and G for gigabytes.

$ rsync -avz --max-size='100K' thegeekstuff@192.168.200.10:/var/lib/rpm/ /root/temp/
Password:
receiving file list ... done
./
Conflictname
Group
Installtid
Name
Sha1header
Sigmd5
Triggername


sent 252 bytes  received 123081 bytes  18974.31 bytes/sec
total size is 45305958  speedup is 367.35

示例 15. 拷贝整个文件

rsync 有个重要优点就是, 可以做到在拷贝的过程中, 只拷贝发生变化了的部分, 而不是发送整个文件.
但是在某些场景中, 比如文件较少, 文件size较小时, 我们的带宽又足够大, cpu资源相对又贫乏, 我们不希望它这样做, 因为毕竟计算源端和目标端的checksum, 并做对比, 也需要额外cpu开销. 这时我们可以使用 -W, --whole-file 选项, 让rsync不用计算那么多, 一上来就直接开始传送文件. 我们可以像下面这么做.

#  rsync -avzW  thegeekstuff@192.168.200.10:/var/lib/rpm/ /root/temp
Password:
receiving file list ... done
./
Basenames
Conflictname
Dirnames
Filemd5s
Group
Installtid
Name


sent 406 bytes  received 15810211 bytes  2874657.64 bytes/sec


total size is 45305958  speedup is 2.87

✨✨ 欢迎关注 ✨✨

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值