Dominik
Dominik

Reputation: 4778

Using the current date in a backup batch script as a parameter for file names

I am trying to backup my MySQL Database with a planned task. The Command to backup the SQL is:

mysqldump.exe -h localhost -u root databasename > databasebackup.sql

What I would like to do is add the current date to the file name so it would be databasebackup_2012-01-31.sql. Even more Ideally I would compress the file databasebackup_2012-01-31.sql in a zip file databasebackup_2012-01-31.zip since the sql file mostly contains very well compressable text. After compressing it the sql file would be deleted and only the zip file remains backed up.

Upvotes: 2

Views: 3632

Answers (1)

Cheeso
Cheeso

Reputation: 192657

There's a pseudo-variable in CMD.exe that provides the date. Also you can get command-line zip tools from http://dotNetZip.codeplex.com .

A batch file to do what you want looks like this:

@echo off
@setlocal
echo The date is %DATE%

@set tag=%DATE:~-4%-%DATE:~7,2%-%DATE:~4,2%
set backupfile=databasebackup.%tag%.sql

echo backing up to:  %backupfile%

echo.
echo ^<do the backup here^>
echo.
echo hello hello hello hello > %backupfile%


@REM The DotNetZip download for ZIP Tools includes a command-line zip utility.
@REM Get it from http://dotnetzip.codeplex.com.

set zipit=\Program Files (x86)\Dino Chiesa\DotNetZip Tools 1.9\ZipIt.exe
set unzip=\Program Files (x86)\Dino Chiesa\DotNetZip Tools 1.9\UnZip.exe

set zipfile=%backupfile%.zip

if exist %zipfile% (
echo deleting existing zip...
del %zipfile%
)

"%zipit%"  %zipfile%  -s Readme.txt  "Backed up on %DATE% at %TIME%"  %backupfile%

echo.
@REM list the contents of the created zip
"%unzip%" -l %zipfile%

echo.
echo ^<delete the sql file here^>
echo.
del %backupfile%

@endlocal

Upvotes: 3

Related Questions