Guest User

Untitled

a guest
Apr 30th, 2018
140
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.03 KB | None | 0 0
  1. #!/usr/bin/env bash
  2.  
  3. set -Eeuo pipefail
  4.  
  5. function setup_pgpass_file {
  6. echo -n "" > ~/.pgpass
  7. chmod 600 ~/.pgpass
  8. }
  9.  
  10. function backup_database {
  11. mkdir -p "/root/db_backup/${1}"
  12. echo "localhost:5432:${1}:${2}:${3}" > ~/.pgpass
  13. pg_dump --exclude-table=temp_template --inserts --host=localhost --username="${2}" "${1}" | gzip > "/root/db_backup/${1}/$(date -Iminutes).sql.gz"
  14. }
  15.  
  16. function cleanup_pgpass_file {
  17. echo "cleaning up"
  18. rm -f ~/.pgpass
  19. }
  20.  
  21. function error_handler {
  22. echo "an error occurred on line ${1}"
  23. cleanup_pgpass_file
  24. }
  25.  
  26. trap 'error_handler $LINENO' ERR
  27.  
  28. setup_pgpass_file
  29.  
  30. ## backup prod database
  31. PASSWORD=$(grep database_password /var/www/prod_thingy/app/config/parameters.yml | cut -d ":" -f2 | tr -d " ")
  32. backup_database "blablaprod" "blablaprod" "$PASSWORD"
  33.  
  34. ## backup test database
  35. PASSWORD=$(grep database_password /var/www/test_thingy/app/config/parameters.yml | cut -d ":" -f2 | tr -d " ")
  36. backup_database "blablatest" "blablatest" "$PASSWORD"
  37.  
  38. cleanup_pgpass_file
Add Comment
Please, Sign In to add comment