A very simple offsite backup over ssh/scp

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!

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

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");
}

How to override Firefox search language settings

July 31st, 2008

I live in Japan, but I really love to have my (Google) search results to be in English. However, every other program out there thinks it knows what I need better than me. 

That exactly the case with Firefox 3 on Mac. It annoyed the hell out of me to get my location detected every single time I do search and search Japanese segment of the web for my English-worded queries. Making changes to my preferred search language in my Google accounts yielded ZERO results. 

But before you go insane, there's an easy solution!

1. Open file: /Applications/Firefox.app/Contents/MacOS/searchplugins/google.xml in your favorite text editor

2. add <Param name="hl" value="en"/> to the parameters section (if you want english as default language) as well as replace {moz:locale} entries to the required language setting.

See the screenshot.

3. Restart Firefox and enjoy!

Permalinks back online

July 31st, 2008

Sorry about downtime. WP 2.6 seemed to change the way permalinks are processed on Apache side, so I had to re-compile my ancient Apache install in order to add mod-rewrite. Everything should work fine now :)

Upgraded to wordpress 2.6

July 25th, 2008

… and publishing this article using iPhone's WordPress application! Pretty cool huh:)

photo

Want to get notified when a long action on remote server completes?

July 20th, 2008

Sometimes you might want to run a long action on a remote server, like dump a few gigs DB, or rsync a few thousands of files between two not very fast computers.

You want to take next action as soon as that long operation completes but don't want to baby sit your servers.

How about getting email notification to your.. say.. iPhone.. when the action completes? It's pretty easy! Have a look!

mysqldump -u user -p mydatabase && echo "Dump finished!" | mail -s "Wake up!" mike@example.com

Once the command mysqldump finishes executing,  you'll get mail with subject "Wake up!" and body "Dump finished!"

You can chain more commands, of course, by adding && between them ( && executes next chained command only if the previous one completed without errors)

Pretty cool to have such a simple but useful trick under your belt, isn't it? :)

Yeah! My iPhone can now ssh into things!

July 20th, 2008

Thanks to the great guys at iphone-dev and their Pwnage 2.0 package, I can now finally ssh into the server I have to manage (as well as perform other tasty tasks in command line!)

 Now, I only need one application to make iPhone 100% usable for me - some utility running on background and reminding me about missed calls and messages (vibrating, making sounds.. anything!).

My second iPhone 3G!

July 18th, 2008

Yesterday, I have noticed one little unpleasant detail in my otherwise perfect iPhone 3G. One bad pixel in top left area of the screen. Just one black dot that never changes its color and just stays the same black pixel forever.

Called Softbank today (my mobile operator) - these guys are clueless! Told me they'll get in touch with me but it never happened. Also called Apple support line and was redirected to Softbank with all the exchange inquiries.

So during lunch I just decided why not have a little trip to Apple store here in Nagoya, because it never hurts to check (in America, people can get their iPhones exchanged right in Apple stores).

And oh miracle! My iPhone WAS exchanged right at the Genius bar counter! The guy over the counter had a look at the screen - "Ah.. yeah I see". Then told me that he has to check Apple's policy regarding bad pixels on iPhones - and there's zero-tolerance in this regard, so you can get your iPhone exchanged even for one dead pixel. Then he checked the inventory, and there was the same model I have! The iPhone came in plain brown box though, not the stylish Apple package - definitely a sign that these units are for replacements only.

I received the unit, asked if I can check the screen for bad pixels - and there were none! I few mins later I was going out of the store with my new, replaced iPhone.

I had my doubts if the same approach that works in the USA, will work in Japan - and I'm very happy to confirm that it does!

Thanks Apple!

Jesus Phone 3G..

July 12th, 2008

..is mine!

 

and I'm Softbank's slave for next 2 years.. Well.. at least I didn't have to sell my soul ;)