My blog runs inside a Docker container on a DigitalOcean Droplet. More precisely, I'm using a docker-compose service consisting of several Docker containers, one among them being a PostgreSQL container holding the database of my blog. Every now and then I manually create backups of that DB:
- I ssh into my droplet (=server)
- on the server, I enter the postgres container to create a dump of my DB in a folder that is bind mounted
- in a last step I rsync the dumped DB from the server onto my local PC.
I have wanted to automate these steps via a bash script ever since my first dump, but I never took the time to do so. Recently, I have started to use Ansible, a tool which is designed for executing commands on remote machines over ssh. So I figured the above use case would be perfectly suitable to implement it with Ansible.
However, in this post we will first set up an Ansible Inventory to save the IP address of the remote machine as a variable so we won't have to type the IP address everytime we execute an Ansible command. Yeah, I'm pretty lazy when it comes to repetitive tasks that can be easily automated :)