Last active
May 29, 2019 11:12
-
-
Save mguzelevich/fd2a472efef9db9dacf1a9ecb1ae7886 to your computer and use it in GitHub Desktop.
dump multiply databases
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
#!/bin/bash | |
# set -x | |
function dump { | |
local CONN=$1 | |
local dbname=$(echo "$CONN" | sed -r 's/dbname=(.*) user.*/\1/') | |
echo -e "process $dbname <$CONN>" | |
local sql=$(docker run -i postgres pg_dump "$CONN" 2>&1) | |
local EXIT_CODE=$? | |
if [ $EXIT_CODE -ne 0 ]; then | |
MSG=$(echo -e "process $dbname FAILED") | |
else | |
echo "$sql" > $dbname.sql | |
MSG=$(echo -e "process $dbname DONE") | |
fi | |
echo -e "$MSG" | |
} | |
function dump_all { | |
local SRC=$1 | |
IFS=$'\n' | |
echo -e "$SRC" | while read line i; do | |
# echo -e "[$i] [$line]"; | |
dump "$line"; | |
done | |
} | |
####################################################### | |
# main | |
####################################################### | |
# source `pwd`/keys.sh | |
### input file format | |
# dbname=db1 user=user password=secret host=localhost sslmode=disable | |
# dbname=db1 user=user password=secret host=localhost sslmode=disable | |
### | |
readonly PROGNAME=$(basename $0) | |
readonly PROGDIR=$(readlink -m $(dirname $0)) | |
readonly ARGS="$@" | |
readonly DB_LIST='databases.csv' | |
function main { | |
readonly TS=$(date +%Y%m%d_%H%M%S) | |
# process input (from file or from pipe) | |
INPUT=$( | |
IFS=$'\n' | |
while read -t 1 stdin_data; do | |
echo -e "$stdin_data" | |
done | |
) | |
if [[ $INPUT == '' ]]; then | |
INPUT=$(cat $DB_LIST) | |
fi | |
readonly DATA=$( | |
echo -e "$INPUT" \ | |
| sort -u \ | |
| grep -vE "^ *$" \ | |
| grep -v "^#" | |
) | |
# echo -e "<<<INPUT\n$INPUT\n>>>" | |
# echo -e "<<<DATA\n$DATA\n>>>" | |
OLDIFS=$IFS | |
dump_all "$DATA" | |
IFS=$OLDIFS | |
} | |
main |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment