Skip to content

Instantly share code, notes, and snippets.

@kenchou
Last active April 13, 2023 07:36
Show Gist options
  • Save kenchou/0ec3486ac588f4cb2cd8494b565919d5 to your computer and use it in GitHub Desktop.
Save kenchou/0ec3486ac588f4cb2cd8494b565919d5 to your computer and use it in GitHub Desktop.
重组 mysqldump 文件,用于 diff 查看差异
#!/usr/bin/env bash
####
# Split MySQL dump SQL file into one file per table
# based on http://blog.tty.nl/2011/12/28/splitting-a-database-dump
####
if [ $# -lt 1 ] ; then
echo "USAGE $0 DUMP_FILE [TABLE]"
exit 1
fi
CSPLIT=csplit
READLINK=readlink
SED=sed
if [[ "Darwin" == $(uname) ]]; then
# Mac OS: `brew install coreutils`
CSPLIT=gcsplit
READLINK=greadlink
SED=gsed
if ! which "$CSPLIT" 2>&1 > /dev/null || ! which "$READLINK" 2>&1 > /dev/null; then
echo "Please install coreutils first!"
echo "brew install coreutils"
exit 2
fi
fi
SRC_FILE=$($READLINK -f $1)
WORK_DIR=$(mktemp -d -t split-mysqldump.XXXXXXXXXX)
pushd $WORK_DIR > /dev/null
if [ $# -ge 2 ] ; then
$CSPLIT -n4 -s -ftable $SRC_FILE "/-- Table structure for table/" "%-- Table structure for table \`$2\`%" "/-- Table structure for table/" "%40103 SET TIME_ZONE=@OLD_TIME_ZONE%1"
else
$CSPLIT -n4 -s -ftable $SRC_FILE "/-- Table structure for table/" {*}
fi
[ $? -eq 0 ] || exit
mv table0000 _head
FILE=$(ls -1 table* | tail -n 1)
if [ $# -ge 2 ] ; then
mv "$FILE" _foot
else
$CSPLIT -n4 -b '%d' -s -f"$FILE" "$FILE" "/40103 SET TIME_ZONE=@OLD_TIME_ZONE/" {*}
mv ${FILE}1 _foot
fi
for FILE in `ls -1 table*`; do
NAME=`head -n1 "$FILE" | cut -d$'\x60' -f2`
mv "$FILE" "$NAME.sql"
# sort KEY
KEY_TMP_FILE=$(mktemp -t split-mysqldump.$NAME.KEY)
# resort KEYs, ensure comma at line end excpet the last line
grep '^\s*KEY ' "$NAME.sql" | sort | $SED 's/,$//g; $!s/$/,/' > $KEY_TMP_FILE
# remove old KEYs
$SED -i '/^\s*KEY /d' "$NAME.sql"
# insert sorted KEYs
$SED -i "/^\s*PRIMARY KEY / r $KEY_TMP_FILE" "$NAME.sql"
done
#rm table* > /dev/null 2>&1
# remove tables that needn't to check
rm *_m.sql {alliance,bushimo}_*.sql event_{live,player}*_ranking_*.sql *_{archive,old,removed}_*.sql *_16_*14????.sql > /dev/null 2>&1
# Merge to single file and remove AUTOINCREMENT
cat _head *.sql _foot | $SED 's/ AUTO_INCREMENT=[0-9]*\b//'
popd > /dev/null
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment