<?xml version="1.0" encoding="utf-8" ?>

<rss version="2.0" 
   xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
   xmlns:admin="http://webns.net/mvcb/"
   xmlns:dc="http://purl.org/dc/elements/1.1/"
   xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
   xmlns:wfw="http://wellformedweb.org/CommentAPI/"
   xmlns:content="http://purl.org/rss/1.0/modules/content/"
   >
<channel>
    <title>Valcora Blog - Scripts</title>
    <link>http://blog.valcora.com/</link>
    <description>Professional MySQL Consulting</description>
    <dc:language>en</dc:language>
    <generator>Serendipity 1.4.1 - http://www.s9y.org/</generator>
    <pubDate>Tue, 05 May 2009 14:01:36 GMT</pubDate>

    <image>
        <url>http://blog.valcora.com/templates/default/img/s9y_banner_small.png</url>
        <title>RSS: Valcora Blog - Scripts - Professional MySQL Consulting</title>
        <link>http://blog.valcora.com/</link>
        <width>100</width>
        <height>21</height>
    </image>

<item>
    <title>Monitoring Disk Space</title>
    <link>http://blog.valcora.com/index.php?/archives/7-Monitoring-Disk-Space.html</link>
            <category>Scripts</category>
    
    <comments>http://blog.valcora.com/index.php?/archives/7-Monitoring-Disk-Space.html#comments</comments>
    <wfw:comment>http://blog.valcora.com/wfwcomment.php?cid=7</wfw:comment>

    <slash:comments>0</slash:comments>
    <wfw:commentRss>http://blog.valcora.com/rss.php?version=2.0&amp;type=comments&amp;cid=7</wfw:commentRss>
    

    <author>nospam@example.com (Valcora)</author>
    <content:encoded>
    Some time back, when a client wanted us to setup MySQL Enterprise Monitor, we were surprised to find out that disk monitoring was not available!  We worked hard to come up with a solution.  Eventually, we decided to setup a custom agent to monitor the disk.  Below is the result of that.&lt;br /&gt;
&lt;br /&gt;
While this script may not work as-is for everyone, it should at least provide a basis for such a script.  This script has been modified to send an email instead of plug directly into the MySQL Enterprise Monitor.  But, it hopefully will get our creative juices flowing...&lt;br /&gt;
&lt;br /&gt;
&lt;pre&gt;#!/bin/bash
#
# This script does a very simple test for checking disk space.
# 
# Valcora: http://www.valcora.com
#

CHECKDISK=`df -h | awk &#039;{print $5}&#039; | grep % | grep -v Use | sort -n | tail -1 | cut -d &quot;%&quot; -f1 -`
ALERT_VALUE=&quot;80&quot;
MAIL_USER=&quot;root@localhost.com&quot;
MAIL_SUBJECT=&quot;Daily Disk Check&quot;

if [ &quot;$CHECKDISK&quot; -ge &quot;$ALERT_VALUE&quot; ]; then
  echo &quot;At least one of my disks is nearly full!&quot; | mail -s &quot;$MAIL_SUBJECT&quot; $MAIL_USER
else
  echo &quot;Disk space normal&quot; | mail -s &quot;$MAIL_SUBJECT&quot; $MAIL_USER
fi
&lt;/pre&gt;&lt;br /&gt;
This would be a simple process to cron.  Let us know if this has been of any help to you! 
    </content:encoded>

    <pubDate>Mon, 04 May 2009 12:15:56 -0600</pubDate>
    <guid isPermaLink="false">http://blog.valcora.com/index.php?/archives/7-guid.html</guid>
    
</item>
<item>
    <title>Export All To CSV</title>
    <link>http://blog.valcora.com/index.php?/archives/6-Export-All-To-CSV.html</link>
            <category>Scripts</category>
    
    <comments>http://blog.valcora.com/index.php?/archives/6-Export-All-To-CSV.html#comments</comments>
    <wfw:comment>http://blog.valcora.com/wfwcomment.php?cid=6</wfw:comment>

    <slash:comments>0</slash:comments>
    <wfw:commentRss>http://blog.valcora.com/rss.php?version=2.0&amp;type=comments&amp;cid=6</wfw:commentRss>
    

    <author>nospam@example.com (Valcora)</author>
    <content:encoded>
    Last night I was asked by a client to export all of the tables into CSV format.  I thought no problem.  Then I saw the long list of table names!  They wanted every one of them in CSV format!  At that point, I started thinking about making a backup and then importing them all as CSV storage engine and then I realized CSV was disabled on the sandbox server!  So, I thought well, I could try MySQL Query Browser, but then I figured I would still have to do it on a table-by-table basis.  That is when I decided to go the Bash script route!&lt;br /&gt;
&lt;br /&gt;
This lead to the formation of the brief script you see below.  It simply connects to a given database, shows the list of tables, and then dumps out the contents in CSV format to individual files named &lt;em&gt;tablename&lt;/em&gt;.csv in the current directory.  After just a few minutes of scripting, it was all over and I had a handy little script for the next time they or another client ask for the same thing!&lt;br /&gt;
&lt;br /&gt;
Hopefully this script will be of some value to everyone else as well.  Just be sure to change the configuration section to meet your needs.&lt;br /&gt;
&lt;br /&gt;
Name the file something like: export_csv.sh.  Then be sure to make it executable.  In Linux, do something like &quot;chmod a+x ./export_csv.sh&quot; and you should be able to rock and roll.  &lt;br /&gt;
&lt;br /&gt;
If you want to have all of the scripts in a certain directory, you could either modify the script or just make the cirectory, &quot;cd&quot; into it, and then run the script.  It assumes you want to create the files in the current working directory.  To change that behavior, you could easily modify the &quot;OUTFILE&quot; variable to something like:&lt;br /&gt;
&lt;pre&gt;OUTFILE=&quot;/my_path/$TABLE.csv&quot;&lt;/pre&gt;&lt;br /&gt;
Anyway, here is the script for your enjoyment and use!&lt;br /&gt;
&lt;br /&gt;
&lt;pre&gt;#!/bin/bash
#
# Simple script to crawl through all tables in a database and export to CSV format.
#
# Valcora: http://www.valcora.com
#

#### Begin Configuration ####
DB=&quot;mydb&quot;
MYSQL_USER=&quot;root&quot;
MYSQL_PASSWD=&quot;mypass&quot;
MYSQL_HOST=&quot;127.0.0.1&quot;
MYSQL_PORT=&quot;3306&quot;
MYSQL=&quot;/usr/bin/mysql&quot;
#### End Configuration ####

MYSQL_CMD=&quot;$MYSQL -u $MYSQL_USER -p$MYSQL_PASSWD -P $MYSQL_PORT -h $MYSQL_HOST&quot;

TABLES=`$MYSQL --batch -N -D $DB -e &quot;show tables&quot;`
for TABLE in $TABLES
do
  SQL=&quot;SELECT * FROM $TABLE;&quot;
  OUTFILE=$TABLE.csv
  $MYSQL --database=$DB --execute=&quot;$SQL&quot; | sed &#039;s/\t/&quot;,&quot;/g;s/^/&quot;/;s/$/&quot;/;s/\n//g&#039; &gt; $OUTFILE
done
&lt;/pre&gt;&lt;br /&gt;
As always, be sure to test this script on a non-critical server first to ensure it is working to your satisfaction.  There is no error trapping, so be sure you don&#039;t accidentally over-write important files! 
    </content:encoded>

    <pubDate>Mon, 04 May 2009 12:00:27 -0600</pubDate>
    <guid isPermaLink="false">http://blog.valcora.com/index.php?/archives/6-guid.html</guid>
    
</item>
<item>
    <title>Converting Storage Engine</title>
    <link>http://blog.valcora.com/index.php?/archives/5-Converting-Storage-Engine.html</link>
            <category>Scripts</category>
    
    <comments>http://blog.valcora.com/index.php?/archives/5-Converting-Storage-Engine.html#comments</comments>
    <wfw:comment>http://blog.valcora.com/wfwcomment.php?cid=5</wfw:comment>

    <slash:comments>0</slash:comments>
    <wfw:commentRss>http://blog.valcora.com/rss.php?version=2.0&amp;type=comments&amp;cid=5</wfw:commentRss>
    

    <author>nospam@example.com (Valcora)</author>
    <content:encoded>
    Have you ever been asked to convert all of the tables in a given database or on a server to a new storage engine?  Well, we have!  Sometimes, if it is only a few tables, firing off a few &quot;ALTER TABLE ... ENGINE=InnoDB;&quot; is fine.  Other times, it is a nightmare if there are lots of tables and/or databases.&lt;br /&gt;
&lt;br /&gt;
To make our life easier in such times, we created the following Bash script.  Save the file as something like &quot;convert_tables.sh&quot; and make sure to make it executable.  You can do this in Linux by executing &quot;chmod a+x ./convert_tables.sh.&quot;  Take a look at the code and we will give you an example of how to run it!&lt;br /&gt;
&lt;br /&gt;
&lt;pre&gt;#!/bin/bash
# Crawl through all of the tables in a database or databases and convert all tables to a given storage engine.
# 
# Valcora: http://www.valcora.com
#

#### Begin Configuration ####
DBNAMES=&quot;mydb1 mydb2&quot;		# Separate list of Databases by spaces
NEW_ENGINE_TYPE=&quot;InnoDB&quot;	# Storage engine to convert all tables to
DBEXCLUDE=&quot;information_schema mysql horde bacula vpopmail&quot; # Databases to ignore
MYSQL_HOST=&quot;127.0.0.1&quot;
MYSQL_PORT=&quot;3306&quot;
MYSQL_USER=&quot;root&quot;
MYSQL_PASSWD=&quot;mypass&quot;
MYSQL=&quot;/usr/bin/mysql&quot;
#### End Configuration ####

# Setup authentication info for mysql client
AUTH=&quot;-u $MYSQL_USER -p$MYSQL_PASSWD -P $MYSQL_PORT -h $MYSQL_HOST&quot;

# Get our list of databases to change...
# NOTE: the DBEXCLUDE feature seemed to only work with Linux regex, GNU sed
if [ &quot;$DBNAMES&quot; = &quot;all&quot; ]; then
  DBNAMES=&quot;`$MYSQL $AUTH --batch -N -e &quot;SHOW DATABASES&quot;`&quot;
  for i in $DBEXCLUDE
  do
    DBNAMES=`echo $DBNAMES | sed &quot;s/\b$i\b//g&quot;`
  done
fi

# Run through each database and execute our ALTER TABLE command for each table...
for i in $DBNAMES
do
  # to fancy up our log file
  echo &quot;&quot;
  echo &quot;Database: $i&quot;
  echo &quot;---------------------------------------------------------&quot;

  DBTABLES=&quot;`$MYSQL $AUTH --batch -N -e &quot;SHOW TABLES&quot; -D $i`&quot;

  for j in $DBTABLES
  do
    echo &quot;ALTER TABLE $j TYPE=$NEW_ENGINE_TYPE;&quot; | $MYSQL $AUTH -D $i
  done
  echo &quot;&quot;
done
&lt;/pre&gt;&lt;br /&gt;
If you want to convert all tables in all databases, you could set the &quot;DBNAMES&quot; variable in the configuration section to &quot;all&quot;.  To convert certain databases, simply set it to a list of database names separated by spaces.  And finally to only convert tables in a given database, you could simply list one name there.&lt;br /&gt;
&lt;br /&gt;
We have foudn this useful over the years when implementing some piece of software that we downloaded.  We love Open Source too mind you!  But, we don&#039;t always like their choice of Storage Engines since a lot of developers use MyISAM.  When appropriate, we like to run InnoDB.  Using this script, it is easy to run through all of the tables and make the conversion in a matter of no time.&lt;br /&gt;
&lt;br /&gt;
Anyway, good luck and as always be sure to test this on a machine that is not critical before implementing.  And of yeah, be sure to make a backup first.  Just in case... 
    </content:encoded>

    <pubDate>Mon, 04 May 2009 11:46:21 -0600</pubDate>
    <guid isPermaLink="false">http://blog.valcora.com/index.php?/archives/5-guid.html</guid>
    
</item>
<item>
    <title>Finding All MyISAM Tables</title>
    <link>http://blog.valcora.com/index.php?/archives/4-Finding-All-MyISAM-Tables.html</link>
            <category>Scripts</category>
    
    <comments>http://blog.valcora.com/index.php?/archives/4-Finding-All-MyISAM-Tables.html#comments</comments>
    <wfw:comment>http://blog.valcora.com/wfwcomment.php?cid=4</wfw:comment>

    <slash:comments>1</slash:comments>
    <wfw:commentRss>http://blog.valcora.com/rss.php?version=2.0&amp;type=comments&amp;cid=4</wfw:commentRss>
    

    <author>nospam@example.com (Valcora)</author>
    <content:encoded>
    Below is a small Bash script to crawl through all the tables in a MySQL database and look for any and all MyISAM tables.  We developed this script when we realized that a team of developers were ignoring our requests to create all tables as InnoDB.  We set the default storage engine to be InnoDB, however, the team would declare the Storage Enginge to be MyISAM which caused MySQL to ignore our default declaration.&lt;br /&gt;
&lt;br /&gt;
Yes, we could run a &quot;SHOW TABLE STATUS,&quot; but that would require staring at a lot of table entries in some databases.  Naturally, we could also query the Information Schema database as well.  All of those methods are fine.  For our purposes, we wanted a fairly simple script that would do the scanning for us lazy types and yet work on pretty well all versions of MySQL.&lt;br /&gt;
&lt;br /&gt;
To solve the problem, we developed this simple script to find any and all tables that were MyISAM format.  With that in mind, all of the values are hard-coded into the script to make cronning it simple!  You could simply define your cron entry to send the output to an email address.&lt;br /&gt;
&lt;br /&gt;
Save the below code as something like &quot;find_myisam_tables.sh&quot; and make sure it is executable by executing a &quot;chmod a+x find_myisam_tables.sh.&quot;  Just be sure to chage the configuration values to match your needs and the locations of the binaries. &lt;br /&gt;
&lt;br /&gt;
&lt;pre&gt;#!/bin/bash
#
# This is a small bash script that checks all tables in a given database and looks for MyISAM tables.
#
# Written by Valcora
# http://www.valcora.com

DBNAMES=$1

MYSQL=&quot;/usr/bin/mysql&quot;
MYSQL_HOST=&quot;localhost&quot;
MYSQL_USER=&quot;root&quot;
MYSQL_PASSWD=&quot;password&quot;
MYSQL_SOCKET=&quot;/tmp/mysql.sock&quot;
MYSQLCONNECT=&quot;$MYSQL -u$MYSQL_USER -p$MYSQL_PASSWD -h $MYSQL_HOST -S $MYSQL_SOCKET&quot;

echo &quot;==========================================&quot;
echo &quot;=             MyISAM Tables              =&quot;
echo &quot;==========================================&quot;

# Use MySQL &#039;SHOW DATABASES&#039;
DATABASES=&quot;`$MYSQLCONNECT --batch -N -e &quot;show databases&quot;`&quot;

# Loop through each instance of MySQL and check all databases in that instance
for DATABASE in $DATABASES
do
  TABLES=&quot;`$MYSQLCONNECT --batch -D $DATABASE -N -e &quot;show tables&quot;`&quot;
  for TABLE in $TABLES 
  do
    STORAGE_ENGINE=`$MYSQLCONNECT -D $DATABASE --batch -N -e &quot;show table status like &#039;$TABLE&#039;&quot; | awk &#039;{print $2}&#039;`
    if [ &quot;$STORAGE_ENGINE&quot; = &quot;MyISAM&quot; ]
    then
      echo &quot;$DATABASE.$TABLE&quot;
    fi
  done
done&lt;/pre&gt;&lt;br /&gt;
To call the script just execute something like:&lt;br /&gt;
&lt;pre&gt;./find_myisam_tables.sh &lt;database name&gt;&lt;/pre&gt;&lt;br /&gt;
Well, feel free to modify the script in any manner you want.  You may want to search for other storage engine types also.  &lt;br /&gt;
&lt;br /&gt;
Let us know if this script has been of any help.  We would love to hear from you! 
    </content:encoded>

    <pubDate>Mon, 04 May 2009 11:31:05 -0600</pubDate>
    <guid isPermaLink="false">http://blog.valcora.com/index.php?/archives/4-guid.html</guid>
    
</item>
<item>
    <title>DBA Scripts</title>
    <link>http://blog.valcora.com/index.php?/archives/3-DBA-Scripts.html</link>
            <category>Scripts</category>
    
    <comments>http://blog.valcora.com/index.php?/archives/3-DBA-Scripts.html#comments</comments>
    <wfw:comment>http://blog.valcora.com/wfwcomment.php?cid=3</wfw:comment>

    <slash:comments>0</slash:comments>
    <wfw:commentRss>http://blog.valcora.com/rss.php?version=2.0&amp;type=comments&amp;cid=3</wfw:commentRss>
    

    <author>nospam@example.com (Valcora)</author>
    <content:encoded>
    Over the years the DBA&#039;s have created simple Bash and PHP scripts to make their life easier.  We feel that sharing these with the MySQL community may help our fellow database administrators and make their life a little easier from time to time!&lt;br /&gt;
&lt;br /&gt;
With that in mind, we will periodically post a new script that we hope will be of some value.  Some of the scripts are meant to perform regular maintenance activity, routine tasks, and/or query the database schema for certain table types, and such.  &lt;br /&gt;
&lt;br /&gt;
Some of the scripts may work as-is, while others may need to be modified to suit your needs.  Either way, it should provide a starting point for accomplishing your work.&lt;br /&gt;
&lt;br /&gt;
Naturally, we can make no guarantees as to the effectiveness of these scripts.  It should go without saying that you need to always test them on a non-critical server before you attempt to run them in a critical production environment.  While, we believe our scripts are safe for most applications, you must be the final judge whether they fit your purpose or not as well as whether they would be safe to run on your systems!&lt;br /&gt;
&lt;br /&gt;
Now that we have our disclaimer out of the way, we hope that you will find these to be as valuable as we have.  Enjoy! 
    </content:encoded>

    <pubDate>Mon, 04 May 2009 11:23:49 -0600</pubDate>
    <guid isPermaLink="false">http://blog.valcora.com/index.php?/archives/3-guid.html</guid>
    
</item>

</channel>
</rss>