Created
November 13, 2019 23:37
-
-
Save thomascate/529f60c494378d4a9d46a2d7feb39115 to your computer and use it in GitHub Desktop.
Chef Automate backup sql
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env bash | |
date_stamp=$(date +"%Y%m%d%H%M%S") | |
backup_path=${1:-/mnt/automate_backups/postgresql/pg_dump/$date_stamp} | |
echo $backup_path | |
mkdir -p $backup_path | |
PATH=$PATH:$(HAB_LICENSE=accept-no-persist hab pkg path core/postgresql11)/bin | |
export PATH | |
source /hab/svc/automate-backend-postgresql/config/functions.sh | |
# primary_connect_string will always contain the IP of the leader in the cluster | |
db_list=$(psql ${primary_connect_string} -d postgres -t -c 'SELECT datname from pg_database') | |
# dump users/roles/etcs | |
#pg_dumpall ${primary_connect_string} --globals-only --clean --encoding=utf8 --if-exists > $backup_path/pg_dump_$date_stamp.sql | |
while read -r database; do | |
if [[ ! $database =~ ^(automate\-cs\-|template).* ]]; then | |
echo "Backing up $database to $backup_path/pg_dump_$date_stamp.sql" | |
pg_dump ${primary_connect_string} $database --clean --encoding=utf8 --if-exists >> $backup_path/pg_dump_$date_stamp.sql | |
fi | |
done <<< "${db_list}" | |
$(hab pkg path core/gzip)/bin/gzip $backup_path/pg_dump_$date_stamp.sql | |
echo "backed up file to $backup_path/pg_dump_$date_stamp.sql.gz" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment