Guest User

Untitled

a guest
Jan 30th, 2019
138
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.71 KB | None | 0 0
  1. #!/bin/bash
  2.  
  3. export AWS_ACCESS_KEY_ID=...
  4. export AWS_SECRET_ACCESS_KEY=...
  5. export AWS_DEFAULT_REGION=...
  6.  
  7. PG_HOST=...
  8. PG_USER=...
  9. PG_PASS='...'
  10. PG_DB=...
  11.  
  12. S3_BUCKET=...
  13.  
  14. function export_and_copy_to_s3() {
  15. NAME=$1
  16. TABLE_OR_SELECT=$2
  17.  
  18. LOCAL_FILE=/tmp/${NAME}.csv
  19.  
  20. PGPASSWORD=$PG_PASS psql -h $PG_HOST -U $PG_USER -d $PG_DB -c "COPY $TABLE_OR_SELECT TO stdout CSV DELIMITER ',' quote '\"' force quote *" > $LOCAL_FILE
  21.  
  22. SNAPSHOT_ID="$(date +%Y%m%d)"
  23. aws s3 cp $LOCAL_FILE s3://${S3_BUCKET}/${SNAPSHOT_ID}/${NAME}/
  24.  
  25. rm $LOCAL_FILE
  26. }
  27.  
  28. export_and_copy_to_s3 "example_table_1" "public.example_table_1"
  29. export_and_copy_to_s3 "example_table_2" "(select column_name_x, column_name_y from public.example_table_2)"
Add Comment
Please, Sign In to add comment