[Cialug] Linux Classes
Matthew Nuzum
newz at bearfruit.org
Tue Mar 29 09:44:23 CDT 2011
On Tue, Mar 29, 2011 at 9:18 AM, albus <albus at iowaconnect.com> wrote:
> 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
> 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
> learn what it takes to create a script for a MySql Database project I want
> to start sooner or later. Mostly create the database
> with the tables needed and make a web GUI for use to add data for a web
> site.
>
>
>
> I’m sure it can be done, just haven’t a clue where to start as yet.
>
>
>
>
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. ;-)
reset-password.sql is just a text file with an SQL query that changes the
passwords for users.
#!/bin/bash
if [ -z $SRV ] ; then
echo "CRITICAL FAILURE: \$SRV environment variable must be set"
exit 1
fi
CANONICAL_SQL=$SRV/snapshots/www.canonical.com.sql
UBUNTU_SQL=$SRV/snapshots/www.ubuntu.com.sql
if ! [ -e $CANONICAL_SQL ];
then
echo "The SQL file $CANONICAL_SQL does not exist!"
echo "Please download a snapshot of the canonical.com SQL database "
echo "and run this script again."
exit 1
fi
if ! [ -e $UBUNTU_SQL ];
then
echo "The SQL file $UBUNTU_SQL does not exist!"
echo "Please download a snapshot of the ubuntu.com SQL database "
echo "and run this script again."
exit 1
fi
DB_LIST=''
# do the databases eist already?
while [ "$DB_LIST" = '' ]; do
# get the mysql root password
read -sp "Enter the MySQL root password: " MYSQL_PW
echo
DB_LIST=`mysql -u root -p$MYSQL_PW <<EOF
show databases;
EOF`
done
echo $DB_LIST | grep -q drupal-dev-canonical > /dev/null 2>&1
if [ $? -eq 0 ]; then
# drupal-dev-canonical db exists, maybe we should ask before we blow it
away
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_
CANONICAL_DB
if [ $KEEP_CANONICAL_DB = "n" ]; then
echo
echo "Dropping and re-creating database drupal-dev-canonical..."
mysql -u root -p$MYSQL_PW <<EOF
drop database \`drupal-dev-canonical\`;
EOF
CREATE_CANONICAL_DB=1
else
echo "Leaving the database drupal-dev-canonical alone..."
CREATE_CANONICAL_DB=0
fi
else
CREATE_CANONICAL_DB=1
fi
if [ $CREATE_CANONICAL_DB -eq 1 ] ; then
echo "Creating the database drupal-dev-canonical and importing data..."
mysqladmin -u root -p$MYSQL_PW create drupal-dev-canonical
mysql -u root -p$MYSQL_PW drupal-dev-canonical <
$SRV/snapshots/www.canonical.com.sql
echo Resetting passwords ...
mysql -u root -p$MYSQL_PW drupal-dev-canonical < reset-password.sql
mysql -u root -p$MYSQL_PW drupal-dev-canonical <<EOF
GRANT ALL on \`drupal-dev-canonical\`.* to drupal at localhost identified by
'password';
EOF
echo "done."
fi
echo $DB_LIST | grep -q drupal-dev-ubuntu > /dev/null 2>&1
if [ $? -eq 0 ]; then
# drupal-dev-canonical db exists, maybe we should ask before we blow it
away
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
NTU_DB
if [ $KEEP_UBUNTU_DB = "n" ] ; then
echo
echo "Dropping and re-creating database drupal-dev-ubuntu..."
mysql -u root -p$MYSQL_PW <<EOF
drop database \`drupal-dev-ubuntu\`;
EOF
CREATE_UBUNTU_DB=1
else
echo "Leaving the database drupal-dev-ubuntu alone..."
CREATE_UBUNTU_DB=0
fi
else
CREATE_UBUNTU_DB=1
fi
if [ $CREATE_UBUNTU_DB -eq 1 ] ; then
echo "Creating the database drupal-dev-ubuntu and importing data..."
mysqladmin -u root -p$MYSQL_PW create drupal-dev-ubuntu
mysql -u root -p$MYSQL_PW drupal-dev-ubuntu <
$SRV/snapshots/www.ubuntu.com.sql
echo Resetting passwords ...
mysql -u root -p$MYSQL_PW drupal-dev-ubuntu < reset-password.sql
mysql -u root -p$MYSQL_PW drupal-dev-canonical <<EOF
GRANT ALL on \`drupal-dev-ubuntu\`.* to drupal at localhost identified by
'password';
EOF
echo "done."
fi
exit 0
--
Matthew Nuzum
newz2000 on freenode, skype, linkedin, identi.ca and twitter
"An investment in knowledge pays the best interest." -Benjamin Franklin
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://cialug.org/pipermail/cialug/attachments/20110329/8e57a52b/attachment.html>
More information about the Cialug
mailing list