Skip to content

Instantly share code, notes, and snippets.

@themasch
Created April 23, 2018 08:40
Show Gist options
  • Save themasch/d5cb30edbe567b8bf56e12683c86141e to your computer and use it in GitHub Desktop.
Save themasch/d5cb30edbe567b8bf56e12683c86141e to your computer and use it in GitHub Desktop.
#!/usr/bin/env bash
set -Eeuo pipefail
function setup_pgpass_file {
echo -n "" > ~/.pgpass
chmod 600 ~/.pgpass
}
function backup_database {
mkdir -p "/root/db_backup/${1}"
echo "localhost:5432:${1}:${2}:${3}" > ~/.pgpass
pg_dump --exclude-table=temp_template --inserts --host=localhost --username="${2}" "${1}" | gzip > "/root/db_backup/${1}/$(date -Iminutes).sql.gz"
}
function cleanup_pgpass_file {
echo "cleaning up"
rm -f ~/.pgpass
}
function error_handler {
echo "an error occurred on line ${1}"
cleanup_pgpass_file
}
trap 'error_handler $LINENO' ERR
setup_pgpass_file
## backup prod database
PASSWORD=$(grep database_password /var/www/prod_thingy/app/config/parameters.yml | cut -d ":" -f2 | tr -d " ")
backup_database "blablaprod" "blablaprod" "$PASSWORD"
## backup test database
PASSWORD=$(grep database_password /var/www/test_thingy/app/config/parameters.yml | cut -d ":" -f2 | tr -d " ")
backup_database "blablatest" "blablatest" "$PASSWORD"
cleanup_pgpass_file
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment