Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/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
Add Comment
Please, Sign In to add comment