Skip to content

Instantly share code, notes, and snippets.

@DarinDev1000
Last active June 20, 2019 01:20
Show Gist options
  • Save DarinDev1000/2106a58b3d64f1de3c2c5a04222b4c46 to your computer and use it in GitHub Desktop.
Save DarinDev1000/2106a58b3d64f1de3c2c5a04222b4c46 to your computer and use it in GitHub Desktop.
Script that exports your database schema to files that are included in your git history

Script that exports your database schema to files that are included in your git history This is in the server repository

  • Create a folder in your root directory called "databaseSchema"
  • In that folder create an executable file "export-db-schema.sh"

File Contents

#!/bin/bash
GIT_MYSQL=./schema
DATABASE_NAME=frasset_data
for T in `mysql -u export -p123 -N -B -e 'show tables from <database-name>'`;
do
    echo "Backing up $T"
#    mysqldump --skip-comments --compact -u <username> -p<password> <database-name> $T > $GIT_MYSQL/$T.sql
    mysqldump --skip-comments --no-data --skip-add-drop-table -u <username> -p<password> frasset_data $T | sed 's/ AUTO_INCREMENT=[0-9]*//g' > $GIT_MYSQL/tables/$T.sql
done;

mysqldump --no-data --skip-add-drop-table -u <username> -p<password> <database-name> | sed 's/ AUTO_INCREMENT=[0-9]*//g' > $GIT_MYSQL/<database-name>_schema-all-tables.sql
  • Replace in the script file

  • Create a sub folder "databaseSchema/schema"

  • Create a sub folder "databaseSchema/schema/tables"

  • In your package.json add this script:
    "export-db-schema": "cd databaseSchema && ./export-db-schema.sh"

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment