2010-09-08 53 views
50

我想将每个mysql表转储到单独的文件中。该手册指出,其语法是自动将所有mysql表转储为单独的文件?

mysqldump [options] db_name [tbl_name ...] 

这表明您事先知道表名。我可以设置知道每个表名的脚本,但是说我在路上添加一个新表,并忘记更新转储脚本。然后我缺少一个或多个表的转储。

有没有办法自动将每个现有表转储到单独的文件?或者我将不得不做一些脚本 - 福;查询数据库,获取所有表名,并通过名称转储它们。

如果我去脚本fu路由,什么脚本语言可以访问mysql数据库?

回答

49

mysqldump命令行程序会为你做这件事 - 尽管the docs对此很不清楚。

需要注意的一件事是〜/ output/dir必须由拥有mysqld的用户写入。在Mac OS X:

sudo chown -R _mysqld:_mysqld ~/output/dir 
mysqldump --user=dbuser --password --tab=~/output/dir dbname 

运行以上后,您将有一个包含每个表的模式(create table语句),并包含数据tablename.txt文件一个文件tablename.sql。

如果你想与模式的转储只,加上--no数据标志:

mysqldump --user=dbuser --password --no-data --tab=~/output/dir dbname 
+6

嗯,我真的想要表中的数据转储为SQL插入也:Þ – user151841 2011-06-10 13:26:26

+0

但是这足够接近。 – user151841 2011-06-10 15:02:38

+0

http://stackoverflow.com/questions/9359053/mysqldumper-dumping-each-table-separately – kenorb 2013-10-03 17:01:12

0

我不是bash高手,但我只是做一个bash脚本。如果不知道MySQL,只要知道数据目录和数据库名称,就可以扫描所有.frm文件(该数据库/目录中的每个表的一个文件)以获取表列表。

我敢肯定有办法让它变得更轻松,并接受参数或什么,但这对我很好。

tables_in_a_db_to_sql.sh

#!/bin/bash 

database="this_is_my_database" 
datadir="/var/lib/mysql/" 
datadir_escaped="\/var\/lib\/mysql\/" 

all_tables=($(ls $datadir$database/*.frm | sed s/"$datadir_escaped$database\/"/""/g | sed s/.frm//g)) 

for t in "${all_tables[@]}"; do 
     outfile=$database.$t.sql 
     echo "-- backing up $t to $outfile" 
     echo "mysqldump [options] $database $t > $outfile" 
     # mysqldump [options] $database $t > $outfile 
done 

填写的[选项]和所需OUTFILE惯例,因为你需要,并取消了最后的mysqldump线。

+0

这段代码当我运行显示此错误信息:语法错误:“(”意想不到(也许它的行号:9)。任何建议 – imsyedahmed 2012-12-24 11:23:59

15

你可以做到这一点:

  1. 获取mysql中
  2. 数据库列表
  3. 转储每个数据库与mysqldump
# Optional variables for a backup script 
MYSQL_USER="root" 
MYSQL_PASS="something" 
BACKUP_DIR=/srv/backup/$(date +%Y-%m-%dT%H_%M_%S); 
test -d "$BACKUP_DIR" || mkdir -p "$BACKUP_DIR" 
# Get the database list, exclude information_schema 
for db in $(mysql -B -s -u $MYSQL_USER --password=$MYSQL_PASS -e 'show databases' | grep -v information_schema) 
do 
    # dump each database in a separate file 
    mysqldump -u $MYSQL_USER --password=$MYSQL_PASS "$db" | gzip > "$BACKUP_DIR/$db.sql.gz" 
done 
+0

当数据库名称有空格时无法工作:( – 2013-08-15 14:14:45

+2

@IgorLadela我在“$ db”周围添加了引号,尝试 – 2013-08-22 13:47:38

+6

问题是关于每个*表*在一个单独的文件中,而不是每个数据库在一个单独的文件。 – Flimm 2014-06-11 13:49:38

38

这是一个将表格数据作为SQL命令转储为单独的压缩文件的脚本。它不需要是MySQL服务器主机上,确实在剧本没有硬编码密码,并且是只为特定分贝,而不是服务器上的所有数据库的:

#!/bin/bash 

# dump-tables-mysql.sh 
# Descr: Dump MySQL table data into separate SQL files for a specified database. 
# Usage: Run without args for usage info. 
# Author: @Trutane 
# Ref: http://stackoverflow.com/q/3669121/138325 
# Notes: 
# * Script will prompt for password for db access. 
# * Output files are compressed and saved in the current working dir, unless DIR is 
# specified on command-line. 

[ $# -lt 3 ] && echo "Usage: $(basename $0) <DB_HOST> <DB_USER> <DB_NAME> [<DIR>]" && exit 1 

DB_host=$1 
DB_user=$2 
DB=$3 
DIR=$4 

[ -n "$DIR" ] || DIR=. 
test -d $DIR || mkdir -p $DIR 

echo -n "DB password: " 
read -s DB_pass 
echo 
echo "Dumping tables into separate SQL command files for database '$DB' into dir=$DIR" 

tbl_count=0 

for t in $(mysql -NBA -h $DB_host -u $DB_user -p$DB_pass -D $DB -e 'show tables') 
do 
    echo "DUMPING TABLE: $DB.$t" 
    mysqldump -h $DB_host -u $DB_user -p$DB_pass $DB $t | gzip > $DIR/$DB.$t.sql.gz 
    tbl_count=$((tbl_count + 1)) 
done 

echo "$tbl_count tables dumped from database '$DB' into dir=$DIR" 
+1

最好的解决方案。 ..我使用这个脚本的修改版本,它允许将密码作为env变量传递。如果有人需要此功能:http://sprunge.us/fBDL?bash – 2014-07-31 14:14:41

+1

imho最佳解决方案太:+ mysqldump --extended-insert = FALSE -u“$ mysql_user”-p“$ mysql_user_pw”“$ db”$ t \ \t | gzip>“$ tables_dump_dir/$ t.sql.gz” 有点慢,但适用于任何数据和任何行数据长度... – 2015-05-16 19:46:26

+0

thx!我用你的想法来生成备份所有数据库的备份脚本:https://github.com/rubo77/mysql-backup.sh/blob/master/mysql-backup.sh – rubo77 2015-11-06 05:43:05

4

下面是相应的进口。

#!/bin/bash 

# import-files-mysql.sh 
# Descr: Import separate SQL files for a specified database. 
# Usage: Run without args for usage info. 
# Author: Will Rubel 
# Notes: 
# * Script will prompt for password for db access. 

[ $# -lt 3 ] && echo "Usage: $(basename $0) <DB_HOST> <DB_USER> <DB_NAME> [<DIR>]" && exit 1 

DB_host=$1 
DB_user=$2 
DB=$3 
DIR=$4 

DIR=$DIR/* 


echo -n "DB password: " 
read -s DB_pass 
echo 
echo "Importing separate SQL command files for database '$DB' into '$DB'" 

file_count=0 


for f in $DIR 

do 
    echo "IMPORTING FILE: $f" 

    gunzip -c $f | mysql -h $DB_host -u $DB_user -p$DB_pass $DB 

    ((file_count++)) 
done 

echo "$file_count files importing to database '$DB'" 
+0

不只是代码转储;请提供一些关于代码的作用的解释。 – rgettman 2013-08-06 19:59:06

+0

下面是如何使用它:./import-files-mysql.sh host_name dbname db ./,我假设你所有的gz文件都在当前文件夹中,请相应地替换你的db登录信息,脚本会提示你输入密码在命令之后。 – 2016-09-15 22:15:05

-1

见圣保利马库斯下面的文章:

Howto split a SQL database dump into table-wise files

Splitting a sql file containing a whole database into per-table files is quite easy: Grep the .sql for any occurence of DROP TABLE. Generate the file name from the table name that is included in the DROP TABLE statement. Echo the output to a file. Here is a little script that expects a .sql file as input:

#!/bin/bash 

file=$1 # the input file 
directory="$file-splitted" # the output directory 
output="$directory/header" # the first file containing the header 
GREP="DROP TABLE" # what we are looking for 

mkdir $directory # create the output directory 

while read line 
do 
    # if the current line contains the wanted statement 
    if [ $(echo "$line" | grep -c "$GREP") == "1" ] 
    then 
     # extract the file name 
     myfile=$(echo $line | awk '{print $5}' | sed -e 's/`//g' -e 's/;//g') 
     # set the new file name 
     output="$directory/$myfile" 
    fi 
     echo "$line" >> $output # write to file 
done < $file 
+0

如果一行包含字符串“DROP TABLE”,该怎么办?这不是一个安全的解决方案。 – Flimm 2014-06-11 13:51:19

1

看样子这里每个人都忘了autocommit=0;SET unique_checks=0;SET foreign_key_checks=0;是想加快导入过程...

#!/bin/bash 
MYSQL_USER="USER" 
MYSQL_PASS="PASS" 

if [ -z "$1" ] 
    then 
    echo "Dumping all DB ... in separate files" 
    for I in $(mysql -u $MYSQL_USER --password=$MYSQL_PASS -e 'show databases' -s --skip-column-names); 
    do 
     echo "SET autocommit=0;SET unique_checks=0;SET foreign_key_checks=0;" > "$I.sql" 
     mysqldump -u $MYSQL_USER --password=$MYSQL_PASS $I >> "$I.sql"; 
     echo "SET autocommit=1;SET unique_checks=1;SET foreign_key_checks=1;commit;" >> "$I.sql" 
     gzip "$I.sql" 
    done 
    echo "END." 
else 
     echo "Dumping $1 ..." 
     echo "SET autocommit=0;SET unique_checks=0;SET foreign_key_checks=0;" > "$1.sql" 
     mysqldump -u $MYSQL_USER --password=$MYSQL_PASS $1 >> "$1.sql"; 
     echo "SET autocommit=1;SET unique_checks=1;SET foreign_key_checks=1;commit;" >> "$1.sql" 
     gzip "$1.sql" 
fi 
2
#!/bin/bash 

for i in $(mysql -uUser -pPASSWORD DATABASE -e "show tables;"|grep -v Tables_in_);do mysqldump -uUSER -pPASSWORD DATABASE $i > /backup/dir/$i".sql";done 

tar -cjf "backup_mysql_"$(date +'%Y%m%d')".tar.bz2" /backup/dir/*.sql 
+0

你可以添加一个解释如何工作? – dpassage 2014-03-21 23:20:58

+0

一个命令,那是所有人! 这太棒了! – Dimitrios 2017-09-28 16:10:42

相关问题