简体   繁体   English

mysql:恢复使用mysqldump中的“--tab”选项创建的备份

[英]mysql: Restore backup that was created with “--tab” option in mysqldump

mysqldump has the "--tab" option to split up the dump separate files for each table. mysqldump具有“--tab”选项,可以为每个表拆分转储单独的文件。 mysql creates a .sql file (with schema) and a .txt file (with data) for each table. mysql为每个表创建一个.sql文件(带有模式)和一个.txt文件(带有数据)。

The .sql files work fine, but how do I import the data from the .txt files? .sql文件工作正常,但如何从.txt文件导入数据?

我自己找到了解决方案,请看这里: http//dev.mysql.com/doc/refman/5.1/en/reloading-delimited-text-dumps.html

Official documentation doesn't covert the case of importing these *.txt and foreign key constrains. 官方文档不会转换导入这些*.txt和外键约束的情况。 There is still open long-lived bug#19996 to implement foreign key ignore in mysqlimport . mysqlimport实现外键忽略仍有开放的长期存在的错误#19996 So it has to be done manually. 所以必须手动完成。

#!/bin/bash -e

DIR=/path/to/csv/backup
DATABASE=database
USER=user
PASSWORD=password

for filename in $DIR/*.txt
do
tablename=`basename $filename .txt`
mysql --user=$USER --password=$PASSWORD $DATABASE <<EOF
  SET FOREIGN_KEY_CHECKS=0;
  LOAD DATA INFILE '$filename' INTO TABLE \`$tablename\`;
EOF
done

Also I would like to note that there's no much sense in storing these per-table *.sql files produced by mysqldump --tab , because there's also a foreign key issue on fields. 另外我想指出,存储mysqldump --tab生成的每个表*.sql文件没有多大意义,因为在字段上也存在外键问题。 As a schema is always known I suggest just to delete them after backup is completed. 由于模式总是已知的,我建议在备份完成后删除它们。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM