版权声明:苏苏吖 https://blog.csdn.net/weixin_44774638/article/details/90896310
3)验证–delete选项的作用
在/opt/dir2/目录下添加测试文件3.txt,将测试子目录2.dir改名为3.dir,现在与/opt/dir1/目录的内容是不一致的:
[root@pc205 ~]# cat /proc/cpuinfo > /opt/dir2/3.txt
[root@pc205 ~]# mv /opt/dir2/2.dir /opt/dir2/4.dir
[root@pc205 ~]# ls -l /opt/dir1/ /opt/dir2/
/opt/dir1/:
总用量 8
-rw-r--r--. 1 root root 497 4月 26 16:48 1.txt
drwxr-xr-x. 2 root root 4096 4月 26 16:51 2.dir
/opt/dir2/:
总用量 16
-rw-r--r--. 1 root root 497 4月 26 16:48 1.txt
-rw-r--r--. 1 root root 721 4月 26 17:06 3.txt //增加的测试文件
drwxr-xr-x. 2 root root 4096 4月 26 16:51 4.dir //改名后的目录
drwxr-xr-x. 3 root root 4096 4月 26 16:51 dir1
未添加–delete选项时,rsync同步操作是单向的,只是把源目录的文档增量更新到目标目录,而目标目标残留的其他文档不会做任何处理:
[root@pc205 ~]# rsync -a /opt/dir1/ /opt/dir2
[root@pc205 ~]# ls -l /opt/dir1/ /opt/dir2/
/opt/dir1/:
总用量 8
-rw-r--r--. 1 root root 497 4月 26 16:48 1.txt
drwxr-xr-x. 2 root root 4096 4月 26 16:51 2.dir
/opt/dir2/:
总用量 20
-rw-r--r--. 1 root root 497 4月 26 16:48 1.txt
drwxr-xr-x. 2 root root 4096 4月 26 16:51 2.dir //新同步来的子目录
-rw-r--r--. 1 root root 721 4月 26 17:06 3.txt
drwxr-xr-x. 2 root root 4096 4月 26 16:51 4.dir
drwxr-xr-x. 3 root root 4096 4月 26 16:51 dir1
若要保持两边一致,即删除“目标目录下有而源目录下没有”的文档,可以添加–delete选项。修改前一条操作如下,可以看到两个目录的内容完全相同(多余文档被删除):
[root@pc205 ~]# rsync -a --delete /opt/dir1/ /opt/dir2
[root@pc205 ~]# ls -l /opt/dir1/ /opt/dir2/
/opt/dir1/:
总用量 8
-rw-r--r--. 1 root root 497 4月 26 16:48 1.txt
drwxr-xr-x. 2 root root 4096 4月 26 16:51 2.dir
/opt/dir2/:
总用量 8
-rw-r--r--. 1 root root 497 4月 26 16:48 1.txt
drwxr-xr-x. 2 root root 4096 4月 26 16:51 2.dir
[root@pc205 ~]#
4)验证-v选项的作用
未添加-v选项时,rsync同步操作是静默执行的,除非报错否则无屏幕输出:
[root@pc205 ~]# rsync -a --delete /opt/dir1/ /opt/dir2
而添加-v选项后,可以观察rsync同步操作的过程信息,方便了解细节:
[root@pc205 ~]# rsync -a -v --delete /opt/dir1/ /opt/dir2
sending incremental file list
sent 70 bytes received 13 bytes 166.00 bytes/sec
total size is 497 speedup is 5.99
5)验证-n选项的作用
选项-n主要用来模拟同步过程,而并不会真正的执行,此选项通常与-v一起使用。比如说,可以先清空/opt/dir2/目录,然后rsync结合-nv选项来了解一下指定的同步会执行哪些操作:
[root@pc205 ~]# rsync -a -nv --delete /opt/dir1/ /opt/dir2
sending incremental file list
./
1.txt //会同步1.txt文件
2.dir/ //会同步2.dir子目录
sent 79 bytes received 22 bytes 202.00 bytes/sec
total size is 497 speedup is 4.92 (DRY RUN)
[root@pc205 ~]# ls -l /opt/dir1/ /opt/dir2/
/opt/dir1/:
总用量 8
-rw-r--r--. 1 root root 497 4月 26 16:48 1.txt
drwxr-xr-x. 2 root root 4096 4月 26 16:51 2.dir
/opt/dir2/: //实际上并未执行
总用量 0
把上述同步操作中的-n选项去掉,才会真正地执行同步操作:
[root@pc205 ~]# rsync -a -v --delete /opt/dir1/ /opt/dir2
sending incremental file list
./
1.txt
2.dir/
sent 616 bytes received 38 bytes 1308.00 bytes/sec
total size is 497 speedup is 0.76
[root@pc205 ~]# ls -l /opt/dir1/ /opt/dir2/
/opt/dir1/:
总用量 8
-rw-r--r--. 1 root root 497 4月 26 16:48 1.txt
drwxr-xr-x. 2 root root 4096 4月 26 16:51 2.dir
/opt/dir2/: //已经执行同步
总用量 8
-rw-r--r--. 1 root root 497 4月 26 16:48 1.txt
drwxr-xr-x. 2 root root 4096 4月 26 16:51 2.dir
步骤二:rsync+SSH同步
1)将远程主机上的 /boot/目录下载备份到本地(选项-z表示压缩)
[root@pc205 ~]# rsync -az --delete [email protected]:/boot /fromssh
[email protected]'s password: //验证对方口令
[root@pc205 ~]# ls /fromssh/ //确认同步结果
boot
2)将本地的/etc/selinux目录上传备份到远程主机
[root@pc205 ~]# du -sh /etc/selinux/ //确认待同步的目录
19M /etc/selinux/
[root@pc205 ~]# rsync -az /etc/selinux [email protected]:/opt/
[email protected]'s password: //验证对方口令
然后切换到远程主机192.168.4.5上确认同步结果:
[root@svr5 ~]# du -sh /opt/selinux/ //确认同步结果
19M /opt/selinux/