rsync

rsync命令是一个远程数据同步工具,可通过LAN/WAN快速同步多台主机间的文件。rsync使用所谓的“rsync算法”来使本地和远程两个主机之间的文件达到同步,这个算法只传送两个文件的不同部分,而不是每次都整份传送,因此速度相当快。 rsync是一个功能非常强大的工具,其命令也有很多功能特色选项,我们下面就对它的选项一一进行分析说明。

对应于以上六种命令格式,rsync有六种不同的工作模式:

拷贝本地文件。当SRC和DES路径信息都不包含有单个冒号”:”分隔符时就启动这种工作模式。如:rsync -a /data /backup
使用一个远程shell程序(如rsh、ssh)来实现将本地机器的内容拷贝到远程机器。当DST路径地址包含单个冒号”:”分隔符时启动该模式。如:rsync -avz *.c foo:src
使用一个远程shell程序(如rsh、ssh)来实现将远程机器的内容拷贝到本地机器。当SRC地址路径包含单个冒号”:”分隔符时启动该模式。如:rsync -avz foo:src/bar /data
从远程rsync服务器中拷贝文件到本地机。当SRC路径信息包含”::”分隔符时启动该模式。如:rsync -av root@192.168.78.192::www /databack
从本地机器拷贝文件到远程rsync服务器中。当DST路径信息包含”::”分隔符时启动该模式。如:rsync -av /databack root@192.168.78.192::www
列远程机的文件列表。这类似于rsync传输,不过只要在命令中省略掉本地机信息即可。如:rsync -v rsync://192.168.78.192/www

这个命令是系统自带的命令

目录之间的同步

1)将目录 /boot 同步到目录 /todir 下
1.1.同步之前

[root@nginx ~]# ls -l /todir
ls: 无法访问/todir: 没有那个文件或目录

1.2将目录1作为目录2的子目录

[root@apache ~]# rsync -a /boot /todir
[root@apache ~]# ls -l /todir
总用量 4
dr-xr-xr-x 5 root root 4096 9月 3 18:13 boot

2)将目录 /boot 下的文档同步到目录 /todir 下

2.1没有同步之前

[root@nginx ~]# ls -l /todir
ls: 无法访问/todir: 没有那个文件或目录

2.2将目录下的目录,同步到目录2下,(查看与1不一样)

[root@nginx ~]# rsync -a /boot/ /todir
[root@nginx ~]# ls -l /todir
总用量 80420
-rw-r–r– 1 root root 140894 8月 23 2017 config-3.10.0-693.el7.x86_64
drwxr-xr-x 3 root root 17 9月 3 17:44 efi
drwxr-xr-x 2 root root 27 9月 3 17:45 grub

3.检测同步的效果

[root@nginx ~]# touch /boot/a.txt
[root@nginx ~]# rm -rf /todir/grub2

3.2查看两个文件的异同[root@nginx ~]# ls -ld /boot/a.txt /todir/a.txt

ls: 无法访问/todir/a.txt: 没有那个文件或目录
-rw-r–r– 1 root root 0 9月 6 11:27 /boot/a.txt
[root@nginx ~]# ls -ld /boot/grub2/ /todir/grub2
ls: 无法访问/todir/grub2: 没有那个文件或目录
drwx——. 5 root root 97 9月 3 17:50 /boot/grub2/

3.3同步两个文件夹

[root@nginx ~]# rsync -a /boot/ /todir/

3.4再一次查看两个文件夹的异同

[root@nginx ~]# ls -ld /boot/a.txt /todir/a.txt
-rw-r–r– 1 root root 0 9月 6 11:27 /boot/a.txt
-rw-r–r– 1 root root 0 9月 6 11:27 /todir/a.txt
[root@nginx ~]# ls -ld /boot/grub2/ /todir/grub2
drwx——. 5 root root 97 9月 3 17:50 /boot/grub2/
drwx—— 5 root root 97 9月 3 17:50 /todir/grub2

二。验证 -a、-v、-n、–delete 选项的含义

1)验证-a选项
当目录1包含文件夹时,若缺少-a或-r选项则文件夹会被忽略:

[root@nginx ~]# rsync /home /testa
skipping directory home
[root@nginx ~]# ls -ld /testa
ls: 无法访问/testa: 没有那个文件或目录

添加-a后才会执行同步:

[root@nginx ~]# rsync -a /home /testa
[root@nginx ~]# ls -ld /testa/
drwxr-xr-x 3 root root 18 9月 6 11:38 /testa/

类似的情况,当目录1中的数据出现权限、归属、修改时间等变化时,若文件内容不变默认不会同步,若希望目录2也同步这些变化,也需要-a选项。
2)验证-v选项
创建测试目录及文档:

[root@nginx ~]# mkdir /fdir
[root@nginx ~]# touch /fdir/1.txt
[root@nginx ~]# rsync -av /fdir/ /tdir
sending incremental file list
created directory /tdir
./
1.txt

sent 82 bytes received 34 bytes 232.00 bytes/sec
total size is 0 speedup is 0.00

添加-v选项时,可以看到操作细节信息,比如第一次同步时:

[root@nginx ~]# touch /fdir/2.txt
[root@nginx ~]# rsync -av /fdir/ /tdir
sending incremental file list
./
2.txt

sent 100 bytes received 34 bytes 268.00 bytes/sec
total size is 0 speedup is 0.00
[root@nginx ~]# ls /fdir/ /tdir/
/fdir/:
1.txt 2.txt

/tdir/:
1.txt 2.txt

3)验证-n选项

将-n、-v选项合用,可以模拟同步过程,显示需要做哪些操作(但并不真的同步)。

在目录/fdir下新建文件3.txt,测试同步操作:

[root@nginx ~]# touch /fdir/3.txt
[root@nginx ~]# rsync -avn /fdir/ /tdir/
sending incremental file list
./
3.txt

sent 78 bytes received 18 bytes 192.00 bytes/sec
total size is 0 speedup is 0.00 (DRY RUN)
[root@nginx ~]# ls -l /tdir/3.txt //并没有实际传输过来
ls: 无法访问/tdir/3.txt: 没有那个文件或目录

-n 是检测,并没有传输

2 案例2:rsync+SSH同步

查看远程主机的 / 目录下有哪些子目录
从远程主机下载 /etc/passwd 文件到当前目录
将远程主机的 /boot/ 目录同步为本地的 /fromssh
将本机的 /etc 目录同步到远程主机的 /opt/下

列出 SSH 服务端资源

rsync user@host:远程目录/

rsync+SSH远程同步操作:

rsync […] user@host:远程目录 本地目录
rsync […] 本地目录 user@host:远程目录

查看远程主机svr7的/目录下有哪些子目录:

[root@nginx ~]# rsync root@192.168.1.21:/
root@192.168.1.21’s password: //对方主机的密码

步骤二:rsync+SSH同步操作

1)从远程主机svr7下载/etc/passwd文件到当前目录

[root@nginx ~]# mkdir /king

[root@nginx king]# rsync root@192.168.1.21:/etc/passwd ./
root@192.168.1.21’s password:
[root@nginx king]# ls
passwd

2)将远程主机svr7的/boot/目录同步为本地的/fromssh

[root@nginx ~]# rsync -a root@192.168.1.21:/boot/ /fromssh
root@192.168.1.21’s password:

[root@nginx ~]# ls /fromssh/
config-3.10.0-693.el7.x86_64
efi
grub
grub2
initramfs-0-rescue-9f5d11896d994e66b3dc5a6a5314ba97.img
initramfs-3.10.0-693.el7.x86_64.img
initrd-plymouth.img

3)将本机的/etc目录同步到远程主机svr7的/opt/下

确认目录大小:

[root@nginx ~]# du -sh /etc
30M /etc

上行同步到远程主机

[root@nginx ~]# rsync -a /etc root@192.168.1.21:/opt
root@192.168.1.21’s password:

查看同步结果

[root@apache ~]# cd /opt/
[root@apache opt]# ls
etc
[root@apache opt]# du -sh etc/
30M etc/

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

运维螺丝钉

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值