Author: admin

Backup all SVN repos

If you need to backup your SVN repositories, then you can use this bash script to do so:

#!/bin/bash
DATE=`date +"%Y-%m-%d"`
BACKUP_DIR=/home/user/svn/backup/${DATE}
mkdir -p $BACKUP_DIR
for dir in `ls /var/svn/`; do
    RES_DIR=/var/svn/$dir;
    svnadmin dump $RES_DIR | gzip > "${BACKUP_DIR}/${dir}.dump.gz";
done

This basically dumps every revision from each repository in your server, and gzip’s them and then puts them in /home/user/svn/backup under the current date

Numerical Data Types in MySQL

In most MySQL GUI’s that offer the ability to alter the table definitions of a schema, if you add a column that is an INT, if defaults the syntax to be:

ALTER TABLE {table} ADD COLUMN {field} INT(11) DEFAULT NULL;

This is okay, but its really important to understand your datatypes.

What does the INT(11) really mean?

Basically, a common misperception is that the number in brackets is the length of the field. Probably because thats how the syntax works for VARCHAR(34) will allow a maximum length of 34 characters.

For integers, thats not strictly true. If you create an INT field, it will be an INT, no matter what you put in brackets. An INT field has the following maximum storage:

Unsigned - Min : 0,           Max : 4294967295
  Signed - Min : -2147483648  Max : 2147483648

So whats the (11) for you ask?

Basically, this is only used by the MySQL command line client, and this is the maximum length that it will display. The full value will be stored FYI.

So, the important issue is, you really should create your fields with the full understanding of what data may be stored within. There are more integer datatypes than just INT.

TINYINT, SMALLINT, MEDIUMINT, INT, BIGINT

The min and max stored data values are as follows

Unsigned (Min is always 0)

TINYINT    - Max : 255
SMALLINT   - Max : 65535
MEDIUMINT  - Max : 16777215
INT        - Max : 4294967295
BIGINT     - Max : 18446744073709551615

Signed

TINYINT    - Min : -127                 Max : 127
SMALLINT   - Min : -32768               Max : 32768
MEDIUMINT  - Min : -8388608             Max : 8388608
INT        - Min : -2147483648          Max : 2147483648
BIGINT     - Min : -9223372036854775808 Max : 9223372036854775808

Annoying IE Compatability Mode

I recently had to create a site for an education facility, and they turn on Compatibility mode for all their networked PCs, for legacy support.

This was messing around with some of my JS/CSS functionality, so I needed a work around.

3 options really:

  1. Get the IT manager to remove the global compatibility layer and only apply to the legacy apps. Not going to happen!
  2. Rewrite the code to use IE friendly techniques. Possible but a ball ache.
  3. Force the browser to not use compatibility mode. Sounds best, right?

So, its really very simple, in the head section of the page:

<meta http-equiv="X-UA-Compatible" content="IE=edge" />

That worked an absolute treat.

Disabling certain days in jquery UI datepicker

I wrote a site for my local pub and it involved a restaurant booking section. The user could select a date and a time (plus name, phone number and number of people)

The kitchen was closed on Mondays, so I needed to stop any bookings for Mondays. I had two options I could see,

  1. Server side, using PHP to check the date and refuse the request
  2. Client side, using JavaScript to block the request

I decided client side was best for the end user, otherwise they have to go back and pick a new date, etc.

I was using jquery UI’s datepicker and its very simple to just block Mondays:

$('.datebox').datepicker({
    "dateFormat"    : "DD dd/MM/yy",
    "beforeShowDay" : function(date) {
        var day_of_week = date.getDay();
        return [(day_of_week != 1), ''];
        }
    });

The day_of_week numbers start at 0 for Sunday, to 6 for Saturday. SIMPLES

Connecting to MSSQL

I have had to create a few PHP sites that connect to a MSSQL server. Not being down with setting up these servers, there are a couple of caveats you should be aware of if you are not accustomed to the server yourself.

TCP/IP access is turned off by default, so if you are trying to connect via IP address, you will need to switch this on. It is in the SQL Server Configuration Manager under the Server Network Configuration > Protocols for xxx

Also, if you are a MySQL guru, you will use user accounts to connect, and MSSQL can have the same, but they are switched off by default. If you connect to your MSSQL server instance using the Management Studio and right click on the server (the top item in the filetree) and choose properties, you will find a “Security” tab, where you will find an option for allowed authentication types.  Switch it to SQL Server and Windows Authentication mode.

This will save you a few hours!

Redirect certain IP address .htaccess

If you ever wanted to prevent certain IP addresses from accessing your site, you can easily acheive this in the .htaccess file.

Simply list the IP addresses and then redirect them to the desired URL:

RewriteEngine on 
RewriteCond %{REMOTE_ADDR} ^xxx\.xxx\.xxx\.xxx$ [OR]
RewriteCond %{REMOTE_ADDR} ^xxx\.xxx\.xxx\.xxx$
RewriteRule ^(.*)$ http://www.redirection-url.com [L]

If you want to add more then do so, but notice there is no [OR] on the last condition.

Now, this could get messy if you have loads, and I am unsure of the performance hit, but its a quick and easy way of redirecting users. A site I work on has 5 servers located worldwide, and I wanted to send LA users attempting to access the UK server back to their designated server

Convert a MySQL database to UTF8

This is a “script” to convert your latin1 myisam database to utf8 innodb
Dump the database

mysqldump -u root -p database_to_convert > db.latin1.sql

Make a copy of the dump

cp db.latin1.sql db.utf8.sql

Change any default table charset to be utf8

sed -i 's/DEFAULT CHARSET=latin1/DEFAULT CHARSET=utf8/g' db.utf8.sql

Now, remove all field collations for Latin1

sed -i 's/COLLATE=latin1_general_ci//g' db.utf8.sql

Change any MyISAM engines to InnoDB

sed -i 's/ENGINE=MyISAM/ENGINE=InnoDB/g' db.utf8.sql

Re-import the database

mysql -u root -p database_to_convert < db.utf8.sql

And thats it.

Convert Excel headers to numbers

Quite often I have to convert datasets from one source to another target. Sometimes I can only export them as CSV to investigate the field order/column structure

Then, once I work out the columns I can import them. But, the headings are often not in the source, so I only have Excel/OpenOffice column headers which go from A to Z and then AA to AZ, and BA to BZ, etc etc

I needed a quick way to convert these to numbers, so I could state “the 141st column is /this/ field”

So here it is:

$(document).ready(function() {
    $("#testes").on("keyup", function(event) {
        var str = $(this).val();
        if (str.length == 1) {
            $("#results").val(get_string_value(str));
        } else {
            var fstr = str.slice(0,1);
            var estr = str.slice(1,2);
            $("#results").val(get_string_value(estr) + (get_string_value(fstr) * 26));
        }
    });
});

function get_string_value(str) {
    return str.charCodeAt() - 96;
}

Or if you like, a fiddle: http://jsfiddle.net/8eAV6/

Find a file containing a text string on Linux

There is a very useful command in Linux, called grep. It is a utility for searching plain text files using regular expressions. You can either search files and folders as a parameter, or pipe command outputs to it, such as grep’ing a tail.

For looking for a file:

grep -r "string to find" /folder/to/look/in/

Thats the simple answer.

If you want to pipe the grep to monitor say a tail

tail -f /var/log/maillog | grep "chris@tatedavies.com"