<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	>

<channel>
	<title>Times New Rohan &#187; Ruby</title>
	<atom:link href="http://robrohan.com/category/ruby/feed/" rel="self" type="application/rss+xml" />
	<link>http://robrohan.com</link>
	<description>任意的技术</description>
	<pubDate>Tue, 13 May 2008 04:35:47 +0000</pubDate>
	<generator>http://wordpress.org/?v=2.5.1</generator>
	<language>en</language>
			<item>
		<title>Server Backup with Amazon S3 (Howto)</title>
		<link>http://robrohan.com/2007/11/03/server-backup-with-amazon-s3-howto/</link>
		<comments>http://robrohan.com/2007/11/03/server-backup-with-amazon-s3-howto/#comments</comments>
		<pubDate>Sat, 03 Nov 2007 18:30:35 +0000</pubDate>
		<dc:creator>小罗</dc:creator>
		
		<category><![CDATA[Linux]]></category>

		<category><![CDATA[Mac]]></category>

		<category><![CDATA[Ruby]]></category>

		<category><![CDATA[Tinkering]]></category>

		<category><![CDATA[Web Apps]]></category>

		<category><![CDATA[S3]]></category>

		<category><![CDATA[Software]]></category>

		<guid isPermaLink="false">http://robrohan.com/2007/11/03/server-backup-with-amazon-s3-howto/</guid>
		<description><![CDATA[I&#8217;ve been interested in using Amazon S3 (Simple Storage Service) for server backups, but I couldn&#8217;t find a simple, straight forward howto. This is a walk though on one way to setup a Unix server to automatically backup data to Amazon S3.
I guess I should start out with what Amazon S3 is I suppose&#8230; It&#8217;s [...]]]></description>
			<content:encoded><![CDATA[<p>I&#8217;ve been interested in using Amazon S3 (Simple Storage Service) for server backups, but I couldn&#8217;t find a simple, straight forward howto. This is a walk though on one way to setup a Unix server to automatically backup data to Amazon S3.</p>
<p>I guess I should start out with what Amazon S3 is I suppose&#8230; It&#8217;s a service that you can use to store reams of data, offsite, online, accessible anytime for a decent price. As of this writing the costs are:</p>
<p>Storage: $0.15 per GB-Month of storage used<br />
Data Transfer: $0.10 per GB - all data transfer in</p>
<p>$0.18 per GB - first 10 TB / month data transfer out<br />
$0.16 per GB - next 40 TB / month data transfer out<br />
$0.13 per GB - data transfer out / month over 50 TB</p>
<p>see <a href="http://www.amazon.com/gp/browse.html?node=16427261">Amazon S3</a> for more details. If you use .mac, you can think of it like an iDisk on steroids - or a huge webdav server - but it&#8217;s not just one sever it&#8217;s a farm of servers with huge amounts of redundancy.</p>
<p>My goal here is to make a simple process that will automatically back up some of my web sites - for this example I am using this blog. What this process is going to do is backup the blogs MySQL database. This will just be a basic setup, but you can tweak it or add to it as you see fit.</p>
<h3>The Plan</h3>
<p>We are going to make some scripts that dump the database to a directory, and then sync that directory with the Amazon Service. The sync will work somewhat like rsync so if we want to add other files to the <i>sync tree</i>, it will just be a matter of adding them into sync area. </p>
<h3>Amazon S3 Account</h3>
<p>To start off, you&#8217;ll need an S3 account. You can sign up (and login) <a href="https://aws-portal.amazon.com/gp/aws/developer/account/index.html">here</a>. As you can tell, I like the Amazon Storage Service; however, their web site lacks in the usability department in my opinion. So if you get a bit confused trying to navigate the site, don&#8217;t worry you&#8217;re not alone.</p>
<p>After you go though the account setup process, you&#8217;ll need to get your <i>access key id</i> (like your username) and your <i>secret access key</i> (like your password). To find these hover over the <i>Your Web Services Button</i> button on the top right, and click <i>AWS Access Identifiers</i> (See shot below).</p>
<div class="movie">
<img src="http://robrohan.com/wp-content/uploads/2007/11/safariscreensnapz001.jpg" height="215" width="306" border="1" hspace="4" vspace="4" alt="Safariscreensnapz001" />
</div>
<p>From that screen, on the right you should see your Access Key ID, and be able to show your Secret key. You&#8217;ll need these for the next steps.</p>
<h3>Using S3 From the Desktop</h3>
<p>You&#8217;ll likely want access to your S3 account from your desktop. If only to verify the backups are running. However, once you get this setup, see how well it works, and see how inexpensive it is you&#8217;ll probably start using it for other stuff (as I do :D).</p>
<p>For desktop access there are quite a few choices. I went the cheap route. I use <a href="http://jets3t.s3.amazonaws.com/applications/cockpit.html">cockpit</a>. It&#8217;s a Java based application that lets you use your S3 account from the desktop. It has the feel of an FTP application. <a href="http://www.jungledisk.com/">Jungle Disk</a> is another one I hear is very good. It mounts the S3 account as if it&#8217;s a remote drive (on Mac) or a drive letter on windows (and it also works on Linux) - Jungle Disk is $20 USD.</p>
<p>I am going to use cockpit in this article, but they should all work about the same.</p>
<p>Start up cockpit. On Mac / Linux, run the file:</p>
<pre>
$ /Applications/jets3t-0.5.0/bin/cockpit.sh
</pre>
<p>(on windows it&#8217;s the cockpit.bat file)</p>
<p>When it first starts up there will be several ways to login. You can read up on the choices on the cockpit website, but for now we&#8217;ll use the <i>Direct Login</i> tab. On this tab is where you&#8217;ll put the keys you got from the S3 Account page (see image)</p>
<div class="movie">
<img src="http://robrohan.com/wp-content/uploads/2007/11/org.jets3t.apps.cockpit.cockpitscreensnapz001.jpg" height="268" width="350" border="1" hspace="4" vspace="4" alt="Org.Jets3T.Apps.Cockpit.Cockpitscreensnapz001" />
</div>
<p>S3 uses things called <i>buckets</i>. You&#8217;ll see that term quite a bit when reading about S3. It&#8217;s just a place to store stuff. You can think of it as a drive letter (if you&#8217;re coming from a windows world), or a database table, or whatever. What we need to do here is make a bucket for our backups. I made one called <i>server.robrohan.com</i> - you can name yours anything you like. The shot below is of my <i>server.robrohan.com</i> bucket and a few backups that have already run.</p>
<div class="movie">
<img src="http://robrohan.com/wp-content/uploads/2007/11/org.jets3t.apps.cockpit.cockpitscreensnapz002.jpg" height="252" width="350" border="1" hspace="4" vspace="4" alt="Org.Jets3T.Apps.Cockpit.Cockpitscreensnapz002" />
</div>
<p>To create a new bucket, click the gear icon on the left and select <i>Create new bucket&#8230;</i>. You&#8217;ll need at least one bucket to continue with this article.</p>
<h3>Database Dump</h3>
<p>Now that we have an Amazon S3 account setup, we can access it from our desktop, and we have one bucket ready to hold stuff we can start doing our backups. The first step in doing a backup of our MySQL database is to get a dump of the database. If you already have a process for this you can skip this section. (Note: This is a very simple way to get a backup of a MySQL database there are better, more efficient ways to do this.)</p>
<p>Here is a simple script to dump a database to a bziped file. You an call this script <i>dbbackup.sh</i>, and it is written so it can be called from cron. (This is pretty insecure, so be sure you&#8217;re ok with that or seek help in making it more secure if you need)</p>
<pre>
#!/bin/sh

# This file backs up a database to a backup dir
# that will be synced with s3

if [ $# != "3" ]; then
    echo &#8220;Usage:&#8221;
    echo &#8221;      $0 [database] [username] [password]&#8221;
    exit 1
fi

DATE=`date +%d`
DB=$1
USER=$2
PASS=$3

BACKUPDIR=/home/my_user_name/backups
DBSTORE=${BACKUPDIR}/database

# create database backup
mysqldump &#8211;add-drop-table \
          -h localhost \
          &#8211;user=${USER} \
          &#8211;password=${PASS} \
          ${DB} \
          | bzip2 -c > ${DBSTORE}/${DB}_${DATE}.sql.bz2
</pre>
<p>Note the variable <i>BACKUPDIR</i>. This should be a directory you create somewhere on your server that you want to &#8220;sync&#8221; with the Amazon service. Meaning that anything you put in that directory will get backed up to S3. In this article we are just putting a database backup in the tree, but you could put server code, documents on the server, copies of configuration files, or whatever. The main point being anything in that directory will get copied to S3 (recursively - subdirectories as well).</p>
<p>The script is run like the following:</p>
<pre>
$ ./dbbackup.sh mydatabase myusername mypassword
</pre>
<p>And would produce the file:</p>
<pre>
/home/my_user_name/backups/database/mydatabase_[day_of_month].sql.bz2
</pre>
<p>In this process database backups will only go back a month. The first of the month will overwrite the last month&#8217;s first of the month backup. If you&#8217;d like to archive forever, just change the <i>DATE</i> variable to be <i>`date +%y%m%d`</i>. For my purposes, doing backups everyday is overkill.</p>
<h3>Syncing with S3</h3>
<p>So now the meat of the thing. There are, as in the desktop access, many server side scripts you can use to write data to S3. What this example is going to use is <a href="http://s3sync.net/wiki">s3sync.rb</a>. s3sync is a ruby script that acts much like rsync. There is also an outdated perl version, and even a <a href="http://code.google.com/p/s3-bash/">bash</a> version. You can try others if you&#8217;d like, but for this I&#8217;ll be using the ruby version. That, of course, means you&#8217;ll need ruby on the sever (not rails or anything, just ruby).</p>
<p>(If you don&#8217;t have ruby you can do <i>apt-get install ruby</i>, <i>yum install ruby</i>, <i>ports install ruby</i>, or whatever. It&#8217;s often very easy to get ruby on your *nix box)</p>
<p>Download and untar the s3sync file:</p>
<pre>
$ wget http://s3.amazonaws.com/ServEdge_pub/s3sync/s3sync.tar.gz
$ tar -zxvf s3sync.tar.gz
</pre>
<p>Copy s3config.yml.example to s3config.yml and edit it. Adding in your Amazon S3 keys. It&#8217;s also a good idea to setup the SSL certificate so your transfers to S3 will be https. Checkout the README.txt on how to do that. If you don&#8217;t care about that remove the -s flag from the script we will be writing in a minute.</p>
<p>(Optional: just to be on the safe side, you might want to make the s3config.yml file owned by the cron user that will run the backup, and then <i>chmod 600 s3config.yml</i>. It&#8217;s not a huge amount of security, but it&#8217;s better than nothing.)</p>
<p>Next up is the wrapper script that will run the s3sync ruby script. This is a simple script that just moves into the directory where we installed s3sync, and runs the script (I named it <i>s3backup.sh</i>):</p>
<pre>
#!/bin/sh

# This file syncs a direcotry with a specified
# bucket.
BACKUPDIR=/home/my_user_name/backups
SYNCDIR=/home/my_user_name/s3sync
S3BUCKET=server.robrohan.com
S3PREFIX=x
S3STORE=${S3BUCKET}:${S3PREFIX}

# move to the ruby sync direcotry where the .yml file is
cd ${SYNCDIR}
./s3sync.rb -r -s ${BACKUPDIR} ${S3STORE}
</pre>
<p>Obviously, you&#8217;ll want to change the paths to your install locations (or make them env variables or an include or whatever), the bucket name, and if you are not doing https, remove the -s flag.</p>
<p>Also, the <i>S3PREFIX</i> is any string you want it to be. It&#8217;s kind of like a drive letter inside the bucket - the &#8220;root&#8221; of the bucket so to speak. You can leave it as <i>x</i>.</p>
<p>At this point you should be able to run this script by hand (don&#8217;t forget <i>chmod u+x</i>):</p>
<pre>
$ ./s3backup.sh
</pre>
<p>And your backup directory should &#8220;sync&#8221; with S3. You can verify that by refreshing the view in cockpit (the gear on the right has &#8220;Refresh object listing&#8221;). You can also right click on an object to delete / download it.</p>
<p>I use the sync term is somewhat lightly because the ruby process doesn&#8217;t delete files that have been deleted from the backup directory. It will only add or update the files on S3 - hardly a show stopper though.</p>
<h3>Cron&#8217;ing It</h3>
<p>The last step is wrapping this all up into a cron job so you don&#8217;t have to think about it. The server should just back itself up.</p>
<p>To do this requires one more simple wrapper <i>server_backup.sh</i>:</p>
<pre>
#!/bin/sh

UTILS=/home/my_user_name/path_to_the_previous_scripts

# Create database backups
${UTILS}/dbbackup.sh database1 myusername1 password1
${UTILS}/dbbackup.sh database2 myusername2 password2

# Send the backups to S3
${UTILS}/s3backup.sh
</pre>
<p>Pretty straight forward. It creates backups of any databases we care about, and then calls the script to sync the backup directory with Amazon S3.</p>
<p>The last step is adding it to cron which you should probably already know how to do, but basically it&#8217;s:</p>
<pre>
$ crontab -e
</pre>
<p>and then adding a line something like:</p>
<pre>
0 2 * * 1,3,5 /home/my_user_name/path_to_the_previous_scripts/server_backup.sh
</pre>
<p>Which will do a backup at 2:00am on Monday, Wednesday, and Friday - tweak as needed of course.</p>
<h3>Conclusion</h3>
<p>When I first started coding almost no one out outside of Rome used source control (Rome meaning corporations). Now, if you&#8217;re not using source control on your projects people look at you like you&#8217;re nuts (and for good reason - it just makes sense to use source control). I think backups are following this same pattern. Everyone knows they should backup, but few actually do - and those that do probably don&#8217;t do it offsite or redundantly.</p>
<p>Amazon S3 (and similar services) offer pretty easy to use, low cost, redundant, high bandwidth, always on, offsite storage. If your data == your business, you&#8217;re nuts not to use Amazon S3 or something like it.</p>
<p>I hope this lifted some of the mystery of how to use this kind of service (the exercise did for me :D).</p>
]]></content:encoded>
			<wfw:commentRss>http://robrohan.com/2007/11/03/server-backup-with-amazon-s3-howto/feed/</wfw:commentRss>
		</item>
		<item>
		<title>Linux GUI Programming with Ruby</title>
		<link>http://robrohan.com/2007/05/21/linux-gui-programming-with-ruby/</link>
		<comments>http://robrohan.com/2007/05/21/linux-gui-programming-with-ruby/#comments</comments>
		<pubDate>Tue, 22 May 2007 02:41:00 +0000</pubDate>
		<dc:creator>小罗</dc:creator>
		
		<category><![CDATA[Linux]]></category>

		<category><![CDATA[Ruby]]></category>

		<guid isPermaLink="false">http://robrohan.com/2007/05/21/linux-gui-programming-with-ruby/</guid>
		<description><![CDATA[I went a little crazy this week-end, and decided to look for a way to write GUI programs on Linux in ruby. I was pleasantly surprised by how easy it is.

 
Linux GUI Programming with Ruby

]]></description>
			<content:encoded><![CDATA[<p>I went a little crazy this week-end, and decided to look for a way to write GUI programs on Linux in ruby. I was pleasantly surprised by how easy it is.</p>
<div class="movie">
<object width="425" height="350"><param name="movie" value="http://www.youtube.com/v/PXpwC1o5AcI"></param> <embed src="http://www.youtube.com/v/PXpwC1o5AcI" type="application/x-shockwave-flash" width="425" height="350"></embed></object><br />
<a href="http://www.youtube.com/watch?v=PXpwC1o5AcI">Linux GUI Programming with Ruby</a>
</div>
]]></content:encoded>
			<wfw:commentRss>http://robrohan.com/2007/05/21/linux-gui-programming-with-ruby/feed/</wfw:commentRss>
		</item>
		<item>
		<title>Au revoir IE</title>
		<link>http://robrohan.com/2006/02/07/au-revoir-ie/</link>
		<comments>http://robrohan.com/2006/02/07/au-revoir-ie/#comments</comments>
		<pubDate>Tue, 07 Feb 2006 08:51:00 +0000</pubDate>
		<dc:creator>小罗</dc:creator>
		
		<category><![CDATA[General]]></category>

		<category><![CDATA[Linux]]></category>

		<category><![CDATA[Ruby]]></category>

		<category><![CDATA[Web Apps]]></category>

		<category><![CDATA[Windows]]></category>

		<guid isPermaLink="false">http://robrohan.com/2006/02/07/au-revoir-ie/</guid>
		<description><![CDATA[The French Gendarmes (The police? - jezz the French have a different word for everything) are dropping Internet Explorer, and going with Firefox as their default browser. It&#8217;s reported that that is about 70,000 desktops and the conversion should be done by end of 2006.
Interestingly, one of the main reason for Firefox over IE was [...]]]></description>
			<content:encoded><![CDATA[<p>The French Gendarmes (The police? - jezz the French have a different word for <em>everything</em>) are dropping Internet Explorer, and going with Firefox as their default browser. It&#8217;s reported that that is about 70,000 desktops and the conversion should be done by end of 2006.</p>
<p>Interestingly, one of the main reason for Firefox over IE was &#8220;that it is based on the global internet standard - W3C, and that it works equally well under Microsoft, Mac or Linux.&#8221; <a href="http://www.techtree.com/techtree/jsp/article.jsp?article_id=71174&amp;cat_id=643" target="_blank">Techtree.com</a> reported.  From what I could see during my testing of IE7, it did horrible on the <a href="http://www.webstandards.org/act/acid2/" target="_blank">Acid2 test</a> (FireFox did quite a bit better, and Safari passed) so I could see why they made this choice if standards was their major criteria.</p>
<p>One of the stats in the article that struck me was the following:</p>
<blockquote><p> Firefox reportedly has taken over almost 18 percent of the French internet browser market, and is soon approaching the European average of 20 percent.</p></blockquote>
<p><a href="http://www.techtree.com/techtree/jsp/article.jsp?article_id=71174&amp;cat_id=643" target="_blank"><br />
Techtree.com</a></p>
<p>20%?! When did that happen?</p>
<p>It seems to me Europe and Japan are getting more and more technologically ahead of America - or are at least developing the newest stuff and coming up with the newest trends (see cell phones, ruby, tests scores, robotics etc). Who are you watching and catering to?</p>
<p>Are we supposed to call it FreedomFox now?</p>
]]></content:encoded>
			<wfw:commentRss>http://robrohan.com/2006/02/07/au-revoir-ie/feed/</wfw:commentRss>
		</item>
		<item>
		<title>MWM, 5&#8242;11&#8243;, Ruby Curious</title>
		<link>http://robrohan.com/2005/12/30/mwm-511-ruby-curious/</link>
		<comments>http://robrohan.com/2005/12/30/mwm-511-ruby-curious/#comments</comments>
		<pubDate>Fri, 30 Dec 2005 08:34:00 +0000</pubDate>
		<dc:creator>小罗</dc:creator>
		
		<category><![CDATA[Ruby]]></category>

		<category><![CDATA[Web Apps]]></category>

		<guid isPermaLink="false">http://robrohan.com/2005/12/30/mwm-511-ruby-curious/</guid>
		<description><![CDATA[About a year or so ago, a friend of mine told me I should check out this new scripting language called Ruby (Hi Raul). He was digging on it. This was before Ruby was cool in the US.
At the time I was in the midst of learning Objective-C, but I told him I&#8217;d check it [...]]]></description>
			<content:encoded><![CDATA[<p>About a year or so ago, a friend of mine told me I should check out this new scripting language called Ruby (Hi Raul). He was digging on it. This was before Ruby was cool in the US.</p>
<p>At the time I was in the midst of learning Objective-C, but I told him I&#8217;d check it out when I could. Well, this holiday break I checked it out. Here is a brief run down of my take of Ruby and Ruby on Rails and a bit of software for the Ruby Curious.</p>
<p>Ruby is object oriented, cross platform, and integrates well with other languages (as a scripting language should). The syntax is easy to pick up, and is a mix between Java/C++ and Perl.</p>
<p>Ruby comes built into Mac OS X 10.4 (just type ruby &#8211;version in a terminal to see), and can be used as a replacement, or enhancement to perl, shell scripts, or applescript. Having it built into OS X makes it easy to learn and tinker. You just write and run. Nothing to install&#8230;ahhhh&#8230; it&#8217;s good to be da&#8217; king.</p>
<p>For a quick intro to Ruby try <a href="http://www.math.umd.edu/~dcarrera/ruby/0.3" target="_blank">this</a>. And if you don&#8217;t have a Mac and don&#8217;t want to install Ruby you can play with this online <a href="http://tryruby.hobix.com" target="_blank">interactive shell</a>.</p>
<p>Now, Ruby on Rails, which I am sure you&#8217;ve heard about by now, is a framework for writing web applications using Ruby as the scripting language. Much like Coldfusion+MachII, ASP+Something, (JSP+Beans)+Struts, etc, but with a strong focus on Model / View / Controller. It&#8217;s important to note that the two are separate - I like Ruby, but I don&#8217;t think I like Ruby on Rails (I&#8217;ve just started playing though).</p>
<p>I like the concept, but the thing about Ruby on Rails I don&#8217;t like very much is the fact that you just run a bunch of scripts to generate a bunch of code. I guess I am old because I hate it when code &#8220;just appears&#8221;. They say Ruby on Rails is great because you don&#8217;t need any config files, but what you get instead is a bunch of scripts that generate code that the noob has no idea where it goes or what it&#8217;s really doing.</p>
<p>Script kiddie web development? If it is, that worries me a bit.</p>
<p>I am all for saving time, and it could be that I just don&#8217;t fully understand it yet, but I&#8217;ve been burned by WSAD generated bean code too many times to just buy into this&#8230;</p>
<p>For a perfect example check out the &#8220;<a href="http://media.rubyonrails.org/video/rails_take2_with_sound.mov" target="_blank">create a blog in 15 minutes</a>&#8221; movie. He keeps saying &#8220;Notice I am not writing any code&#8221;, but tell me - what the hell are all those scripts he keeps running generating? Hopefully he knows what they are doing, and I hope the Rails developers do too.</p>
<p>In my opinion scripting is good to save time, but only *if you know what the scripts are saving you from*. Blindly running scripts to create code you don&#8217;t edit or understand seems like a recipe for disaster to me.</p>
<p>Slight rant aside, I&#8217;ve only been playing with Rails for a couple days and the jury is still out for me until I really understand what is going on. I do encourage you to check out Ruby and Ruby on Rails despite my vocal bias back there.</p>
<p>To try out Rails:<br />
Instead of setting up a whole development server, setting up a database, and messing with Apache to get the whole Ruby on Rails environment going check out <a href="http://locomotive.sf.net" target="_blank">locomotive</a> . It&#8217;s a full Ruby on Rails development server complete with a copy of Ruby, an integrated web server, and an sql server all on one dmg (Free). It wont mess with your system settings, Apache, or your local OS copy of Ruby. Plus, if you don&#8217;t like it you can just delete the app file - no harm no foul.</p>
<p>To edit Ruby (+Rails) code you can use <a href="http://www.robrohan.com/projects/afae" target="_blank">Afae</a>($), or <a href="http://macromates.com" target="_blank">Textmate</a>($) which integrates with locomotive (and is the editor used in the above movies), or you can try a <a href="http://www.radrails.org" target="_blank">Ruby on Rails Eclipse plugin</a> (Free), or BBEdit, or whatever.</p>
<p>In my opinion, there is something to the hype. Ruby is simple, fun, easy to learn, and powerful - I don&#8217;t know about Rails yet though.</p>
]]></content:encoded>
			<wfw:commentRss>http://robrohan.com/2005/12/30/mwm-511-ruby-curious/feed/</wfw:commentRss>
<enclosure url="http://media.rubyonrails.org/video/rails_take2_with_sound.mov" length="54364199" type="video/quicktime" />
		</item>
	</channel>
</rss>
