5

I am unix man, and I really don't like work on windows. I am asking You for help.

I am trying to make regular backups of mysql database in microsoft windows cmd. Here is my current script for that:

C:\xampp\mysql\mysqldump.exe -hlocalhost -uroot -ppass stp > V:\backup_3.2.2.6__%date:/=%.sql

I would like to file name have structure like that:

backup_3.2.2.6__2015-06-02_10:25:35.sql

where is:

name__currentDate_currentTime.sql

So I just need to add current time to file name.

Please help

masterdany88
  • 5,041
  • 11
  • 58
  • 132

2 Answers2

6

Here is working solution:

C:\xampp\mysql\mysqldump.exe -hlocalhost -uroot -ppass stp > V:\backup_3.2.2.6__%date:/=%_%time:~0,2%-%time:~3,2%-%time:~6,2%.sql

Where "pass" is Your password for root user

masterdany88
  • 5,041
  • 11
  • 58
  • 132
  • I have found this approach to work in Windows 7. A nice one line solution. It also works with producing a zipped file. Really good solution. – edesz Jun 27 '16 at 14:28
  • It works, but also depends on the set date format. Would you care to explain your solution and how would you set the time format to hh:MM:ss instead of hh-MM-SS – David Feb 09 '18 at 07:37
4

You can not have : as a part of the file name on Windows. Try the following batch file, it will create file with the name in the format such as: backup_3.2.2.6__2015-06-02_17-50-44.sql:

@echo off
for /f "tokens=2 delims==" %%I in ('wmic os get localdatetime /format:list') do set datetime=%%I
set datetime=%datetime:~0,4%-%datetime:~4,2%-%datetime:~6,2%_%datetime:~8,2%-%datetime:~10,2%-%datetime:~12,2%
"C:\xampp\mysql\mysqldump.exe" -hlocalhost -uroot -ppass svp > "V:\backup_3.2.2.6__%datetime%.sql"

I have used wmic example from the answer at: https://stackoverflow.com/a/18024049/4947851

Community
  • 1
  • 1
pisamce
  • 533
  • 2
  • 6
  • I don't understand Your code. Why to use for loop to get current date and time? – masterdany88 Jun 02 '15 at 09:37
  • Code in `for` loop will create string which is independent of windows locale settings. `%date%` on its own will give different results on different locale settings. As I don't know your locale this was a suggestion how to make it work on any windows machine. To run this code, save it as `something.bat` and run it from command line. However, if you know `%date%` format will always be same, you can easily format string based on that. – pisamce Jun 02 '15 at 10:00