Advertisement
Guest User

Untitled

a guest
Jul 2nd, 2016
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 1.87 KB | None | 0 0
  1.  
  2. #!/usr/bin/env bash
  3.  
  4. # Strict execution
  5. set -o errexit
  6. set -o nounset
  7. set -o pipefail
  8.  
  9. # Load PPAS environment
  10. source "/opt/PostgresPlus/9.5AS/pgplus_env.sh"
  11. export PGPASSWORD=""
  12.  
  13. # General configuration
  14. declare -A cfg
  15. cfg['directory']="/tmp"
  16. cfg['format']="custom"
  17. cfg['suffix']="$(date +%Y%m%d%H%M)"
  18.  
  19. # Log configuration
  20. declare -A log
  21. log['directory']="/var/log/support/ppas"
  22. log['file']="${log['directory']}/$(basename "${BASH_SOURCE[0]%.*}").log"
  23.  
  24. # External backup configuration
  25. declare -A storage
  26. storage['active']="0"
  27. storage['directory']=""
  28. storage['host']=""
  29. storage['user']=""
  30.  
  31. # Redirect output to log['file'] and stdout
  32. exec &> >(tee -a "${log['file']}")
  33.  
  34. # Add current timestamp to the logs
  35. echo "$(date)"
  36.  
  37. # Usage function
  38. function usage() {
  39.   echo -n "$(basename $0) [OPTION]...
  40. PostgreSQL backup script
  41. Options:
  42.    -d <database>   Backup <database>
  43.    -h              Display this help and exit
  44. "
  45. }
  46.  
  47. # Backup function
  48. function backup() {
  49.     echo "Saving database ${1}" "${1}"
  50.     pg_dump --format="${cfg['format']}" --username=postgres --host="127.0.0.1" --port=5432 --password "${1}" | gzip > "${cfg['directory']}/${1}${cfg['suffix']}.${cfg['format']}.gz"
  51.     echo "Database successfully saved to ${cfg['directory']}/${1}${cfg['suffix']}.${cfg['format']}.gz"
  52.  
  53.     # If external storage is active, send the backup to it
  54.     if [[ "${storage['active']}" -eq 1 ]]; then
  55.         scp "${cfg['directory']}/${1}${cfg['suffix']}.${cfg['format']}.gz" "${storage['user']}@${storage['host']}:${storage['directory']}"
  56.     fi
  57. }
  58.  
  59. # Display usage if no arguments were passed
  60. if [[ "${#}" -eq 0 ]]; then
  61.     usage
  62.     exit 0
  63. fi
  64.  
  65. # If arguments were passed, parse them
  66. while getopts "d:h" parameter; do
  67.     case "${parameter}" in
  68.         d) backup "${OPTARG}" ;;
  69.         h) usage && exit 0 ;;
  70.         \?) usage && exit 0 ;;
  71.     esac
  72. done
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement