【Linux】循序渐进学运维-服务篇-rsync实战

大家好,我是高胜寒,本文是Linux运维-循序渐进学运维-服务篇的第10篇文章

前言

前几篇文章我们探讨了rsync的原理,使用场景,安装及简单使用,配置文件,今天我们来谈谈如何使用配置文件进行rsync的备份。
如果你还没有系统的学习rsync相关的基础知识请参考下方链接:

【Linux】循序渐进学运维-服务篇-rysnc原理

【Linux】循序渐进学运维-服务篇-rysnc安装及使用

【Linux】循序渐进学运维-服务篇-rsync配置文件

实验环境

服务器端: gaosh-64 192.168.1.64 centos7
客户端: gaosh-1 192.168.1.22 centos6

实验步骤

1. 修改配置文件

服务器端:
方便学员复制,配置文件的注释加在最下面

[root@gaosh-64 ~]# vim /etc/rsyncd.conf
[root@gaosh-64 ~]# cat /etc/rsyncd.conf 

uid = root
git = root
address = 192.168.1.64
port =873
host allow = 192.168.1.0/24
use chroot = yes
max connections = 5
pid file =/var/run/rsyncd.pid
lock file = /var/run/rsyncd.lock
log file = /var/log/rsyncd.log
motd file = /etc/rsyncd.motdc

[backup]
path = /web-back/
comment = rsync backup
read only = false
list = yes
auth users = rsyncuser
secrets file = /etc/rsync.password


注释版:

uid = root                          	#运行进程的身份。
gid = root                          	#运行进程的组。
address =192.168.1.64		             #监听IP。
port =873                               #监听端口。
hosts allow =192.168.1.0/24   #允许同步客户端的IP地址,可以是网段,或者用*表示所有 192.168.1.1/24或192.168.1.0/255.255.255.0
use chroot = yes   #是否囚牢,锁定家目录,rsync被黑之后,黑客无法再rsync运行的家目录之外创建文件,选项设置为yes。
max connections =5                     #最大连接数。
pid file =/var/run/rsyncd.pid          #进程PID,自动生成。
lock file =/var/run/rsync.lock      	#指max connectios参数的锁文件。
log file =/var/log/rsyncd.log         	#日志文件位置。
motd file =/etc/rsyncd.motdc     	   #客户端登陆之后弹出的消息,需要创建。
[backup]                              #共享模块名称。
path =/web-back/                      #路径。
comment = rsync backup      #描述。
read only = false                     #设置服务端文件的读写权限。
list = yes                           #是否允许查看模块信息。
auth users = rsyncuser               #备份的用户,和系统用户无关。
secrets file =/etc/rsync.passwd     #存放用户的密码文件,格式是  用户名:密码。
2. 创建提示文件及用户密码
1) 编辑欢迎信息
[root@gaosh-64 ~]#  echo "欢迎来学习高胜寒主讲的rsync服务" > /etc/rsyncd.motd
2) 创建密码文件并给权限
[root@gaosh-64 ~]# cat /etc/rsync.password 
rsyncuser:password123
[root@gaosh-64 ~]# chmod 600 /etc/rsync.password 
3. 启动服务
[root@gaosh-64 ~]# systemctl restart xinetd
[root@gaosh-64 ~]# systemctl enable xinetd
[root@gaosh-64 ~]# rsync --daemon --config=/etc/rsyncd.conf   ## 加载配置文件
[root@gaosh-64 ~]# netstat -antup |grep rsync   ## 查看是否运行
tcp        0      0 192.168.1.64:873        0.0.0.0:*               LISTEN      25717/rsync         
[root@gaosh-64 ~]# 

4. 测试
1) 服务器端创建配置文件里的目录web-back
[root@gaosh-64 ~]# mkdir /web-back/
[root@gaosh-64 ~]# chmod 600 /web-back/

2) rsync备份测试

测试一: 不指定测试文件

[root@gaosh-1 ~]# rsync -avz /var/www/html/ [email protected]::backup

Password: 
sending incremental file list

sent 22 bytes  received 8 bytes  6.67 bytes/sec
total size is 0  speedup is 0.00

每次需要输入密码太麻烦,我们可以在客户端创建和服务器端一样的rsync.password的文件,并写入密码,然后在命令后面直接指定文件即可:

测试二: 指定密码文件

[root@gaosh-1 ~]# cat /etc/rsync.password 
123456
[root@gaosh-1 ~]# chmod 600 /etc/rsync.password 

指定文件测试:

[root@gaosh-1 ~]# rsync -avz /var/www/html/ [email protected]::backup  --password-file=/etc/rsync.password

总结

本文主要讨论了rsync 通过配置文件来实现文件的备份传输,一个rysnc有这么多玩法,你崩溃了吗,别急,后面还会有rsync与其他工具的结合使用,到时候在说崩溃的事。

我是高胜寒,一个在教培行业不忘初心的人,欢迎点赞收藏,我们下篇文章讨论rsync与其他软件配合实现数据的实时同步。

rsync系列文章:

【Linux】循序渐进学运维-服务篇-rysnc原理

【Linux】循序渐进学运维-服务篇-rysnc安装及使用

【Linux】循序渐进学运维-服务篇-rsync配置文件

【Linux】循序渐进学运维-服务篇-rsync实战

猜你喜欢

转载自blog.csdn.net/xinshuzhan/article/details/107367717