Quick and Dirty MySQL Backup Script on Windows


08.19.07 Posted in Websites by Barry

I’ve wrote a small backup script to backup a remote MySQL database.

Requires mysqldump.exe which is part of most/all stan­dard MySQL installations.

@echo off
:: vari­ables
set drive=D:\Backup\DB

for /f “tokens=2 – 4 delims=/ ” %%g in (‘date /t’) do (
set yy=%date:~-4%
set mm=%date:~-7,2%
set dd=%date:~-10,2%
)
set today=%dd%-%mm%-%yy%

:: Switch to mysqldump.exe direc­tory
cd C:\wamp\mysql\bin\

echo ### Back­ing up Blog…
mysql­dump –opt –Q –h mysql1​.host​ing​com​pany​.ie –u database_user database_name > %drive%\database_name%today%.sql

echo Backup Com­plete!
@pause

Thats it, save it as a .bat file and run it.

It will cre­ate the backup file with todays date

It will also prompt for a pass­word, if you want to include the pass­word in the file (alarm bells!)

mysql­dump –opt –Q –h mysql1​.host​ing​com​pany​.ie –u database_user -–pass­word database_password database_name > %drive%\database_name%today%.sql

I should also append this script to cor­rectly check for exist­ing files, and incor­po­rate some com­pres­sion. This script is for a rel­a­tively unim­por­tant data­base, if I had a more mis­sion crit­i­cal data­base, I’d be using a proper backup & recov­ery sys­tem from my data­base provider.

Fur­ther Read­ing.
http://​dev​.mysql​.com/​d​o​c​/​r​e​f​m​a​n​/​5​.​0​/​e​n​/​m​y​s​q​l​d​u​m​p​.​h​tml

Share and Enjoy:
  • Twitter
  • del.icio.us
  • Facebook
  • Google Bookmarks
  • StumbleUpon


blog comments powered by Disqus