本地及远程rsync同步

问题
本案例要求通过rsync命令工具来完成本地、远程同步操作,了解增量同步的效果、相关命令选项的用途。
需要完成的配置任务如下:
测试rsync上传、下载同步的基本用法
测试rsync的命令选项-a、-v、–delete、-n的用途
使用rsync从SSH服务器下载 /boot/ 目录
使用rsync将本地的/etc/ 目录到上传到SSH服务器
方案
rsync的备份方式是增量备份,只传输本地与目标相比更改多的文档,从而减少了备份时间,也避免保留多次完全备份而占用巨量存储空间。
rsync支持在本地的目录之间执行同步,用法如下(源目录跟/符号表示被同步的是目录下的文档而不是整个目录):
1)rsync [选项…] 本地目录1 本地目录2
rsync [选项…] 本地目录1/ 本地目录2
rsync也支持在本地与远程主机之间执行同步,以SSH服务器为例,用法如下(上行同步时,认证的用户必须对目标目录有写入权限):
1)下行:rsync [选项…] user@host:源目录 本地目录
上行:rsync [选项…] 本地目录 user@host:目标目录
rsync常用的命令选项:
1)-a:归档模式,相当于递归、保留权限等多个选项的组合
2)-v:显示同步过程详细信息
3)-z:传输过程中启用压缩
4)-A:保留文件的ACL属性信息
5)-n:测试同步过程,不做实际修改
6)–delete:删除目标文件夹内多余的文档
使用两台RHEL6虚拟机,其中一台为rsync同步提供源目录(192.168.4.5),另外一台作为rsync同步操作的发起端(192.168.4.205),如图-1所示。
在这里插入图片描述
图-1
步骤
实现此案例需要按照如下步骤进行。
步骤一:基本同步操作
1)准备测试目录及文件
创建两个测试目录/opt/dir1、/opt/dir2,并在/opt/dir1目录下建立测试文件1.txt、测试子目录2.dir:

[root@pc205 ~]# mkdir  /opt/dir1  /opt/dir2
[root@pc205 ~]# ifconfig eth0 > /opt/dir1/1.txt
[root@pc205 ~]# mkdir /opt/dir1/2.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/:
总用量 0

2)验证-a归档选项、源目录后是否跟/的作用
未添加-a选项时,当被同步的文档包含目录时,无法向下递归:

[root@pc205 ~]# rsync /opt/dir1  /opt/dir2    
skipping directory dir1  								//目录被跳过

[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

添加-a选项,再次执行上述同步,被同步的是整个/opt/dir1目录(末尾无/):

[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/:
总用量 4
drwxr-xr-x. 3 root root 4096 4月  26 16:51 dir1  		//作为整体被同步过来

修改上述操作,将源目录携程/opt/dir1/,只同步此目录下的文档:

[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/:
总用量 12
-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 	//同上
drwxr-xr-x. 3 root root 4096 4月  26 16:51 dir1

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 root@192.168.4.5:/boot  /fromssh  
root@192.168.4.5'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  root@192.168.4.5:/opt/
root@192.168.4.5's password:  							//验证对方口令

然后切换到远程主机192.168.4.5上确认同步结果:

[root@svr5 ~]# du -sh /opt/selinux/  					//确认同步结果
19M     /opt/selinux/
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值