目录
文章目录
- 目录
- 1. 概述
- 2. 机器准备
- 3. 检查机器环境
- 4. 配置ssh key密钥实现数据免登陆验证(rsync服务端-rsync节点)
- 5. 具体要求
- 6. 拓扑图
- 7. 配置rsync服务端(dscq-122)
- 8. 配置rsync节点1(dscq-201)
1. 概述
rsync以守护进程(socket) 的方式传输数据,这个是rsync自身的重要的功能。
2. 机器准备
主机名 | 主机IP | 默认网关 | 用途 |
---|---|---|---|
dscq-122 | 192.168.2.122 | 192.168.2.1 | rsync服务端 |
dscq-201 | 192.168.2.201 | 192.168.2.1 | rsync节点 |
dscq-225 | 192.168.2.225 | 192.168.2.1 | rsync节点 |
3. 检查机器环境
3.1 rsync服务端
[root@dscq-122 ~]# ifconfig
[root@dscq-122 ~]# cat /etc/redhat-release
CentOS Linux release 7.2.1511 (Core)
[root@dscq-122 ~]# which rsync
/usr/bin/rsync
[root@dscq-122 ~]# rpm -qa|grep rsync
rsync-3.1.2-6.el7_6.1.x86_64
3.2 rsync节点1
[root@dscq-201 ~]# ifconfig
[root@dscq-201 ~]# cat /etc/redhat-release
CentOS Linux release 7.2.1511 (Core)
[root@dscq-201 ~]# which rsync
/usr/bin/rsync
[root@dscq-201 ~]# rpm -qa|grep rsync
rsync-3.1.2-10.el7.x86_64
3.3 rsync节点2
[root@dscq-225 ~]# ifconfig
[root@dscq-225 ~]# cat /etc/redhat-release
CentOS Linux release 7.2.1511 (Core)
[root@dscq-225 ~]# which rsync
/usr/bin/rsync
[root@dscq-225 ~]# rpm -qa|grep rsync
rsync-3.1.2-6.el7_6.1.x86_64
4. 配置ssh key密钥实现数据免登陆验证(rsync服务端-rsync节点)
[root@dscq-122 ~]# ssh-keygen
.
.
.
[root@dscq-122 ~]# ssh-copy-id -i /root/.ssh/id_rsa.pub -p22 root@192.168.2.201
[root@dscq-122 ~]# ssh-copy-id -i /root/.ssh/id_rsa.pub -p22 root@192.168.2.225
5. 具体要求
要求在dscq-122以rsync守护进程的方式部署rsync服务,使得其他所有rsync节点客户
端主机,可以把本地数据通过rsync的方式实时备份到这台服务器dscq-122上。本例的客
户端仅以dscq-201、dscq-255 为例。
6. 拓扑图
7. 配置rsync服务端(dscq-122)
7. 1 首先确认系统是否安装rsync并找到其配置文件/etc/rsyncd.conf
[root@dscq-122 ~]# rpm -qa|grep rsync
rsync-3.1.2-6.el7_6.1.x86_64
[root@dscq-122 ~]# rpm -ql rsync
/etc/rsyncd.conf ##rsync服务的主配置文件##
/etc/sysconfig/rsyncd
/usr/bin/rsync
...
...
...
[root@dscq-122 ~]# ll /etc/rsyncd.conf
-rw-r--r--. 1 root root 458 4月 26 2019 /etc/rsyncd.conf
7. 2 编辑/etc/rsyncd.conf
[root@dscq-122 ~]# cat /etc/rsyncd.conf
#created by yuki 15:01 2020-10-27#
#QQ:12345678 Tel:12345678912#
##rsyncd.conf start##
uid = rsync ###用root用户最方便###
gid = rsync ###用root用户组最方便###
use chroot = no
max connections = 200
timeout = 300
pid file = /var/run/rsyncd.pid
lock file = /var/run/rsync.lock
log file = /var/log/rsyncd.log
[dataDirectoryPath]
path = /dataDirectoryPath/
ignore errors
read only = false
list = false
hosts allow =192.0.0.0/24
hosts deny = 0.0.0.0/32
auth users = rsync_backup
secrets file = /etc/rsync.password
##rsyncd.conf end##
[root@dscq-122 ~]# man rsyncd.conf ###可以查看rsyncd.conf配置详解
7. 3 服务端配置文件/etc/rsyncd.conf参数讲解
7.4 创建rsync用户和用户组
[root@dscq-122 ~]# useradd rsync -s /sbin/nologin
[root@dscq-122 ~]# id rsync
uid=1007(rsync) gid=1007(rsync) 组=1007(rsync)
7.5 创建rsync数据处理目录并修改其权限由用户rsync管理
[root@dscq-122 ~]# mkdir /dataDirectoryPath
[root@dscq-122 ~]# ll -d /dataDirectoryPath/
drwxr-xr-x. 2 root root 6 10月 27 11:40 /dataDirectoryPath/
[root@dscq-122 ~]# chown -R rsync:rsync /dataDirectoryPath/
[root@dscq-122 ~]# ll -d /dataDirectoryPath/
drwxr-xr-x. 2 rsync rsync 6 10月 27 11:40 /dataDirectoryPath/
7.6 创建并设置支持的虚拟用户名对应的密码文件并设置密码文件权限
[root@dscq-122 ~]# echo 'rsync_backup:rsync123456' >>/etc/rsync.password
[root@dscq-122 ~]# cat /etc/rsync.password
rsync_backup:rsync123456
[root@dscq-122 ~]# ll /etc/rsync.password
-rw-r--r--. 1 root root 25 10月 27 11:44 /etc/rsync.password
[root@dscq-122 ~]# chmod 600 /etc/rsync.password
[root@dscq-122 ~]# ll /etc/rsync.password
-rw-------. 1 root root 25 10月 27 11:44 /etc/rsync.password
7.7 关闭防火墙、selinux等(以免客户端不能连过来)
[root@dscq-122 ~]# firewall-cmd --state
running
[root@dscq-122 ~]# systemctl stop firewalld
[root@dscq-122 ~]# firewall-cmd --state
not running
[root@dscq-122 ~]# sed -i "s/SELINUX=enforcing/SELINUX=disabled/g" /etc/sysconfig/selinux
[root@dscq-122 ~]# setenforce 0
[root@dscq-122 ~]# getenforce
Permissive
7.8 以守护进程方式启动rsync服务
[root@dscq-122 run]# rsync --daemon --config=/etc/rsyncd.conf
[root@dscq-122 run]# cat /var/run/rsyncd.pid
16235
[root@dscq-122 run]# tail -f /var/log/rsyncd.log
2020/10/27 14:05:48 [16235] rsyncd version 3.1.2 starting, listening on port 873
[root@dscq-122 run]# ps -ef |grep rsync
root 16235 1 0 14:05 ? 00:00:00 rsync --daemon --config=/etc/rsyncd.conf
root 16315 125359 0 14:06 pts/0 00:00:00 grep --color=auto rsync
[root@dscq-122 run]# netstat -lntup |grep 873
tcp 0 0 0.0.0.0:873 0.0.0.0:* LISTEN 16235/rsync
tcp6 0 0 :::873 :::* LISTEN 16235/rsync
拓展:rsync的进程参数选项
-
–daemon #表示以守护进程的方式启动rsync服务
-
–address #绑定指定ip地址
-
–config=FILE #更改配置文件路径,而不是默认的/etc/rsyncd.conf
-
–port=PORT #更改其它端口提供服务,而不是缺省的873端口
7.9 设置开机自启动rsync服务
[root@dscq-122 ~]# echo "/usr/bin/rsync --daemon --config=/etc/rsyncd.conf" >> /etc/rc.d/rc.local
[root@dscq-122 ~]# tail /etc/rc.d/rc.local
# this script will NOT be run after all other services.
#
# Please note that you must run 'chmod +x /etc/rc.d/rc.local' to ensure
# that this script will be executed during boot.
touch /var/lock/subsys/local
/usr/bin/rsync --daemon --config=/etc/rsyncd.conf
7.10 如何停止或重启rsync服务
[root@dscq-122 log]# pkill rsync
[root@dscq-122 log]# rsync --daemon --config=/etc/rsyncd.conf
[root@dscq-122 log]# ps -ef |grep rsync
root 15291 1 0 13:54 ? 00:00:00 rsync --daemon
root 15301 125359 0 13:54 pts/0 00:00:00 grep --color=auto rsync
[root@dscq-122 log]# netstat -lntup |grep 873
tcp 0 0 0.0.0.0:873 0.0.0.0:* LISTEN 15291/rsync
tcp6 0 0 :::873 :::* LISTEN 15291/rsync
拓展:几个进程管理命令
pkill rsync # 一次性彻底杀进程
killall rsync # 可能一次杀不死,要连按连杀
killall -9 rsync # 强制杀死进程 -9
kill -9 进程号 # 利用端口号加-9 强制进程
提示:不管是推送还是拉取数据,执行rsync同步命令一定是在客户端机器上,将客户端数据推到rsync服务端或者从rsync服务端拉取数据到客户端。
8. 配置rsync节点1(dscq-201)
8.1 首先确认系统是否安装rsync并创建rsync用户和用户组
[root@dscq-201 ~]# rpm -qa|grep rsync
rsync-3.1.2-6.el7_6.1.x86_64
[root@dscq-201 ~]# useradd rsync -s /sbin/nologin
[root@dscq-201 ~]# id rsync
uid=0(root) gid=0(root) 组=0(root) 环境=unconfined_u:unconfined_r:unconfined_t:s0-s0:c0.c1023
8.2 将rsync服务端配置的虚拟用户密码放到自行建立的密码文件中并修改密码文件的权限
[root@dscq-201 ~]# echo 'rsync123456' >>/etc/rsync.password
[root@dscq-201 ~]# cat /etc/rsync.password
rsync123456
[root@dscq-201 ~]# ls -l /etc/rsync.password
-rw-r--r--. 1 root root 12 10月 27 14:12 /etc/rsync.password
[root@dscq-201 ~]# chmod 600 /etc/rsync.password
[root@dscq-201 ~]# ls -l /etc/rsync.password
-rw-------. 1 root root 12 10月 27 14:12 /etc/rsync.password
8.3 关闭rsync节点1防火墙、selinux等
[root@dscq-201 ~]# firewall-cmd --state
running
[root@dscq-122 ~]# systemctl stop firewalld
[root@dscq-122 ~]# firewall-cmd --state
not running
[root@dscq-122 ~]# sed -i "s/SELINUX=enforcing/SELINUX=disabled/g" /etc/sysconfig/selinux
[root@dscq-122 ~]# setenforce 0
[root@dscq-122 ~]# getenforce
Permissive
8.4 开始从rsync服务端(dscq-122)拉取数据到rsync节点1(dscq-201)上
8.4.1 先检查rsync服务端(dscq-122)上数据
[root@dscq-122 ~]# ll -d /dataDirectoryPath/ #配置文件中配置的rsync服务端处理数据的目录
drwxr-xr-x. 2 rsync rsync 30 10月 27 14:40 /dataDirectoryPath/
[root@dscq-122 ~]# ll /dataDirectoryPath/
总用量 4
-rw-r--r--. 1 root root 12 10月 27 14:40 iamfromserver.log
[root@dscq-122 ~]# chown -R rsync:rsync /dataDirectoryPath/
[root@dscq-122 ~]# ll /dataDirectoryPath/
总用量 4
-rw-r--r--. 1 rsync rsync 12 10月 27 14:40 iamfromserver.log
8.4.2 再检查rsync节点1(dscq-201)上数据
[root@dscq-201 ~]# ll -d /home/yuki_test/
drwxr-xr-x. 2 root root 6 10月 27 14:42 /home/yuki_test/
[root@dscq-201 yuki_test]# ll /home/yuki_test/
总用量 0
-rw-r--r--. 1 root root 0 10月 27 14:44 iamfromclient.txt
root@dscq-122 ~]# chown -R rsync:rsync /home/yuki_test/
[root@dscq-122 ~]# ll /home/yuki_test/
总用量 4
-rw-r--r--. 1 rsync rsync 0 10月 27 14:44 iamfromclient.txt
8.4.3 在rsync节点1(dscq-201)上执行拉取命令(需要手动输入虚拟用户的密码)
rsync -avz rsync_backup@192.168.2.122::dataDirectoryPath /home/yuki_test/
关键语法说明:
-avz相当于-vzrtopgDl,表示同步时文件和目录属性不变。
rsync_backup:rsync服务端配置的支持验证的虚拟用户名
192.168.2.122:rsync服务端IP
dataDirectoryPath:配置文件中配置的rsync服务端处理数据的目录
/home/yuki_test/:本地rsync节点/客户端目录
[root@dscq-201 yuki_test]# rsync -avz rsync_backup@192.168.2.122::dataDirectoryPath /home/yuki_test/
Password: ##输入rsync服务端配置文件中配置的rsync_backup虚拟用户的密码##
receiving incremental file list
created directory /home/test
./
iamfromserver.log
sent 50 bytes received 159 bytes 12.67 bytes/sec
total size is 12 speedup is 0.06
[root@dscq-201 yuki_test]# ll /home/yuki_test/
总用量 4
-rw-r--r--. 1 root root 0 10月 27 14:44 iamfromclient.txt
-rw-r--r--. 1 root root 12 10月 27 14:40 iamfromserver.log ##刚刚执行命令之后从rsync服务端拉取的##
8.4.4 在rsync节点1(dscq-201)上执行拉取命令(免除手动输入密码)
rsync -avz rsync_backup@192.168.2.122::dataDirectoryPath /home/yuki_test/ --password-file=/etc/rsync.password
[root@dscq-201 yuki_test]# rm -rf /home/yuki_test/iamfromserver.log
[root@dscq-201 yuki_test]# ll /home/yuki_test/
总用量 0
-rw-r--r--. 1 root root 0 10月 27 14:44 iamfromclient.txt
[root@dscq-201 yuki_test]# rsync -avz rsync_backup@192.168.2.122::dataDirectoryPath /home/yuki_test/ --password-file=/etc/rsync.password
receiving incremental file list
./
iamfromserver.log
sent 50 bytes received 159 bytes 418.00 bytes/sec
total size is 12 speedup is 0.06
[root@dscq-201 yuki_test]# ll /home/yuki_test/
总用量 4
-rw-r--r--. 1 root root 0 10月 27 14:44 iamfromclient.txt
-rw-r--r--. 1 root root 12 10月 27 14:40 iamfromserver.log ##刚刚执行命令之后从rsync服务端拉取的##
提示:已经存在的文件不会再拉取或推送,这是因为rsync进行了客户端和服务端文件对比,只做增量或者改变的拉取,这也是rsync比cp、scp命令强大的地方。
[root@dscq-201 yuki_test]# rsync -avz rsync_backup@192.168.2.122::dataDirectoryPath /home/yuki_test/ --password-file=/etc/rsync.password
receiving incremental file list
sent 24 bytes received 96 bytes 240.00 bytes/sec
total size is 12 speedup is 0.10
8.4.5 在rsync节点1(dscq-201)上执行拉取命令(第三种方式)
rsync -avz rsync://rsync_backup@192.168.2.122/dataDirectoryPath /home/yuki_test/ --password-file=/etc/rsync.password
注意:模块前面的/并不表示路径,只是将前面的ip地址和它隔开。
[root@dscq-201 yuki_test]# rm -rf /home/yuki_test/iamfromserver.log
[root@dscq-201 yuki_test]# rsync -avz rsync://rsync_backup@192.168.2.122/dataDirectoryPath /home/yuki_test/ --password-file=/etc/rsync.password
receiving incremental file list
./
iamfromserver.log
sent 50 bytes received 159 bytes 418.00 bytes/sec
total size is 12 speedup is 0.06
[root@dscq-201 yuki_test]# ll /home/yuki_test/
总用量 4
-rw-r--r--. 1 root root 0 10月 27 14:44 iamfromclient.txt
-rw-r--r--. 1 root root 12 10月 27 14:40 iamfromserver.log
8.5 开始将rsync节点1(dscq-201)上的数据推送到rsync服务端(dscq-122)
8.5.1 先检查rsync服务端(dscq-122)上数据
[root@dscq-122 ~]# ll -d /dataDirectoryPath/ #配置文件中配置的rsync服务端处理数据的目录
drwxr-xr-x. 2 rsync rsync 30 10月 27 14:40 /dataDirectoryPath/
[root@dscq-122 ~]# ll /dataDirectoryPath/
总用量 4
-rw-r--r--. 1 root root 12 10月 27 14:40 iamfromserver.log
8.5.2 再检查rsync节点1(dscq-201)上数据
[root@dscq-201 ~]# ll -d /home/yuki_test/
drwxr-xr-x. 2 root root 6 10月 27 14:42 /home/yuki_test/
[root@dscq-201 yuki_test]# ll /home/yuki_test/
总用量 0
-rw-r--r--. 1 root root 0 10月 27 14:44 iamfromclient.txt
8.5.3 在rsync节点1(dscq-201)上执行推送命令(第一种方式)
rsync -avz /home/yuki_test/ rsync_backup@192.168.2.122::dataDirectoryPath --password-file=/etc/rsync.password
注意:
/home/yuki_test/ 只推送目录里面的内容;
/home/yuki_test 会连目录本身一起推送;
[root@dscq-201 yuki_test]# rsync -avz /home/yuki_test/ rsync_backup@192.168.2.122::dataDirectoryPath --password-file=/etc/rsync.password
sending incremental file list
./
iamfromclient.txt
sent 135 bytes received 46 bytes 362.00 bytes/sec
total size is 0 speedup is 0.00
8.5.4 在rsync节点1(dscq-201)上执行推送命令(第二种方式)
rsync -avz /home/yuki_test/ rsync://rsync_backup@192.168.2.122/dataDirectoryPath --password-file=/etc/rsync.password
注意:模块前面的/并不表示路径,只是将前面的ip地址和它隔开。
[root@dscq-201 yuki_test]# rsync -avz /home/yuki_test/ rsync://rsync_backup@192.168.2.122/dataDirectoryPath --password-file=/etc/rsync.password
sending incremental file list
./
iamfromclient.txt
sent 135 bytes received 46 bytes 362.00 bytes/sec
total size is 0 speedup is 0.00