Chris Byatt
Chris Byatt

Reputation: 3819

Add timestamp to filename with msqldump from batch file

I'm trying to add a timestamp to a mysql database dump file with a .bat file but it's not going too well. The timestamp isn't added - i just get a backup-.sql file. Any tips?

My file:

@echo off
cls
echo Date format = %date%
echo dd = %date:~0,2%
echo mm = %date:~3,2%
echo yyyy = %date:~6,4%
echo.
echo Time format = %time%
echo hh = %time:~0,2%
echo mm = %time:~3,2%
echo ss = %time:~6,2%
echo.
echo Timestamp = %date:~6,4%-%date:~3,2%-%date:~0,2%-%time:~0,2%-%time:~3,2%-%time:~6,2%

pushd "C:\Program Files\MySQL\MySQL Server 5.5\bin"
mysqldump --user=root --password=***** leaverequest>c:\backup\backup-%timestamp%.sql

Upvotes: 0

Views: 1151

Answers (1)

cure
cure

Reputation: 2688

quick edit should fix it:

@echo off
cls
echo Date format = %date%
echo dd = %date:~0,2%
echo mm = %date:~3,2%
echo yyyy = %date:~6,4%
echo.
echo Time format = %time%
echo hh = %time:~0,2%
echo mm = %time:~3,2%
echo ss = %time:~6,2%
echo.
set timestamp=%date:~6,4%-%date:~3,2%-%date:~0,2%-%time:~0,2%-%time:~3,2%-%time:~6,2%

pushd "C:\Program Files\MySQL\MySQL Server 5.5\bin"
mysqldump --user=root --password=***** leaverequest>"c:\backup\backup-%timestamp%.sql"

Upvotes: 1

Related Questions