2
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

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

Posted at

ここからお借りした

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

おわり

2
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?