5

I have a project that uses MySQL database. I want to backup the database every day, so I use this:

mysqldump -h host -u user -p password database > 'location.sql'

I want the files to be named with timestamp, i.e.:

Today the file will be named something-07-05-2014 08-00-00
Tomorrow will be, something-08-05-2014 08-00-00

How to append a formatted timestamp with the exported file name ?

Ayman
  • 1,257
  • 4
  • 18
  • 30
  • 2
    That's not a mysql question that's a question for whatever batch script language you use (windows, bash, perl, python, ...) – a_horse_with_no_name May 07 '14 at 12:41
  • I execute this script from Windows command line or Linux Terminal, both give the same result. My question is how to include timestamp when dumping MySQL database via mysqldump tool – Ayman May 07 '14 at 12:44
  • 1
    The filename you specify is not under the control of the tool `mysqldump`. Your question has nothing to do with MySQL and everything with the environment _where_ and _how_ you run this. – a_horse_with_no_name May 07 '14 at 12:59
  • @Ayman: for windows it's more complicated, see i.e. http://stackoverflow.com/questions/203090/how-to-get-current-datetime-on-windows-command-line-in-a-suitable-format-for-us. I would recommend you the use of powershell. – VMai May 07 '14 at 13:03

2 Answers2

17

You can use something like this

mysqldump -h host -u user -p password database >  $(date "+%b_%d_%Y_%H_%M_%S").sql
Ashley Medway
  • 6,659
  • 7
  • 43
  • 63
Pranav Maniar
  • 1,435
  • 10
  • 20
9

you can do

mysqldump -h host -u user -p password database > something-$(date +%d-%m-%Y %H %M %S).sql
user3470953
  • 8,718
  • 2
  • 13
  • 17
  • What shell are you running this in? It doesn't seem to work in `cmd` or PowerShell. – alroc May 07 '14 at 13:51
  • 2
    Doesn't work in Bash either. The command `date +%d-%m-%Y %H %M %S` needs quotes as in @Pranav's answer below. – Jack Frost Oct 28 '14 at 17:05
  • Also, once the date formatting is sorted, `mysqldump` returns a syntax error, `-bash: syntax error near unexpected token `.sql'` Escaping doesn't seem to help. The `echo` command returns expected results. `echo something-$(date "+%d-%m-%Y %H %M %S").sql` > "something-28-10-2014 10 11 29.sql" – Jack Frost Oct 28 '14 at 17:12