-
Notifications
You must be signed in to change notification settings - Fork 0
feat(历史数据归档): 增加历史数据归档脚本说明 #60
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
xiaorongnie
wants to merge
2
commits into
master
Choose a base branch
from
feat/pt/nxr
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,59 @@ | ||
| # pt-archiver 数据归档脚本 | ||
|
|
||
| 该脚本用于将 MySQL 表中的历史数据从源数据库归档到目标数据库,基于 `percona-toolkit` 的 `pt-archiver` 工具,并以 Docker 方式运行。 | ||
|
|
||
| ## 脚本内容 | ||
|
|
||
| ```bash | ||
| #!/bin/bash | ||
| set -euo pipefail | ||
| #set -x | ||
|
|
||
| # ====== 源数据库 ====== | ||
| SRC_HOST=127.0.0.1 | ||
| SRC_PORT=3306 | ||
| SRC_USER=root | ||
| SRC_PASS=xxxx | ||
| SRC_DB=maintain | ||
| SRC_TABLE=jtt808_position | ||
|
|
||
| # ====== 归档目标数据库 ====== | ||
| DST_HOST=127.0.0.1 | ||
| DST_PORT=3306 | ||
| DST_USER=root | ||
| DST_PASS=xxxx | ||
| DST_DB=maintain | ||
| DST_TABLE=jtt808_position_history | ||
|
|
||
| # ====== 归档参数:开始ID,结束ID,每次批量归档数量 ====== | ||
| START_ID=0 | ||
| END_ID=100000 | ||
| BATCH_SIZE=30000 | ||
|
|
||
| LOG_DIR=/var/log/pt-archiver | ||
| mkdir -p "$LOG_DIR" | ||
| LOG_FILE="$LOG_DIR/archive_$(date +%F_%H%M%S).log" | ||
|
|
||
| # ====== 构造 where 条件 ====== | ||
| WHERE="id > ${START_ID} AND id <= ${END_ID}" | ||
|
|
||
| # ====== 执行归档 ====== | ||
| docker run --rm -t --network host \ | ||
| -e LANG=C.UTF-8 -e PERL_UNICODE=SDA \ | ||
| percona/percona-toolkit \ | ||
| pt-archiver \ | ||
| --source h=$SRC_HOST,P=$SRC_PORT,u=$SRC_USER,p=$SRC_PASS,D=$SRC_DB,t=$SRC_TABLE,L=1,A=utf8mb4 \ | ||
| --dest h=$DST_HOST,P=$DST_PORT,u=$DST_USER,p=$DST_PASS,D=$DST_DB,t=$DST_TABLE,L=1,A=utf8mb4 \ | ||
| --charset=utf8mb4 \ | ||
| --where "$WHERE" \ | ||
| --limit $BATCH_SIZE \ | ||
| --bulk-insert \ | ||
| --nosafe-auto-increment \ | ||
| --no-delete \ | ||
| --progress 10000 \ | ||
| --sleep 1 \ | ||
| --statistics \ | ||
| 2>&1 | tee -a "$LOG_FILE" | ||
|
|
||
| echo "Archive finished at $(date)" >> "$LOG_FILE" | ||
| ``` |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
改MD说明文档