Backing Up Our Database
One of the final tasks we need to address is how to back up a postgres database running inside a docker container.
Not that difficult:
docker exec postgres pg_dump -U user_todo db_todo > db_backups/backup.sql
After running the command above if you check the contents of your server/db_backups directory you should see a backup.sql file containing all your data.
Our Makefile that we created in Part 1 of this tutorial we will now populate with common Docker shortcut commands that come in handy.
# server/Makefile
pytest:
docker-compose run server pytest
test:
docker-compose run server ./manage.py test
build:
docker-compose build
reboot:
docker-compose down && docker-compose up -d
prod:
docker-compose up -d
dev:
docker-compose -f docker-compose.yml -f docker-compose-dev.yml up -d
up-non-daemon:
docker-compose up
start:
docker-compose start
stop:
docker-compose stop
down:
docker-compose down
restart:
docker-compose stop && docker-compose start
restart-dev:
docker-compose down && docker-compose -f docker-compose.yml -f docker-compose-dev.yml up -d
restart-frontend:
docker-compose stop frontend && docker-compose start frontend
restart-server:
docker-compose stop server && docker-compose start server
shell-server:
docker exec -ti server bash
shell-frontend:
docker exec -ti frontend bash
shell-db:
docker exec -ti postgres bash
log-server:
docker-compose logs server
log-frontend:
docker-compose logs frontend
log-db:
docker-compose logs postgres
collectstatic:
docker exec server /bin/sh -c "python manage.py collectstatic --noinput"
migrations:
docker exec server /bin/sh -c "python manage.py makemigrations; python manage.py migrate"
So now, when we want to reboot instead of having to run:
docker-compose down
docker-compose up -d
… we can simply run:
make reboot
… or running migrations:
make migrations
… instead of what we have been doing throughout this tutorial:
docker-compose run server ./manage.py makemigrations
docker-compose run server ./manage.py migrate
You will have to have Make installed on your development computer. I’m using a Mac so for me I only to run:
brew install make
Note! If you are using VSCode, as I am, you need to hit command->shift->p and add “Convert Indentation to Tabs” otherwise you will get the following error:
web:todo-app-graphql-v2 ronleeson$ make test
Makefile:3: *** missing separator. Stop.
Makefile is picky about tabs. You can’t use spaces in place of tabs.
Installing Make on Windows is something else, I’ll leave that to you reader to figure that out.