percona-backup-mongodb安装部署
环境:
OS:Centos 7
DB:4.2.9
192.168.118:28001 主
192.168.85:28002 从
192.168.85:28003 仲裁节点
副本集:replSet=replzhibo
说明:pbm不适用于单机部署的mongodb,需要副本集的方式
1.下载
wget https://downloads.percona.com/downloads/percona-backup-mongodb/percona-backup-mongodb-1.6.1/binary/tarball/percona-backup-mongodb-1.6.1-x86_64.tar.gz
wget https://downloads.percona.com/downloads/percona-backup-mongodb/percona-backup-mongodb-1.7.0/binary/tarball/percona-backup-mongodb-1.7.0-x86_64.tar.gz
2.0版本以上报如下错误:
[root@dsc1 percona-backup-mongodb-2.0.0]# ./pbm config --mongodb-uri="mongodb://test:test123@192.168.56.101:29001/admin?authSource=admin&replicaSet=myrepl" --file /opt/pbm_backup/config/pbm_config.yaml
Error: connect to mongodb: get config server connetion URI: mongo: no documents in result
2.解压
[root@localhost tmp]# tar -xvf percona-backup-mongodb-1.6.1-x86_64.tar.gz
percona-backup-mongodb-1.6.1/
percona-backup-mongodb-1.6.1/pbm
percona-backup-mongodb-1.6.1/pbm-agent
percona-backup-mongodb-1.6.1/pbm-speed-test
3.转移目录
[root@threegene-biz-uj4an8x-test soft]# mv percona-backup-mongodb-1.6.1 /opt/
4.查看版本
[root@percona-backup-mongodb-1.6.1]# pwd
/opt/percona-backup-mongodb-1.6.1
[root@percona-backup-mongodb-1.6.1]# ./pbm version
Version: 1.6.1
Platform: linux/amd64
GitCommit: aebbc24a03f21f9ddee6ebb72f510ff1d1c58fdc
GitBranch: release-1.6.1
BuildTime: 2021-11-03_12:12_UTC
GoVersion: go1.16.9
6.创建yaml配置文件,指定存储类型
[root@tmp]# mkdir -p /tmp/mongobak/
vi /tmp/pbm_config.yaml
storage:
type: filesystem
filesystem:
path: /tmp/mongobak/
7.创建备份账号
/usr/local/services/mongodb/bin/mongo localhost:28001
use admin
db.auth("hxl","hxl123");
db.getSiblingDB("admin").createRole({ "role": "pbmAnyAction",
"privileges": [
{ "resource": { "anyResource": true },
"actions": [ "anyAction" ]
}
],
"roles": []
});
db.getSiblingDB("admin").createUser({user: "pbmuser",
"pwd": "pbmuser",
"roles" : [
{ "db" : "admin", "role" : "readWrite", "collection": "" },
{ "db" : "admin", "role" : "backup" },
{ "db" : "admin", "role" : "clusterMonitor" },
{ "db" : "admin", "role" : "restore" },
{ "db" : "admin", "role" : "pbmAnyAction" }
]
});
7.初始化配置
[root@localhost percona-backup-mongodb-1.6.1]# ./pbm config --file /tmp/pbm_config.yaml --mongodb-uri="mongodb://pbmuser:pbmuser@192.168.1.118:28001"
pitr:
enabled: false
oplogSpanMin: 0
storage:
type: filesystem
filesystem:
path: /tmp/mongobak/
8.在每个实例启动pbm-agent代理进程
我这里只是在主节点上启动,192.168.1.118,必须启动代理进程才能进行备份
nohup /opt/percona-backup-mongodb-1.6.1/pbm-agent --mongodb-uri "mongodb://pbmuser:pbmuser@192.168.1.118:28001" >/tmp/pbm-agent.log 2>&1 &
192.168.1.85:28002
nohup /opt/percona-backup-mongodb-1.6.1/pbm-agent --mongodb-uri "mongodb://pbmuser:pbmuser@192.168.1.85:28002" >/tmp/pbm-agent28002.log 2>&1 &
192.168.1.85:28003 仲裁节点可以不需要启动
nohup /opt/percona-backup-mongodb-1.6.1/pbm-agent --mongodb-uri "mongodb://pbmuser:pbmuser@192.168.1.85:28003" >/tmp/pbm-agent28003.log 2>&1 &
15.备份
[root@localhost tmp]# /opt/percona-backup-mongodb-1.6.1/pbm backup --mongodb-uri="mongodb://pbmuser:pbmuser@192.168.1.118:28001/?replicaSet=replzhibo"
Starting backup '2022-06-02T06:19:00Z'....
Backup '2022-06-02T06:19:00Z' to remote store '/tmp/mongobak/' has started
备份完成后查看备份文件
[root@localhost mongobak]# ls -1
2022-06-02T06:19:00Z.pbm.json
2022-06-02T06:19:00Z_replzhibo.dump.s2
2022-06-02T06:19:00Z_replzhibo.oplog.s2
16.查看备份
在主库上查看,192.168.1.118:28001
/opt/percona-backup-mongodb-1.6.1/pbm list --mongodb-uri "mongodb://pbmuser:pbmuser@192.168.1.118:28001/?replicaSet=replzhibo"
[root@localhost tmp]# /opt/percona-backup-mongodb-1.6.1/pbm list --mongodb-uri "mongodb://pbmuser:pbmuser@192.168.1.118:28001/?replicaSet=replzhibo"
Backup snapshots:
2022-06-02T06:19:00Z [complete: 2022-06-02T06:19:05]
PITR <off>:
从库上也可以看得到,192.168.1.85:28002
[root@localhost percona-backup-mongodb-1.6.1]# /opt/percona-backup-mongodb-1.6.1/pbm list --mongodb-uri "mongodb://pbmuser:pbmuser@192.168.1.85:28002/?replicaSet=replzhibo"
Backup snapshots:
2022-06-02T06:19:00Z [complete: 2022-06-02T06:19:05]
PITR <off>:
17.尝试数据恢复
这里尝试先删除表,我们这里在主节点上执行,这里是在192.168.1.118上执行
[root@localhost mongobak]# /usr/local/services/mongodb/bin/mongo localhost:28001
MongoDB shell version v4.2.9
connecting to: mongodb://localhost:28001/test?compressors=disabled&gssapiServiceName=mongodb
Implicit session: session { "id" : UUID("38d25256-097f-42f2-a3b7-4ab01f19381a") }
MongoDB server version: 4.2.9
replzhibo:PRIMARY> use admin
switched to db admin
replzhibo:PRIMARY> db.auth("hxl","hxl123");
1
replzhibo:PRIMARY> show databases;
admin 0.001GB
config 0.000GB
local 0.014GB
mgdb_zhibo 0.009GB
replzhibo:PRIMARY> use mgdb_zhibo
switched to db mgdb_zhibo
replzhibo:PRIMARY> show tables;
t_chartroom_msg
t_chartroom_msg_client
tb_test01
replzhibo:PRIMARY> db.tb_test01.drop();
true
这里我们删除了集合tb_test01
18.尝试恢复
192.168.1.118上执行,一般是在主库上执行恢复
/opt/percona-backup-mongodb-1.6.1/pbm restore 2022-06-02T06:19:00Z --mongodb-uri "mongodb://pbmuser:pbmuser@192.168.1.118:28001/?replicaSet=replzhibo"
将主库的备份目录拷贝到从库,发现也可以恢复
scp -r /tmp/mongobak root@192.168.1.85:/tmp/
192.168.1.85上执行
/opt/percona-backup-mongodb-1.6.1/pbm restore 2022-06-02T06:19:00Z --mongodb-uri "mongodb://pbmuser:pbmuser@192.168.1.85:28002/?replicaSet=replzhibo"
这种方式报如下错误:
192.168.1.85上执行
[root@localhost mongobak]# /opt/percona-backup-mongodb-1.6.1/pbm restore 2022-06-02T06:19:00Z --mongodb-uri "mongodb://pbmuser:pbmuser@192.168.1.118:28001/?replicaSet=replzhibo"
2022-06-02T15:10:38.000+0800 D [restore/2022-06-02T06:19:00Z] get lock: duplicate operation: 6299b456acae92d51fe7ded7 [Snapshot restore]
2022-06-02T15:10:38.000+0800 D [restore/2022-06-02T06:19:00Z] skip: lock not acquired
2022-06-02T15:10:38.000+0800 E [restore/2022-06-02T06:19:00Z] unbale to run the restore while another operation running
重新登录mongodb可以查看到之前删除的表已经恢复
replzhibo:PRIMARY> show tables;
t_chartroom_msg
t_chartroom_msg_client
tb_test01
已经存在的表,恢复的时候是把原有的表删除后再恢复
从日志可以看出
2022-06-02T14:37:16.886+0800 dropping collection mgdb_zhibo.t_chartroom_msg_client before restoring
2022-06-02T14:37:16.892+0800 creating collection mgdb_zhibo.t_chartroom_msg_client with no metadata
数据恢复完成后会自动创建索引
2022-06-02T14:37:22.428+0800 restoring indexes for collection mgdb_zhibo.t_chartroom_msg from metadata
2022-06-02T14:37:22.429+0800 run create Index command for indexes: idx_create_time, idx_deleted, idx_room_id
19.查看状态
[root@localhost mongobak]# /opt/percona-backup-mongodb-1.6.1/pbm status --mongodb-uri "mongodb://pbmuser:pbmuser@192.168.1.85:28002/?replicaSet=replzhibo"
Cluster:
========
replzhibo:
- replzhibo/192.168.1.118:28001: pbm-agent v1.6.1 OK
- replzhibo/192.168.1.85:28002: pbm-agent v1.6.1 OK
- replzhibo/192.168.1.85:28003: pbm-agent v1.6.1 OK
PITR incremental backup:
========================
Status [OFF]
Currently running:
==================
(none)
Backups:
========
FS /tmp/mongobak/
Snapshots:
2022-06-02T06:19:00Z 3.54MB [complete: 2022-06-02T06:19:05]