Skip to content

Instantly share code, notes, and snippets.

@baisong
Created May 15, 2012 19:07
Show Gist options
  • Save baisong/2704261 to your computer and use it in GitHub Desktop.
Save baisong/2704261 to your computer and use it in GitHub Desktop.
Imports a huge mysqldump (.sql) file by removing a one-line error.
# db-remove-error-line.sh
# Imports a huge mysqldump (.sql) file by removing a one-line error.
# @see http://forums.mysql.com/read.php?103,411576,411576 (Error that inspired this workaround)
# @see http://dev.mysql.com/doc/refman/5.1/en/news-5-1-24.html (release notes of MySQL fix)
# [or] 2012-05-15
# Config - Replace this number with the number an error occurs on.
ERRORLINE='100000'
# Names filepaths to use.
## The original mysqldump file.
ORIGPATH='sync-orig.sql'
## The top half, before error line.
HEADPATH='sync-head.sql'
## The bottom half, after error line.
TAILPATH='sync-tail.sql'
## The two merged into one file.
BOTHPATH='sync-both.sql'
# Formats new files for top half and bottom half, omitting the error line.
## Counts lines in original dump.
LINES=`wc -l $ORIGPATH`
## Calculates lines before the error.
HEADN=`expr $ERRORLINE - 1`
## Calculates lines after the error.
TAILN=`expr $LINES - $HEADN`
# Creates new files for top and bottom and merges them.
## Creates top half.
head -n $HEADN $ORIGPATH > $HEADPATH
## Creates bottom half.
tail -n $TAILN $ORIGPATH > $TAILPATH
## Merges the two files together.
cat $HEADPATH $TAILPATH > $BOTHPATH
## Imports newly created mysqldump file.
mysql --database=$DBNAME --host=$DBHOST --user=$DBUSER --password=$DBPASS \
--default-character-set=utf8 < $BOTHPATH
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment