Archive for August, 2008

Spammers @Red Hat…

Saturday, August 30th, 2008

I'm getting bombarded with "RHN Errata Alert" messages from Red Hat (our company is a legitimate paid user of their OS) for last THREE days already…..

What the fuck?! Can't they just make a bloody knowledge article and link there? This is just getting annoyed. And am I supposed to update my systems every hour, when a new alert comes? Or should I wait till the flood from Red Hat stops?? Man… what an annoyance… :/

A very simple offsite backup over ssh/scp

Wednesday, August 27th, 2008

I've been setting up a very simple backup of one site's MySQL database to another server today.

What I needed to be done is to have the MySQL database files to get archived, compressed and transferred to my other server, and be named after the actual backup date and time. And that operation should happen every night.

Here's the full code for those who are in hurry (a cool one-liner heh :), and more detailed explanation of steps taken will follow.

ssh root@remoteserver.example.com 'mysqladmin flush-tables –socket=/tmp/mysql.sock; rm -f remote_db.tar* && tar cf remote_db.tar /var/lib/mysql && bzip2 remote_db.tar' && scp root@remoteserver.example.com:~/remote_db.tar.bz2 /home/mike/remote-backups/`date +%y%m%d_%H%M%S`.tar.bz2

And the explanation.

First, the server I'm backup up from is "remoteserver.example.com" and the forementioned command is invoked from the server I am backing up to. Also, I have my public ssh key installed for root account on the remote server, so I don't need to enter password to log into the remote server. You can read more about setting up SSH keys here.

First step is to log into the remote server, which is achieved simply by running

ssh root@remoteserver.example.com

However, actually instead of logging into the server, I only need to execute some commands on the remote server. This can be accomplished by giving a string of commands to execute as a second (last parameter to ssh command), for example:

ssh root@remoteserver.example.com 'ls -lh'

will give you a listing of files of remote server root's home directory.

So, now that we are connected to the remote server, we actually just need to prepare backup files which we will later transfer from remote to local server. Though the command is actually a one-liner, I'll split the lines for easier understanding, and give them numbers. Also, please note that the command concatenation with the && symbol does the following - it runs next command in chain only of previous command executed successfully.

1. mysqladmin flush-tables –socket=/tmp/mysql.sock
2. rm -f remote_db.tar*
3. tar cf remote_db.tar /var/lib/mysql
4. bzip2 remote_db.tar

#1 flushes mysql tables (so everything that is possibly in memory cache is writted to disk).

#2 removes any previous backup files (that's easy)

#3 this archives the whole MySQL data directory (this path can differ from mine, depending on your installaion parameters!). Also, please note that this approach is NOT SAFE! You can easily get a corrupted backup if any data changes during the archival process. The reason why I'm doing it myself is because I have 100% guarantee that the database will not be updated (the backup is for some inter-corporate system, and I have 100% guarantee that nobody is accessing the database at 3 o'clock in the morning when my backup task is running). You might want to lock tables during backup and unlock them once it is complete. Also, I'm backing everything up this way because I need a drop-in backup - anything happens, and I can just drop the backed up DB in place of the old one.

#4 and the final step is just to compress the backup archive to lessen the time required for transfer. You can compress it in one step actually, using 'tar cjf yourfile.tar ….'. The reason I'm running it in two steps is in order to lessen the time require for creating a database snapshot, so there's even less probability of database being modified during archivation process (tar takes ~5 seconds, tar with bzipping takes almost a minute).

Now, we have the backup file prepared on remote server, and all we need to do is to transfer it to our local server. That's an easy task.

Using the scp command for that task (you can read a little more about it here)

scp root@remoteserver.example.com:~/remote_db.tar.bz2 /home/mike/remote-backups/`date +%y%m%d_%H%M%S`.tar.bz2

There's a little trick here though! I am naming backup files after the date and time I copied them to the local server.

Well that's all. Hopefully somebody find info here helpful :)

PS: Also notice, there is no error checking and notifications if something goes wrong in this script! So feel free to enchance the functionality yourself.

PPS: Oh and I almost forgot! Of course in order to do daily (or hourly, or any periodic backups for that matter), you need to add this one-liner to crontab on your backup server!

New Apple goodness!

Sunday, August 24th, 2008

Hey what's in those boxes?!

Apple Goodies!!!

AppleTV is a very nice device. Got it to be able to show my daugther downloaded Russian movies, but the device actually way better than I initially thought - synchronizing photos and then showing them to our friends on our 46" HD TV should be a very satisfying experience :) (Thanks Paul for shipping it my way!)

And AirPort Extreme is pretty fast. I thought the speed will be faster - I'm getting about 3.5MBytes/s between my Mac and AppleTV in 812.11n-only 5GHz mode, though Internet download speeds are maxing at around 5MBytes/sec, so 3.5 is definitely not the device's limit, but probably AppleTV's one. And the configuration utility is best of its kinds! I was getting mighty sick of web-based config utility of my old wireless router.

And the new Mac, which is by the way the top-of-current-line model with maxed-out RAM (24-inch Core2Duo 3.06MHz iMac, NVidia 8800GT 512MB, 4GB RAM) - its a screamer! Bye-bye frustrating swapping and overall slowness of my old iMac (though it was a screamer in its time of course :)

Check radio group for selected elements with Prototype

Friday, August 8th, 2008

Just a quick one today.

Here's a little snippet on how you can check if any element of a radio button group is selected, using Javascript and the Prototype library.

function $FR(formElement, radioName) {
var el = formElement.getInputs('radio', radioName).find(function(radio) { return radio.checked; });
return el;
}

Usage:

var theform = $("mainform");
if ($FR(theform, "radio_group_name")==undefined) {
alert("Please select at least one element of the radio group");
}