0
Help us understand the problem. What are the problem?

More than 1 year has passed since last update.

posted at

mysql dump fileをテーブルごとに分ける

ここからお借りした

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

tar.gzになってれば先に gunzip -x して .sql にしておく。

叩く

chmod +x split.sh
./split.sh dump.sql

こんなファイルができる

-rw-r--r--   1 root root  840 2020-08-23 23:42:27 table00
-rw-r--r--   1 root root  16G 2020-08-23 23:44:23 table01
-rw-r--r--   1 root root 171M 2020-08-23 23:44:24 table02
-rw-r--r--   1 root root  82M 2020-08-23 23:44:25 table03
-rw-r--r--   1 root root  12G 2020-08-23 23:46:06 table04
-rw-r--r--   1 root root 2.9M 2020-08-23 23:46:06 table05
-rw-r--r--   1 root root 5.2K 2020-08-23 23:46:06 table06
-rw-r--r--   1 root root 405K 2020-08-23 23:46:06 table07
-rw-r--r--   1 root root 5.8K 2020-08-23 23:46:06 table08
-rw-r--r--   1 root root 5.6K 2020-08-23 23:46:06 table09
-rw-r--r--   1 root root  47M 2020-08-23 23:46:06 table10
-rw-r--r--   1 root root  17M 2020-08-23 23:46:06 table11
-rw-r--r--   1 root root  27K 2020-08-23 23:46:06 table12

table名はなんかgrepすればわかる。mysqldumpはalphabet順で出すみたいなんで、だいたい数字のままでもわかる

そしたらimportする

cat table* | mysql -h mysql-server mydb

おわり

Why not register and get more from Qiita?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
Sign upLogin
0
Help us understand the problem. What are the problem?