备份mysql数据

发布于 2024-11-05 18:39:41 字数 38 浏览 1 评论 0原文

有没有办法使用 PHP 只备份 MySQL 数据库表中的数据?

Is there a way using PHP to make a backup of data only from MySQL database tables?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

伴我心暖 2024-11-12 18:39:41

看看 http://davidwalsh.name/backup-mysql-database-php并删除他生成 sql 命令以重新创建结构的部分。

Have a look at http://davidwalsh.name/backup-mysql-database-php and remove the parts where he generates sql commands for re-creating the structure.

两个我 2024-11-12 18:39:41

使用SHOW TABLES(http://dev.mysql.com/doc/refman/5.0/en/show-tables.html)获取每个表,使用PHP使用SELECT *循环它们FROM -表名-。然后循环这个记录集,使用implode(',' $results)获取数据(CSV)。当然,您可能想为每个表创建一个单独的表或其他表。

Use SHOW TABLES (http://dev.mysql.com/doc/refman/5.0/en/show-tables.html) to get each table, loop them with PHP using SELECT * FROM -tablename-. Then loop this record set, use implode(',' $results) to get the data (CSV). You might want to create a seperate for or something for each table of course.

桃扇骨 2024-11-12 18:39:41

您同意您想要“想要原始数据(例如:作为 CSV)而不是任何形式的 INSERT 语句” - 但您接受的答案恰恰是后者,使用例如这行代码:

$return.= 'INSERT INTO '.$table.' VALUES(';

如果确实如此,那很好你想要什么,但如果我是你,我就会对 mysqldump 进行系统调用。您可以让 mysqldump 以安全、可靠的方式生成一组 INSERT INTO 语句,而不必担心“我是否关闭了双引号?这里应该有一个额外的逗号吗?” 但是

我不知道为什么你也不想导出模式 - 如果你指定 --add-drop-table 那么你的数据库转储将成为整个数据库的一个很好的、整洁的恢复,如果最坏的情况发生 - 如果您只想导出数据,并且可以在 PHP 脚本中对 mysqldump 进行系统调用,你可以运行:

mysqldump --skip-triggers --compact --no-create-info db_name | gzip -c > /tmp/db_name_backup.sql.gz

--no-create-info 排除所有 CREATE TABLE 语句;但正如我所说,我宁愿包括 CREATE 和 DROP IF EXISTS,这样您就有了数据和模式的完整备份。

我真的建议使用 mysqldump 生成的 SQL 进行备份 - MySQL 天生就能理解它,并且可以正确处理编码、空格等 - 而不是 CSV 之类的东西 - AFAIK 实际上不是任何类型的国际公认标准,并且无法通过管道传输返回到 mysql 以在稍后恢复数据库:

gunzip -c < /tmp/db_name_backup.sql.gz | mysql db_name

如果您的备份是每个表的单独 CSV 文件,则恢复该备份将成为一项更加困难的任务。

You agree that you want "want the raw data (e.g.: as CSV) rather than any form of INSERT statements" - but the answer you've accepted does precisely the latter, using e.g. this line of code:

$return.= 'INSERT INTO '.$table.' VALUES(';

Which is fine if that's really what you want, but if I were you I would then just make a system call to mysqldump instead. You can get mysqldump to just generate a set of INSERT INTO statements in a safe, robust way without worrying about "did I close that double quote? Should there be an extra comma here?" etc.

I don't know why you don't want to export the schema as well - if you specify --add-drop-table then your database dump becomes a nice, neat restore of your whole db if the worst happens - but if you just want to export your data, and you can make a system call to mysqldump in your PHP script, you could run:

mysqldump --skip-triggers --compact --no-create-info db_name | gzip -c > /tmp/db_name_backup.sql.gz

--no-create-info excludes all the CREATE TABLE statements; but as I say, I would rather include both CREATE and DROP IF EXISTS, so you have a complete backup of data and schema.

I really recommend backing up using mysqldump's generated SQL - which MySQL innately understands, and which handles encoding, whitespace etc. properly - rather than something like CSV - which AFAIK isn't actually any kind of internationally recognized standard, and can't be piped back into mysql to restore your database at a later date:

gunzip -c < /tmp/db_name_backup.sql.gz | mysql db_name

If your backup was instead a separate CSV file for every single table, restoring that backup becomes a far more difficult task.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文