Skip to content

Commit 3ceeae9

Browse files
committed
feat(历史数据归档): 增加历史数据归档脚本
1 parent 6c709a4 commit 3ceeae9

2 files changed

Lines changed: 53 additions & 0 deletions

File tree

mysql-backup/pt-archiver.sh

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
#!/bin/bash
2+
set -euo pipefail
3+
#set -x
4+
5+
# ====== 基础配置 ======
6+
SRC_HOST=127.0.0.1
7+
SRC_PORT=3306
8+
SRC_USER=root
9+
SRC_PASS=xxxx
10+
SRC_DB=maintain
11+
SRC_TABLE=jtt808_position
12+
13+
DST_HOST=127.0.0.1
14+
DST_PORT=3306
15+
DST_USER=root
16+
DST_PASS=xxxx
17+
DST_DB=maintain
18+
DST_TABLE=jtt808_position_history
19+
20+
# ====== 归档参数 ======
21+
START_ID=0
22+
END_ID=100000
23+
BATCH_SIZE=30000
24+
25+
LOG_DIR=/home/docker-compose/opt/pt-archiver
26+
mkdir -p "$LOG_DIR"
27+
LOG_FILE="$LOG_DIR/archive_$(date +%F_%H%M%S).log"
28+
29+
# ====== 构造 where 条件 ======
30+
WHERE="id > ${START_ID} AND id <= ${END_ID}"
31+
32+
# ====== 执行 ======
33+
docker run --rm -t --network host \
34+
-e LANG=C.UTF-8 -e PERL_UNICODE=SDA \
35+
percona/percona-toolkit \
36+
pt-archiver \
37+
--source h=$SRC_HOST,P=$SRC_PORT,u=$SRC_USER,p=$SRC_PASS,D=$SRC_DB,t=$SRC_TABLE,L=1,A=utf8mb4 \
38+
--dest h=$DST_HOST,P=$DST_PORT,u=$DST_USER,p=$DST_PASS,D=$DST_DB,t=$DST_TABLE,L=1,A=utf8mb4 \
39+
--charset=utf8mb4 \
40+
--where "$WHERE" \
41+
--limit $BATCH_SIZE \
42+
--bulk-insert \
43+
--nosafe-auto-increment \
44+
--no-delete \
45+
--progress 10000 \
46+
--sleep 1 \
47+
--statistics \
48+
2>&1 | tee -a "$LOG_FILE"
49+
50+
echo "Archive finished at $(date)" >> "$LOG_FILE"

mysql8/conf/my.cnf

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,9 @@
44

55
[mysqld]
66

7+
# 开启 LOAD DATA LOCAL INFILE
8+
local_infile = ON
9+
710
# 不区分大小写 注:mysql8.x创建数据库后不能更改
811
lower_case_table_names = 1
912

0 commit comments

Comments
 (0)