目的
汇聚多个SQL文件到一个SQL文件,导入数据库
背景
工作需要从mysql数据库迁移数据到类mysql数据库中,迁移数据只能用执行语句。
通过datagrip 导出mysql插入数据,导出数据是按照每张表用一个sql文件进行存放,需要进行insert 语句汇总。
方便数据的迁移。
使用
demo.sh 内容如下
#!/usr/bin/env bash
fromPath=$1
destFile=~/out.sql
function SQLFileToDest() {
cat $1 | while read line
do
# shellcheck disable=SC1073
echo $line
if [[ $line == INSERT* ]]; then
echo $line >> $destFile
fi
done
}
function scanPath() {
cd $fromPath
for file in `find $PWD `
do
if [ "${file##*.}"x = "sql"x ] ;then
SQLFileToDest $file
fi
done
}
scanPath fromPath
执行
## shellin为sql文件列表存放的路径, 生成的数据存放在~/out.sql
./demo.sh ~/shellin