Search code examples
mysqlbatch-filescheduled-tasksbackup

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.


Solution

  • 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