代码拉取完成,页面将自动刷新
#!/bin/bash
MySQL_user='root'
MySQL_host='127.0.0.1'
MySQL_password='123456'
MySQL_port=3306
MySQL_database='test' # 备份表所在库
MySQL_bak_dir='/root/dba/bak' # 保存备份文件的地址
MySQL_SqlInfo_file='/root/1_sql_info.txt' # 需要执行的 SQL 文件
#格式化后SQL保存文件
MySQL_Read_SqlFile='/root/.mysql_sql_tmp.txt'
# 获取时间
Date=$(date +%y%m%d_%H%M%S)
# 获取mysqldump命令路径
MysqlDump_Command=$(/usr/bin/which mysqldump)
# 获取mysql命令路径
Mysql_Command=$(/usr/bin/which mysql)
# 拼接MySQL连接参数
MySQL_Connection="${Mysql_Command} -u"${MySQL_user}" -p"${MySQL_password}" -h${MySQL_host} -P${MySQL_port}"
# 总行数大于这个值就进行提示
Rows_Sum=10
# 过滤语法##########################################################################################################################################
function Match_Regular() {
Delete_Line=$(echo ${line}|egrep -ci '^delete\s+from\s+(`)?(\w+)?(`)?(.)?(`)?\w+(`)?\s+where\s+.*;')
Create_index_Line=$(echo ${line}|egrep -ci '^CREATE\s+(UNIQUE|FULLTEXT|SPATIAL)?\s*INDEX\s+\w+\s+ON\s+(`)?(\w+)?(`)?(.)?(`)?\w+(`)?\s*\(\w+\)\s*;')
Drop_index_Line=$(echo ${line}|egrep -ci '^drop\s+INDEX\s+\w+\s+ON\s+(`)?(\w+)?(`)?(.)?(`)?\w+(`)?\s*;')
Insert_Line=$(echo ${line}|egrep -ci '^insert\s+into\s+(`)?(\w+)?(`)?(.)?(`)?\w+(`)?\s*(\(.*\))?\s+values\s*(.*)\s*;')
Create_table_Line=$(echo ${line}|egrep -ic '^create\s+table\s+(\`)?(\w+)?(\`)?(.)?(\`)?\w+(\`)?\s*(.*).*;')
Alter_Line=$(echo ${line} | grep -iPc '^alter\s+table\s+(`)?(\w+)?(`)?(.)?(`)?\w+(`)?\s+(ADD|MODIFY|DROP|RENAME|ENGINE|CHARSET|AUTO_INCREMENT|COMMENT)?.*;')
Update_Line=$(echo ${line}|egrep -ic '^update\s+(`?[a-zA-Z0-9_]+`?\.)?(`?[a-zA-Z0-9_]+`?)\s+(join\s+(`?[a-zA-Z0-9_]+`?\.)?(`?[a-zA-Z0-9_]+`?)\s+on\s+.+\s+)?set\s+(.*)\s+where\s+(`)?\w+(`)?.*;')
Bak_table_Line=$(echo ${line}|egrep -ic '^(`)?[a-zA-Z0-9_]*(`)?$')
}
Bak_table_Line_Format=$(egrep -v '^$|^#|^\s+#' ${MySQL_SqlInfo_file}|egrep -ic '^(`)?[a-zA-Z0-9_]*(`)?$')
# 2、检查语法
function Check_Syntax() {
while read line
do
# 调用正则配置语法
Match_Regular
if [[ ${Insert_Line} != 1 && ${Delete_Line} != 1 && ${Update_Line} != 1 && ${Alter_Line} != 1 && ${Create_index_Line} != 1 && ${Drop_index_Line} != 1 && ${Create_table_Line} != 1 && ${Bak_table_Line_Format} = 0 ]];then
echo "${MySQL_SqlInfo_file} 文件如下内容,格式错误:"
echo ' '
echo "${line}"|fmt
exit 1
fi
done < ${MySQL_Read_SqlFile}
}
# 检查库表行数
function Check_database_table_rows() {
while read -u 3 line
do
# 调用正则配置语法
Match_Regular
# delete 语句####################################################################################################################################
if [[ ${Delete_Line} -eq 1 ]];then
# 获取where条件
Where_Info=$(echo "${line}"|sed -n 's/^delete\s\+from\s\+\([`a-zA-Z0-9_.]*\)\s\+WHERE\s\+\(.*\);/\2/Ip')
# 获取表名
MySQL_TableName=$(echo "${line}"|sed -n 's/^delete\s\+from\s\+\([`a-zA-Z0-9_.]*\)\s\+WHERE\s\+\(.*\);/\1/Ip'|sed 's#`##g'|awk -F'.' '{print $NF}')
# 获取库表是否存在
${MySQL_Connection} -N -e "desc ${MySQL_database}.${MySQL_TableName}" &>/dev/null
if [[ $? -ne 0 ]];then
echo "${MySQL_database}.${MySQL_TableName} 库表不存在或mysql无法连接"
exit 1
fi
# 获取备份的行数
Sql_Count=$(${MySQL_Connection} -N -e "select count(1) from ${MySQL_database}.${MySQL_TableName} where ${Where_Info}" 2>/dev/null)
if [[ ${Sql_Count} -ge ${Rows_Sum} ]];then
echo "${MySQL_TableName} 表备份的数据量为${Sql_Count}"
read -t 20 -e -p "是否继续(y/n)" Answer
case $Answer in
Y | y)
echo "确认继续"
;;
N | n)
echo "确认退出"
exit 1
;;
*)
echo "输入有误"
exit 1
;;
esac
fi
# update 语句####################################################################################################################################
elif [[ ${Update_Line} -eq 1 ]];then
# 获取where条件
Where_Info=$(echo "${line}"|sed -E -n 's/^update\s+(`?[a-zA-Z0-9_]+`?\.)?(`?[a-zA-Z0-9_]+`?)\s+(join\s+(`?[a-zA-Z0-9_]+`?\.)?(`?[a-zA-Z0-9_]+`?)\s+on\s+.+\s+)?set\s+(.*)\s+where\s+(.*);/\7/Ip')
# 获取表名
MySQL_TableName=$(echo "${line}"|sed -E -n 's/^update\s+(`?[a-zA-Z0-9_]+`?\.)?(`?[a-zA-Z0-9_]+`?)\s+(join\s+(`?[a-zA-Z0-9_]+`?\.)?(`?[a-zA-Z0-9_]+`?)\s+on\s+.+\s+)?set\s+(.*)\s+where\s+(.*);/\2/Ip'|sed 's/`//g')
# 获取库表是否存在
${MySQL_Connection} -N -e "desc ${MySQL_database}.${MySQL_TableName}" &>/dev/null
if [[ $? -ne 0 ]];then
echo "${MySQL_database}.${MySQL_TableName} 库表不存在或mysql无法连接"
exit 1
fi
# 获取备份的行数
Sql_Count=$(${MySQL_Connection} -N -e "select count(1) from ${MySQL_database}.${MySQL_TableName} where ${Where_Info}" 2>/dev/null)
if [[ ${Sql_Count} -ge ${Rows_Sum} ]];then
echo "${MySQL_TableName} 表备份的数据量为${Sql_Count}"
read -t 20 -e -p "是否继续(y/n)" Answer
case $Answer in
Y | y)
echo "确认继续"
;;
N | n)
echo "确认退出"
exit 1
;;
*)
echo "输入有误"
exit 1
;;
esac
fi
# insert 语句####################################################################################################################################
elif [[ ${Insert_Line} -eq 1 ]];then
MySQL_TableName=$(echo "${line}"|egrep -i 'INSERT\s+INTO\s+.*\s+VALUES.*;'|sed -n 's/insert\s\+into\s\+\([^ ]*\).*values\s*(.*)\s*;/\1/Ip'|awk -F'(' '{print $1}'|awk -F'.' '{print $NF}'|sed 's#`##g')
# 获取库表是否存在
${MySQL_Connection} -N -e "desc ${MySQL_database}.${MySQL_TableName}" &>/dev/null
if [[ $? -ne 0 ]];then
echo "${MySQL_database}.${MySQL_TableName} 库表不存在或mysql无法连接"
exit 1
fi
# alter 语句####################################################################################################################################
# 提取DML语句表名
elif [[ ${Alter_Line} -eq 1 ]];then
MySQL_TableName=$(echo "${line}"|sed -En 's/^alter\s+table\s+(`?[a-zA-Z0-9_]+`?\.)?(`?[a-zA-Z0-9_]+`?)\s+(ADD|MODIFY|DROP|RENAME|ENGINE|CHARSET|AUTO_INCREMENT|COMMENT)?.*;/\2/Ip'|sed 's/`//g')
# 获取库表是否存在
${MySQL_Connection} -N -e "desc ${MySQL_database}.${MySQL_TableName}" &>/dev/null
if [[ $? -ne 0 ]];then
echo "${MySQL_database}.${MySQL_TableName} 库表不存在或mysql无法连接"
exit 1
fi
# 获取表的行数
Sql_Count=$(${MySQL_Connection} -N -e "select count(1) from ${MySQL_database}.${MySQL_TableName}" 2>/dev/null)
if [[ ${Sql_Count} -ge ${Rows_Sum} ]];then
echo "${MySQL_TableName} 表备份的数据量为${Sql_Count}"
read -t 20 -e -p "是否继续(y/n)" Answer
case $Answer in
Y | y)
echo "确认继续"
;;
N | n)
echo "确认退出"
exit 1
;;
*)
echo "输入有误"
exit 1
;;
esac
fi
# create index语句支持#############################################################################################################################
# 提取create index 语句表名
elif [[ ${Create_index_Line} -eq 1 ]];then
MySQL_TableName=$(echo "${line}"|egrep -i 'CREATE\s+(UNIQUE|FULLTEXT|SPATIAL)?\s*INDEX\s+.*\s+ON*\(*\)*'|sed -n "s/create\s\+.*\s*index\s\+.*\s\+ON\s\+\([^ ]*\)\s*(.*).*/\1/Ip"|awk -F. '{print $NF}'|sed 's/`//g')
MySQL_TableIndex=$(echo "${line}"|egrep -i 'CREATE\s+(UNIQUE|FULLTEXT|SPATIAL)?\s*INDEX\s+.*\s+ON*\(*\)*'|sed -n "s/create\s\+.*\s*index\s\+\([^ ]*\)\s\+ON\s\+\([^ ]*\)\s*(.*).*/\1/Ip"|sed 's/`//g')
# 获取库表是否存在
${MySQL_Connection} -N -e "desc ${MySQL_database}.${MySQL_TableName}" &>/dev/null
if [[ $? -ne 0 ]];then
echo "${MySQL_database}.${MySQL_TableName} 库表不存在或mysql无法连接"
exit 1
fi
# 检查索引是否存在
Index_exists=$(${MySQL_Connection} -N -e "use ${MySQL_database};show index from ${MySQL_database}.${MySQL_TableName} where key_name=\"${MySQL_TableIndex}\";" 2>/dev/null | grep -ci "${MySQL_TableIndex}")
if [[ Index_exists -ne 0 ]];then
echo "${MySQL_database}.${MySQL_TableName} 表的${MySQL_TableIndex} 索引已存在"
exit 1
fi
# 获取表的行数
Sql_Count=$(${MySQL_Connection} -N -e "select count(1) from ${MySQL_database}.${MySQL_TableName}" 2>/dev/null)
if [[ ${Sql_Count} -ge ${Rows_Sum} ]];then
echo "${MySQL_TableName} 表备份的数据量为${Sql_Count}"
read -t 20 -e -p "是否继续(y/n)" Answer
case $Answer in
Y | y)
echo "确认继续"
;;
N | n)
echo "确认退出"
exit 1
;;
*)
echo "输入有误"
exit 1
;;
esac
fi
# drop index 语句支持#############################################################################################################################
# 提取drop index 语句表名
elif [[ ${Drop_index_Line} -eq 1 ]];then
MySQL_TableName=$(echo "${line}"|egrep -i '^drop\s+INDEX\s+\w+\s+ON\s+(`)?(\w+)?(`)?(.)?(`)?\w+(`)?\s*;'|sed -En 's/^drop\s+index\s+\w+\s+ON\s+(`?[a-zA-Z0-9_]+`?\.)?(`?[a-zA-Z0-9_]+`?).*;/\2/Ip'|sed 's/`//g')
MySQL_TableIndex=$(echo "${line}"|egrep -i '^drop\s+INDEX\s+(`?[a-zA-Z0-9_]+`?)\s+ON\s+(`)?(\w+)?(`)?(.)?(`)?\w+(`)?\s*;'|sed -En 's/^drop\s+index\s+`?(\w+)`?\s+ON\s+(`)?(\w+)?(`)?(.)?(`)?\w+(`)?\s*;/\1/Ip')
# 获取库表是否存在
${MySQL_Connection} -N -e "desc ${MySQL_database}.${MySQL_TableName}" &>/dev/null
if [[ $? -ne 0 ]];then
echo "${MySQL_database}.${MySQL_TableName} 库表不存在或mysql无法连接"
exit 1
fi
# 检查索引是否存在
Index_exists=$(${MySQL_Connection} -N -e "use ${MySQL_database};show index from ${MySQL_database}.${MySQL_TableName} where key_name=\"${MySQL_TableIndex}\";" 2>/dev/null | grep -ci "${MySQL_TableIndex}")
if [[ Index_exists -ne 1 ]];then
echo "${MySQL_database}.${MySQL_TableName} 表的${MySQL_TableIndex} 索引不存在"
exit 1
fi
# 获取表的行数
Sql_Count=$(${MySQL_Connection} -N -e "select count(1) from ${MySQL_database}.${MySQL_TableName}" 2>/dev/null)
if [[ ${Sql_Count} -ge ${Rows_Sum} ]];then
echo "${MySQL_TableName} 表备份的数据量为${Sql_Count}"
read -t 20 -e -p "是否继续(y/n)" Answer
case $Answer in
Y | y)
echo "确认继续"
;;
N | n)
echo "确认退出"
exit 1
;;
*)
echo "输入有误"
exit 1
;;
esac
fi
# Create table 语句支持###########################################################################################################################
elif [[ ${Create_table_Line} -eq 1 ]];then
MySQL_TableName=$(echo "${line}"|sed -n 's/create\s\+table\s\+\([^ ]*\)\s*(.*).*;/\1/Ip'|sed 's#`##g'|awk -F'.' '{print $NF}')
# 获取库表是否存在
${MySQL_Connection} -N -e "desc ${MySQL_database}.${MySQL_TableName}" &>/dev/null
if [[ $? -eq 0 ]];then
echo "${MySQL_database}.${MySQL_TableName} 库表已存在"
exit 1
fi
else
continue
fi
done 3< ${MySQL_Read_SqlFile}
echo 'SQL语法检查完成'
}
# 4、执行备份
function Execute_backup() {
while read line
do
# 调用正则配置语法
Match_Regular
# delete 语句#####################################################################################################################################
if [[ ${Delete_Line} -eq 1 ]];then
# 获取where条件
Where_Info=$(echo "${line}"|sed -n 's/^delete\s\+from\s\+\([`a-zA-Z0-9_.]*\)\s\+WHERE\s\+\(.*\);/\2/Ip')
# 获取表名
MySQL_TableName=$(echo "${line}"|sed -n 's/^delete\s\+from\s\+\([`a-zA-Z0-9_.]*\)\s\+WHERE\s\+\(.*\);/\1/Ip'|sed 's#`##g'|awk -F'.' '{print $NF}')
# 带where条件备份
${MysqlDump_Command} -u${MySQL_user} -p${MySQL_password} -h${MySQL_host} -P${MySQL_port} --no-create-info --single-transaction --skip-extended-insert --where="${Where_Info}" ${MySQL_database} ${MySQL_TableName} 2>/dev/null >> ${MySQL_bak_dir}/${MySQL_database}.${MySQL_TableName}_${Date}.sql
# 判断是否备份成功
if [[ ${PIPESTATUS[0]} -eq 0 && ${PIPESTATUS[1]} -eq 0 ]];then
echo "${MySQL_TableName} 表备份完成"
else
echo "${MySQL_TableName} 表备份失败"
exit 1
fi
# update 语句#####################################################################################################################################
elif [[ ${Update_Line} -eq 1 ]];then
# 获取where条件
Where_Info=$(echo "${line}"|sed -E -n 's/^update\s+(`?[a-zA-Z0-9_]+`?\.)?(`?[a-zA-Z0-9_]+`?)\s+(join\s+(`?[a-zA-Z0-9_]+`?\.)?(`?[a-zA-Z0-9_]+`?)\s+on\s+.+\s+)?set\s+(.*)\s+where\s+(.*);/\7/Ip')
# 获取表名
MySQL_TableName=$(echo "${line}"|sed -E -n 's/^update\s+(`?[a-zA-Z0-9_]+`?\.)?(`?[a-zA-Z0-9_]+`?)\s+(join\s+(`?[a-zA-Z0-9_]+`?\.)?(`?[a-zA-Z0-9_]+`?)\s+on\s+.+\s+)?set\s+(.*)\s+where\s+(.*);/\2/Ip'|sed 's/`//g')
# 带where条件备份
${MysqlDump_Command} -u${MySQL_user} -p${MySQL_password} -h${MySQL_host} -P${MySQL_port} --no-create-info --single-transaction --skip-extended-insert --where="${Where_Info}" ${MySQL_database} ${MySQL_TableName} 2>/dev/null >> ${MySQL_bak_dir}/${MySQL_database}.${MySQL_TableName}_${Date}.sql
# 判断是否备份成功
if [[ ${PIPESTATUS[0]} -eq 0 && ${PIPESTATUS[1]} -eq 0 ]];then
echo "${MySQL_TableName} ���备份完成"
else
echo "${MySQL_TableName} 表备份失败"
exit 1
fi
# alter 语句######################################################################################################################################
# 提取DML语句表名
elif [[ ${Alter_Line} -eq 1 ]];then
MySQL_TableName=$(echo "${line}"|sed -En 's/^alter\s+table\s+(`?[a-zA-Z0-9_]+`?\.)?(`?[a-zA-Z0-9_]+`?)\s+(ADD|MODIFY|DROP|RENAME|ENGINE|CHARSET|AUTO_INCREMENT|COMMENT)?.*;/\2/Ip'|sed 's/`//g')
# 备份表
${MysqlDump_Command} -u${MySQL_user} -p${MySQL_password} -h${MySQL_host} -P${MySQL_port} --single-transaction --skip-extended-insert ${MySQL_database} ${MySQL_TableName} 2>/dev/null >> ${MySQL_bak_dir}/${MySQL_database}.${MySQL_TableName}_${Date}.sql
# 判断是否备份成功
if [[ ${PIPESTATUS[0]} -eq 0 && ${PIPESTATUS[1]} -eq 0 ]];then
echo "${MySQL_TableName} 表备份完成"
else
echo "${MySQL_TableName} 表备份失败"
exit 1
fi
# create index 语句支持############################################################################################################################
# 提取DML语句表名
elif [[ ${Create_index_Line} -eq 1 ]];then
MySQL_TableName=$(echo "${line}"|egrep -i 'CREATE\s+(UNIQUE|FULLTEXT|SPATIAL)?\s*INDEX\s+.*\s+ON*\(*\)*'|sed -n "s/create\s\+.*\s*index\s\+.*\s\+ON\s\+\([^ ]*\)\s*(.*).*/\1/Ip"|awk -F. '{print $NF}'|sed 's/`//g')
# 备份表
${MysqlDump_Command} -u${MySQL_user} -p${MySQL_password} -h${MySQL_host} -P${MySQL_port} --single-transaction --skip-extended-insert ${MySQL_database} ${MySQL_TableName} 2>/dev/null >> ${MySQL_bak_dir}/${MySQL_database}.${MySQL_TableName}_${Date}.sql
# 判断是否备份成功
if [[ ${PIPESTATUS[0]} -eq 0 && ${PIPESTATUS[1]} -eq 0 ]];then
echo "${MySQL_TableName} 表备份完成"
else
echo "${MySQL_TableName} 表备份失败"
exit 1
fi
# drop index 语句支持#############################################################################################################################
# 提取DML语句表名
elif [[ ${Drop_index_Line} -eq 1 ]];then
MySQL_TableName=$(echo "${line}"|egrep -i '^drop\s+INDEX\s+\w+\s+ON\s+(`)?(\w+)?(`)?(.)?(`)?\w+(`)?\s*;'|sed -En 's/^drop\s+index\s+\w+\s+ON\s+(`?[a-zA-Z0-9_]+`?\.)?(`?[a-zA-Z0-9_]+`?).*;/\2/Ip'|sed 's/`//g')
# 备份表
${MysqlDump_Command} -u${MySQL_user} -p${MySQL_password} -h${MySQL_host} -P${MySQL_port} --single-transaction --skip-extended-insert ${MySQL_database} ${MySQL_TableName} 2>/dev/null >> ${MySQL_bak_dir}/${MySQL_database}.${MySQL_TableName}_${Date}.sql
# 判断是否备份成功
if [[ ${PIPESTATUS[0]} -eq 0 && ${PIPESTATUS[1]} -eq 0 ]];then
echo "${MySQL_TableName} 表备份完成"
else
echo "${MySQL_TableName} 表备份失败"
exit 1
fi
# insert 语句支持#################################################################################################################################
elif [[ ${Insert_Line} -eq 1 ]];then
MySQL_TableName=$(echo "${line}"|egrep -i 'INSERT\s+INTO\s+.*\s+VALUES.*;'|sed -n 's/insert\s\+into\s\+\([^ ]*\).*values\s*(.*)\s*;/\1/Ip'|awk -F'(' '{print $1}'|awk -F'.' '{print $NF}'|sed 's#`##g')
echo "${MySQL_TableName} 表为insert语句跳过备份"
continue
# Create table 语句支持###########################################################################################################################
elif [[ ${Create_table_Line} -eq 1 ]];then
MySQL_TableName=$(echo "${line}"|sed -nE 's/create\s+table\s+(`?[a-zA-Z0-9_]+`?\.)?(`?[a-zA-Z0-9_]+`?)\s*(.*).*;/\2/Ip'|sed 's/`//g')
echo "${MySQL_TableName} 表为create table语句跳过备份"
continue
# backup table 支持##############################################################################################################################
elif [[ ${Bak_table_Line} -eq 1 ]];then
MySQL_TableName=$(echo "${line}"|egrep -i '^(`)?[a-zA-Z0-9_]*(`)?$'|sed 's/`//g')
# 备份表
${MysqlDump_Command} -u${MySQL_user} -p${MySQL_password} -h${MySQL_host} -P${MySQL_port} --single-transaction --skip-extended-insert ${MySQL_database} ${MySQL_TableName} 2>/dev/null >> ${MySQL_bak_dir}/${MySQL_database}.${MySQL_TableName}_${Date}.sql
# 判断是否备份成功
if [[ ${PIPESTATUS[0]} -eq 0 && ${PIPESTATUS[1]} -eq 0 ]];then
echo "${MySQL_TableName} 表备份完成"
else
echo "${MySQL_TableName} 表备份失败"
exit 1
fi
fi
done < ${MySQL_Read_SqlFile}
}
# 5、执行SQL
function Execute_SQL() {
while read line
do
if [[ ${Bak_table_Line_Format} -ne 0 ]];then
continue
else
${MySQL_Connection} -N -vv -e "use ${MySQL_database};${line}" |fmt
if [[ ${PIPESTATUS[0]} -ne 0 || ${PIPESTATUS[1]} -ne 0 ]];then
echo "${line} SQL执行失败"
fi
fi
done < ${MySQL_Read_SqlFile}
}
# 将多行的SQL转化为一行
# 去除文件中的空行和注释行
function Format_SQL() {
if [[ ${Bak_table_Line_Format} -eq 0 ]];then
cat ${MySQL_SqlInfo_file}|egrep -v '^$|^#|^\s+#' |awk -F ';' BEGIN{RS=EOF}'{gsub(/\n/," ");print}' |awk 'BEGIN{i=1}{gsub(/;/,";""\n");i++;print}'|sed 's/^[[:space:]]*//'|grep -v '^$' >${MySQL_Read_SqlFile}
else
cat ${MySQL_SqlInfo_file}|egrep -v '^$|^#|^\s+#'|sed -n 's/\s*\([a-zA-Z0-9_]*\)\s*/\1/Ip'|sed 's/`//g' >${MySQL_Read_SqlFile}
fi
}
# 1、格式化SQL
Format_SQL
# 2、检查语法
Check_Syntax
# 3、检查库表行数
Check_database_table_rows
# 4、执行备份
Execute_backup
# 5、执行SQL
Execute_SQL
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。