rsync同步

[root@1 ~]# yum -y install rsync-3.0.9-18.el7.x86_64

没有/todir目录
[root@1 ~]# ls -l /todir
ls: cannot access /todir: No such file or directory

[root@1 ~]# ls /boot
config-3.10.0-693.el7.x86_64
efi
grub
grub2
initramfs-0-rescue-936505720c7f47309671011b53cd525b.img
initramfs-3.10.0-693.el7.x86_64.img
initrd-plymouth.img
symvers-3.10.0-693.el7.x86_64.gz
System.map-3.10.0-693.el7.x86_64
vmlinuz-0-rescue-936505720c7f47309671011b53cd525b
vmlinuz-3.10.0-693.el7.x86_64

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

同步后,新建/todir目录
[root@1 ~]# ls -l /todir
total 4
dr-xr-xr-x. 5 root root 4096 Aug 24 19:40 boot

[root@1 ~]# ls /boot
config-3.10.0-693.el7.x86_64
efi
grub
grub2
initramfs-0-rescue-936505720c7f47309671011b53cd525b.img
initramfs-3.10.0-693.el7.x86_64.img
initrd-plymouth.img
symvers-3.10.0-693.el7.x86_64.gz
System.map-3.10.0-693.el7.x86_64
vmlinuz-0-rescue-936505720c7f47309671011b53cd525b
vmlinuz-3.10.0-693.el7.x86_64

[root@1 ~]# rm -rf /todir

删除/todir目录,以便再次测试
[root@1 ~]# ls /todir
ls: cannot access /todir: No such file or directory

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

把/boot目录内容同步到了/todir目录
[root@1 ~]# ls -l /todir
total 86272
-rw-r–r–. 1 root root 140894 Aug 22 2017 config-3.10.0-693.el7.x86_64
drwxr-xr-x. 3 root root 17 Aug 24 19:34 efi
drwxr-xr-x. 2 root root 27 Aug 24 19:35 grub
drwx——. 5 root root 97 Aug 24 19:40 grub2
-rw——-. 1 root root 51370320 Aug 24 19:38 initramfs-0-rescue-936505720c7f47309671011b53cd525b.img
-rw——-. 1 root root 20930078 Aug 24 19:40 initramfs-3.10.0-693.el7.x86_64.img
-rw-r–r–. 1 root root 611350 Aug 24 19:36 initrd-plymouth.img
-rw-r–r–. 1 root root 293027 Aug 22 2017 symvers-3.10.0-693.el7.x86_64.gz
-rw——-. 1 root root 3228420 Aug 22 2017 System.map-3.10.0-693.el7.x86_64
-rwxr-xr-x. 1 root root 5877760 Aug 24 19:38 vmlinuz-0-rescue-936505720c7f47309671011b53cd525b
-rwxr-xr-x. 1 root root 5877760 Aug 22 2017 vmlinuz-3.10.0-693.el7.x86_64

先在/boot目录下创建a.txt,在/todir目录下没有a.txt
删除/todir目录下的grub2,不影响/boot目录内的内容
同步后,两个目录内的内容都一致,也就是/todir目录下多了a.txt和grub2两个文件

[root@1 ~]# touch /boot/a.txt
[root@1 ~]# ls /boot
a.txt
config-3.10.0-693.el7.x86_64
efi
grub
grub2
initramfs-0-rescue-936505720c7f47309671011b53cd525b.img
initramfs-3.10.0-693.el7.x86_64.img
initrd-plymouth.img
symvers-3.10.0-693.el7.x86_64.gz
System.map-3.10.0-693.el7.x86_64
vmlinuz-0-rescue-936505720c7f47309671011b53cd525b
vmlinuz-3.10.0-693.el7.x86_64

没有a.txt文件
[root@1 ~]# ls /todir
config-3.10.0-693.el7.x86_64
efi
grub
grub2
initramfs-0-rescue-936505720c7f47309671011b53cd525b.img
initramfs-3.10.0-693.el7.x86_64.img
initrd-plymouth.img
symvers-3.10.0-693.el7.x86_64.gz
System.map-3.10.0-693.el7.x86_64
vmlinuz-0-rescue-936505720c7f47309671011b53cd525b
vmlinuz-3.10.0-693.el7.x86_64

[root@1 ~]# rm -rf /todir/grub2

没有了grub2文件
[root@1 ~]# ls /todir
config-3.10.0-693.el7.x86_64
efi
grub
initramfs-0-rescue-936505720c7f47309671011b53cd525b.img
initramfs-3.10.0-693.el7.x86_64.img
initrd-plymouth.img
symvers-3.10.0-693.el7.x86_64.gz
System.map-3.10.0-693.el7.x86_64
vmlinuz-0-rescue-936505720c7f47309671011b53cd525b
vmlinuz-3.10.0-693.el7.x86_64

对/todir的操作,不影响/boot目录内容
[root@1 ~]# ls /boot
a.txt
config-3.10.0-693.el7.x86_64
efi
grub
grub2
initramfs-0-rescue-936505720c7f47309671011b53cd525b.img
initramfs-3.10.0-693.el7.x86_64.img
initrd-plymouth.img
symvers-3.10.0-693.el7.x86_64.gz
System.map-3.10.0-693.el7.x86_64
vmlinuz-0-rescue-936505720c7f47309671011b53cd525b
vmlinuz-3.10.0-693.el7.x86_64

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

[root@1 ~]# ls /boot
a.txt
config-3.10.0-693.el7.x86_64
efi
grub
grub2
initramfs-0-rescue-936505720c7f47309671011b53cd525b.img
initramfs-3.10.0-693.el7.x86_64.img
initrd-plymouth.img
symvers-3.10.0-693.el7.x86_64.gz
System.map-3.10.0-693.el7.x86_64
vmlinuz-0-rescue-936505720c7f47309671011b53cd525b
vmlinuz-3.10.0-693.el7.x86_64

和/boot目录内容一致,a.txt和grub2都有了
[root@1 ~]# ls /todir
a.txt
config-3.10.0-693.el7.x86_64
efi
grub
grub2
initramfs-0-rescue-936505720c7f47309671011b53cd525b.img
initramfs-3.10.0-693.el7.x86_64.img
initrd-plymouth.img
symvers-3.10.0-693.el7.x86_64.gz
System.map-3.10.0-693.el7.x86_64
vmlinuz-0-rescue-936505720c7f47309671011b53cd525b
vmlinuz-3.10.0-693.el7.x86_64

rsync同步工具的常用选项:
-n:测试同步过程,不做实际修改
–delete:删除目标文件夹内多余的文档
-a:归档模式,相当于-rlptgoD
-v:显示详细操作信息
-z:传输过程中启用压缩/解压

当目录1包含文件夹时,若缺少-a或-r选项则文件夹会被忽略,当目录1中的数据出现权限、归属、修改时间等变化时,若文件内容不变默认不会同步,若希望目录2也同步这些变化,也需要-a选项。
[root@1 ~]# ls /home
chihiro harry kenji

[root@1 ~]# ls /testa
ls: cannot access /testa: No such file or directory

[root@1 ~]# rsync /home /testa
skipping directory home

[root@1 ~]# rsycn -a /home/ /testa
-bash: rsycn: command not found

[root@1 ~]# rsync -a /home /testa

[root@1 ~]# ls /testa
home

添加-v选项时,可以看到操作细节信息,比如第一次同步时
[root@1 ~]# mkdir /fdir

[root@1 ~]# touch /fdir/1.txt

[root@1 ~]# 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

[root@1 ~]# ls /tdir
1.txt

[root@1 ~]# touch /fdir/2.txt

[root@1 ~]# ls /fdir/2.txt
/fdir/2.txt

[root@1 ~]# 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@1 ~]# ls /tdir
1.txt 2.txt

[root@1 ~]# ls /fdir /tdir
/fdir:
1.txt 2.txt
/tdir:
1.txt 2.txt

将-n、-v选项合用,可以模拟同步过程,显示需要做哪些操作(但并不真的同步)
[root@1 ~]# touch /fdir/3.txt

[root@1 ~]# ls /fdir
1.txt 2.txt 3.txt

[root@1 ~]# rsync -anv /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@1 ~]# ls /tdir
1.txt 2.txt

去掉-n后,同步成功
[root@1 ~]# rsync -av /fdir/ /tdir/
sending incremental file list
./
3.txt
sent 114 bytes received 34 bytes 296.00 bytes/sec
total size is 0 speedup is 0.00

[root@1 ~]# ls /tdir
1.txt 2.txt 3.txt

rsync同步操作默认只是将目录1的数据同步到目录2,但如果目录2存在多余的文件却并不会去除,除非添加—delete选项。

在目录/fdir、/tdir已经完成同步后,删除/tdir/2.txt文件,再次同步
[root@1 ~]# ls /fdir /tdir
/fdir:
1.txt 2.txt 3.txt
/tdir:
1.txt 2.txt 3.txt

[root@1 ~]# rm -rf /fdir/2.txt

[root@1 ~]# ls /fdir
1.txt 3.txt

[root@1 ~]# rsync -a /fdir/ /tdir/

[root@1 ~]# ls /fdir /tdir
/fdir:
1.txt 3.txt
/tdir:
1.txt 2.txt 3.txt

[root@1 ~]# rsync -a –delete /fdir/ /tdir/

加上–delete后,同步成功
[root@1 ~]# ls /fdir /tdir
/fdir:
1.txt 3.txt
/tdir:
1.txt 3.txt

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值