0

I am trying to mysqldump my database with a data stump on the file. I cant do it on a cmd because when I add %data% something weird happens. it adds a "1" caracter in the middle of the sentence like: if i do

mysqldump args > file.sql (works fine)

mysqldump args > file_%data%.sql (i get error)

with the data inside the cmd actually tries to run this:

mysqldump args 1 > file_%data%.sql (adds a "1" somehow - and the error is table "1" unknown - DOH)

so i tried this dump on powershell, and it works, i just need:

mysqldump args |  Out-File file_$(get-date -f yyyy-MM-dd).sql -encoding UTF8

it runs but the encoding is wrong, it does not encode in utf8 but the data in the file name works fine

so my next try is to run a script in powershell that launches the cmd to make the .sql well exported, and exit the cmd, and back in the powershell rename the file to the final name (with date). But this i cant figure out how can i run powershell then launch a cmd script and come back to powershell.

Any tips how can i do this pls?

I searched a LOT there's a million posts of how to make the dump in cmd all together with a "for" and other non-sense nothing I neither understood or could make run.
In the powershell there's something wrong with the dump cuz the special characters inside the database get all messed up, both with utf8 and ascii encoding.

Sorry about my bad English. TY for tips.

(using win10 64 pt-pt)

  • how do you set data variable in cmd? – Mike Twc Jan 03 '19 at 16:09
  • It's unclear how the `1` would get in there. Are you showing us a command placed in a batch file? At what point do you see the `1`? If `mysqldump` produces UTF-8 output, you need to (temporarily) set `[console]::OutputEncoding] = [Text.Encoding]::UTF8` in order for the output to be interpreted correctly by PowerShell. – mklement0 Jan 03 '19 at 16:28
  • 1
    **1.** Use `-r` option instead of `>` redirection; read [Dumping and importing from/to MySQL in an UTF-8 safe way](https://makandracards.com/makandra/595-dumping-and-importing-from-to-mysql-in-an-utf-8-safe-way). **2.** Check whether `show variables like 'character%';` is consistent with `show global variables like 'character%';`. – JosefZ Jan 03 '19 at 20:51
  • Consider [switching from MySQL’s utf8 to utf8mb4](https://mathiasbynens.be/notes/mysql-utf8mb4#utf8-to-utf8mb4) (read [In MySQL, never use “utf8”. Use “utf8mb4”](https://medium.com/@adamhooper/in-mysql-never-use-utf8-use-utf8mb4-11761243e434) as well). I did it and since then no more [mojibake](https://en.wikipedia.org/wiki/Mojibake) occurred on input/output (using win10 64 cs-CZ)! By then, try `--default-character-set=binary` switch for `mysqldump`. In Powershell, use `&` call operator as `& mysqldump --default-character-set=binary -r outputfile -databases x` – JosefZ Jan 06 '19 at 00:30

1 Answers1

0

(mysqldump args)> file_%data%.sql

And for PowerShell:

mysqldump args | Out-File file_$(get-date -f yyyy-MM-dd).sql -Encoding OEM
  • Why would `cmd.exe` do that? Note that while `1>...` is the same as `>...`, `1 >...` (a space between the number and the redirection) is not. I think the intent is to create UTF-8 output. – mklement0 Jan 03 '19 at 16:25
  • In a batch file with echo set to on you'll see the inserted stream number **always** [how-does-the-windows-command-interpreter-cmd-exe-parse-scripts](https://stackoverflow.com/questions/4094699/how-does-the-windows-command-interpreter-cmd-exe-parse-scripts) –  Jan 03 '19 at 16:48
  • Interesting, but a 1 *followed by a space* is not a stream number, it‘s an argument to whatever command is being invoked. – mklement0 Jan 03 '19 at 16:51
  • @mklement0 True, that's why I suggested to enclose in parentheses, I'm unsure if OP inserted that space or if some of the unknown args may interfere. OP João Horta, could use `-r, --result-file=name` instead of redirecing output –  Jan 03 '19 at 17:05
  • 1
    In any event, it’s not true that cmd.exe inserts a 1 as shown in the question, so please remove or clarify that statement. Avoiding the redirection is a good idea and bypasses PowerShell’s misinterpretation of mysqldump’s output – mklement0 Jan 03 '19 at 17:12