Link to home
Start Free TrialLog in
Avatar of Brogrim
BrogrimFlag for Ireland

asked on

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
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.
REM
 
setlocal
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="\\192.168.1.8\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)
 
:service_stopped
    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
 
:service_running
    echo MySQL Service is already running.
    call :dump_and_zip
    goto end
     
:dump_and_zip:
    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"
 
     
:end

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"
Avatar of Steve Bink
Steve Bink
Flag of United States of America image

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.
Avatar of Brogrim

ASKER

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.
ASKER CERTIFIED SOLUTION
Avatar of Steve Bink
Steve Bink
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial