Loading...
 

Backup and Restore

How to backup the databases

All the commands below must be launched in sudo mode

For PostgreSQL :

docker exec -e PGPASSWORD="docker" postgresql pg_dump -h localhost -U docker -f BU.sql docker
docker cp postgresql:/BU.sql .
docker exec postgresql rm /BU.sql

 

For MongoDB :

docker exec -it mongodb mongodump -h localhost -o /tmp/BU
docker cp mongodb:/tmp/BU .
docker exec -it mongodb rm -r /tmp/BU

 

How to restore

For PostgreSQL :

docker exec -e PGPASSWORD="docker" -i -t postgresql psql -h localhost -U docker -c "UPDATE pg_database SET datistemplate = 'false' WHERE datname ='docker'"
docker exec -e PGPASSWORD="docker" -i -t postgresql psql -h localhost -U docker -c "select pg_terminate_backend(pid) from pg_stat_activity where datname='docker' AND pid <> pg_backend_pid()"
docker exec -e PGPASSWORD="docker" -i -t postgresql dropdb -h localhost -U docker docker

#Then recreate the db (the commands are on your deployment files).
docker exec -e PGPASSWORD="docker" -i -t postgresql createdb -h localhost -U docker --encoding='utf-8' --template=template0 -O docker docker
docker exec -e PGPASSWORD="docker" -i -t postgresql psql -d docker -h localhost -U docker -c "UPDATE pg_database SET datistemplate='true' WHERE datname='docker'"
docker exec -e PGPASSWORD="docker" -i -t postgresql psql -d docker -h localhost -U docker -c "CREATE EXTENSION ltree;"
docker exec -e PGPASSWORD="docker" -i -t postgresql psql -d docker -h localhost -U docker -c "CREATE EXTENSION postgis;"
docker exec -e PGPASSWORD="docker" -i -t postgresql psql -d docker -h localhost -U docker -c "GRANT ALL ON geometry_columns TO PUBLIC;"
docker exec -e PGPASSWORD="docker" -i -t postgresql psql -d docker -h localhost -U docker -c "GRANT ALL ON geography_columns TO PUBLIC;"
docker exec -e PGPASSWORD="docker" -i -t postgresql psql -d docker -h localhost -U docker -c "GRANT ALL ON spatial_ref_sys TO PUBLIC;"

docker cp BU.sql postgresql:.
docker exec -e PGPASSWORD="docker" -i -t postgresql psql -h localhost -U docker -d docker -f /BU.sql

 

For MongoDB :

 
docker cp BU mongodb:/tmp/BU
docker exec -it mongodb mongorestore /tmp/BU