Skip to content

Instantly share code, notes, and snippets.

@rectalogic
Created April 20, 2017 20:39
Show Gist options
  • Save rectalogic/657d858a5bd546890a29cb98c9804c4e to your computer and use it in GitHub Desktop.
Save rectalogic/657d858a5bd546890a29cb98c9804c4e to your computer and use it in GitHub Desktop.
#!/bin/bash
# Copies an entire mongodb collection into an RDS table, skipping existing rows.
MONGOHOST=""
RDS=""
TABLE=""
DB="cureatr"
COLLECTION=""
usage () { echo "$0 [-h] -r <RDS url> -t <RDS table> -m <mongo host> -d <mongo database> -c <mongo collection>"; }
OPTIND=1
while getopts "h?r:t:m:d:c:" opt; do
case "$opt" in
h|\?)
usage
exit 0
;;
r) RDS=$OPTARG;;
t) TABLE=$OPTARG;;
m) MONGOHOST=$OPTARG;;
d) DB=$OPTARG;;
c) COLLECTION=$OPTARG;;
esac
done
shift $((OPTIND-1))
if [ ! "$MONGOHOST " ] || [ ! "$RDS " ] || [ ! "$TABLE" ] || [ ! "$DB" ] || [ ! "$COLLECTION" ]; then
usage
exit 1
fi
mongoexport --quiet --host ${MONGOHOST} --db ${DB} --collection ${COLLECTION} --ssl --sslAllowInvalidCertificates | \
psql --single-transaction \
-c "CREATE TEMPORARY TABLE \"#${TABLE}\"(document jsonb);" \
-c "COPY \"#${TABLE}\"(document) FROM stdin csv QUOTE e'\x01' DELIMITER e'\x02' ENCODING 'utf-8';" \
-c "CREATE TABLE IF NOT EXISTS \"${TABLE}\"(_id CHAR(24) PRIMARY KEY, document JSONB);" \
-c "INSERT INTO \"${TABLE}\" (_id, document) SELECT document#>>'{_id,\$oid}',document FROM \"#${TABLE}\" ON CONFLICT DO NOTHING;" \
-c "DROP TABLE \"#${TABLE}\";" \
"${RDS}"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment