备份服务rsync

老虎死了还有狼 / 2024-10-10 / 原文

1.备份服务目标

1.备份服务应用场景
2.两台机器之间传输数据的命令:scp,rsync
3.备份服务使用流程.服务端,客户端.
4.服务使用流程中的排错(总结至少5个故障与排查流程).
5.备份项目:所有服务器数据备份,在备份服务器检查,邮件发送结果.

2.备份服务概述

2.1 目标

目标:主要解决数据不丢,辅助实现高可用;

2.2 备份服务

备份服务:存放已有的备份,一般与定时任务,脚本搭配使用.
备份服务:rsyncd服务,不同主机之间数据传输.
备份服务器配置:硬盘空间大.
rsyncd特点:

1.rsync是个服务也是命令(客户端)
2.使用方便,具有多种模式.
3.传输数据的时候是增量传输,第1次传输还是全量.

2.3 增量与全量

全量: 无论多少数据全部推送走(scp).

增量: 只会把修改,新建的文件传输走(rsync).

# backup/10.0.0.67 备份服务器
# nfs01/10.0.0.68  存储服务器

[root@nfs01 ~]#  mkdir /opt/test
[root@nfs01 ~]#  mkdir /opt/test2

# scp远程传输
[root@backup ~]# scp /etc/hostname root@10.0.0.68:/opt/test
The authenticity of host '10.0.0.68 (10.0.0.68)' can't be established.
ECDSA key fingerprint is SHA256:axJqd8bfgjoW8H0pOHKDpzYBIeUXTbLp3fIcOiAoKEY.
Are you sure you want to continue connecting (yes/no/[fingerprint])? yes
Warning: Permanently added '10.0.0.68' (ECDSA) to the list of known hosts.

Authorized users only. All activities may be monitored and reported.
root@10.0.0.68's password: 
hostname                                                                                                         100%    7     3.6KB/s   00:00    
[root@backup ~]# 
[root@nfs01 ~]#  ls /opt/test/
etc  hostname
[root@backup ~]# scp -r /etc/ root@nfs01:/opt/test/
The authenticity of host 'nfs01 (172.16.1.68)' can't be established.
ECDSA key fingerprint is SHA256:axJqd8bfgjoW8H0pOHKDpzYBIeUXTbLp3fIcOiAoKEY.
Are you sure you want to continue connecting (yes/no/[fingerprint])? yes
Warning: Permanently added 'nfs01,172.16.1.68' (ECDSA) to the list of known hosts.

Authorized users only. All activities may be monitored and reported.
root@nfs01's password: 
mtab                                                                        100%    0     0.0KB/s   00:00    
fstab                                                                       100%  579   532.4KB/s   00:00    
crypttab                                                                    100%    0     0.0KB/s   00:00   
...
[root@nfs01 ~]# ls /opt/test/etc/
abrt                    csh.precmd               grub.d          ld.so.cache               named                polkit-1          sensors.d           terminfo
adjtime                 dbus-1                   gshadow         ld.so.conf                named.conf           popt.d            services            tmpfiles.d
...
# 第二次传输同样内容时传输全量内容
[root@backup ~]# scp -r /etc/ root@nfs01:/opt/test/
The authenticity of host 'nfs01 (172.16.1.68)' can't be established.
ECDSA key fingerprint is SHA256:axJqd8bfgjoW8H0pOHKDpzYBIeUXTbLp3fIcOiAoKEY.
Are you sure you want to continue connecting (yes/no/[fingerprint])? yes
Warning: Permanently added 'nfs01,172.16.1.68' (ECDSA) to the list of known hosts.

Authorized users only. All activities may be monitored and reported.
root@nfs01's password: 
mtab                                                                        100%    0     0.0KB/s   00:00    
fstab                                                                       100%  579   532.4KB/s   00:00    
crypttab
...

#rsync传输
[root@backup ~]# rsync -av /etc/ root@10.0.0.68:/opt/test2/

Authorized users only. All activities may be monitored and reported.
root@10.0.0.68 password: 
sending incremental file list
./
.kyinfo
.productinfo
.pwd.lock
.updated
...
sent 23,151,028 bytes  received 15,205 bytes  3,088,831.07 bytes/sec
total size is 23,085,163  speedup is 1.00
[root@backup ~]# touch /etc/1.txt
# 第二次传输同样内容时只传输1.txt
[root@backup ~]# rsync -av /etc/ root@10.0.0.68:/opt/test2/

Authorized users only. All activities may be monitored and reported.
root@10.0.0.68 password: 
sending incremental file list
./
1.txt

sent 34,355 bytes  received 328 bytes  6,306.00 bytes/sec
total size is 23,085,163  speedup is 665.60
[root@backup ~]#
[root@nfs01 ~]# ls /opt/test2
1.txt                   csh.login                grub2.cfg       latrace.d                 my.cnf.d             pm                sensors3.conf       tcsd.conf
abrt                    csh.precmd               grub.d          ld.so.cache               named                polkit-1          sensors.d           terminfo
adjtime                 dbus-1                   gshadow         ld.so.conf                named.conf           popt.d            services            tmpfiles.d
...

2.4 rsync命令中/etc与/etc/区别

Linux下面/etc与/etc/一般来说是没有区别的.但是在rsync命令中是有区别的
/etc 传输目录下面的内容,包含目录本身.
/etc/ 传输目录下面的内容,不包含目录本身.

3.Rsync企业应用场景

应用场景(业务场景)

应用建议

rsync作为命令使用

临时拉取,推送数据.未来这个需求可以通过scp命令实现.

定时备份:rsync服务+定时任务

定时备份,定期备份案例.(定时任务进行备份+通过rsync传输备份)

实时同步:rsync服务+sersync/lsyncd实现实时同步

解决存储服务单点问题 (这个服务只有1个/1台,如果挂了影响严重)

rsync服务与异地容灾

找一个异地的服务器存放备份

详解

# rsync命令模式
临时拉取,推送数据,多台服务器的内容备份到backup服务器中,此时和scp命令作用一样
# rsync服务+定时备份 
1.backup服务器rsync服务端接收备份数据(来自nfs存储,web,db等)
2.backup服务器本地存放位置如/nfsbackup/ /webbackup/  /dbbackup/等

# rsync实时同步
1.sersync/nfs存储等文件是否变化
2.有变化发送数据到backup服务器

# 定时备份+rsync传输异地备份
backup服务器rsync服务端接收异地容灾数据(来自异地nfs存储,web,db)

4.推送与拉取

  • 本质:
    • 在于你当前在哪台节点(node)
    • 你要的东西在哪里
rsync -a     源文件                 目标
推送:rsync   /etc/hostname          root@10.0.0.68:/tmp
拉取:rsync   root@nfs01:/etc/hosts  /opt/

5.rsync使用模式

模式

应用场景

本地模式(了解)

不推荐使用

远程模式

传输数据(临时使用可以使用scp替代)

rsync守护进程模式(daemon) 也叫rsync服务端 rsyncd

传输数据(不需要密码),用于定时备份,定时同步.

远程模式与守护进程模式中:ip地址可以改为主机名或域名.

6.rsync不同的模式

服务器 IP DNS
备份服务器 10.0.0.67 backup
存储服务器 10.0.0.68 nfs01

6.1 rsync本地模式(了解)

rsync -a /etc/ /tmp/
rsync -a /etc /opt/

⚠ 在rsync对于目录 /etc/ /etc 是有区别的.
/etc /etc目录+目录的内容
/etc/ /etc/目录下面的内容

6.2 远程模式⭐⭐⭐⭐⭐

1对1进行远程传输数据

rsync -a

源文件 目标

推送:rsync

/etc/hostname root@10.0.0.68:/tmp

拉取:rsync

root@nfs01:/etc/hosts /opt/

远程模模式使用

#推送/etc/hostname 到10.0.0.68的/opt/test目录
[root@backup ~]# rsync -av /etc/hostname root@10.0.0.68:/opt/test/

#推送/etc 目录及目录内容 到68的 /opt/test下面
##推送第1次 全量
[root@backup ~]# rsync -av /etc/ root@10.0.0.68:/opt/test/

Authorized users only. All activities may be monitored and reported.
root@10.0.0.68 password: 
sending incremental file list
./
.kyinfo
.productinfo
.pwd.lock
.updated
...
sent 23,151,028 bytes  received 15,205 bytes  3,088,831.07 bytes/sec
total size is 23,085,163  speedup is 1.00

##推送第2次 发现没有推送
[root@backup ~]# rsync -av /etc/ root@10.0.0.68:/opt/test/
##创建文件再次推送
[root@backup ~]# touch /etc/1.txt
# 第二次传输同样内容时只传输1.txt
[root@backup ~]# rsync -av /etc/ root@10.0.0.68:/opt/test/

Authorized users only. All activities may be monitored and reported.
root@10.0.0.68 password: 
sending incremental file list
./
1.txt
sent 34,355 bytes  received 328 bytes  6,306.00 bytes/sec
total size is 23,085,163  speedup is 665.60

#通过scp推送 /etc 目录及目录内容 到31的 /opt下面  -r 递归传输
[root@backup ~] scp -r /etc/ root@10.0.0.68:/opt/

6.3 守护进程模式 ⭐⭐⭐⭐⭐

 6.3.1 环境准备:(规划) rsyncd sshd daemon

角色 主机名 ip
rsync服务端 backup 10.0.0.67/172.16.1.67
rsync客户端 nfs01 10.0.0.68/172.16.1.68

 服务使用流程

1.部署
2.配置
3.启动、使用
4.优化
5.故障
6.自动化(备份,监控,日志,安全,自动部署,容器)

# rsync服务端
1.部署
2.修改配置rsync配置文件/etc/rsyncd.conf
3.准备环境(用户,目录,目录权限,服务端密码文件,共享目录)
4.启动与检查(端口,进程),测试(传输)

# rsync客户端
1.部署
2.测试(传输数据)
3.创建客户端密码文件并修改权限(仅存放密码)
4.书写脚本(进行备份+传输备份)+定时任务

6.3.2 rsync服务端(backup)操作

6.3.2.1 检查是否安装

[root@backup ~]# rpm -qa rsync
rsync-3.1.3-7.ky10.x86_64
[root@backup ~]#
[root@backup ~]# yum install -y rsync
上次元数据过期检查:2:56:14 前,执行于 2024年10月09日 星期三 14时55分54秒。
软件包 rsync-3.1.3-7.ky10.x86_64 已安装。                                                                                                                                 2/2 
...
已升级:
  rsync-3.1.3-9.ky10.x86_64                                                                                                                                                

完毕!
[root@backup ~]# rpm -ql rsync
/etc/rsyncd.conf  # 配置文件(服务端配置文件,守护进程模式)
/etc/sysconfig/rsyncd
/usr/bin/rsync  # rsync命令
/usr/lib/systemd/system/rsyncd.service   # systemctl控制rsyncd服务的配置文件

6.3.2.2 进行配置

配置详解

##rsyncd.conf start##
fake super =yes   # 如果不开启,则C7传输报错(伪装成root权限)
uid = rsync       # rsync运行 用户(虚拟)
gid = rsync
use chroot = no
max connections = 2000  # 最大连接数
timeout = 600           # 连接超市时间
pid file = /var/run/rsyncd.pid  # 存放服务pid号
lock file = /var/run/rsync.lock # 进程、服务的锁文件,防止重复运行
log file = /var/log/rsyncd.log  # rsync服务端日志
ignore errors      # 忽略错误
read only = false  # 可以进行读写
list = false       # 关闭rsync服务端列表功能
#hosts allow = 10.0.0.0/24  # 白名单(哪些ip或网段可以访问)
#hosts deny = 0.0.0.0/32    # 黑名单 (哪些ip或网段拒绝访问)
auth users = rsync_backup   # rsync服务端用户认证(用户名)
secrets file = /etc/rsync.password  # rsync服务端验证密码(密码文件)
#####################################
[data]      # 模块名字,用户客户端访问服务端时指定
comment = www by xk 14:18 2024-10-10  # 模块备注
path = /data  # 模块对应的目录

#[其他模块名字]      # 模块名字,用户客户端访问服务端时指定
#comment = www by xk 14:18 2024-10-10  # 其他模块备注
#path = /data2  # 其他模块对应的目录

进行配置(/etc/rsyncd.conf)

注意:参数(=)后面尽量别加注释,不然会报错

##rsyncd.conf start##
fake super =yes 
uid = rsync
gid = rsync
use chroot = no
max connections = 2000
timeout = 600
pid file = /var/run/rsyncd.pid
lock file = /var/run/rsync.lock
log file = /var/log/rsyncd.log
ignore errors
read only = false
list = false
#hosts allow = 10.0.0.0/24
#hosts deny = 0.0.0.0/32
auth users = rsync_backup
secrets file = /etc/rsync.password
#####################################
[data]
comment = www by xk 14:18 2024-1-13
path = /data

后续配置

#1.添加虚拟用户
[root@backup ~]# useradd -s /sbin/nologin -M rsync
[root@backup ~]# id rsync
用户id=1001(rsync) 组id=1001(rsync) 组=1001(rsync)

#2.创建密码文件(密码文件格式: 用户名:密码)
[root@backup ~]# echo 'rsync_backup:1' > /etc/rsync.password
[root@backup ~]# 
[root@backup ~]# ll /etc/rsync.password 
-rw-r--r-- 1 root root 15 10月  9 19:20 /etc/rsync.password
[root@backup ~]# 
[root@backup ~]# chmod 600 /etc/rsync.password 
[root@backup ~]# 
[root@backup ~]# ll /etc/rsync.password 
-rw------- 1 root root 15 10月  9 19:20 /etc/rsync.password

#3.共享目录与权限
[root@backup ~]# mkdir -p /data/
[root@backup ~]# 
[root@backup ~]# ll -d /data
drwxr-xr-x 2 root root 6 10月  9 19:22 /data
[root@backup ~]# chown rsync.rsync /data/
[root@backup ~]# 
[root@backup ~]# ll -d /data
drwxr-xr-x 2 rsync rsync 6 10月  9 19:22 /data

启动服务

# 启动服务
[root@backup ~]# systemctl enable --now rsyncd
Created symlink /etc/systemd/system/multi-user.target.wants/rsyncd.service → /usr/lib/systemd/system/rsyncd.service.
[root@backup ~]# 
[root@backup ~]# systemctl restart rsyncd

# 检查服务
[root@backup ~]# ps -ef | grep rsync
root       57865       1  0 19:24 ?        00:00:00 /usr/bin/rsync --daemon --no-detach
root       57887   55788  0 19:25 pts/1    00:00:00 grep --color=auto rsync
[root@backup ~]# 
# 检查端口
[root@backup ~]# ss -lntup | grep rsync
tcp     LISTEN   0        5                0.0.0.0:873           0.0.0.0:*       users:(("rsync",pid=57865,fd=5))                                               
tcp     LISTEN   0        5                   [::]:873              [::]:*       users:(("rsync",pid=57865,fd=6))        

6.3.3 访问测试(客户端)

backup服务端本地测试

[root@backup ~]# rsync -av /etc/hostname rsync_backup@10.0.0.67::data
Password: 
sending incremental file list
hostname

sent 104 bytes  received 43 bytes  58.80 bytes/sec
total size is 7  speedup is 0.05
[root@backup ~]# 
[root@backup ~]# ll /data/
总用量 4
-rw-r--r-- 1 rsync rsync 7 10月  9 11:19 hostname
[root@backup ~]# 

nfs01客户端测试

[root@nfs01 ~]# touch /opt/2.txt
[root@nfs01 ~]# 
[root@nfs01 ~]# rsync -avz /opt/2.txt rsync_backup@backup::data
sending incremental file list
2.txt

sent 88 bytes  received 43 bytes  87.33 bytes/sec
total size is 0  speedup is 0.00
[root@nfs01 ~]# 

免密码传输数据到服务端

# 第一种密码明文
[root@nfs01 ~]# export RSYNC_PASSWORD=1
[root@nfs01 ~]# rsync -av /etc/hostname rsync_backup@10.0.0.67::data
sending incremental file list
hostname

sent 103 bytes  received 49 bytes  304.00 bytes/sec
total size is 6  speedup is 0.04
[root@nfs01 ~]# 
[root@nfs01 ~]# rsync -av /etc/hosts rsync_backup@10.0.0.67::data
sending incremental file list
hosts

sent 406 bytes  received 43 bytes  898.00 bytes/sec
total size is 311  speedup is 0.69
[root@nfs01 ~]# 

# 第二种密码写到文件里
[root@nfs01 ~]# echo '1' >/etc/rsync.client
[root@nfs01 ~]# chmod 600 /etc/rsync.client 
[root@nfs01 ~]# ll /etc/rsync.client 
-rw------- 1 root root 2 10月  9 19:56 /etc/rsync.client
[root@nfs01 ~]# 
[root@nfs01 ~]# cat -A /etc/rsync.client 
1$
[root@nfs01 ~]# touch /opt/1.txt
[root@nfs01 ~]# 
[root@nfs01 ~]# rsync -av /opt/1.txt rsync_backup@backup::data --password-file=/etc/rsync.client 
sending incremental file list
1.txt

sent 91 bytes  received 43 bytes  268.00 bytes/sec
total size is 0  speedup is 0.00
[root@nfs01 ~]# 

6.3.4 完整的rsyncd服务端小结

rsyncd服务与客户使用流程

# rsync服务端
1.配置文件
2.添加虚拟用户
3.secret文件,密码文件,文件权限
4.创建共享目录和修改权限
5.启动或重启,开机自启动.
6.测试
# rsync客户端
1.密码文件、权限
2.客户端命令测试
3.脚本与定时任务、测试