Code Share

Code share is my attempt at sharing some of the code I have developed with others. Some of the code here is probably done elsewhere on the web, but I could not find them, or I flat want to play with my own version. I am releasing this is the public domain. All I ask is that if you use it and like it, stroke my ego by sending me an email

Linux Utilities

The following are bash scripts

archive_gdg
[an error occurred while processing this directive]

archive_gdg is a unix script file to allow a finite number copies of a file to be aged, and the oldest one deleted when a new one is added.

View more

A generation data group(gdg) is a series of files that are related to the same thing over time. These can be daily files that may have been updated or they may be backups, where multiple copies of the backup are stored. (it's always a good idea to have multiple generations of backups so if an error happens in the data and you don't catch it for a coupler of backups, you still can recover.)

Usage: archive_gdg file_name genCount

Example:

archive_gdg /home/mydir/db/daily_backup 10

In the above example, ten copies of the daily_backup file will be kept in the given directory as /home/bydir/db/backup.G01 through .G10 with backup .G01 always being the latest file.

When the script is executed, it first deletes the oldest generation, if it exists. Next,. generations 1 through the genCount, in the example 10, are renamed to the next generation up G09 becomes G10, G08 becomes G09 ... G01 becomes G02. Then file specified on the script line is renamed to G01.

When you are doing autmatic backups of files or databases, it's nice to have aged files. I have had to go back and restore old databases. Yes, I know svn, et.al. supports revisions, but it doesn't roll off the back after a certain time. If you are backing up a 3G file, you can quickly eat 100G with svn type software.

Z/OS users -- yea, it's not quite a g0000v00 but *nix doesn't understand (0) in an fopen statement (grin).

dbbkup
[an error occurred while processing this directive]

dbbkup is a mysql backup management script to easily backup and restore MySQL files utilizing archive_gdg. It can be used to backup all databases in the system, backup one or more named databases, or restore one or more databases.

View more

  Usage: dbbkup [OPTIONS] Database ...   
   -a          Backup all databases               
   -h --help   Display this help screen           
   -l          list MySQL databases               
   -s          Display script settings            
   -v          Display version   
   --ls        Display backup directory in -l format             
   --delete database ...
               The delete function drops a database from MySQL
               and deletes all but the latest (G01) generation from
               the backup directory.  G01 must be manually deleted.
   --dir /backup/dir
               Override backup directory.                                        
   --restore database [G##]...           
               Restpre a database frp, backup.  dbbkup will 
               default to the current generation to restore (G01).  
               To restore an older generation, enter the database name, 
               a space, then the generation number.  The script checks
               to see if the database exists. If the database 
               is not defined to MySQL, the script will do a 
               CREATE DATABASE itself.

Configuring the script

There are four (4) parameters that need to be customized for each installation: These are near the top of the script and are well documented.

defaults_file=$HOME/.my.cnf

The defaults file contains the MySQL login parameters. It's usually going to be the user's .my.cnf file. The contents, at a minimum should be the user and password:

       [client]
       user=dbuserid        # mysql defaults to logon id.
       password=dbpassword
       host=dbhost          # if not local host

Remember to chmod 600 .my.cnf for security. Of course if this is executed by cron, it needs to be run by the owner of .my.cnf. I also had a problem with the script in my cron.daily directory that $HOME was not set. I manually set it (HOME=/root) before running dbbkup -a See the MySQL manual for more information on .my.cnf files.

bkupDir="/winxp/mysql/dvlp"

The next parameter specifies the backup directory. In this case, I have nfs pointing /winxp to my xp desktop. GET THE BACKUPS OFF THE SAME DRIVE AS THE ORIGINAL IF AT ALL POSSIBLE!

numGens=10

Number of generations to keep. See archive_gdg above.

bypass="bible freq information_schema zipCode"

The bypass variable contains the names of the databases that are not backed up daily. Each database should be entered with just a spaces between them. These are static databases that that rarely change. According to MySQL, information_schema contains only views generated from the 'mysql' database and does not need to be backed up. The other databases are static that have been backed up once.

Using the Script

Caveats:

  • This script utilizes mysqldump. Be sure to read, understand, and heed the caveats associated with mysqldump I do a simple dump and don't worry about timing. This code is used for backing up development, and I am not worried about the tables being updated during the backup.
  • This script calls archive_gdg. Make sure you download that to /usr/local/bin
  • When the database is backed up the mysqldump output is gziped and stored in the backup directory as database.gz.G01.

    When using --restore to restore a database, the script looks at the next parameter after the database name. If the parameter is a generation numger, i.e. G04, it will restore that generation of the database. Otherwise, it will restore G01.

    Example:

    dbbkup --restore acctpay payroll G04

    The acctpay database will be restored with G01, and payroll will be restored to G04.

    Javascript

    Highlight Current Menu Selection

    I wanted a way on my menus to highlight the current menu selection on my navigation menu. All the code I saw had to code something in every screen to indicate what screen was being displayed. I wanted a way to automagically highlinght the current menu selection without having to mark every screen. (see code).

    View more

    Apache/html/cgi scripts

    Server Side Includes

    If you are not familiar with server side includes, LEARN ABIOUT THEM! Being able to include code as part of your html is very easy and makes maintenance of web pages so much easier. There is lots of talk about javascript includes and css links, I feel that html includes are just as important.

    View more

    The first thing you'll notice about server side include code is that it has the extension of shtml. The S is for server side commmands.

    If you are using Microsoft'S® IIS package, it appears IIS supports the same syntax (see MSDN IIS Server-Side Include Directives.) Since I work in a LAMP shop, I can barely spell IIS. I did a quick web search to find this page. It's nice to see IIS and Apache working together once in a while.

    All code that is common to multiple pages should be in it's own file. That file should then be included on the locations as needed. There is also no rule that the file can only be included once in a file. Need a state dropdown list for several fields? Put the option list in a separate file and include it where needed.

    The navigation bar on the left is a perfect example of using a server side include:

     <!-- file:  Natigation.htm --> 
     <script type="text/javascript" src="/javascript.js"></script>
     <div id="container">
     <img border="0" src="/images/NewLogo.jpg" /></h1>
     <div class="nav">
     <a href="/index.shtml">Home</a><br>
     <a href="/aboutus.shtml">About Us</a><BR>
     <a href="/ctOnline.shtml">CharityTrak Online</a><BR>
     .
     .
     .
     <a href="/SelfConfidence.shtml">Self Confidence</a><br></p>
    </div>
       

    In the code above there is the script statement to include my javascript code for the Highlight Current Menu Selection. Then comes the html code setting up some divisions, Display the company header, then all the links for the vareious pages.

    Now on each web page I simply code

    <body>
    <!--#include file="Navigation.htm" -->

    and the header imag and navigation bar are defined to the page. When I add a new page, I add it once to Navigation.htm and it's now on every page.

    getVersion

    Being lazy, I didn't want to have to update this page every time I put a new version of the above scripts on the web. So, I wrote (after much research) a little cgi script that works with apache to get the script to tell me the version.

    View more

    The following script is in my /var/www/cgi-bin directory:

     #!/bin/sh
     echo Content-type: text/hmtl
     echo
     # usage:  <!-- #include virtual="/cgi-bin/getversion?file=code/archive_gdg" -->;
    
     # This next line pulls the file name from the url GET data
    
     file=`echo "$QUERY_STRING" | sed -n 's/^.*file=\([^&]*\).*$/\1/p' | \
          Sed "s/%20/ /g"` $DOCUMENT_ROOT/$file -v
    
     $DOCUMENT_ROOT/$file -v    # Call program to get version
    

    That is the whole script (copy from here). Now in my shtml script I enter

    <!--#include virtual="/cgi-bin/getversion?file=code/archive_gdg" -->

    Now my html program is calling a cgi script that calls the script asking for the version. If you have access to your apache setup you probably could set up the configuration to execute the cgi code directly, but this works without messing with the apache setup.