Skip to content

Instantly share code, notes, and snippets.

@thepsion5
Created January 30, 2015 18:49
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save thepsion5/19ae573f6939ff40992f to your computer and use it in GitHub Desktop.
Save thepsion5/19ae573f6939ff40992f to your computer and use it in GitHub Desktop.
Shell Script to run migrations and database seeders on a Laravel 4 application and dump the resulting DB contents to a SQL file
#!/bin/bash
#Retrieves an environment variable from the correct environment file
#Based on the specified environment
#Example: getEnvVariable local DB_USER VARIABLE
#Will attempt to retrieve the DB_USER array value from the file at .env.local.php
#And then store the result in VARIABLE
getEnvVariable(){
local RESULT=$3
local LOCAL_RESULT=$(php -r '
$file = getcwd() . "/.env." . trim($argv[1]) . ".php";
$env = include($file);
print $env[trim($argv[2])]; ' $1 $2)
eval $RESULT="'$LOCAL_RESULT'"
}
C_OFF="\033[0m"
C_CYAN="\033[1;36m"
FILENAME_BASE=testing
DB_USER=''
DB_PASS=''
DB_DATABASE=''
DB_HOST='localhost'
if [ -e bootstrap/environment.php ]
then
ENV=$(php -r 'print include(getcwd() . "/bootstrap/environment.php");')
else
ENV=local
fi
echo -e "ENVIRONMENT: $ENV$C_OFF"
if [ $ENV = "production" ];then
echo -e "THIS TASK CANNOT BE PERFORMED ON PRODUCTION"
exit
elif [ $ENV = "dev" ];then
ENV_APPEND="_dev"
elif [ $ENV = "oir-admin" ];then
ENV_APPEND="_admin"
elif [ $ENV = "dev-amazon" ];then
ENV_APPEND="_amazon"
DB_HOST="super secret amazon server located deep inside the earth\'s crust"
fi
DUMP_FILE="tests/_data/$FILENAME_BASE$ENV_APPEND.sql"
echo -e "${C_CYAN}GETTING CREDENTIALS FROM ENVIRONMENT FILE${C_OFF}"
getEnvVariable $ENV 'DB_USER' DB_USER
getEnvVariable $ENV 'DB_PASS' DB_PASS
getEnvVariable $ENV 'DB_DATABASE' DB_DATABASE
echo -e "${C_CYAN}MIGRATING AND SEEDING DATABASE${C_OFF}"
php artisan migrate --seed
echo -e "${C_CYAN}EXPORTING DATABASE TO '$DUMP_FILE'${C_OFF}"
mysqldump -h$DB_HOST -u$DB_USER -p$DB_PASS $DB_DATABASE --lock-tables=false --routines > ${DUMP_FILE}
echo -e "${C_CYAN}DUMP COMPLETE${C_OFF}"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment