Backup your server data to Dropbox

Though personal blogs are not that serious comparing to your account data in the bank, it still worths some backup. Linode does offer sophisticated official backup service starting from $5 per month, however if your website doesn’t have huge traffic and the database/webroot size reasonably, you can choose some free alternative approaches.
You’ve already known Dropbox,(If not, click here to register, which can bring both of us extra 250M quotas:)) the state-of-the-art file syncing service. Good news is that it can be running without GUI, which makes the backup on your server as easy as simply compressing and copying.

So let’s start.

Install Dropbox

Firstly, open Putty, ssh to your server, and install Dropbox.

$ cd ~
$ wget http://dl-web.dropbox.com/u/17/dropbox-lnx.x86-0.7.110.tar.gz
$ tar xzf dropbox-lnx.x86-0.7.110.tar.gz
$ .dropbox-dist/dropbox

Dropbox will spit out numerous messages informing you that you need to link your server to your Dropbox account.

This client is not linked to any account…
Please visit https://www.dropbox.com/cli_link?host_id=blahblahblahblahblah23445435365 to link this machine.

Copy and paste that link into a browser on your client machine. You will then have to log in to Dropbox’s website using your Dropbox account. It won’t be obvious at first that anything happened. If you review the “Events” tab on the website, however, you will see a message indicating that your server has been linked to your account. Now you can go back to the command line.

Kill the Dropbox process with Control-Z.

Now there should be a folder named ‘Dropbox’ sitting in your home.

mkdir /home/kymair/Dropbox/Linode

Creating a Service to Run at Boot

$ cd /etc/init.d
$ wget http://www.kymair.com/files/dropbox.txt
$ mv dropbox.txt dropbox
$ chmod +x /etc/init.d/dropbox
$ update-rc.d dropbox defaults

Create daily backup script

I’ve created a specific user within MySQL to perform the backup task. In order to export data, the user has to be granted ‘LOCK TABLES’ and global ‘SELECT’ privileges.

mkdir /root/scripts
vi daily_backup.sh

Paste the text below to create the script.

#!/bin/sh
echo "Exporting MySQL databases..."
mysqldump -S /tmp/mysql.sock -u admin -p12345678 --all-databases > mysql.sql
zip mysql.zip mysql.sql
rm mysql.sql

echo "Compressing htdocs..."
zip -r htdocs.zip /srv/htdocs
echo "Moving to Dropbox sync folder"
mv mysql.zip htdocs.zip /home/kymair/Dropbox/Linode/

At the end, add the script into cron jobs.

crontab -e

Add following line

0 3 * * * /root/scripts/daily_backup.sh > /dev/null &2>1

And don’t forget to start Dropbox daemon

service dropbox start
Posted in Hosting | Leave a comment

WordPress 3.0 is out!

WordPress 3.0 “Thelonious”
You may already know this, WordPress 3.0 is finally coming out! The new default theme called Twenty Ten is so sexy, and the admin page is just gorgeous as well!!

Needless to say too much, please go ahead and upgrade to this milestone version right now. I’m quite sure you’ll like it. As you see, I’ve switched my theme to ‘Twenty Ten’.

By the way, I’ve migrated my hosting to Linode, the famous VPS provider recommended by lots of people around. Linode is now celebrating its 7th anniversary, increasing RAM across all plans by about +42%, so the previous Linode360 is now Linode512. I’m not sure how long it will last but it is definitely a big bonus.

In coming days I’ll write a series of tutorials in terms of various softwares installation on my brand new Linode.

Posted in WordPress | Leave a comment

How am I connected to Twitter

My arsenal has Twip, Tweetie, Dabr and Metrist and Nginx.

Twip is a simple script written in PHP which redirects HTTP request from user to Twitter official API endpoint, as long as you have a web hosting in U.S, you can easily set up a API proxy for your own’s sake.
Simply grab the tarball, extract it and rename index-example.com to index.php, modify the options within it to meet your requirement, such as open the access to your friends only.
If you are using Nginx like me, the default .htaccess won’t work, you have to write the rewrite rule yourself. It’s easy, add one line to the PHP fastcgi_parm part first, then rewrite all requests to index.php accordingly.

fastcgi_param   HTTP_AUTHORIZATION    $http_authorization;
location /twip {
    if(!-e $request_filename){
        rewrite ^/twip/(.*)$ /twip/index.php last;
    }
}

For desktop client, I’m using one Chrome extension ‘Metrist‘, it is beautiful and supports 3rd API proxy, plus it is written by a Chinese. (Though sometimes it is not that stable, maybe caused by the API proxy.)

On my iPod touch, surely the choice is Tweetie, needless to say it’s super awesome.

Also I’ve installed Dabr as well as it’s more convenient to navigate historical tweets and to access Twitter on any machine.
The version I adopted is provided by Blogkid which has been encrypted, so suppose it should be more safe to use and much more difficult to detect. Anyway you might sacrifice some speed to trade for that.

Sometimes Twip doesn’t work together with Metrist, and I haven’t figured out why, so I setup another API proxy utilizing Nginx as reversed proxy, it’s super fast, but lacks of authorization means, leave a comment if you know how:)

server
{
	listen      80;
	server_name t.yourname.com;

	location / {
		proxy_pass http://twitter.com/;
		proxy_redirect off;
		proxy_set_header X-Real-IP $remote_addr;
		proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
	}
}

Welcome to follow me on Twitter.

Posted in Web | 1 Comment

MoinMoin + Nginx

Recently I’ve done quite a lot times of setting up LNMP(Linux + Nginx + Mysql + PHP). During this I notice there’s not much resource on the internet talking about the installation of MoinMoin on Nginx, so here’s what I’ve done to make it run, hope will help. Actually those are collected from other places from internet,

  1. Grab the latest version of spawn-fcgi from http://redmine.lighttpd.net/projects/spawn-fcgi/news and install it.
    tar zxf spawn-fcgi-1.6.3.tar.gz
    cd spawn-fcgi-1.6.3
    ./configure && make && make install
    
  2. Create a script under /etc/init.d/, name it anything you like such as moin-fcgi
    #!/bin/sh
    
    NAME="moin"
    DIR="/srv/share/moin/server"
    FCGIAPP="./moin.fcgi"
    PREFIX="/usr/local"
    
    PORTS="1080"
    
    start_on_port () {
        # Start moin instance on port, leaving pid file
        port=$1
    
        cd "${DIR}" && sudo -u www "${PREFIX}/bin/spawn-fcgi" \
            -f "${FCGIAPP}" \
            -p $port \
            -P "${NAME}-${port}.pid" \
            > /dev/null
    }
    
    kill_on_port () {
        # Try to kill process using pid in pid file, then remove the pid file
        pidFile="${DIR}/${NAME}-$1.pid"
        kill `cat "$pidFile"` && rm -f "$pidFile" > /dev/null
    }
    
    start () {
        for port in $PORTS; do start_on_port $port; done
    }
    
    stop () {
        for port in $PORTS; do kill_on_port $port; done
    }
    
    case "$1" in
    start)
        # XXX starting twice will break pid files (bug in spawn-fcgi)
        start && echo -n " $NAME"
            ;;
    stop)
            stop && echo -n " $NAME"
            ;;
    restart)
        stop
        start && echo -n " $NAME"
            ;;
    *)
            echo "Usage: `basename $0` {start|stop|restart}" >&2
            ;;
    esac
    exit 0
    
  3. Assume we’d like to bind MoinMoin under sub-folder /wiki, modify nginx.conf like this:
    rewrite ^/wiki$ /wiki/;
    location /wiki {
      include fastcgi_params;
      if ($fastcgi_script_name ~ ^/wiki(.*)) {
        set $path_info $1;
      }
      fastcgi_param PATH_INFO $path_info;
      fastcgi_param SCRIPT_NAME /wiki;
      fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
    
      if (!-f $request_filename) {
        fastcgi_pass 127.0.0.1:1080;
      }
    }
    

    From John Leach

That’s it. For how to install MoinMoin, please check the manual on official website.

Posted in Web | 1 Comment

If your company blocks port 22

Hmm..Then you won’t be able to access your own public server via SSH.
Here’s a simple way to work around it. Just re-bind the sshd daemon of your server to port 443.

# sed -i 's/Port\s22/Port 443/' /etc/ssh/sshd_config
# /etc/init.d/ssh restart

443 is for HTTPS connection, basically you will not use it. (Unless you are running some serious commercial website which requires SSL). Most importantly your company firewall won’t block this port. So everything would be fine:) You can sometimes check up your own website at work now… But don’t do that frequently, you need to be a good employee or your boss is not happy.

Posted in Hosting | Leave a comment

My First Time to modify .class file directly

I’ve been implementing a email dispatching and archiving program recently. To avoid reinventing the wheel, I simply did several searches on Google, one open source software called MailArchiva can get the archiving job done perfectly. It is built to support millions of emails and thousands of mailboxes. So without doubt its performance can easily meet my requirement – simply listen to one single mailbox that won’t have more than 1000 emails a day.

So I just download the open source edition of MailArchiva (They sell the enterprise version which offers more advanced features like WebServices), upload it to my local server, run the install, then all are settled down. It runs smoothly and I can see the emails won’t stay more than 3 seconds within the inbox.

However, this software doesn’t ship with a configuration to let me modify which folder of the IMAP account the application listens to. Before the archiving, there should be another program written by myself to parse the incoming messages and dispatch accordingly. But now it turns out my program don’t even get to read the message before it is archived.

I’ve considered quite a few approaches to solve this problem, such as giving up this MailArchiva and implement a archiving program myself, or move the desired message to other folders on the server side via Exchange rules.

:) And I finally get things done by modifying one class of MailArchiva which is in charge of connecting to IMAP, changing default folder ‘INBOX’ to my own specified one. It’s my first time to modify .class file directly. There’re some existing tools outside, but I chose to edit it with UltraEdit as Vim doesn’t work correctly somehow. (The file modified by Vim cannot be loaded due to some ‘Extra bytes at the end of file’ problem.) Also, as I don’t want to spend the time to dig out where the length of String is, so instead of ‘ARCHIVE’, I named the folder ‘MAGIC’ as it’s of the same length with ‘INBOX’ :)

Sometimes, things can be so straightforward. You just haven’t found the right way. That’s what I’ve learned from today’s experience. Keep DRY(Don’t Repeat Yourself) principle in the mind all the time.

Posted in Development | 1 Comment

Go ahead Chrome

Now I’m totally a Chrome fan.

Speed is the very first thing Google has been always looking for, Firefox apparently is not fast enough.

Let’s make the web faster! Chrome is surely one of the most important part. If it can keep gaining popularity, then Google is possible to roll out more evolutionary changes to protocols and network infrastructures.

Without doubt, yes, after getting used to Chrome’s speed, you won’t be willing to waste the time with Firefox.

In the Firefox era I used to keep it open all the time as it required such a long time to boot up. Now I don’t have to with Chrome. It might be a little exaggerated but starting Chrome itself feels like opening a new tab in Firefox! (Actually the tabs in Chrome are indeed separate processes.)

Not to mention Chrome finally starts to support extensions, bookmarks sync and gorgeous themes. Currently I’m using only one extension named Metrist, a good-looking and enough powerful Twitter client. Bookmark sync is fantastic, the only thing I’m wondering is why Google stores them in Docs instead of existing bookmark app. For themes, oh it’s amazing that Google releases lots of themes created by various artists. Awesome!

Unfortunately someone still has to stick with Firefox as because of Firebug or similar extensions used for UI Development.  Anyway supposedly you can use them both, get a fresh Firefox only with Firebug to develop, and a super-fast Chrome for daily navigation.

I’m looking forward to Chrome OS’s official release. I’ll definitely burn it into my netbook and use it everyday. (Thanks to the VPN, There’s no longer a strong wall standing in front of me.)

BTW, sometimes I really want to talk, but am not able to express with English in a good manner. So, there’s another Chinese blog here. Basically it’ll be just about my personal life which I’m not sure whether you’ll be interested. Anyway it’s always been a pleasure to write stuffs. As jjhou said, “Publishing is the best way to memorize.”

Posted in IT | Leave a comment

Tabs Vs. Spaces

Yesterday I came across a weird problem which took me a whole day to troubleshoot but without luck. Speaking simply, one YAML configuration cannot be parsed due to whatever ParserException..

Alright this morning, I finally got it solved by editing the file with UltraEdit instead of Vim. As always I’d like to dig out what exactly was going on there. Then I found out that when I insert a new line, both Vim and UltraEdit can indent automatically for me, the difference is, the latter uses spaces but the former users tabs!!

But what’s wrong with Tabs???

Then I was embarrassed to find one of the only two items of YAML’s FAQ talks about this:

Why does YAML forbid tabs?
Tabs have been outlawed since they are treated differently by different editors and tools. And since indentation is so critical to proper interpretation of YAML, this issue is just too tricky to even attempt. Indeed Guido van Rossum of Python has acknowledged that allowing TABs in Python source is a headache for many people and that were he to design Python again, he would forbid them.

So that’s it! It is a classical “religious war” within computer science.(Just like the war between Emacs & Vi). Check out this good article.

I instinctively feel that Tabs should be the one, like the article above said

Simply put, tabs is proper, and spaces are improper. Why? This may seem ridiculously simple given the de facto ball of confusion: the semantics of tabs is what indenting is about, while, using spaces to align code
is a hack.
Now, tech geekers may object this simple conclusion because they itch to drivel about different editors and so on. The alleged problem created by tabs as seen by the industry coders are caused by two things: (1) tech geeker’s sloppiness and lack of critical thinking which lead them to not understanding the semantic purposes of tab and space characters. (2) Due to the first reason, they have created and
propagated a massive none-understanding and mis-use, to the degree that many tools (e.g. vi) does not deal with tabs well and using spaces to align code has become widely practiced, so that in the end spaces seem to be actually better by popularity and seeming simplicity.
In short, this is a phenomenon of misunderstanding begetting a snowball of misunderstanding, such that it created a cultural milieu to embrace this malpractice and kick what is true or proper. Situations like this
happens a lot in unix. For one non-unix example, is the file name’s suffix known as “extension”, where the code of file’s type became part of the file name. (e.g. “.txt”, “.html”, “.jpg”).
Another well-known example is HTML practices in the industry, where badly designed tags from corporation’s competitive greed, and stupid coding and misunderstanding by coders and their tools are so
wide-spread such that they force the correct way to the side by the eventual standardization caused by sheer quantity of inproper but set practice.

It’s good to get my problem solved, and it’s of so much fun to read these stories :)

Posted in Development | Leave a comment

How do you get things done

Eventually I discovered the importance of being well organized by doing those things I hated to do before: make notes and manage to-dos.
What I’ve got is a iPod touch, though there can’t be internet connection anytime anywhere, one sync per day is enough for me. The GTD software I use is called TODO app. Everything works fine including syncing. RememberTheMilk should be the best of those services but apparently I don’t have the money to upgrade to Pro account, anyway the alternative Toodledo fits me well. I decide to list all my to-dos every night and get them done the next day. Keep doing this and I’ll surely be organized.

Making notes is really useful and important. I’m still not happy of Google discontinuing the development of Google Notebook, however it is still my major tool to make notes. EverNote is awesome, but you know what we want is a single ultimate account which meets every needs of our internet life, Google should be the one, sorry EverNote.
Reading my own past notes(Email, also) is quite a weird but funny thing. And it really helps to track the mind and streamline the study process.

Hope I can get used to these good behavior.

Posted in Thought | 2 Comments

Use ‘import static’ to simplify your code

To write very robust and safe code, you should always treat the client as a potential attacker, a bad guy. Consider he will do his best to break your function with invalid arguments.Therefore the good practice is, always validate the input arguments, and throw NullPointerException or IllegalArgumentException accordingly.

Usually for sake of reuse and simplicity, there would be a utility class consisting of several static methods to do this. Then we call it like this

StringUtil.isNullOrEmpty(inputString);

However after read Google Guice’s code accidentally, I now realized this can be rewritten with utilization of import static

checkNotNullNorEmpty(cells);

As you need have the validation inserted at the beginning of every public method, the new approach is surely a time saver. Moreover, you can extend the utility class into a complete Preconditions class, and use it everywhere you want to ‘assert’ something.

Well this is just a very small tip about usage of import static, but life does get better, right? I bet you can also recall JUnit’s assertion way.

Posted in Development | Leave a comment