0

I can't get this batch file to work piping the mysqldump output to gzip. However doing it in two separate lines does work, so where am I doing it wrong please?

set dbUser=root
set dbPassword=mypassword
set mysqldump="C:\xampp\mysql\bin\mysqldump.exe"
set gzip="C:\Program Files\7-Zip\7z.exe"

:: do the database dump -this works!
:: %mysqldump% --host=localhost --user=%dbUser% --password=%dbPassword% %DBNAME% --single-transaction  --hex-blob --routines --triggers --all-databases > C:\temp\MySQL-ALL-DATABASES.sql
:: then GZIP it

:: this line works too!
:: %gzip% a -tgzip C:\temp\MySQL-ALL-DATABASES.sql.gz C:\temp\MySQL-ALL-DATABASES.sql

:: BUT replace above with one line does NOT work?? gives "mysqldump write error 22"
%mysqldump% --host=localhost --user=%dbUser% --password=%dbPassword% %DBNAME% --single-transaction  --hex-blob --routines --triggers --all-databases | %gzip% > C:\temp\MySQL-ALL-DATABASES.sql.gz

cmdline for the one-liner reads:

"C:\xampp\mysql\bin\mysqldump.exe" --host=localhost --user=root --password=mypassword  --single-transaction  --hex-blob --routines --triggers --all-databases   | "C:\Program Files\7-Zip\7z.exe"  1>C:\temp\MySQL-ALL-DATABASES.sql.gz
RiggsFolly
  • 93,638
  • 21
  • 103
  • 149
ChrisH
  • 127
  • 1
  • 11
  • Where did the `1` come from in `7z.exe" 1` – RiggsFolly Nov 07 '21 at 18:21
  • 1
    I'm sorry, but I do not understand the correlation between your two working lines and your non working one. You have clarified that the output from `%mysqldump% --host=localhost --user=%dbUser% --password=%dbPassword% %DBNAME% --single-transaction --hex-blob --routines --triggers --all-databases` is redirected to `C:\temp\MySQL-ALL-DATABASES.sql` fine, and that `%gzip% a -tgzip C:\temp\MySQL-ALL-DATABASES.sql.gz C:\temp\MySQL-ALL-DATABASES.sql` works as expected. However, I do not understand how each of the originally redirected lines, piped to `"C:\Program Files\7-Zip\7z.exe"` is relevant. – Compo Nov 07 '21 at 18:21
  • 1
    What exactly does an example line from the former return, because unless it looks exactly like this, `a -tgzip C:\temp\MySQL-ALL-DATABASES.sql.gz C:\temp\MySQL-ALL-DATABASES.sql`, there is no correlation between the two things, and you just do not understand the difference between piping STDIN to `gzip.exe`, and passing parameters/arguments to it. @RiggsFolly, the `1` was added automatically when parsed, because it is the correct syntax, and the OP had decided to not use that correct syntax, in favor of lazy coding. – Compo Nov 07 '21 at 18:23
  • OK, found the answer. Compo is right, following the pipe it should be ``` | %gzip% -si a C:\temp\MySQL-ALL-DATABASES.sql.gz ``` so now it works. hope it helps someone else – ChrisH Nov 07 '21 at 18:26

0 Answers0