背景
TiDB 数据库是一款开源、云原生、分布式的数据库系统,它兼容MySQL协议,支持弹性水平扩展和实时分析。TiDB 数据库基于 Raft 协议实现分布式事务的强一致性,每个tikv数据节点都可以提供读写能力,当对TiDB 数据库进行物理备份时,通过访问 PD 节点,获取所有 TiKV 节点访问地址以及数据分布信息,并把每个tikv节点上的 leader region 数据写到备份存储上,因此,每个TiKV 节点都要挂载共享的备份存储。TiDB 支持 Amazon S3、Google Cloud Storage (GCS)、Azure Blob Storage 和 NFS 作为备份恢复的存储。
备份存储选择
TiDB 数据库支持 cloud 环境上部署、On-Premise 环境上部署。
当TiDB 数据库部署在cloud环境上,推荐使用Amazon S3、Google Cloud Storage (GCS)、Azure Blob Storage等云存储服务;
如果 TiDB 集群部署在On-Premise (自建机房)中,则推荐以下方式:
- 搭建 MinIO 作为备份存储系统,使用 S3 协议将数据备份到 MinIO 中。
- 挂载 NFS(如 NAS)盘到 br 工具和所有的 TiKV 实例,使用 POSIX file system 接口将备份数据写入对应的 NFS 目录中。
本文主要介绍 MinIO 和 NFS 两种方式作为备份存储,对数据库进行备份。
备份存储搭建
搭建 MinIO
1、下载 MinIO 安装包
wget http://dl.minio.org.cn/server/minio/release/linux-amd64/minio
#赋执行权限
chmod u+x minio
2、编辑配置文件
#该文件在路径/opt/soft/minio/config下,文件名为minio
#配置如下:
#定义root账户名称
MINIO_ROOT_USER="minioadmin"
#定义root密码
MINIO_ROOT_PASSWORD="minioadmin"
#定义工作目录
MINIO_VOLUMES="/brdata"
#定义文件服务地址和端口
MINIO_OPTS="--address IP:9000"
#定义控制台的地址和端口
MINIO_OPTS1="--console-address IP:19001"
3、添加 systemd 自启动服务
vim /etc/systemd/system/minio.service
文件内容如下:
[Unit]
Description=MinIO
Documentation=https://docs.min.io
Wants=network-online.target
After=network-online.target
AssertFileIsExecutable=/opt/soft/minio/bin/minio
[Service]
WorkingDirectory=/brdata
#ProtectProc=invisible
EnvironmentFile=-/opt/soft/minio/config/minio
ExecStartPre=/bin/bash -c "if [ -z \"${MINIO_VOLUMES}\" ]; then echo \"Variable MINIO_VOLUMES not set in /data/minio/data\"; exit 1; fi"
ExecStart=/opt/soft/minio/bin/minio server --address IP:9000 $MINIO_OPTS $MINIO_OPTS1 $MINIO_VOLUMES
# Let systemd restart this service always
Restart=on-failure
RestartSec=5
# Specifies the maximum file descriptor number that can be opened by this process
LimitNOFILE=65536
# Specifies the maximum number of threads this process can create
TasksMax=infinity
# Disable timeout logic and wait until process is stopped
TimeoutStopSec=infinity
SendSIGKILL=no
[Install]
WantedBy=multi-user.target
4、启动服务
#加载配置
systemctl daemon-reload
#启动minio
systemctl start minio.service
#开启开机自启
systemctl enable minio.service
5、登录服务
登录地址:IP:19001
账户:minioadmin
密码:minioadmin
6、创建bucket
创建数据库使用的备份bucket为:brbucket
7、创建serviceAccount
首先创建一个用户:用户名为br123
然后在该用户下创建serviceAccount
现在使用的秘钥是:
ACCESS_KEY:1hjBhmvm8eXWeSPa0bDy
SECRET_ACCESS_KEY:oxzXcazaCNSr5VgUmEZqlmdj7KJZJdH6FvnQtaKA
8、使用br备份到MinIO存储中
cat br_backup.sh
#!/bin/bash
export AWS_ACCESS_KEY_ID=nXl4EgFuVYk5jZjq
export AWS_SECRET_ACCESS_KEY=A1aeZwuynRX47TSe31d9SFlHq6H0d89J
######参数#########
#tidb的pd地址ip:port,多个端口用英文逗号分隔
pdPath=PDIP1:2379,PDIP2:2379,PDIP3:2379
#minio的bucket
minioPath="brbucket"
#日志保存地址 不需要/结尾
logPath=/home/tidb
#br执行文件的地址
brPath=/home/tidb/tidb-toolkit-v8.5.0-linux-amd64/bin/
#minio服务器地址http://ip:port
minioServer="http://IP:9000"
#执行命令
commandStr0="${brPath}br backup full
--pd \"${pdPath}\"
--storage \"s3://${minioPath}/fullbackup\"
--s3.endpoint ${minioServer}"
commandStr0
挂载 NFS 盘
1、安装nfs软件包
sudo yum install nfs-utils
2、编辑配置文件
vim /etc/exports
/nfs_share *(rw,sync,no_root_squash,no_subtree_check)
3、重启生效
systemctl restart nfs-kernel-server
4、挂载到其他服务器上
#创建挂载目录
mkdir /data_nfs
mount -t nfs server_ip:/nfs_share /data_nfs
编辑/etc/fstab文件,实现自动挂载
server_ip:/nfs_share /data_nfs nfs defaults 0 0
验证是否挂载成功
df -h
5、使用br备份到nas盘上
./br backup full --pd "${PD_IP}:2379" --storage "local:///data_nfs/backupfull"