Can't find mongodump on Windows/Docker - windows

I am trying to dump my mongodb, which is currently in a docker container on windows. I run the following command:
docker run --rm --link docker-mongodb_1:mongo --net docker_default -v /backup:/backup mongo bash -c "mongodump --out /backup/ --host mongo:27017"
The output is something like this (with no errors):
"writing db.entity to "
"done dumping db.entity"
However, I cannot find the actual dump. I have checked C:/backup, my local directory. Tried renaming the output and volumes, but with no luck. Does anyone know where the dump is stored?

I have been trying to do the same. I have written a shell script which does this process of backing up the data as you require. You first need to run the container with a name (whatever you wish that container name to be)
BACKUP_DIR="/backup"
DB_CONTAINER_ID=$(docker ps -aqf "name=<**name of your container**>")
NETWORK_ID=$(docker inspect -f "{{ .NetworkSettings.Networks.root_default.NetworkID }}" $DB_CONTAINER_ID)
docker run -v $BACKUP_DIR:/backup --network $NETWORK_ID mongo:3.4 su -c "cd /backup && mongodump -h db -u <username> -p <password> --authenticationDatabase <db_name> --db <db_name>"
tar -zcvf $BACKUP_DIR/db.tgz $BACKUP_DIR/dump
rm -rf $BACKUP_DIR/dump

Related

Docker: "pg_restore input file does not appear to be a valid archive" error

I backup a PostgreSql database using the following command and it creates an sql file:
cmd /c 'docker exec -t <container-name> pg_dump <db_name> -U postgres -c
-v > C:\\backup\\<db_name>.sql'
However, I cannot restore the sql backup file using the following command:
first I drop and create an empty db:
docker exec <container-name> bash -c "dropdb -U postgres <db_name>"
docker exec <container-name> bash -c "createdb -U postgres <db_name>"
then restore:
cmd /c "docker exec -i <container-name> pg_restore -C -U postgres -d
<db_name> -v < C:\\<db_name>.sql"
gives "pg_restore: error: input file does not appear to be a valid archive" error. So,
1. how can I restore the database with sql file?
2. how can I backup PostgreSql db in Docker on Windows?
A plain-text pg_dump is restored with psql, not with pg_restore.
I don't understand why you want to run that inside the container. Install a PostgreSQL client on the host operating system and simplify the procedure. Besides, a backup should be on a different machine than the database.

Unable to run queries from a file using psql command line with docker exec

I have a bash file should bring the postgres docker container online and then run a .sql file to create the databases. But it's throwing the error.
psql: error: provision-db.sql: No such file or directory
I have checked the path and the file exists at the same level of this bash script. Following is the content of my bash file.
#!/usr/bin/env bash
docker-compose up -d db
# Ensure the Postgres server is online and usable
until docker exec -i boohoo.postgres pg_isready --host="${POSTGRES_HOST}" --username="${POSTGRES_USER}"
do
echo "."
sleep 1
done
docker exec -i boohoo.postgres psql -h "${POSTGRES_HOST}" -U "${POSTGRES_USER}" -a -q -f provision-db.sql
And this is the provision-db.sql file.
DROP DATABASE "boo-hoo";
CREATE DATABASE "boo-hoo";
GRANT ALL PRIVILEGES ON DATABASE "boo-hoo" TO postgres;
This is the part of docker-compose.yml
version: '3.3'
services:
db:
container_name: boohoo.postgres
hostname: postgres.boohoo
image: postgres
ports:
- "15432:5432"
environment:
POSTGRES_USER: "postgres"
POSTGRES_PASSWORD: "postgres"
The short version
This works
cat provision-db.sql | docker exec -i boohoo.postgres bash -c 'psql -U ${POSTGRES_USER} -w -a -q -f -'
The long version
multiple things here
1) why does following command not find the provision-db.sql?
docker exec -i boohoo.postgres psql -h "${POSTGRES_HOST}" -U "${POSTGRES_USER}" -a -q -f provision-db.sql
because the provision-db.sql is on your host and not in your container. Therefore, when you execute the psql command inside the container it can not find the file
2) Why didn't my first solution work?
cat provision-db.sql | docker exec -i boohoo.postgres psql -h "${POSTGRES_HOST}" -U "${POSTGRES_USER}" -a -q -f - should do the trick asuming provision-db.sql
That is due to the fact, that the variables ${POSTGRES_USER} and ${POSTGRES_PASSWORD} get evaluated on your host machine and I guess they are not set there. In addition, I forgot to specify the -w flag to avoid the password prompt
3) Why does that work?
cat provision-db.sql | docker exec -i boohoo.postgres bash -c 'psql -U ${POSTGRES_USER} -w -a -q -f -'
Well, let's go through it step by step.
First, we print the content of provision-db.sql, which resides on the host machine to stdout and pipe it to the next command via |.
docker-exec executes a command in the container specified (boohoo.postgres). By specifying the -i flag we allow the stdin from your host to go to stdin in the container <- that's important.
In the container, we execute bash -c which is just a wrapper to avoid evaluating the bash variables on the host. We want the variables from the container and by putting it into single quotes we can do that.
docker-exec boohoo.postgres bash -c "echo $POSTGRES_USER"
evaluates the host env variable named POSTGRES_USER.
docker-exec boohoo.postgres bash -c "echo $POSTGRES_USER"
evaluates the container env variable named POSTGRES_USER.
Next we just have to get our postgres command in order.
psql -U ${POSTGRES_USER} -w -a -q -f -
-U specifies the user
-w does not ask for password
-q do it quietly
-f - process whatever you get from stdin
-f is an option for psql and not for docker exec, and psql is running inside the container, so it can only access the file if it is inside the container as well.

How do I inject a local file as an argument to a command to run inside a docker container?

Scenario:
I have a postgres container named db running on a machine. I am in a directory on the host and have an SQL script named patch.sql. I wish to apply this script to the database inside the container.
Were I to be inside the container and have the script also inside the container, I would run
psql -U user -d db -f patch.sql
Since I am outside the container, I could naively try
docker exec -i db psql -U user -d db -f patch.sql
but of course, this would look for a file named patch.sql inside the container, while it is actually on the host machine.
My current workaround is
cat patch.sql | docker exec -i db /bin/sh -c "cat $# > patch.sql"
docker exec -i db psql -U user -d db -f patch.sql
docker exec -i db rm patch.sql
Is there away to elegantly reduce this to a one-liner?
I am aware, how to place the file inside the container, this is exactly what my workaround does. I am thinking of some trick with I/O redirection to place the file into the command.
I do not want to mount volumes and I cannot do this, since the container is already running anyway. The idea is to avoid moving the file into the container.
Maybe could try directly pipe the patch.sql file content to psql, like
cat patch.sql | docker exec -i db psql -U user -d db -f -
or just
cat patch.sql | docker exec -i db psql -U user -d db

bash: How do I write a shell script to sftp a mongodump from a MongoDB Docker container inside a DigitalOcean droplet to backup the MongoDB database?

I'm running a MeteorJS webapp deployed with meteor-up on a DO droplet. I would like to have a .sh run from my local machine to get backups occasionally. Folders being able to have timestamps would be a plus.
This is what I am trying to achieve with a single .sh file:
on local machine:
ssh root#my.droplet.address
# <prompt for password>
inside droplet:
docker exec -it mongodb bash
in mongodb docker:
rm -rf dump
mongodump -h 127.0.0.1 -d app
on remote droplet:
rm -rf dump
docker cp mongodb:/dump dump
on local machine:
sftp root#my.droplet.address
in remote droplet (sftp):
DATE=`date +%Y-%m-%d_%H%M%S`
get -r dump $DATE
Is it possible to get all these in one .sh file?
In the script you can run the commands in the following way:
ssh root#my.droplet.address "docker exec mongodb rm -rf dump && docker exec mongodb mongodump -h 127.0.0.1 -d app && rm -rf dump && docker cp mongodb:/dump dump && docker cp mongodb:/dump dump"
ssh root#my.droplet.address:/dump $(date +%Y-%m-%d_%H%M%S) >/dev/null 2
This is how I managed to do it with one .sh file. Without ssh keys, this will prompt you twice for the root password. the dump folder will be copied to ./$DATE. where $DATE = current datetime on the local machine.
#!/bin/bash
DATE=`date +%Y-%m-%d_%H%M%S`
ssh root#cleanr.ivanho.me "docker exec mongodb rm -rf dump && docker exec mongodb mongodump -h 127.0.0.1 -d app && rm -rf dump && docker cp mongodb:/dump dump"
scp -r root#cleanr.ivanho.me:/root/dump $DATE

Dockerfile CMD not running at container start

So i've written a Dockerfile for a project, i've defined a CMD to run on starting the container to bootstrap the application.
The Dockerfile looks like
# create our mount folders and volumes
ENV MOUNTED_VOLUME_DIR=sites
RUN mkdir /$MOUNTED_VOLUME_DIR
ENV PATH=$MOUNTED_VOLUME_DIR/sbin:$MOUNTED_VOLUME_DIR/common/bin:$PATH
RUN chown -Rf www-data:www-data /$MOUNTED_VOLUME_DIR
# Mount folders
VOLUME ["/$MOUNTED_VOLUME_DIR/"]
# Expose Ports
EXPOSE 443
# add our environment variables to the server
ADD ./env /env
# Add entry point script
ADD ./start.sh /usr/bin/startContainer
RUN chmod 755 /usr/bin/startContainer
# define entrypoint command
CMD ["/bin/bash", "/usr/bin/startContainer"]
The start.sh script, does some git stuff like cloning the right repo, setting environment vars, as well as starting supervisor.
The start script begins with this
#!/bin/bash
now=$(date +"%T")
echo "Container Start Time : $now" >> /tmp/start.txt
/usr/bin/supervisord -n -c /etc/supervisord.conf
I start my new container like this
docker run -d -p expoPort:contPort -t -i -v /$MOUNTED_VOLUME_DIR/$PROJECT:/$MOUNTED_VOLUME_DIR $CONTAINER_ID /bin/bash
when i login to the container i see that supervisor hasn't been started, and neither has nginx or php5-fpm. the /tmp/start.txt file with a timestamp set from the startContainer script doesn't exist, showing its never ran the CMD in the Dockerfile.
Any hints on to get this fixed would be great
This:
docker run -d -p expoPort:contPort -t -i -v /$MOUNTED_VOLUME_DIR/$PROJECT:/$MOUNTED_VOLUME_DIR $CONTAINER_ID /bin/bash
Says 'run /bin/bash' after instantiating the container. E.g. skip CMD.
Try this:
docker run -d -p expoPort:contPort -t -i -v /$MOUNTED_VOLUME_DIR/$PROJECT:/$MOUNTED_VOLUME_DIR $CONTAINER_ID

Resources