Handling errors and debugging
While running our backup script, we can encounter several errors: access to the database might be blocked, the pg_dump
process might get killed, we may be out of disk space, or any other error preventing us from completing a full database dump.
In any of those cases, we will need to catch the error and handle it gracefully.
Additionally, we might want to refactor the script to make it configurable, make use of functions, and debug the script. Debugging will prove very useful, especially when dealing with larger scripts.
Let’s dive right into it and start with adding a function:
#!/usr/bin/env bash function run_dump() { database_name=$1 pg_dump -U postgres $database_name > $database_name.sql } run_dump mydatabase
We’ve added a run_dump
function that takes one argument and sets a local variable called database_name
with the content of this argument. It then uses this local variable to pass options to...