Freitag, 15. August 2008

SQL-Dump zerlegen


SQL-Dumps kommen üblicherweise als Textfile daher. Und haben häufig die unangenehme Eigenschaft, riesig zu werden. Dieser Beitrag stellt ein kleines Python-Script vor und zur Verfügung, mit dem sich der Dump einer MySQL-Datenbank in mehrere Dateien zerlegen lässt, die jeweils nur noch die Daten einer einzelnen Tabelle enthalten. Eine MySQL-Datenbank mit all ihren Tabellen lässt sich etwa so prima exportieren:

mysqldump --opt -h 127.0.0.1 -u DBUSER -pDBPASSWD DATABASE >dump.sql

Was aber tun, wenn 'dump.sql' viel größer als gewünscht ist? Nun, zerlegen wir die Datei doch in kleinere Häppchen. Das lässt sich per Hand mit einem Editor bewältigen, ist dann aber schnell Sklavenarbeit und dazu noch fehleranfällig. Dann lieber ein Tool nehmen, dass diese Arbeit maschinell erledigt. Vermutlich gibt es dieses Tool bereits zuhauf - ich habe nicht danach gesucht. Wer so ein Tool kennt möge mir doch eine Zeile schreiben.

In meinem Fall hatte ich nicht die große Allround-Lösung im Sinn. Auch habe ich mich nicht damit beschäftigt, welche syntaktischen Klimmzüge das Programm da bei den Kommentaren im Dump hinterlässt. Vielmehr habe ich ein Tool geschrieben, dass mir genau die Arbeit abnimmt, die ich sonst per Hand gemacht hätte: Database-Dump nehmen, jeden Abschnitt daraus, der eine Tabelle enthält, in eine eigene Datei stecken, die 'tabellenname.sql' heißt, und dafür sorgen, dass auch hier die Kommentare am Anfang und am Ende der Datei wieder vorhanden sind.

So ist splitsql.py entstanden, ein Python Opensource Programm, das beliebig verwendet werden darf. Beispieldaten kann man sich hier anschauen. Was Spaß macht: Es lässt sich leicht modifizieren, und so ist es nicht schwer, ihm beizubringen, zusätzliche Informationen auszugeben, wie etwa die Vorlage für ein Shellscript oder Texte, die man wieder in den Quelltext einbauen kann. Viel Spaß!

Donnerstag, 7. August 2008

The One Line Webserver

It can be so easy - once you know. A lot of people do some web development at home. Beginners reach a major frontier once they leave the terrain of pure (X)HTML and CSS. For instance dealing with AJAX in general requires a web server. Mostly you'll find the advice to install one of those great all-in-one packages like XAMPP from apachefriends or to turn to Linux right away. And this is where you'll surely end if you follow your web developer career. But there may be a lot of reasons why you wouldn't want to follow this lane and prefer an inobtrusive and small solution to start with.

All right, here it comes. I'm talking of Windows here. But the solution is applicable to other operating systems as well (if it's needed there at all ...). My advice is to install Python on your machine. This is just a matter of a few clicks once you have grabbed the complete installer package from the download page of python.org. Currently version 2.5.2 is what you should choose. It will propose to install to C:\Python25 which you should do. Make sure that this folder is added to your path. Installing Python isn't a danger or risk. It doesn't bloat your system and can be easiely removed using the uninstaller. But you'll never feel the need to do so.

Now create a folder as the root of your webserver and a subfolder where you put your documents. Let's say we now have ".../MyServer/htdocs". Create a file "MyServer/htdocs/helloworld.txt" with "Hello World!" as content. Open a command line window and navigate to the "MyServer/htdocs" folder. And one single command gets your web server on your local machine up and running:

C:\MyServer\htdocs> python -m CGIHTTPServer

Serving HTTP on 0.0.0.0 port 8000 ...

WARNING: Do not start this server while you're connected to an untrustable net - like the internet - as it may expose your complete machine. Do local testing only!

What basically happens is that you start Python which will look for a python module CGIHTTPServer. The module will then be imported and as it finds out that it's running as a main module it activates "a little test routine" which will run forever until you press CTRL+C and the next http-request is encountered. The "little test routine" in fact is a complete webserver with some abilities. Do the test:

http://localhost:8000/helloworld.txt

and "Hello World!" is what you should see in the browser. You've tasted blood and would like to try some serverside active scripting? That's easy. Add a second folder named 'htdocs/cgi-bin' and place a little text file called "helloworld.py" there. It contains a single line:

print "Hello World!"


So now you have:

MyWebserver/ <- just to organize everything
MyWebserver/htdocs <- your webspace! start server here.
MyWebserver/htdocs/cgi-bin <- place Python scripts here
MyWebserver/htdocs/helloworld.txt
MyWebserver/htdocs/cgi-bin/helloworld.py

Now open this URL in your browser:

http://localhost:8000/cgi-bin/helloworld.py

You got this?
Congratulations!


Python will start helloworld.py as a CGI-script and direct all output to your web page. Amazing, isn't it? There won't be PHP and MySQL unless they already existed on your machine. But Python programmers will now have hundreds of ideas of how to inspect and extend the code. You aren't a Python programmer? Well, become one! It's more than advisable!

extra key words: "poor man's web server"