如何为 Windows 中的每个文件安排一个 MySQL 数据库备份 table?
How to schedule a MySQL database backup of one table per file in Windows?
我正在使用 MySQL Server 5.5,需要安排每日数据库备份。
目前正在批处理文件中执行以下操作:
set currdate=%date:~4%
Set FileDate=%currdate:/=_%
mysqldump -u root-proot db > "C:\backup\database\db%FileDate%.sql"
它将所有 table 导出到一个文件中。我想为每个 table.
导出一个文件
以下首先将所有 table 名称输出到一个临时文件,然后遍历它们,将每个名称转储到一个适当命名的文件中:
@echo off
set currdate=%date:~4%
set filedate=%currdate:/=_%
mysql --skip-column-names -u root -proot db -e "show tables;" > tables.tmp
for /f "skip=3 delims=|" %%t in (tables.tmp) do (
mysqldump -u root -proot db %%t > "C:\backup\database\db_%%table_%filedate%.sql"
)
del tables.tmp
由于您没有指出所提供的答案有什么问题,这里有一个类似的答案,(请注意,这完全未经测试):
@Echo Off
Rem modify as necessary
Set "buDest=C:\backup\database"
Set "dbName=db"
Set "dbPass=root"
Set "dbUser=root"
Rem If binaries are not in %CD% or %PATH% modify the Set line below
Rem Example: Set "sqlBin=C:\Program Files\MySQL\MySQL Server 5.5\bin"
Set "sqlBin=."
Set "dStamp="
For /F "Tokens=1-3 Delims=/ " %%A In ('RoboCopy/NJH /L "\|" Null') Do If Not Defined dStamp Set "dStamp=%%A_%%B_%%C"
If Not Exist "%buDest%\" MD "%buDest%" 2>Nul || Exit /B
"%sqlBin%\mysql" --user=%dbUser% --password=%dbPass% -D %dbName% -Bse "Show Tables";|^
For /F "Skip=3 Delims=| " %%A In ('FindStr "^|"') Do (
mysqldump --user=%dbuser% --password=%dbpass% %%A >"%buDest%\%dbName%-%%A-%dStamp%.sql")
只需确保 3
、4
、5
、6
和 9
行的值数据已修改根据需要
我正在使用 MySQL Server 5.5,需要安排每日数据库备份。 目前正在批处理文件中执行以下操作:
set currdate=%date:~4%
Set FileDate=%currdate:/=_%
mysqldump -u root-proot db > "C:\backup\database\db%FileDate%.sql"
它将所有 table 导出到一个文件中。我想为每个 table.
导出一个文件以下首先将所有 table 名称输出到一个临时文件,然后遍历它们,将每个名称转储到一个适当命名的文件中:
@echo off
set currdate=%date:~4%
set filedate=%currdate:/=_%
mysql --skip-column-names -u root -proot db -e "show tables;" > tables.tmp
for /f "skip=3 delims=|" %%t in (tables.tmp) do (
mysqldump -u root -proot db %%t > "C:\backup\database\db_%%table_%filedate%.sql"
)
del tables.tmp
由于您没有指出所提供的答案有什么问题,这里有一个类似的答案,(请注意,这完全未经测试):
@Echo Off
Rem modify as necessary
Set "buDest=C:\backup\database"
Set "dbName=db"
Set "dbPass=root"
Set "dbUser=root"
Rem If binaries are not in %CD% or %PATH% modify the Set line below
Rem Example: Set "sqlBin=C:\Program Files\MySQL\MySQL Server 5.5\bin"
Set "sqlBin=."
Set "dStamp="
For /F "Tokens=1-3 Delims=/ " %%A In ('RoboCopy/NJH /L "\|" Null') Do If Not Defined dStamp Set "dStamp=%%A_%%B_%%C"
If Not Exist "%buDest%\" MD "%buDest%" 2>Nul || Exit /B
"%sqlBin%\mysql" --user=%dbUser% --password=%dbPass% -D %dbName% -Bse "Show Tables";|^
For /F "Skip=3 Delims=| " %%A In ('FindStr "^|"') Do (
mysqldump --user=%dbuser% --password=%dbpass% %%A >"%buDest%\%dbName%-%%A-%dStamp%.sql")
只需确保 3
、4
、5
、6
和 9
行的值数据已修改根据需要