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

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.

Related

Bash script, remote psql command over ssh, pipe sql file, password fails

I have a bash script. I want to run a postgres command with ssh that pipes a local file. The problem is the psql command prompts for a password, and my sql file gets piped into that. How do I write a command that pipes after I type in the password?
ssh server "psql -W -h db_host -p 5432 -U db_user -d postgres" < staging.sql
I suggest to break it down into multiple steps:
# Transfer the sql file to the server
scp staging.sql server
# Excute the queries in that file with psql over ssh
# Notes:
# - ssh -t enforces terminal allocation. You may try it without this option and see if it still works.
# - psql -f FILENAME reads commands from file
#
ssh -t server \
'psql -W -h db_host -U db_user -d postgres -f staging.sql; rm staging.sql'

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

Can't find mongodump on Windows/Docker

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

how to periodically dump a set of database tables to a local machine from a psql database in a single bash script run on Mac OS X?

I tried this but it does not work.
#!/bin/bash
TABLENAMES="user_stats"
ssh -t railsapps#xxx.xxx.xxx.xx -p xxx bash -c "'
for TABLENAME in $TABLENAMES
do
psql -d mydb -P format=unaligned -P tuples_only -P fieldsep=\, -c "SELECT * FROM $TABLENAME" > /tmp/$TABLENAME
done
'"
You should use the Postgres COPY command, and stuff them all in to a single sql file, that you then run with psql. Then you can take that script and either feed it to cron on Mac OS X, or launchd to run the script periodically.

Resources