mysql常用的脚本集合
# 过滤某些表数据导出
#!/bin/bash
MYSQL='/usr/bin/mysql'
MYSQL_DUMP='/usr/bin/mysqldump'
BACKUP_DIR='/tmp/scripts'
MYSQL_HOST='地址'
MYSQL_USER=账号
MYSQL_PWD=密码
MYSQL_PORT=3306
DATE=$(date +%Y%m%d)
#参数说明
#mysqldump --skip-add-drop-table --no-create-info只备份数据而不包含表结构,不包含删除表的指令
#1. 获取所有表
#2. 遍历使用mysqldump获取数据,遍历过程判断哪些表数据不需要
# test11主库导出
TABLES=$(mysql -u${MYSQL_USER} -h${MYSQL_HOST} -P${MYSQL_PORT} -p${MYSQL_PWD} -D test11 -e "show tables;" | awk '{print $1}' | grep -v '^Tables')
for table in $TABLES
do
#if [[ " ${IGNIOR_TABLES[@]} " =~ " ${table} " ]];then
# echo "$table don't need to be backup"
# continue
#else
echo "use test11;" >> test11_data.sql
mysqldump -u ${MYSQL_USER} -h${MYSQL_HOST} -P${MYSQL_PORT} -p${MYSQL_PWD} test11 $table --skip-add-drop-table --no-create-info >> test11_data.sql
echo "Backup $table successful"
#fi
done
# 不导出的表
declare -a IGNIOR_TABLES=("user11" "user22" "ueser33")
#遍历所有co数据库
DB_LIST=$($MYSQL -u${MYSQL_USER} -p${MYSQL_PWD} -P${MYSQL_PORT} -h${MYSQL_HOST} -e "show databases;" | awk -F '|' '/co/{print $1}'|xargs)
for DB_NAME in ${DB_LIST};do
echo "Starting backup DB: ${DB_NAME}"
#1. 遍历分库,导出所有表数据
CO_TABLES=$(mysql -u${MYSQL_USER} -h${MYSQL_HOST} -P${MYSQL_PORT} -p${MYSQL_PWD} -D ${DB_NAME} -e "show tables;" | awk '{print $1}' | grep -v '^Tables')
echo "use $DB_NAME;" >> ${DB_NAME}.sql
for table in ${CO_TABLES[@]};do
#检查当前表是否在排除外
skip_table=false
for IGNIOR_TABLE in "${IGNIOR_TABLES[@]}";do
if [ "$table" = "$IGNIOR_TABLE" ];then
skip_table=true
break
fi
done
#如果表在排除列表中,则跳过导出
if [ "$skip_table" = true ];then
echo "跳过表$table"
else
#echo "truncate $table" >> ${DB_NAME}.sql
mysqldump -u $MYSQL_USER -h${MYSQL_HOST} -P${MYSQL_PORT} -p${MYSQL_PWD} ${DB_NAME} $table --skip-add-drop-table --no-create-info >> ${DB_NAME}.sql
echo "Backup $table successful"
fi
done
echo "Done ${DB_NAME}!"
done
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# 清理一些带日期的表
#!/bin/bash
# MySQL数据库连接信息
MYSQL_USER="账号"
MYSQL_PASSWORD="密码"
MYSQL_HOST="地址"
MYSQL_PORT="端口"
MYSQL_DATABASE="数据库"
# 清除 user11_ 日期的所有数据【2023年以前】
DELETE_QUERY="select table_name from information_schema.tables where table_schema = '数据库' and table_name like 'user11_%' and substring(table_name, -8) <= '20221231';"
TABLES=$(mysql -u$MYSQL_USER -p$MYSQL_PASSWORD -h$MYSQL_HOST -P$MYSQL_PORT -N -e "$DELETE_QUERY")
#执行删除操作
for TABLE in $TABLES
do
DELETE_QUERY="TRUNCATE TABLE $TABLE"
echo $DELETE_QUERY
mysql -u$MYSQL_USER -p$MYSQL_PASSWORD -h$MYSQL_HOST -P$MYSQL_PORT -D$MYSQL_DATABASE -e "$DELETE_QUERY"
done
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
# 备份一些表数据,清理一些表数据
#!/bin/bash
DB_USER="账号"
DB_PASS="密码"
DB_NAME="数据库"
DB_HOST="地址"
#备份表
backup_table(){
table_names=("user11" "user22")
for table_name in "${table_names[@]}";do
backup_file="$table_name-$(date +'%Y%m%d%H%M%S').sql"
mysqldump -h"$DB_HOST" -u"$DB_USER" -p"$DB_PASS" "$DB_NAME" "$table_name" > "$backup_file"
if [ $? -eq 0 ];then
echo "备份表 $table_name 成功,备份文件名:$backup_file"
else
echo "备份表 $table_name 失败"
fi
done
}
#清空表数据
clear_table(){
table_names=("user11" "user22")
for table_name in "${table_names[@]}";do
mysql -h"$DB_HOST" -u"$DB_USER" -p"$DB_PASS" "$DB_NAME" -e "truncate table $table_name" "$DB_NAME"
if [ $? -eq 0 ];then
echo "清除表 $table_name 成功"
else
echo "清除表 $table_name 失败"
fi
done
}
backup_table
clear_table
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
上次更新: 2024/01/11, 19:45:49