Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/env bash
- # Strict execution
- set -o errexit
- set -o nounset
- set -o pipefail
- # Load PPAS environment
- source "/opt/PostgresPlus/9.5AS/pgplus_env.sh"
- export PGPASSWORD=""
- # General configuration
- declare -A cfg
- cfg['directory']="/tmp"
- cfg['format']="custom"
- cfg['suffix']="$(date +%Y%m%d%H%M)"
- # Log configuration
- declare -A log
- log['directory']="/var/log/support/ppas"
- log['file']="${log['directory']}/$(basename "${BASH_SOURCE[0]%.*}").log"
- # External backup configuration
- declare -A storage
- storage['active']="0"
- storage['directory']=""
- storage['host']=""
- storage['user']=""
- # Redirect output to log['file'] and stdout
- exec &> >(tee -a "${log['file']}")
- # Add current timestamp to the logs
- echo "$(date)"
- # Usage function
- function usage() {
- echo -n "$(basename $0) [OPTION]...
- PostgreSQL backup script
- Options:
- -d <database> Backup <database>
- -h Display this help and exit
- "
- }
- # Backup function
- function backup() {
- echo "Saving database ${1}" "${1}"
- 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"
- echo "Database successfully saved to ${cfg['directory']}/${1}${cfg['suffix']}.${cfg['format']}.gz"
- # If external storage is active, send the backup to it
- if [[ "${storage['active']}" -eq 1 ]]; then
- scp "${cfg['directory']}/${1}${cfg['suffix']}.${cfg['format']}.gz" "${storage['user']}@${storage['host']}:${storage['directory']}"
- fi
- }
- # Display usage if no arguments were passed
- if [[ "${#}" -eq 0 ]]; then
- usage
- exit 0
- fi
- # If arguments were passed, parse them
- while getopts "d:h" parameter; do
- case "${parameter}" in
- d) backup "${OPTARG}" ;;
- h) usage && exit 0 ;;
- \?) usage && exit 0 ;;
- esac
- done
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement