Neha Shettar
Neha Shettar

Reputation: 713

How to schedule a MySQL database backup of one table per file in Windows?

I'm using MySQL Server 5.5 and need to schedule a daily database backup. Am currently doing the following in a batch file:

set currdate=%date:~4%
Set FileDate=%currdate:/=_%

mysqldump -u root-proot db > "C:\backup\database\db%FileDate%.sql"

It exports all tables in a single file. I want to export one file per table.

Upvotes: 4

Views: 398

Answers (2)

Steve Chambers
Steve Chambers

Reputation: 39394

The following outputs all table names to a temporary file first and then iterates through them, dumping each one to an appropriately named file:

@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

Upvotes: 2

Compo
Compo

Reputation: 38579

As you have not indicated what is wrong with the answer provided, here's a similar answer, (please note that this is completely untested):

@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")

Just ensure that the value data on lines 3, 4, 5, 6 and possibly 9 is modified as necessary

Upvotes: 0

Related Questions