Categories
Work

Simple MySQL Command line DB dump for cron

If it doesn’t already exists, create the file .my.cnf in your home directory.

Add the following to it, replacing db_username and db_password with the database username and password. make sure the file permissions are set to 600 (chmod 600 .my.cnf) so it is readable by you alone.

[mysqldump]
user=db_username
password=db_password

The following command will dump the database db_name to the db_backups directory with the filename yyyymmdd.sql where yyyy is the current year, mm is the current montyh and dd the current day:

mysqldump -u db_username db_name > db_backups/"$(date +%Y%m%d).sql"

This will not ask for a password, so can be used in a shell script or in cron.

Categories
Work

Location of Gnucash Invoice Template on Ubuntu

This is one of those things that drives me mad, that I can never find in the documentation. I’m currently running Gnucash 2.4.11 on Mint 14 (based on Ubuntu) and sometimes need to edit the invoice template. I can never find where the damn thing is.

Currently, it’s here (along with other templates):

/usr/share/gnucash/guile-modules/gnucash/report/invoice.scm

Categories
Work

Enabling mod_rewrite on Apache

This is one of those things I often forget to do when setting up WordPress on a new server, which means pretty permalinks don’t work: If you have a problem with permalinks not working on WordPress, it may be because Apache does not have mod_rewrite enabled.

An easy way to check is to make a simple php script, saved in the root directory of your webserver as info.php (or whatever takes your fancy):

<?php phpinfo(); ?>

This will give you an output of all the various modules and settings you have enabled for Apache. If you don’t see mod_rewrite under the Loaded Modules section, you need to enable it.

In Ubuntu this is pretty simple:

$ sudo a2enmod rewrite

Don’t forget to restart Apache

$ sudo service restart apache

Categories
Work

Restart Apache Webserver

To Restart Apache on Ubuntu or Mint (and possibly more):

$ sudo service apache2 restart

Categories
Work

Get the last rows updated in a database

When debugging applications, it can be useful to see what’s going on in the database without referring to the code. In MySQL, you can use the information_schema database to get this information. here’s what I do:

Select table_name, table_rows, update_time
from tables 
where table_schema = "[database_name]"
order by update_time desc
Categories
Work

Spliting strings with awk / nawk

I mainly use nawk to extract elements from a string where I know the position (eg to get just the filenames when running a grep and I’m just too lazy to remember the correct grep syntax).

so, to extract field number x from strings in where the field separator (FS) is y:

$ nawk '{ FS="y"; print $x }'
Categories
Work

Checking for a valid domainname

Very quick function to test a domain name is valid:

    function validDomain($domain)
    { 
        // Test domain is actually valid
        if (preg_match('/^[a-z]+([a-z0-9-]*[a-z0-9]+)?(.([a-z]+([a-z0-9-]*[a-z0-9]+)?)+)*$/i', $domain))
        {
            return TRUE;
        }
        return FALSE;
    }

The key here is the regular expression (delimited by the forward slashes at each end):

/^[a-z]+([a-z0-9-]*[a-z0-9]+)?(.([a-z]+([a-z0-9-]*[a-z0-9]+)?)+)*$/

This basically translates as follows

  • ^[a-z]+    : First chacter must be alphabet
  • ([a-z0-9-]*[a-z0-9]+)    : A group. The first character may be in the range, a-z or 0-9, or a hyphen. This pattern can repeat, although the last character may not be a hyphen
  • ?    :  That last group is optional
  • (.     :  Start of next group, and it must be a dot
  • ([a-z]+ : First letter must be in range a-z
  • ([a-z0-9-]*[a-z0-9]+)?  :  Then an optional group of a-z, 0-9 and hypen, though last  character may not be hyphem (as above)
  • )+)  : Do the last bit at least once
  • :  The last bit is optional and can repeat as much as you like
  • $:   And that’s the end of the string

It’s not a perfect reg-exp for the job, but it does the job where a quick quick is required (user signups fro example), rather than mission critical domain testing.

A good tool for testing your regular expressions can be found here