Last active
May 19, 2025 14:25
-
-
Save dantheman213/aff70ee42a11f2d1fa46983878cd62e1 to your computer and use it in GitHub Desktop.
Revisions
-
Daniel Gillespie revised this gist
Nov 14, 2016 . 1 changed file with 1 addition and 1 deletion.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -8,7 +8,7 @@ # 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 -
Daniel Gillespie revised this gist
Nov 14, 2016 . No changes.There are no files selected for viewing
-
Daniel Gillespie revised this gist
Nov 14, 2016 . No changes.There are no files selected for viewing
-
Daniel Gillespie revised this gist
Nov 13, 2016 . 1 changed file with 3 additions and 0 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -8,6 +8,9 @@ # Export your app's table schemas and stored functions from a PostgreSQL # database into individual *.sql files for easy management and source control. # Import 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) -
Daniel Gillespie revised this gist
Nov 13, 2016 . 1 changed file with 2 additions and 2 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -5,8 +5,8 @@ # 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. # INSTALLATION # 1. Install script at /usr/bin or /usr/sbin (if you -
Daniel Gillespie revised this gist
Nov 13, 2016 . 1 changed file with 14 additions and 6 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -1,13 +1,21 @@ #!/bin/sh # AUTHOR # DANIEL E. GILLESPIE (2016) # https://github.com/dantheman213 # DESCRIPTION # Export all table schemas and functions from a PostgreSQL database into # individual *.sql files for easy management and source control. # 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" -
Daniel Gillespie revised this gist
Nov 13, 2016 . 1 changed file with 1 addition and 1 deletion.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -9,7 +9,7 @@ # 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" -
Daniel Gillespie revised this gist
Nov 13, 2016 . 1 changed file with 9 additions and 0 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -1,13 +1,22 @@ #!/bin/sh # Description # Export all table schemas and functions into individual *.sql files for easy management and source control. # 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 APP ### 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 -
Daniel Gillespie revised this gist
Nov 13, 2016 . 1 changed file with 1 addition and 0 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -43,6 +43,7 @@ echo "Exporting stored functions..." csplit -f function -b '%d.sql' db_functions.sql '/FUNCTION/' '{*}' # Rename the function files to match the function name in the file # then place the files into the target backup directory counter=1 while [ -f $TEMP_WORK_PATH/function$counter.sql ] do -
Daniel Gillespie revised this gist
Nov 13, 2016 . 1 changed file with 1 addition and 1 deletion.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -3,7 +3,7 @@ # Export all table schemas and functions into individual *.sql files for easy management and source control. 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" -
Daniel Gillespie revised this gist
Nov 13, 2016 . 1 changed file with 2 additions and 2 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -4,8 +4,8 @@ TEMP_WORK_PATH="/tmp/postgres" BACKUP_ROOT_PATH="/vagrant/backup" DATABASE_NAME="myapp_db" DATABASE_TABLES_PREFIX="myapp_" POSTGRES_USER="postgres" POSTGRES_PASSWORD="postgres" -
Daniel Gillespie created this gist
Nov 13, 2016 .There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,55 @@ #!/bin/sh # Export all table schemas and functions into individual *.sql files for easy management and source control. TEMP_WORK_PATH="/tmp/postgres" BACKUP_ROOT_PATH="/vagrant/backup" DATABASE_NAME="pen_db" DATABASE_TABLES_PREFIX="pen_" POSTGRES_USER="postgres" POSTGRES_PASSWORD="postgres" [ -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 <<EOF SELECT pg_get_functiondef(f.oid) FROM pg_catalog.pg_proc f INNER JOIN pg_catalog.pg_namespace n ON (f.pronamespace = n.oid) WHERE n.nspname = 'public'; EOF # Split function file into separate files per function echo "Exporting stored functions..." csplit -f function -b '%d.sql' db_functions.sql '/FUNCTION/' '{*}' # Rename the function files to match the function name in the file counter=1 while [ -f $TEMP_WORK_PATH/function$counter.sql ] do name=$(head -1 function$counter.sql | awk {'print $5'}) name=$(echo $name | cut -d "." --f 2 | cut -d "(" --f 1) mv function$counter.sql $BACKUP_ROOT_PATH/routines/$name.sql counter=$((counter+1)) done echo "done"