|
| 1 | +#!/usr/bin/env bash |
| 2 | + |
| 3 | +if [[ $APP =~ "pa-back-prod-test" ]] ; then |
| 4 | + echo "Dump de la DB de prod vers la db de perfs" |
| 5 | + |
| 6 | + if [ -z "$PROD_DATABASE_URL" ]; then |
| 7 | + echo "error: env var PROD_DATABASE_URL must be set" |
| 8 | + exit 1 |
| 9 | + fi |
| 10 | + |
| 11 | + if [ -z "$DATABASE_URL" ]; then |
| 12 | + echo "error: env var DATABASE_URL must be set" |
| 13 | + exit 1 |
| 14 | + fi |
| 15 | + |
| 16 | + if [[ $DATABASE_URL == *"pa_back_pro_817"* ]]; then |
| 17 | + echo "Error: Target must not be production" |
| 18 | + exit 1 |
| 19 | + fi |
| 20 | + |
| 21 | + export PATH=$HOME/bin:$PATH |
| 22 | + dbclient-fetcher psql 13 |
| 23 | + |
| 24 | + echo "Drop conflicting extensions (if they exist)" |
| 25 | + psql --dbname $DATABASE_URL -c "DROP EXTENSION IF EXISTS postgis CASCADE;" |
| 26 | + psql --dbname $DATABASE_URL -c "DROP EXTENSION IF EXISTS postgis_tiger_geocoder CASCADE;" |
| 27 | + psql --dbname $DATABASE_URL -c "DROP EXTENSION IF EXISTS postgis_topology CASCADE;" |
| 28 | + psql --dbname $DATABASE_URL -c "DROP EXTENSION IF EXISTS pg_trgm CASCADE;" |
| 29 | + |
| 30 | + echo "Dump production database (excluding unnecessary schemas)" |
| 31 | + pg_dump --clean --if-exists --format c --no-owner --no-privileges --no-comments \ |
| 32 | + -n 'public' -n 'sequelize' \ |
| 33 | + --exclude-schema 'information_schema' --exclude-schema '^pg_*' \ |
| 34 | + --exclude-schema 'tiger' --exclude-schema 'tiger_data' --exclude-schema 'topology' \ |
| 35 | + --dbname $PROD_DATABASE_URL --file dump.pgsql |
| 36 | + |
| 37 | + echo "Restore database dump (WITHOUT extensions yet)" |
| 38 | + pg_restore --clean --if-exists --no-owner --no-privileges --no-comments --dbname $DATABASE_URL dump.pgsql |
| 39 | + |
| 40 | + echo "Recreate extensions AFTER restore" |
| 41 | + psql --dbname $DATABASE_URL -c "CREATE EXTENSION postgis;" |
| 42 | + psql --dbname $DATABASE_URL -c "CREATE EXTENSION postgis_tiger_geocoder CASCADE;" |
| 43 | + psql --dbname $DATABASE_URL -c "CREATE EXTENSION postgis_topology;" |
| 44 | + psql --dbname $DATABASE_URL -c "CREATE EXTENSION pg_trgm;" |
| 45 | + |
| 46 | + echo "Grant correct privileges" |
| 47 | + psql --dbname $DATABASE_URL -c "GRANT USAGE, CREATE ON SCHEMA public TO pa_back_pro_4110;" |
| 48 | + psql --dbname $DATABASE_URL -c "GRANT ALL PRIVILEGES ON ALL TABLES IN SCHEMA public TO pa_back_pro_4110;" |
| 49 | + |
| 50 | +fi |
| 51 | + |
| 52 | +rm -f dump.pgsql |
0 commit comments