#!/bin/sh # AUTHOR # DANIEL E. GILLESPIE (2016) # https://github.com/dantheman213 # DESCRIPTION # Export your app's table schemas and stored functions from a PostgreSQL # database into individual *.sql files for easy management and source control. # Batch import *.sql files with this script located here: # https://gist.github.com/dantheman213/a281a0f7364218bfd1013565aac28830 # INSTALLATION # 1. Install script at /usr/bin or /usr/sbin (if you # want to make this root/admin privs only) # 2. chmod +x /usr/bin/export_db_structure.sh # 3. Make sure your Postgres database will accept a local # connection with password authentication # 4. Execute the script.. check BACKUP_ROOT_PATH directory # for backup of your table schemas and stored functions ### CHANGE THESE TO YOUR SERVER/APP INFO ### TEMP_WORK_PATH="/tmp/postgres" BACKUP_ROOT_PATH="/vagrant/backup" # This is where your *.sql files will be exported at DATABASE_NAME="myapp_db" DATABASE_TABLES_PREFIX="myapp_" POSTGRES_USER="postgres" POSTGRES_PASSWORD="postgres" ### END CONFIGURATION ### [ -d $TEMP_WORK_PATH ] || mkdir -p $TEMP_WORK_PATH rm -rf $TEMP_WORK_PATH/* [ -d $BACKUP_ROOT_PATH ] || mkdir -p $BACKUP_ROOT_PATH rm -rf $BACKUP_ROOT_PATH/* mkdir $BACKUP_ROOT_PATH/tables mkdir $BACKUP_ROOT_PATH/routines export PGPASSWORD=$POSTGRES_PASSWORD cd $TEMP_WORK_PATH # Get all table schemas and write to individual files echo "Export table schemas..." for table in $(psql -U $POSTGRES_USER -d $DATABASE_NAME -t -c "Select table_name From information_schema.tables Where table_type='BASE TABLE' and table_name like '$DATABASE_TABLES_PREFIX%'"); do pg_dump -st $table -U $POSTGRES_USER $DATABASE_NAME > $BACKUP_ROOT_PATH/tables/$table.sql; done; # Get all functions in db and output to one file echo "Getting stored functions..." psql -U $POSTGRES_USER -At $DATABASE_NAME > $TEMP_WORK_PATH/db_functions.sql <