本地及远程rsync同步(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/
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值