Take the 2-minute tour ×

I have a problem with bat or maybe schtasks on windows.

I want to backup my mysql databases every day automatically, so I wrote a bat file like this, we call it backup.bat:

@echo off & setlocal ENABLEEXTENSIONS

set BACKUP_PATH=D:\Backup\
set DATABASES=database1 database2 database3
set USERNAME=root
set PASSWORD=123456
set WINRAR=C:\Progra~1\WinRAR\Rar.exe

set YEAR=%date:~0,4%
set MONTH=%date:~5,2%
set DAY=%date:~8,2%
set MINUTE=%time:~0,2%
set SECOND=%time:~3,2%


:: create dir
if not exist %DIR% (
    mkdir %DIR% 2>nul
if not exist %DIR% (
    echo backup path: %DIR% not exists, create dir failed.
    goto exit

:: backup
echo start dump databases
for %%D in (%DATABASES%) do (
    echo dumping database %%D ...
    %MYSQL%mysqldump -u%USERNAME% -p%PASSWORD% %%D > %DIR%%%D_%ADDON%.sql 2>nul
    :: winrar
    if exist %WINRAR% (
 %WINRAR% a -k -r -s -m1 -ep1 %DIR%%%D_%ADDON%.rar %DIR%%%D_%ADDON%.sql 2>nul
 del /F /S /Q %DIR%%%D_%ADDON%.sql 2>nul
echo done


I also wrote a bat file to add it to schtasks, just call it add.bat:

@echo off

set FILE=D:\cron\cron_backup.bat



set DATETIME=00:30:00

set NAME="Backup Cron Job"

set USER="System"


Then I run the add.bat, the job was added to system successfully.

It also run periodically too. But when I look up the backup dir, and unrar backup file, the file exists, not empty, just incomplete. It only contains a few line of what should be complete backup sqls, so the backup operation failed.

I tried to run backup.bat manually, do not use schtasks, it works perfectly.

It seems that the backup operation was interrupt by something.

So my question is: Is there someting I wrote wrong, or missed, or else why schtasks run failed ?

share|improve this question
Maybe the account the scheduled task is running under doesn't have enough permissions? –  David Dec 1 '10 at 3:59
Are you sure that the scheduled task is not being somehow interrupted whenever it is run? For example, if the computer is being restarted or a network drive is taken offline after the scheduled task begins, that may be why your file is incomplete. –  Cody Gray Dec 1 '10 at 4:00
@David, The System account should have permissions to run it, .sql and .rar file was created, so maybe something else wrong but should note be permissions. Thanks for reply. –  micate Dec 1 '10 at 6:35
@Cody Gray, I run it for a few days already(At first I thought there was no problem), it created 7 or 8 dirs on my server, also created .rar files. None of these files are complete. So it may not be external reason, I think it strongly should be problem of my code. I just donot know how to correct it, -_-||. –  micate Dec 1 '10 at 6:43
add comment

1 Answer

I modified my bat file and it works fine now. I already post a blog with correct codes, so if you're interested, please visit http://micate.me/windows-mysql-backup.note .

share|improve this answer
add comment

Your Answer


