MySQL create update and modify

Short post to keep in memories for a very common task in MySQL with regards of two things:

  • keeping track of record timestamp with regards to created/modified
  • updating a record if it exists or creating one if absent

The above two issues are pretty common and many frameworks use them and have a way around them. Today I decided to make a small script that would use DB to keep a single table with a list of IP addresses as well as created and modified timestamps for them (kinda admin task), so I got into MySQL to sort out the table and had few thoughts on how to do it better. Googling for few minutes brought me to the article here that shows an interesting way of setting current timestamps on created and modified, while mysql documentation shows a INSERT … ON DUPLICATE KEY  section that I never saw before.

So having a table with a structure like:

CREATE TABLE ip_list(
    ip VARCHAR(20) NOT NULL PRIMARY KEY,
    souce_fw VARCHAR(50) NOT NULL,
    created TIMESTAMP DEFAULT '0000-00-00 00:00:00',
    modified TIMESTAMP DEFAULT NOW() ON UPDATE NOW()
);

 
I could use a simple statement to insert update the IP (adjusting the source field if needed) as follows:

INSERT INTO ip_list (ip,source,created,modified) 
    VALUES ('8.8.8.8','myfw1.com',NULL,NULL)  
    ON DUPLICATE KEY UPDATE source='myfw1.com';

 
Which would either insert a record in the DB with created/modified set to NOW() or update the row with given IP setting new source and adjusting modified to current time.

As usual: simple but cool!

PuTTYCS

Recently I wrote a post about ClusterSSH which I found very handy to use when dealing with a bunch of similar servers for doing same thing on all of them in parallel. Today I was asked by my colleague if there is anything similar to that, but for Windows (controlling multiple SSH/Telnet clients from Windows desktop) and after a bit of googling I found PuTTYSC: a small, but very handy tool for controlling multiple instances of PuTTY ssh/telnet clients. Just specify which running instances you want to control and send a command to all of them at ones.

I haven’t personally tested this software as I don’t have Windows running somewhere near me, but I checked the screenshots on Google Images and the think looks pretty nice and easy to use.

ClusterSSH

ClusterSSH – a perfect tool for dealing with cluster of servers:

ClusterSSH is a tool for making the same change on multiple servers at the same time. The ‘cssh’ command opens an administration console and an xterm to all specified hosts. Any text typed into the administration console is replicated to all windows. All windows may also be typed into directly.

This tool is intended for (but not limited to) cluster administration where the same configuration or commands must be run on each node within the cluster. Performing these commands all at once via this tool ensures all nodes are kept in sync.

There are whole lot of different automation tools these days and there are bunch of common practices, but sometimes we just need to deal with servers manually. For the past month I am dealing a few clusters of similar servers where I need to run same things and while the cluster is still under construction, I have to put more things on each node and do it manually. Today I got too lazy to login to each one to run few commands so I start googling around for a nice solution. In my case, I can’t use something like pssh, as my commands involve typing in passwords and doing some interactive things, so cluster ssh is just the right tool! As per description from their web site – it opens terminal per each server as well as gives a place to type a command that will be replicated to all those terminals. Very handy and strongly advisable to any system administrator. In addition it allows to define different clusters in a config file to be referred to later on.

Widgetsoid

Widgetsoid is yet another handy app for Android, allowing to have switch panel in notification bar. All the time I saw XDA firmware I was feeling jealous about those nice switches in the notification bar, that allowed one to toggle WiFi, 3g, sound and so on. These features are used all the time, so they have to be somewhere close enough, but having a standard power panel on the screen takes too much space.

Finally I found what I was looking for. Very flexible tool that did all I need that much. You can use it for multiple notification toggle switch bars, or even for normal widget panels. Customize buttons, icons, colors and whatever else. Very handy and strongly advised for people on default firmware.

The only reason I haven’t switched to XDA firmware is that I have Nexus, that receives updates from Google directly. BTW got Android 4.1.2 update yesterday. Nothing special or something I can note, but you know that feeling :-)

Windows Server

Oh yeah. Last time I did installation of Windows Server was years ago and I completely forgot the headache one normally get in the process, but these days I had to do an installation for one particular case, so here we go.

The hardware for installation is a tower server with LSI MegaRaid controller installed in PCI, hosting two drives in mirror. Additionally there is an IBM LTO tape drive. Server needs to do some specific backup of some specific windows stuff, so no chance to have it running Linux. First decision (not mine anyway) was to install Win Srv 2008.

Booting the server, configuring RAID, running Win 2008 install. All looks fine but at some moment I get a warning that while installation can be done on my drives, it won’t be able to boot from them afterwards. It sounded weird to me, so I just ignored the warning and went on with the setup. HDDs were formatted, files copied and Windows setup went into reboot as normal, but it really failed to boot from the drives… Ok, few manipulations here and there with BIOS, RAID Controller and Windows Setup partitioning part and I manage to install Win 2008 and boot it up.

First thing I see is some kind of welcome control panel, that offers to do different things in order to finish configuration of the server. While it looks nice and fancy – it is really hard to find something you are looking for there, but OK, you get used to it. Missing some drivers and updates – all installed in few hours and then what I see: LTO drive is not recognized, non of the drivers help. We spend quite some time tweaking and tricking all possible aspects of the system and no luck to see the drive. After some reading here and there we found out that Win 2008 changed something in the way they treat tape drives and so on, so we decided to downgrade to Win 2003 instead, as it is not critical on which version of Windows will run, but we need drive to work.

So next thing is Windows 2003 Server CD goes into USB CD-ROM for installation. Booting up setup and it turns out that this version of Windows has no driver for LSI RAID controller where we have our HDDs connected and setup can not continue unless I supply drivers ON A FLOPPY DISK :-( Minutes later I had what Windows asked for and I told the setup to go and get the drivers from the floppy. It read the floppy, told me that it found drivers for my controller, but then it informed me there is no such hardware in my computer 0_o

Tried different drivers from the CD supplied along with the hardware including the ones for Win 2003 32bit, Win 2003 64bit, Win 2000 and whatever else I could find. Tried updated versions of driver from the vendors web site, same shit. While booted to Linux live CD I could see all the hardware properly working as well confirmed that drivers used for Windows setup are correct (according to the info in .sys file and lspci output).

Finally, we decided to make a custom Windows CD with nLite and put drivers in, and it worked! We used same Windows CD for OS sources, same drivers that we used on floppy and so on. How come? This is out of my understanding…

As I am in the process of finalizing Windows 2003 install now, I hope that tape drive will work, though I will have no wonder if something else will fail.

P.S.: I had no intentions to say that Windows is terribly bad, especially compared to something else, in this article. I just wanted to point out some difficulties I had in this particular case.