Created
April 25, 2022 11:23
-
-
Save kilfu0701/ce0e53664c2bb74b74bade35de334aa2 to your computer and use it in GitHub Desktop.
Dump Postgres one table script
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 -e | |
set -o pipefail | |
# https://gist.github.com/kilfu0701/2f545f7f1a8a9d0bd923be8d38028817 | |
function echo_c() | |
{ | |
WHITE='\033[1;37m' | |
RED='\033[1;31m' | |
GREEN='\033[1;32m' | |
YELLOW='\033[1;33m' | |
BLUE='\033[1;34m' | |
PURPLE='\033[1;35m' | |
CYAN='\033[1;36m' | |
NC='\033[0m' | |
str=$1 && color=$2 | |
printf "${!color}$str$NC\n" | |
} | |
function echo_done() | |
{ | |
echo_c " Done!" "WHITE" | |
echo "" | |
} | |
# check to see if this file is being run or sourced from another script | |
_is_sourced() { | |
# https://unix.stackexchange.com/a/215279 | |
[ "${#FUNCNAME[@]}" -ge 2 ] \ | |
&& [ "${FUNCNAME[0]}" = '_is_sourced' ] \ | |
&& [ "${FUNCNAME[1]}" = 'source' ] | |
} | |
_send_notification() { | |
local msg=$1 | |
curl -X POST -H "Content-Type:application/json" -d "{\"text\": \"$1\"}" 'https://chat.googleapis.com/v1/spaces/XXXX/messages?key=YYYYYY&token=ZZZZZ' | |
} | |
_dump_process() { | |
local dbName=$1 | |
local tableName=$2 | |
start=`date +%s%3N` | |
echo_c "[_dump_process] Start DB=$dbName TABLE=$tableName" "CYAN" | |
mkdir -p /tmp/$tableName | |
PGPASSWORD=postgres pg_dump -h 0.0.0.0 -U postgres -Z0 -j 64 --data-only -Fd --table=$tableName $dbName -f "/tmp/$tableName" | |
echo_c "[_dump_process] Dump finished" "WHITE" | |
end=`date +%s%3N` | |
runtime=$( echo "$end - $start") | |
hours=$((runtime / 3600 / 1000)) | |
minutes=$(( (runtime/1000 % 3600) / 60 )) | |
seconds=$(( (runtime/1000 % 3600) % 60 )) | |
ms=$((runtime % 1000)) | |
#_send_notification "Dump finished. (DB=$dbName, TABLE=$tableName) \nRuntime: $hours:$minutes:$seconds.$ms sec." | |
} | |
_main() { | |
if [ $# -eq 2 ] | |
then | |
_dump_process $1 $2 | |
else | |
echo_c "invalid argument please pass 2 argument." "RED" | |
exit 1 | |
fi | |
} | |
if ! _is_sourced; then | |
_main "$@" | |
fi |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Usage