Skip to content

Instantly share code, notes, and snippets.

@subfuzion
Last active September 21, 2016 01:00
Show Gist options
  • Save subfuzion/346271fb37edc3178b68 to your computer and use it in GitHub Desktop.
Save subfuzion/346271fb37edc3178b68 to your computer and use it in GitHub Desktop.
docker-machine ssh command for mongodump backup

Set up

Create a Docker data volume container

$ docker create --name dbdata -v /dbdata mongo /bin/true

Start mongo, expose port to connect

$ docker run -d --name mongo --volumes-from dbdata mongo

Verify you can connect to mongo using the mongo client

$ docker run -it --link mongo:mongo --rm mongo sh -c 'exec mongo "$MONGO_PORT_27017_TCP_ADDR:$MONGO_PORT_27017_TCP_PORT/test"'

Backup your mongo database using docker-machine ssh and scp

The docker-machine ssh takes a host and a command argument to execute on the host.

The following doesn't work:

$  docker-machine ssh demo 'docker run --rm --link mongo:mongo -v $(pwd):/backup mongo bash -c "mongodump --out /backup --host $MONGO_PORT_27017_TCP_ADDR"'

SSH cmd error!
command: docker run --rm --link mongo:mongo -v $(pwd):/backup mongo bash -c "mongodump --out /backup --host $MONGO_PORT_27017_TCP_ADDR"
err    : exit status 3
output : 2015-09-15T16:30:20.460+0000   error parsing command line options: expected argument for flag `-h, --host'
2015-09-15T16:30:20.462+0000    try 'mongodump --help' for more information

Using separate steps, it works:

$ docker-machine ssh demo
root@demo:~# docker run --rm --link mongo:mongo -v $HOME:/backup mongo bash -c 'mongodump --out /backup --host $MONGO_PORT_27017_TCP_ADDR'
2015-09-15T16:34:02.676+0000    writing test.samples to /backup/test/samples.bson
2015-09-15T16:34:02.678+0000    writing test.samples metadata to /backup/test/samples.metadata.json
2015-09-15T16:34:02.678+0000    done dumping test.samples (1 document)
2015-09-15T16:34:02.679+0000    writing test.system.indexes to /backup/test/system.indexes.bson

The flag parsing terminator (--) can be used to get the ssh command to pass the entire command string, but now we get a different error:

$ docker-machine ssh demo -- docker run --rm --link mongo:mongo -v $HOME:/backup mongo bash -c 'mongodump --out /backup --host $MONGO_PORT_27017_TCP_ADDR'
SSH cmd error!
command: docker run --rm --link mongo:mongo -v /Users/tony:/backup mongo bash -c mongodump --out /backup --host $MONGO_PORT_27017_TCP_ADDR
err    : exit status 1
output : 2015-09-15T16:53:07.717+0000   Failed: error connecting to db server: no reachable servers

My latest attempt appears to execute the command correctly, although the data isn't written to the system and the session hangs:

$ docker-machine ssh demo -- $(docker run --rm --link mongo:mongo -v $HOME:/backup mongo bash -c 'mongodump --out /backup --host $MONGO_PORT_27017_TCP_ADDR')
2015-09-15T18:02:03.347+0000    writing test.samples to /backup/test/samples.bson
2015-09-15T18:02:03.349+0000    writing test.samples metadata to /backup/test/samples.metadata.json
2015-09-15T18:02:03.349+0000    done dumping test.samples (1 document)
2015-09-15T18:02:03.350+0000    writing test.system.indexes to /backup/test/system.indexes.bson

http://stackoverflow.com/questions/32591809/docker-machine-ssh-command-for-mongodump

@subfuzion
Copy link
Author

I just need to make one change now to ensure the correct path to write to on the remote (DigitalOcean) machine host (I don't want $HOME on my system, but the home directory for root on the remote host):

$ docker run \
    --rm \
    --link mongo:mongo \
    -v /root:/backup \
    mongo \
    bash -c 'mongodump --out /backup --host $MONGO_PORT_27017_TCP_ADDR'

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment