MySQL Backup

I have a batch file that exports into a "Self-Contained file"

@echo off
REM MySQL Dump / Backup Script for Windows NT Systems.
REM This Script will dump all tables from your MySQL Instance to a 7zip archive; 
REM it will Also take care of starting and stopping the MySQL Service on the machine.
set mysql_username="root"
set mysql_password="password"
set mysql_service="MySQL"
set mysql_path="C:\Program Files\MySQL\MySQL Server 5.5\bin"
set zip_path="C:\Program Files\7-Zip"
set output_path="\\\server2\Back_Up"
REM Start of Script.
IF NOT EXIST %output_path% (mkdir %output_path%)
REM Check to see if the MySQL Service is running
for /f "tokens=*" %%a IN ('sc query "%mysql_service%" ^| find "RUNNING"') do set servicerunning=%%a
if "X%servicerunning%%" == "X" (goto service_stopped) ELSE (goto service_running)
    echo Starting MySQL Service: %mysql_service%
    net start %mysql_service%
    call :dump_and_zip
    echo Stopping MySQL Service: %mysql_service%
    net stop %mysql_service%
    goto end
    echo MySQL Service is already running.
    call :dump_and_zip
    goto end
    REM Dump out the MySQL Database to a timestamped.sql file
    for /f "tokens=1,2,3 delims=/ " %%a in ('DATE /T') do set date=%%c-%%b-%%a
    for /f "tokens=1,2 delims=:" %%a in ('TIME /T') do set time=%%a-%%b
    %mysql_path%\mysqldump.exe --user %mysql_username% --password=%mysql_password% --databases ddai > "%output_path%\%date%_%time%.sql"

Open in new window

My problem is that when I try to restore the database it is raising an error and I lose the majority of my data, I have taken to manually exporting the database to a  "Dump Project Folder".

Is there anyway to edit the above batch file to to a "Dump Project Folder" instead of a "Self-Contained Folder"
BrogrimInformation Systems Development ManagerAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Steve BinkCommented:
What error do you receive when you try to restore?  Is the data present in the SQL file?  Is the SQL file complete?  What do you mean by "Dump Project Folder instead of a Self-Contained Folder"?
Restoring the DB wipes the old data.  If you want to have an option  to do a restore to a specific time, you have to use the mysqldump along with bin-log optionthe mysqldump is the full backup, while bin-log is the transactional information.

The backup you have when used will drop tables and restore the data to the state of data when backup was run.
BrogrimInformation Systems Development ManagerAuthor Commented:
Hi guys thanks for the comments, I should point out that I am sing MySQL Server 5.2 CE

The error I get when restoring is:

ERROR 1054 (42S22) at line 5107: Unknown column 'ddaiid' in 'NEW'
Operation failed with exitcode 1

I mean "Dump Project Folder folder" when all the tables are dumped separately into a folder, one file per table instead of one large SQL file (3.6GB) "Self Contained file".

With regard to bin-log option I have had a brief look at it and it seems very complicated
you would need to first get the tables in the database, show tables in databasename;
then you would cycle through using mysqldump with table

Note that restore have to follow the constraint, fk, triggers, rules.
Steve BinkCommented:
Here is an *untested* process you can try:
This can be run directly in cmd (I wrapped the line but it should not be wrapped):

mysql.exe -uroot -p1234 -s -N -e "SHOW DATABASES" |
  for /F "usebackq" %D in (`findstr /V "information_schema performance_schema"`)
    do mysqldump %D -uroot -p1234 > S:\Backup\MySQL\%D.sql
In a batch file you will need to escape % with an additional %, that is use %%D.

Batch File

mysql.exe -uroot -p1234 -s -N -e "SHOW DATABASES" |
  for /F "usebackq" %%D in (`findstr /V "information_schema performance_schema"`)
    do mysqldump %%D -uroot -p1234 > S:\Backup\MySQL\%%D.sql

Open in new window

Courtesy of newtover

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
MySQL Server

From novice to tech pro — start learning today.