本地及远程rsync同步 步骤一

版权声明:苏苏吖 https://blog.csdn.net/weixin_44774638/article/details/90896304

问题
本案例要求通过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

猜你喜欢

转载自blog.csdn.net/weixin_44774638/article/details/90896304
今日推荐