#!/bin/bash
# ARGUMENTS
# ---------
# required:
# URL: postgresql://user[:passwd]@host[:port]/dbname (1st positional argument)
# optional:
# --split
# --compress
# --schema
SCHEMA="public"
POSITIONAL=()
while [[ $# -gt 0 ]]
do
key="$1"
case $key in
--split)
SPLIT=true
shift
;;
--compress)
COMPRESS=true
shift
;;
--schema)
SCHEMA="$2"
shift
shift
;;
*) # unknown option
POSITIONAL+=("$1") # save it in an array for later
shift # past argument
;;
esac
done
set -- "${POSITIONAL[@]}" # restore positional parameters
PGURL=$1
PGDATABASE=$(echo $PGURL | sed 's/.*\/\(.*\)$/\1/') # last token in URL is the DB name
echo "Host: $PGURL"
echo "Database: $PGDATABASE"
echo "Schema: $SCHEMA"
echo "Compress: $COMPRESS"
echo "Split: $SPLIT"
if [ -z "$PGURL" ];
then
echo "ERROR: No URL connection has been provided!"
exit 1
fi
# Export & Split & Compress
if [ "$SPLIT" = true ] && [ "$COMPRESS" = true ];
then
psql $PGURL --no-align --tuples-only \
--command="select tablename from pg_tables where schemaname='$SCHEMA'" | \
while read TBL; do
file_name="$PGDATABASE-$TBL."
tmp_file_name="/tmp/$(cat /proc/sys/kernel/random/uuid).csv"
psql $PGURL --command="COPY $SCHEMA.$TBL TO STDOUT WITH (FORMAT csv, NULL 'NULL')" > $tmp_file_name
split \
--line-bytes=100MB \
--numeric-suffixes \
--additional-suffix=".csv" \
--filter="gzip > \$FILE.gz" $tmp_file_name $file_name
rm $tmp_file_name
done
exit 0
fi
# Export & Split
if [ "$SPLIT" = true ];
then
psql $PGURL \
--no-align \
--tuples-only \
--command="select tablename from pg_tables where schemaname='$SCHEMA'" | \
while read TBL;
do
file_name=$PGDATABASE-$TBL
tmp_file_name="/tmp/$(cat /proc/sys/kernel/random/uuid).csv"
psql $PGURL --command="COPY $SCHEMA.$TBL TO STDOUT WITH (FORMAT csv, NULL 'NULL')" > $tmp_file_name
split \
--line-bytes=1000MB \
--numeric-suffixes \
--additional-suffix=".csv" $tmp_file_name $file_name.
rm $tmp_file_name
done
exit 0
fi
# Export & Compress
if [ "$COMPRESS" = true ];
then
psql $PGURL \
--no-align \
--tuples-only \
--command="select tablename from pg_tables where schemaname='$SCHEMA'" | \
while read TBL;
do
file_name=$PGDATABASE-$TBL
tmp_file_name="/tmp/$(cat /proc/sys/kernel/random/uuid).csv"
psql $PGURL --command="COPY $SCHEMA.$TBL TO PROGRAM 'gzip > $file_name.gz' WITH (FORMAT csv, NULL 'NULL')"
done
exit 0
fi
# Export
psql $PGURL --no-align --tuples-only \
--command="select tablename from pg_tables where schemaname='$SCHEMA'" | \
while read TBL; do
psql $PGURL --command="COPY $SCHEMA.$TBL TO STDOUT WITH (FORMAT csv, NULL 'NULL')" $PGDATABASE > $TBL.csv
done