Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Exit code on db:dump is always 0 #929

Open
schmengler opened this issue Jan 17, 2022 · 7 comments
Open

Exit code on db:dump is always 0 #929

schmengler opened this issue Jan 17, 2022 · 7 comments
Assignees
Labels
bug help wanted reproduced Issue could be reproduced

Comments

@schmengler
Copy link

schmengler commented Jan 17, 2022

Describe the bug

When running the db:dump command and mysqldump fails, magerun will still return with an exit code of 0.

Expected behaviour

Non-zero exit code on error

Steps to reproduce the issue

  1. Change app/etc/env.php to invalid database credentials
  2. Run magerun db:dump
  3. See error like mysqldump: Got error: 2005: "Unknown MySQL server host ''bielefeld'' (-2)" when trying to connect
  4. Check error code, e.g. with echo $?

Technical details

  • Host Machine OS (Windows/Linux/Mac): Linux

Possible Fix

In script, I currently work around the issue with $(magerun db:dump --only-command) (together with set -e -o pipefail)

Additional context

This is important for scripting, e.g. database backup as part of the deployment pipeline. If a backup fails, the pipeline should not continue.

@schmengler schmengler added the bug label Jan 17, 2022
@cmuench cmuench self-assigned this Jan 17, 2022
@cmuench
Copy link
Member

cmuench commented Jan 17, 2022

Bug confirmed in v4.9.1

@cmuench cmuench added the reproduced Issue could be reproduced label Jan 17, 2022
@cmuench
Copy link
Member

cmuench commented Jan 17, 2022

Found an issue in result of the piped command.

Example:

mysqldump --single-transaction --quick -h'db'  -u'db' --password='db1' 'db' | LANG=C LC_CTYPE=C LC_ALL=C sed -E 's/DEFINER[ ]*=[ ]*`[^`]+`@`[^`]+`/DEFINER=CURRENT_USER/g' > 'db.sql' 2>&1

We have a special pipehandling for bash compatible shells included.
The check with getenv('SHELL') is not working anymore.

@cmuench
Copy link
Member

cmuench commented Jan 25, 2022

Tried with Symfony/Process component. Seems that internally any shell_exec or proc_open with piped commands is executed in OS default shell.
On some system, like my ddev test environment based on Debian, the "bin/sh" is a link to "dash". Dash does not support set -o pipefail which is currently needed to stop the pipe.
In our command line call the last command is a sed which always returns "0" status code.
I am still looking for a good solution.

@convenient
Copy link
Contributor

@cmuench Spotted this and wonder if you have come across this before? http://cfajohnson.com/shell/cus-faq-2.html#Q11

Might be useful, may still produce a dump file but allow you to get the exit code somehow

@cmuench
Copy link
Member

cmuench commented Apr 20, 2022

@cmuench Spotted this and wonder if you have come across this before? http://cfajohnson.com/shell/cus-faq-2.html#Q11

Might be useful, may still produce a dump file but allow you to get the exit code somehow

I am thankful for every hint.
Thought it's easy to fix but then I spent hours to try out possible solutions.

@convenient
Copy link
Contributor

🙈 Maybe it's a case of doing it in multiple processes / steps?

mysqldump --single-transaction --quick -h'db'  -u'db' --password='db1' 'db' > db.sql.tmp
# if exit code !=0 delete db.sql.tmp and exit 1

LANG=C LC_CTYPE=C LC_ALL=C sed -i -E 's/DEFINER[ ]*=[ ]*`[^`]+`@`[^`]+`/DEFINER=CURRENT_USER/g' db.sql.tmp
# if exit code !=0 delete db.sql.tmp and exit 1

mv db.sql.tmp db.sql
# exit 0

It's probably more convoluted PHP code though

@cmuench
Copy link
Member

cmuench commented Apr 20, 2022

see_no_evil Maybe it's a case of doing it in multiple processes / steps?

mysqldump --single-transaction --quick -h'db'  -u'db' --password='db1' 'db' > db.sql.tmp
# if exit code !=0 delete db.sql.tmp and exit 1

LANG=C LC_CTYPE=C LC_ALL=C sed -i -E 's/DEFINER[ ]*=[ ]*`[^`]+`@`[^`]+`/DEFINER=CURRENT_USER/g' db.sql.tmp
# if exit code !=0 delete db.sql.tmp and exit 1

mv db.sql.tmp db.sql
# exit 0

It's probably more convoluted PHP code though

I agree. This command btw. needs some refactoring.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug help wanted reproduced Issue could be reproduced
Projects
None yet
Development

No branches or pull requests

3 participants