<div class="gmail_quote">On Tue, Mar 29, 2011 at 9:18 AM, albus <span dir="ltr"><<a href="mailto:albus@iowaconnect.com">albus@iowaconnect.com</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex;">




















<div lang="EN-US" link="blue" vlink="blue">

<div>

<p class="MsoNormal"><font size="2" color="navy" face="Arial"><span style="font-size:10.0pt;font-family:Arial;color:navy">I’d consider such a class. Problem
is I don’t know JACK about scripting and would most likely be the dumbest
in the room<br>
on it. I’ve played around very little with bash scripting mostly for a
cron job but that’s about it. If nothing else I’d like to<br>
learn what it takes to create a script for a MySql Database project I want to
start sooner or later. Mostly create the database<br>
with the tables needed and make a web GUI for use to add data for a web site.</span></font></p>

<p class="MsoNormal"><font size="2" color="navy" face="Arial"><span style="font-size:10.0pt;font-family:Arial;color:navy"> </span></font></p>

<p class="MsoNormal"><font size="2" color="navy" face="Arial"><span style="font-size:10.0pt;font-family:Arial;color:navy">I’m sure it can be done, just haven’t
a clue where to start as yet.</span></font></p>

<p class="MsoNormal"><font size="2" color="navy" face="Arial"><span style="font-size:10.0pt;font-family:Arial;color:navy"> </span></font></p>

<p class="MsoNormal"><font size="2" color="navy" face="Arial"><span style="font-size:10.0pt;font-family:Arial;color:navy"></span></font></p></div></div></blockquote><div><br></div><div>Here is a script I wrote that does some work with MySQL. Note that this is Bash, not Posix /bin/sh. (Ubuntu uses Posix sh for /bin/sh to improve boot speed so if you're on Ubuntu this script needs a hash-bang of #!/bin/bash not #!/bin/sh) This script is part of the tool that sets up our Drupal development environment for VMs and personal workstations. This is not used for production machines (so nothing you find here will help you poking at our websites. ;-)</div>

<div><br></div><div>reset-password.sql is just a text file with an SQL query that changes the passwords for users.</div><div><br></div><div><div>#!/bin/bash</div><div><br></div><div>if [ -z $SRV ] ; then</div><div>  echo "CRITICAL FAILURE: \$SRV environment variable must be set"</div>

<div>  exit 1</div><div>fi</div><div><br></div><div>CANONICAL_SQL=$SRV/snapshots/www.canonical.com.sql</div><div>UBUNTU_SQL=$SRV/snapshots/www.ubuntu.com.sql</div><div><br></div><div>if ! [ -e $CANONICAL_SQL ];</div><div>

then</div><div>    echo "The SQL file $CANONICAL_SQL does not exist!"</div><div>    echo "Please download a snapshot of the <a href="http://canonical.com">canonical.com</a> SQL database "</div><div>    echo "and run this script again."</div>

<div>    exit 1</div><div>fi</div><div><br></div><div>if ! [ -e $UBUNTU_SQL ];</div><div>then</div><div>    echo "The SQL file $UBUNTU_SQL does not exist!"</div><div>    echo "Please download a snapshot of the <a href="http://ubuntu.com">ubuntu.com</a> SQL database "</div>

<div>    echo "and run this script again."</div><div>    exit 1</div><div>fi</div><div><br></div><div><br></div><div>DB_LIST=''</div><div># do the databases eist already?</div><div>while [ "$DB_LIST" = '' ]; do</div>

<div># get the mysql root password</div><div>read -sp "Enter the MySQL root password: " MYSQL_PW</div><div>echo</div><div>DB_LIST=`mysql -u root -p$MYSQL_PW <<EOF</div><div>show databases;</div><div>EOF`</div>

<div>done</div><div><br></div><div>echo $DB_LIST | grep -q drupal-dev-canonical > /dev/null 2>&1</div><div>if [ $? -eq 0 ]; then</div><div>  # drupal-dev-canonical db exists, maybe we should ask before we blow it away</div>

<div>  read -N 1 -p "The drupal-dev-canonical database exists, would you like to keep it? Y to keep it, N to drop and recreate it: [Y/n] " KEEP_</div><div>CANONICAL_DB</div><div>  if [ $KEEP_CANONICAL_DB = "n" ]; then</div>

<div>    echo</div><div>    echo "Dropping and re-creating database drupal-dev-canonical..."</div><div>    mysql -u root -p$MYSQL_PW <<EOF</div><div>drop database \`drupal-dev-canonical\`;</div><div>EOF</div>

<div>    CREATE_CANONICAL_DB=1</div><div>  else</div><div>    echo "Leaving the database drupal-dev-canonical alone..."</div><div>    CREATE_CANONICAL_DB=0</div><div>  fi</div><div>else</div><div>  CREATE_CANONICAL_DB=1</div>

<div>fi</div><div><br></div><div>if [ $CREATE_CANONICAL_DB -eq 1 ] ; then</div><div>  echo "Creating the database drupal-dev-canonical and importing data..."</div><div>  mysqladmin -u root -p$MYSQL_PW create drupal-dev-canonical</div>

<div>  mysql -u root -p$MYSQL_PW drupal-dev-canonical < $SRV/snapshots/www.canonical.com.sql</div><div>  echo Resetting passwords ...</div><div>  mysql -u root -p$MYSQL_PW drupal-dev-canonical < reset-password.sql</div>

<div>  mysql -u root -p$MYSQL_PW drupal-dev-canonical <<EOF</div><div>GRANT ALL on \`drupal-dev-canonical\`.* to drupal@localhost identified by 'password';</div><div>EOF</div><div>  echo "done."</div>

<div>fi</div><div><br></div><div>echo $DB_LIST | grep -q drupal-dev-ubuntu > /dev/null 2>&1</div><div>if [ $? -eq 0 ]; then</div><div>  # drupal-dev-canonical db exists, maybe we should ask before we blow it away</div>

<div>  read -n 1 -p "The drupal-dev-ubuntu database exists, would you like to keep it? Y to keep it, N to drop and recreate it: [Y/n] " KEEP_UBU</div><div>NTU_DB</div><div>  if [ $KEEP_UBUNTU_DB = "n" ] ; then</div>

<div>    echo</div><div>    echo "Dropping and re-creating database drupal-dev-ubuntu..."</div><div>    mysql -u root -p$MYSQL_PW <<EOF</div><div>drop database \`drupal-dev-ubuntu\`;</div><div>EOF</div><div>

    CREATE_UBUNTU_DB=1</div><div>  else</div><div>    echo "Leaving the database drupal-dev-ubuntu alone..."</div><div>    CREATE_UBUNTU_DB=0</div><div>  fi</div><div>else</div><div>  CREATE_UBUNTU_DB=1</div><div>

fi</div><div><br></div><div>if [ $CREATE_UBUNTU_DB -eq 1 ] ; then</div><div>  echo "Creating the database drupal-dev-ubuntu and importing data..."</div><div>  mysqladmin -u root -p$MYSQL_PW create drupal-dev-ubuntu</div>

<div>  mysql -u root -p$MYSQL_PW drupal-dev-ubuntu < $SRV/snapshots/www.ubuntu.com.sql</div><div>  echo Resetting passwords ...</div><div>  mysql -u root -p$MYSQL_PW drupal-dev-ubuntu < reset-password.sql</div><div>

  mysql -u root -p$MYSQL_PW drupal-dev-canonical <<EOF</div><div>GRANT ALL on \`drupal-dev-ubuntu\`.* to drupal@localhost identified by 'password';</div><div>EOF</div><div>  echo "done."</div><div>

fi</div><div><br></div><div>exit 0</div></div><div><br></div><div><br></div></div>-- <br>Matthew Nuzum<br>newz2000 on freenode, skype, linkedin, <a href="http://identi.ca" target="_blank">identi.ca</a> and twitter<br><br>

"An investment in knowledge pays the best interest." -Benjamin Franklin <br><br>