mysqlbashcsplit

How to split a big mysqldump file for get each structure and data separately?


Based on https://gist.github.com/jasny/1608062#file-mysql_splitdump-sh

#!/bin/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
fi

if [ $# -ge 2 ] ; then
  csplit -s -ftable $1 "/-- Table structure for table/" "%-- Table structure for table \`$2\`%" "/-- Table structure for table/" "%40103 SET TIME_ZONE=@OLD_TIME_ZONE%1"
else
  csplit -s -ftable $1 "/-- Table structure for table/" {*}
fi

[ $? -eq 0 ] || exit

mv table00 head

FILE=`ls -1 table* | tail -n 1`
if [ $# -ge 2 ] ; then
  mv $FILE foot
else
  csplit -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`
  cat head $FILE foot > "$NAME.sql"
done

rm head foot table*

I want to split my big mysqldump file for get each table structure and table data separately.

I'm no expert in bash script and I search the pattern for split the dump file to get several files like :


Solution

  • You can also split dump with awk script: cat dumpfile | gawk -f script.awk (or ./script.awk < dumpfile if you make it executable). It will create pairs of tablename.schema.sql, tablename.data.sql files in current directory for each table in mysqldump in one pass.

    Warning: there is no special handling for characters in table names - they are used as is in filenames.

    #!/usr/bin/gawk -f
    BEGIN {
        is_data=0; is_struct=0; is_header=1; is_footer=0; i=0; tname="UNKNOWN_TABLE";
    }
    
    /^($|-- |\/\*\!)/ && !/^-- Table struct/ {
      # accumulate header lines
        if (is_header && !is_struct) { header[i]= $0; i++; } 
    }
    
    /^-- Table structure for table/ {
        is_struct=1; is_header=0; i=0; is_data=0;
        tname=substr($6,2,length($6)-2);
        tables[tname]=1;
        print "--" > tname".schema.sql"
        for (i in header) print header[i] >> tname".schema.sql";
        ###print "STRUCT:", tname;
    }
    
    /^-- Dumping data for table/ {
        is_data=1; is_header=0; is_struct=0;
        tname=substr($6,2,length($6)-2);
        print "--" > tname".data.sql"
        for (i in header) print header[i] >> tname".data.sql";
        ###print "DATA_START for table ",tname;
        i=0
    }
    
    { if (is_struct) { print $0 >> tname".schema.sql"} }
    { if (is_data) { print $0 >> tname".data.sql" } }
    
    /^UNLOCK TABLES/ {
        is_data=0; is_struct=0;
        ###print "DATA_END for table ",$tname;
    }
    
    /^(--|\/\*.40[0-9]{3} SET .+\*\/;$)/ {
        if (!(is_header || is_struct || is_data)) {
            # accumulate footer lines
            is_footer=1;
            footer[$0]=1;
        }
    }
    
    END { 
        # append footer to files
        for (t in tables) {
            for (f in footer) {
                print f >> t".schema.sql";
                print f >> t".data.sql";
            }
        }
    }