28

What I try to implement is invoking mysqldump in container and dump the database into the container's own directory.

At first I try command below:

$ docker exec container-name mysqldump [options] database | xz > database.sql.xz

That's not working, so I try another one which is :

$ docker exec container-name bash -c 'mysqldump [options] database | xz > database.sql.xz'

This time it worked.

But that's really lame.

Then I try using docker-py this time cmd option that worked looks like this:

cmd=['bash', '-c', 'mysqldump [options]  database | xz > database.sql.xz']

the logger event as below:

level="info" msg="-job log(exec_start: bash -c mysqldump [options]  database | xz > database.sql.xz, fe58e681fec194cde23b9b31e698446b2f9d946fe0c0f2e39c66d6fe68185442, mysql:latest) = OK (0)"

My question:

is there a more elegant way to archive my goal?

pingz
  • 555
  • 1
  • 10
  • 20

1 Answers1

48

You are almost there, you just need to add the -i flag to make the pipe work:

-i, --interactive    Keep STDIN open even if not attached

docker exec -i container-name mysqldump [options] database > database.sql.xz

I replaced the pipe by a file redirection but it will work the same with a Pipe. Just make sure to don't use the -t option as this will break it.


Extra:

To import back the sql dump into mysql:

docker exec -i container-name mysql [options] database < database.sql.xz

This little script will detect if I am running mysql in a pipe or not:

#!/bin/bash
if [ -t 0 ]; then
    docker exec -it container-name mysql "$@"
else
    docker exec -i container-name mysql "$@"
fi
Philippe Blanc
  • 776
  • 8
  • 7
  • 6
    A deeper explaining on why -t would break it would be nice to have :) – drAlberT Jun 27 '18 at 06:24
  • 4
    For those looking to import a database file, like the above, but using docker-compose, the following may help: `docker-compose exec -T SERVICE mysql database < database.sql.xz` Using `-T` will disable pseudo-tty allocation which allows you keep stdin open without tty. – justinpage Jan 17 '20 at 00:49
  • 2
    I don't understand how this is creating a file inside of the container. Using the `<` to read I always get `bash: /app/db/database.sql: No such file or directory`. It instead seem to create the file on the system you're locally on. – luckydonald Feb 09 '21 at 18:04