如何使用批处理脚本转储单个 MySQL 表以进行选择性恢复?

发布于 2025-01-07 21:17:46 字数 630 浏览 0 评论 0原文

我们目前有一个 DOS 批处理脚本,可以在单个文件中对整个数据库执行 MySQL 转储。我想做的是按表分解数据库,以便在每个“日期”文件夹中,都会有一个包含各个表转储的“db”文件夹。这允许我们恢复选定的表而不是恢复整个数据库。

@echo off
md C:\backups\%DATE:~-4%-%DATE:~4,2%-%DATE:~7,2%
cd C:\Program Files\MySQL\MySQL Workbench 5.2 CE\
mysqldump -h -u -p --databases db0 > c:\backups\%DATE:~-4%-%DATE:~4,2%-%DATE:~7,2%\db0.sql
mysqldump -h -u -p --databases db1 > c:\backups\%DATE:~-4%-%DATE:~4,2%-%DATE:~7,2%\db1.sql

我找到了使用 shell 的可能解决方案: https://stackoverflow.com/a/134296/679449

这似乎会允许我导出到单独的表中,但是我不确定如何/是否可以批量写入。如有任何帮助,我们将不胜感激。

We currently have a DOS batch script that performs a MySQL dump on the entire database in a single file. What I would like to do is breakup the database by tables, so that within each 'date' folder, there would be a 'db' folder containing individual table dumps. This allows us to restore selected tables rather than restoring an entire db.

@echo off
md C:\backups\%DATE:~-4%-%DATE:~4,2%-%DATE:~7,2%
cd C:\Program Files\MySQL\MySQL Workbench 5.2 CE\
mysqldump -h -u -p --databases db0 > c:\backups\%DATE:~-4%-%DATE:~4,2%-%DATE:~7,2%\db0.sql
mysqldump -h -u -p --databases db1 > c:\backups\%DATE:~-4%-%DATE:~4,2%-%DATE:~7,2%\db1.sql

I found a possible solution using shell: https://stackoverflow.com/a/134296/679449

This appears that it would allow me to export into individual tables, however I'm not sure how to/if it's possible to write in batch. Any help with that is appreciated.

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

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

发布评论

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

评论(1

七七 2025-01-14 21:17:46

我的解决方案:

@echo off
md C:\backups\%DATE:~-4%-%DATE:~4,2%-%DATE:~7,2%\db0
md C:\backups\%DATE:~-4%-%DATE:~4,2%-%DATE:~7,2%\db1

cd C:\Program Files\MySQL\MySQL Workbench 5.2 CE\

mysql -s -e "SHOW TABLES FROM db0" -u -p --skip-column-names > C:\backups\tables.txt
for /f %%A in (C:\backups\tables.txt) DO (mysqldump -h -u -p db0 %%A > c:\backups\%DATE:~-4%-%DATE:~4,2%-%DATE:~7,2%\db0\%%A.sql)

mysql -s -e "SHOW TABLES FROM db1" -u -p --skip-column-names > C:\backups\tables.txt
for /f %%A in (C:\backups\tables.txt) DO (mysqldump -h -u -p db1 %%A > c:\backups\%DATE:~-4%-%DATE:~4,2%-%DATE:~7,2%\db1\%%A.sql)

My solution:

@echo off
md C:\backups\%DATE:~-4%-%DATE:~4,2%-%DATE:~7,2%\db0
md C:\backups\%DATE:~-4%-%DATE:~4,2%-%DATE:~7,2%\db1

cd C:\Program Files\MySQL\MySQL Workbench 5.2 CE\

mysql -s -e "SHOW TABLES FROM db0" -u -p --skip-column-names > C:\backups\tables.txt
for /f %%A in (C:\backups\tables.txt) DO (mysqldump -h -u -p db0 %%A > c:\backups\%DATE:~-4%-%DATE:~4,2%-%DATE:~7,2%\db0\%%A.sql)

mysql -s -e "SHOW TABLES FROM db1" -u -p --skip-column-names > C:\backups\tables.txt
for /f %%A in (C:\backups\tables.txt) DO (mysqldump -h -u -p db1 %%A > c:\backups\%DATE:~-4%-%DATE:~4,2%-%DATE:~7,2%\db1\%%A.sql)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文