<?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"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Sandeep Ghael</title>
	<atom:link href="http://sandeepghael.com/feed/" rel="self" type="application/rss+xml" />
	<link>http://sandeepghael.com</link>
	<description>acts_as_blog</description>
	<lastBuildDate>Wed, 20 Apr 2011 14:48:41 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.2</generator>
		<item>
		<title>Nexus One portable hotspot speed &#8211; Android 2.2 Froyo + T-Mobile 3G</title>
		<link>http://sandeepghael.com/2010/05/nexus-one-portable-hotspot-speed-android-2-2-froyo-t-mobile-3g/</link>
		<comments>http://sandeepghael.com/2010/05/nexus-one-portable-hotspot-speed-android-2-2-froyo-t-mobile-3g/#comments</comments>
		<pubDate>Sun, 23 May 2010 23:11:53 +0000</pubDate>
		<dc:creator>sghael</dc:creator>
				<category><![CDATA[Uncategorized]]></category>

		<guid isPermaLink="false">http://sandeepghael.com/?p=415</guid>
		<description><![CDATA[Thru speed test of Nexus One Android (Froyo 2.2) &#8211; Wireless Hotspot tethering on T-Mobile 3G. Macbook Pro using Nexus One + Android 2.2 (Froyo) + T-mobile 3G: Macbook Pro using ATT DSL: [ author makes no claim on scientific nature of this testing ]]]></description>
			<content:encoded><![CDATA[<p>Thru speed test of Nexus One Android (Froyo 2.2) &#8211; Wireless Hotspot tethering on T-Mobile 3G.</p>
<p><strong>Macbook Pro using Nexus One + Android 2.2 (Froyo) + T-mobile 3G:</strong></p>
<p><a href="http://sandeepghael.com/wp-content/uploads/2010/05/Tmobile-3g.png"><img class="size-full wp-image-416 alignnone" title="Tmobile 3g" src="http://sandeepghael.com/wp-content/uploads/2010/05/Tmobile-3g.png" alt="" width="313" height="183" /></a></p>
<p><strong>Macbook Pro using ATT DSL:</strong></p>
<p><a href="http://sandeepghael.com/wp-content/uploads/2010/05/DSL.png"><img class="size-full wp-image-418 alignnone" title="DSL" src="http://sandeepghael.com/wp-content/uploads/2010/05/DSL.png" alt="" width="307" height="177" /></a></p>
<p>[ author makes no claim on scientific nature of this testing ]</p>
]]></content:encoded>
			<wfw:commentRss>http://sandeepghael.com/2010/05/nexus-one-portable-hotspot-speed-android-2-2-froyo-t-mobile-3g/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>the &#8220;Google Job Experiment&#8221;</title>
		<link>http://sandeepghael.com/2010/05/the-google-job-experiment/</link>
		<comments>http://sandeepghael.com/2010/05/the-google-job-experiment/#comments</comments>
		<pubDate>Tue, 18 May 2010 01:26:48 +0000</pubDate>
		<dc:creator>sghael</dc:creator>
				<category><![CDATA[Uncategorized]]></category>

		<guid isPermaLink="false">http://sandeepghael.com/?p=406</guid>
		<description><![CDATA[A dash of social engineering + Google ad words + $6. Brilliant.]]></description>
			<content:encoded><![CDATA[<p>A dash of social engineering + Google ad words + $6.   Brilliant.  </p>
<p><object width="640" height="385"><param name="movie" value="http://www.youtube.com/v/7FRwCs99DWg&#038;hl=en_US&#038;fs=1&#038;"></param><param name="allowFullScreen" value="true"></param><param name="allowscriptaccess" value="always"></param><embed src="http://www.youtube.com/v/7FRwCs99DWg&#038;hl=en_US&#038;fs=1&#038;" type="application/x-shockwave-flash" allowscriptaccess="always" allowfullscreen="true" width="640" height="385"></embed></object></p>
]]></content:encoded>
			<wfw:commentRss>http://sandeepghael.com/2010/05/the-google-job-experiment/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>TWiST 00 &#8211; Jason Clacanis on online marketing tactics</title>
		<link>http://sandeepghael.com/2010/05/397/</link>
		<comments>http://sandeepghael.com/2010/05/397/#comments</comments>
		<pubDate>Fri, 14 May 2010 16:51:17 +0000</pubDate>
		<dc:creator>sghael</dc:creator>
				<category><![CDATA[Uncategorized]]></category>

		<guid isPermaLink="false">http://sandeepghael.com/?p=397</guid>
		<description><![CDATA[Some pretty solid insight into SEO, SEM, email marketing and general online hustling tactics: http://thisweekin.com/thisweekin-startups/twist-interview-with-100-things/ As always, great story telling from Calacanis.]]></description>
			<content:encoded><![CDATA[<p>Some pretty solid insight into SEO, SEM, email marketing and general online hustling tactics:</p>
<p>http://thisweekin.com/thisweekin-startups/twist-interview-with-100-things/</p>
<p>As always, great story telling from Calacanis.</p>
<p><object width="640" height="385"><param name="movie" value="http://www.youtube.com/v/yIXQ5qc_uXc&#038;hl=en_US&#038;fs=1&#038;"></param><param name="allowFullScreen" value="true"></param><param name="allowscriptaccess" value="always"></param><embed src="http://www.youtube.com/v/yIXQ5qc_uXc&#038;hl=en_US&#038;fs=1&#038;" type="application/x-shockwave-flash" allowscriptaccess="always" allowfullscreen="true" width="640" height="385"></embed></object></p>
]]></content:encoded>
			<wfw:commentRss>http://sandeepghael.com/2010/05/397/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Simple Red5 start/stop script (Mac OSX ready)</title>
		<link>http://sandeepghael.com/2010/01/simple-red5-startstop-script-mac-osx-ready/</link>
		<comments>http://sandeepghael.com/2010/01/simple-red5-startstop-script-mac-osx-ready/#comments</comments>
		<pubDate>Wed, 27 Jan 2010 17:49:05 +0000</pubDate>
		<dc:creator>sghael</dc:creator>
				<category><![CDATA[MacOSX]]></category>
		<category><![CDATA[Red5]]></category>

		<guid isPermaLink="false">http://sandeepghael.com/?p=362</guid>
		<description><![CDATA[Notes: Make sure to create a writeable log file for red5 at &#8220;/var/log/red5.log&#8221;. Usage of the script is straightforward: &#8220;/etc/init.d/red5 start&#8221;. [cci lang="bash"] #! /bin/sh # Author: Sandeep Ghael # put these contents at: /etc/init.d/red5 # change red5 directory path below as necessary RED5_DIR=/usr/share/red5 start() { echo &#8220;Starting Red5 Service&#8221; sudo su root -c &#8220;cd [...]]]></description>
			<content:encoded><![CDATA[<p>Notes:  Make sure to create a writeable log file for red5 at &#8220;/var/log/red5.log&#8221;.  Usage of the script is straightforward: &#8220;/etc/init.d/red5 start&#8221;.</p>
<p>[cci lang="bash"]<br />
#! /bin/sh<br />
# Author: Sandeep Ghael<br />
# put these contents at: /etc/init.d/red5<br />
# change red5 directory path below as necessary<br />
RED5_DIR=/usr/share/red5</p>
<p>start()<br />
{<br />
   echo &#8220;Starting Red5 Service&#8221;<br />
   sudo su root -c &#8220;cd $RED5_DIR; ./red5.sh > /var/log/red5.log &#038;&#8221;<br />
   return<br />
}</p>
<p>stop()<br />
{<br />
   echo &#8220;Shutting down red5&#8243;<br />
   sudo su root -c &#8216;killall red5 java&#8217;<br />
   return<br />
}</p>
<p>case &#8220;$1&#8243; in<br />
    start)<br />
      start<br />
      ;;<br />
    stop)<br />
      stop<br />
      ;;<br />
    restart)<br />
      stop<br />
      start<br />
      ;;<br />
    *)<br />
      echo &#8220;Usage: {start|stop|restart}&#8221;<br />
      exit 1<br />
      ;;<br />
esac<br />
exit $?<br />
[/cci]</p>
]]></content:encoded>
			<wfw:commentRss>http://sandeepghael.com/2010/01/simple-red5-startstop-script-mac-osx-ready/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Ruby Server monitoring scripts w/ email alerts</title>
		<link>http://sandeepghael.com/2009/10/ruby-server-monitoring-scripts-w-email-alerts/</link>
		<comments>http://sandeepghael.com/2009/10/ruby-server-monitoring-scripts-w-email-alerts/#comments</comments>
		<pubDate>Tue, 27 Oct 2009 19:20:16 +0000</pubDate>
		<dc:creator>sghael</dc:creator>
				<category><![CDATA[Rails]]></category>
		<category><![CDATA[Ubuntu]]></category>

		<guid isPermaLink="false">http://sandeepghael.com/?p=324</guid>
		<description><![CDATA[While working on several rails projects, I&#8217;ve found it useful to write a couple of scripts to monitor real-time server health. Basically, I&#8217;d like to automatically be notified by email if anything peculiar is occurring on the server. Two pretty obvious things to alert on are low disk space and high average load. While there [...]]]></description>
			<content:encoded><![CDATA[<p>While working on several rails projects, I&#8217;ve found it useful to write a couple of scripts to monitor real-time server health.  Basically, I&#8217;d like to automatically be notified by email if anything peculiar is occurring on the server.  Two pretty obvious things to alert on are <strong>low disk space</strong> and <strong>high average load</strong>.  While there are great tools out there like Munin/Nagios that will give you detailed instrumentation for your server, I just needed something lightweight that I could periodically cron.  These scripts are designed for use on Ubuntu 9.04.  YMMV.</p>
<h2>disk_usage.rb</h2>
<p>This script will email foo@example.com when the disk space left on either sda1 and sda2 falls below 5GB.  The threshold is just a variable you can change.  You can also add to or remove from the list of sda devices you want to monitor using the disks array.</p>
<p>[cci lang="ruby"]<br />
#!/usr/bin/env ruby</p>
<p>require &#8216;rubygems&#8217;<br />
require &#8216;send_gmail&#8217;</p>
<p>TO_EMAIL = &#8216;foo@example.com&#8217;</p>
<p>filesys = `df -h`</p>
<p>disks = [1,2]</p>
<p>disks.each do |i|<br />
  r = filesys[/sda#{i}\s+[\d\.]+[MG]\s+[\d\.]+[MG]\s+([\d\.]+[MG])/,1]<br />
  message = &#8220;Low disk space on /sda#{i}: only #{r.to_i}GB remaining&#8221;<br />
  hsh={:to=>TO_EMAIL, :subject=>&#8217;Low disk space warning!&#8217;, :body=>message}<br />
  SendGMail.send_gmail(hsh) if r.to_i < 5 or r[/M$/]<br />
  puts message if r.to_i < 5 or r[/M$/]<br />
end<br />
[/cci]</p>
<h2>load_check.rb</h2>
<p>This script will use the uptime command to get the avarage load.  I&#8217;m just using the 1min and 5min averages.  If the 1min average load is over 9 or the 5min average load is over 4, bar@example.com will get an email.</p>
<p>[cci lang="ruby"]<br />
#!/usr/bin/env ruby</p>
<p>require &#8216;rubygems&#8217;<br />
require &#8216;send_gmail&#8217;</p>
<p>TO_EMAIL = &#8216;bar@example.com&#8217;</p>
<p>uptime = `uptime`</p>
<p>load_1min = uptime.split(&#8221; &#8220;)[8].chop()<br />
load_5min = uptime.split(&#8221; &#8220;)[9].chop()</p>
<p>message = &#8220;Load on server is 1min/5min avg: #{load_1min} / #{load_5min} &#8221;</p>
<p>hsh={:to=>TO_EMAIL, :subject=>&#8217;Load warning!&#8217;, :body=>message}<br />
SendGMail.send_gmail(hsh) if (load_1min.to_i > 9) or (load_5min.to_i > 4)<br />
[/cci]</p>
<p>You may have noticed an included module &#8220;send_gmail&#8221; and a call to &#8220;SendGMail&#8221; in the previous scripts.  Since I use Gmail for all my outbound emails, the underlying alert system needs a way to make calls to the Gmail servers.  Problem solved using this nifty little Ruby Gmailer script I found on at <a href="http://codingfrenzy.alexpmay.com/2007/12/sending-gmail-from-standalone-ruby.html">http://codingfrenzy.alexpmay.com/2007/12/sending-gmail-from-standalone-ruby.html</a>.  I did make a few modifications to the script, so I&#8217;m including it here.  You&#8217;ll want to set your gmail account/domain info if you do use this mailer.</p>
<h2>send_gmail.rb</h2>
<p>[cci lang="ruby"]<br />
#!/usr/bin/env ruby</p>
<p>require &#8216;rubygems&#8217;</p>
<p>gem &#8216;actionmailer&#8217;<br />
require &#8216;action_mailer&#8217;<br />
require &#8216;openssl&#8217;<br />
require &#8216;net/smtp&#8217;</p>
<p>module SendGMail</p>
<p>@user_name=&#8217;someone@example.com&#8217;<br />
@domain=&#8217;example.com&#8217;<br />
@password=&#8217;password&#8217;</p>
<p>def SendGMail.send_gmail(hsh)</p>
<p>raw_attachments=hsh.fetch(:raw_attachements, [])<br />
if hsh.has_key?(:raw_attachment)<br />
 raw_attachments.push(hsh[:raw_attachment])<br />
end</p>
<p>mail=TMail::Mail.new<br />
mail.to=hsh[:to]<br />
mail.date=Time.now<br />
mail.from=@user_name<br />
mail.subject=hsh[:subject]</p>
<p>main=mail<br />
main=TMail::Mail.new<br />
main.body = hsh[:body]<br />
main.set_content_type(&#8216;text/plain&#8217;, nil, &#8216;charset&#8217;=>&#8217;utf-8&#8242;)<br />
mail.parts.push(main)</p>
<p>for raw_attachment in raw_attachments<br />
 part = TMail::Mail.new<br />
 transfer_encoding=raw_attachment[:transfer_encoding]<br />
 body=raw_attachment[:body]<br />
 case (transfer_encoding || &#8220;&#8221;).downcase<br />
   when &#8220;base64&#8243; then<br />
     part.body = TMail::Base64.folding_encode(body)<br />
   when &#8220;quoted-printable&#8221;<br />
     part.body = [body].pack(&#8220;M*&#8221;)<br />
   else<br />
     part.body = body<br />
 end</p>
<p> part.transfer_encoding = transfer_encoding<br />
 part.set_content_type(raw_attachment[:mime_type], nil, &#8216;name&#8217; => raw_attachment[:filename])<br />
 part.set_content_disposition(&#8220;attachment&#8221;, &#8220;filename&#8221;=>raw_attachment[:filename])<br />
 mail.parts.push(part)<br />
end</p>
<p>mail.set_content_type(&#8216;multipart&#8217;, &#8216;mixed&#8217;)<br />
ActionMailer::Base.deliver(mail)</p>
<p>end</p>
<p>ActionMailer::Base.smtp_settings = {<br />
:address => &#8216;smtp.gmail.com&#8217;,<br />
:domain => @domain,<br />
:authentication => :plain,<br />
:port => 587,<br />
:user_name => @user_name,<br />
:password => @password<br />
}</p>
<p>Net::SMTP.class_eval do<br />
private<br />
def do_start(helodomain, user, secret, authtype)<br />
 raise IOError, &#8216;SMTP session already started&#8217; if @started<br />
 check_auth_args user, secret if user or secret</p>
<p> sock = timeout(@open_timeout) { TCPSocket.open(@address, @port) }<br />
 @socket = Net::InternetMessageIO.new(sock)<br />
 @socket.read_timeout = 60 #@read_timeout<br />
 @socket.debug_output = STDERR #@debug_output</p>
<p> check_response(critical { recv_response() })<br />
 do_helo(helodomain)</p>
<p> raise &#8216;openssl library not installed&#8217; unless defined?(OpenSSL)<br />
 starttls<br />
 ssl = OpenSSL::SSL::SSLSocket.new(sock)<br />
 ssl.sync_close = true<br />
 ssl.connect<br />
 @socket = Net::InternetMessageIO.new(ssl)<br />
 @socket.read_timeout = 60 #@read_timeout<br />
 @socket.debug_output = STDERR #@debug_output<br />
 do_helo(helodomain)</p>
<p> authenticate user, secret, authtype if user<br />
 @started = true<br />
ensure<br />
 unless @started<br />
   # authentication failed, cancel connection.<br />
     @socket.close if not @started and @socket and not @socket.closed?<br />
   @socket = nil<br />
 end<br />
end</p>
<p>def do_helo(helodomain)<br />
  begin<br />
   if @esmtp<br />
     ehlo helodomain<br />
   else<br />
     helo helodomain<br />
   end<br />
 rescue Net::ProtocolError<br />
   if @esmtp<br />
     @esmtp = false<br />
     @error_occured = false<br />
     retry<br />
   end<br />
   raise<br />
 end<br />
end</p>
<p>def starttls<br />
 getok(&#8216;STARTTLS&#8217;)<br />
end</p>
<p>def quit<br />
 begin<br />
   getok(&#8216;QUIT&#8217;)<br />
 rescue EOFError, OpenSSL::SSL::SSLError<br />
 end<br />
end<br />
end<br />
end</p>
<p>[/cci]</p>
]]></content:encoded>
			<wfw:commentRss>http://sandeepghael.com/2009/10/ruby-server-monitoring-scripts-w-email-alerts/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Ultimate Snow Leopard Web Development machine (part 3) &#8211; Databases</title>
		<link>http://sandeepghael.com/2009/10/ultimate-snow-leopard-web-development-machine-part-3-databases/</link>
		<comments>http://sandeepghael.com/2009/10/ultimate-snow-leopard-web-development-machine-part-3-databases/#comments</comments>
		<pubDate>Tue, 27 Oct 2009 18:37:28 +0000</pubDate>
		<dc:creator>sghael</dc:creator>
				<category><![CDATA[Databases]]></category>
		<category><![CDATA[MacOSX]]></category>
		<category><![CDATA[64bit]]></category>
		<category><![CDATA[Mac OS X]]></category>
		<category><![CDATA[MySQL]]></category>
		<category><![CDATA[Postgres]]></category>
		<category><![CDATA[Snow Leopard]]></category>

		<guid isPermaLink="false">http://sandeepghael.com/?p=295</guid>
		<description><![CDATA[Depending on the webapp project I&#8217;m working on, I flip back and forth between MySQL and PostgresSQL, generally using Postgres for anything that needs spatial or mapping related functionality. One pain point I encountered with Snow Leopard is getting everything compiled for true 64bit, and then getting the appropriate bindings for MySQL or Postgres to [...]]]></description>
			<content:encoded><![CDATA[<p>Depending on the webapp project I&#8217;m working on, I flip back and forth between MySQL and PostgresSQL, generally using Postgres for anything that needs spatial or mapping related functionality.</p>
<p>One pain point I encountered with Snow Leopard is getting everything compiled for true 64bit, and then getting the appropriate bindings for MySQL or Postgres to work with frameworks like Rails (Ruby) or Django (Python).  I work with both, so by the end of the post you&#8217;ll be prepared to use both DB servers for either framework.</p>
<h1>Step 1: Install MySQL from source</h1>
<p>We refer here, again to the excellent tutorial from HiveLogic:</p>
<p><a href="http://hivelogic.com/articles/compiling-mysql-on-snow-leopard/"> http://hivelogic.com/articles/compiling-mysql-on-snow-leopard/</a></p>
<p>After completing the install, set up your root password:</p>
<p>[cci lang="bash"]</p>
<p>$ mysqladmin -u root password NEWPASSWORD</p>
<p>[/cci]</p>
<h1>Step 2: Install PostgreSQL from source</h1>
<p>As for Postgres, I found these instructions found on InvisionPower as a good basis to start my install from. </p>
<p><a href="http://community.invisionpower.com/topic/292849-installing-postgresql-server-on-mac-os-x-snow-leopard/">http://community.invisionpower.com/topic/292849-installing-postgresql-server-on-mac-os-x-snow-leopard/</a></p>
<p>Couple of note regarding Postgres.</p>
<p>1) the instructions in the link above seem to provide a dead link for the Postgres source.  You can <strong>try this curl command instead of theirs</strong>:</p>
<p>[cci lang="bash"]<br />
$ curl http://ftp9.us.postgresql.org/pub/mirrors/postgresql/source/v8.4.1/postgresql-8.4.1.tar.gz -O<br />
[/cci]</p>
<p>2) <strong>Make sure to compile as a 64 bit application</strong>, using the &#8220;ARCH=x86_64&#8243; and other flags specified during your &#8220;make&#8221; step.</p>
<p>3) when creating and setting user/group info, I found an error in the 8th command line instructions.  The original command references a group &#8220;_postgres&#8221; before it has been created.  Do this instead:</p>
<p>Enough changes were required to make things compile and work on my new system, that I&#8217;ll simply transcribe my exact steps here: </p>
<p>[cci lang="bash"]<br />
$ sudo mkdir /usr/local/src<br />
$ cd /usr/local/src</p>
<p>$ sudo su</p>
<p>$ curl http://ftp9.us.postgresql.org/pub/mirrors/postgresql/source/v8.4.1/postgresql-8.4.1.tar.gz -O<br />
$ tar -zvxf postgresql-8.4.1.tar.gz<br />
$ rm postgresql-8.4.1.tar.gz<br />
$ cd postgresql-8.4.1</p>
<p>$ ./configure &#8211;prefix=/usr/local/postgresql-8.4.1</p>
<p>$ ARCH=x86_64 CFLAGS=&#8221;-arch x86_64&#8243; LDFLAGS=&#8221;-arch x86_64&#8243; make<br />
$ make install</p>
<p>$ ln -s /usr/local/postgresql-8.4.1 /usr/local/pgsql<br />
$ echo &#8220;PATH=/usr/local/pgsql/bin:$PATH&#8221; >> ~/.profile<br />
$ source ~/.profile<br />
$ mkdir /usr/local/pgsql/data/</p>
<p>$ dscl . list /Users UniqueID<br />
[/cci]</p>
<p>I use &#8221; dscl . list /Users UniqueID | grep <##>&#8221; to find the first available number past 75.  For me it was 80.</p>
<p>[cci lang="bash"]<br />
$ dscl localhost create /Local/Default/Users/_postgres<br />
$ dscl localhost create /Local/Default/Users/_postgres PrimaryGroupID 0<br />
$ dscl localhost create /Local/Default/Users/_postgres UniqueID 80<br />
$ dscl localhost create /Local/Default/Users/_postgres UserShell /bin/bash<br />
$ dscl localhost passwd /Local/Default/Users/_postgres<br />
$ dscl localhost create /Local/Default/Users/_postgres NFSHomeDirectory /var/home/_postgres<br />
$ mkdir -p /var/home/_postgres<br />
$ chown -Rf _postgres /var/home/_postgres<br />
$ dscl localhost create /Local/Default/Groups/_postgres<br />
$ dscl localhost create /Local/Default/Groups/_postgres UniqueID 80<br />
$ dscl localhost append /Local/Default/Groups/_postgres GroupMembership _postgres<br />
$ chown -Rf _postgres:_postgres /var/home/_postgres</p>
<p>$ defaults write /Library/Preferences/com.apple.loginwindow.plist Hide500Users -bool TRUE<br />
$ chown -R _postgres /usr/local/postgresql-8.4.1/<br />
[/cci]</p>
<p>I did not exit su at this point, as suggested in the original InvisionPower article.  I needed to be su to change to user &#8220;_postgres&#8221;.</p>
<p>[cci lang="bash"]<br />
$ su &#8211; _postgres<br />
$ /usr/local/pgsql/bin/initdb -E UTF8 -D /usr/local/pgsql/data/<br />
$ /usr/local/pgsql/bin/pg_ctl -D /usr/local/pgsql/data/ -l /usr/local/pgsql/data/postgresql.log start<br />
[/cci]</p>
<p>Now test creating a new DB:</p>
<p>[cci lang="bash"]<br />
$ /usr/local/pgsql/bin/createdb test<br />
$ /usr/local/pgsql/bin/psql test<br />
[/cci]</p>
<p>Type \q to quit.</p>
<p>[cci lang="bash"]<br />
$ exit<br />
$ sudo su<br />
$ rm -rf /var/home<br />
$ dscl localhost delete /Local/Default/Users/_postgres NFSHomeDirectory<br />
$ dscl localhost passwd /Local/Default/Users/_postgres<br />
$ exit<br />
[/cci]</p>
<p>At this point your server is installed and tested.  The InvisionPower post provides a great script that you can use to start/stop/restart the server from the command line.  See the bottom of <a href="http://community.invisionpower.com/topic/292849-installing-postgresql-server-on-mac-os-x-snow-leopard/">their article.</a></p>
<p><em></em></p>
]]></content:encoded>
			<wfw:commentRss>http://sandeepghael.com/2009/10/ultimate-snow-leopard-web-development-machine-part-3-databases/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>Ultimate Snow Leopard Web Development machine (part 2) &#8211; Command Line tweaks</title>
		<link>http://sandeepghael.com/2009/10/ultimate-snow-leopard-web-development-machine-part-2-command-line-tweaks/</link>
		<comments>http://sandeepghael.com/2009/10/ultimate-snow-leopard-web-development-machine-part-2-command-line-tweaks/#comments</comments>
		<pubDate>Tue, 13 Oct 2009 22:50:04 +0000</pubDate>
		<dc:creator>sghael</dc:creator>
				<category><![CDATA[MacOSX]]></category>

		<guid isPermaLink="false">http://sandeepghael.com/?p=256</guid>
		<description><![CDATA[In part one of this series, we covered the very basics of doing a clean install of Snow Leopard, including the XCode SDK packages, and installing version control tool Git. Today we&#8217;ll cover setting up your CLI (command line interface). Tweak the CLI If you have any hair on your chest (apologies to my female [...]]]></description>
			<content:encoded><![CDATA[<p>In part one of this series, we covered the very basics of doing a clean install of Snow Leopard, including the XCode SDK packages, and installing version control tool Git.  Today we&#8217;ll cover setting up your CLI (command line interface).</p>
<h1>Tweak the CLI</h1>
<p>If you have any hair on your chest (apologies to my female readers), you&#8217;re going to be using the command line a lot.  Let&#8217;s make the CLI more friendly for daily use.  I used this excellent thread from StackOverflow to compose my .profile file:  <a href="http://stackoverflow.com/questions/3746/whats-in-your-bashrc">http://stackoverflow.com/questions/3746/whats-in-your-bashrc</a>.</p>
<p>Things are a bit confusing in MacOS X world, since there is a .profile file in your home directory, but there is also a bashrc file in /private/etc.  As far as I can tell, best practice is to put user specific stuff in .profile, and put global stuff in /private/etc/bashrc.</p>
<p>To edit bashrc, use the sudo command and enter your password.</p>
<p>[cci lang="bash"]</p>
<p>$ sudo nano /private/etc/bashrc</p>
<p>[/cci]</p>
<p>Since I want colorized prompt for all users, my /private/etc/bashrc file looks like</p>
<p>[cci lang="bash"]<br />
# System-wide .bashrc file for interactive bash(1) shells.<br />
if [ -z "$PS1" ]; then<br />
return<br />
fi</p>
<p>WHITE=&#8221;\[\033[1;37m\]&#8221;<br />
GREEN=&#8221;\[\033[0;32m\]&#8221;<br />
CYAN=&#8221;\[\033[0;36m\]&#8221;<br />
GRAY=&#8221;\[\033[0;37m\]&#8221;<br />
BLUE=&#8221;\[\033[0;34m\]&#8221;<br />
BLACK=&#8221;\[\033[0;1m\]&#8221;<br />
RESET=&#8217;\[\033[00m\]&#8216;</p>
<p>export PS1=&#8221;${GREEN}\u${CYAN}@${BLUE}\h:${CYAN}\w${GREEN} &gt;$ ${RESET}&#8221;</p>
<p>export CLICOLOR=1<br />
export LSCOLORS=ExFxCxDxBxegedabagacad</p>
<p># Make bash check its window size after a process completes<br />
shopt -s checkwinsize<br />
[/cci]</p>
<p>The next time you open a command prompt things will be more readable.</p>
<p>Now lets edit your user specific .profile file :</p>
<p>[cci lang="bash"]<br />
$ nano ~/.profile<br />
[/cci]</p>
<p>and make the contents as such:</p>
<p>[cci lang="bash"]<br />
export HISTCONTROL=erasedups<br />
export HISTSIZE=10000</p>
<p>bind &#8216;set match-hidden-files off&#8217;</p>
<p>alias cds=&#8221;cd;clear&#8221;<br />
alias cd..=&#8221;cd ..&#8221;<br />
alias ..=&#8221;cd ..&#8221;<br />
alias &#8230;=&#8221;cd ../..&#8221;<br />
alias &#8230;.=&#8221;cd ../../..&#8221;<br />
alias ll=&#8221;ls -al&#8221;</p>
<p>export PATH=&#8221;/usr/local/bin:/usr/local/sbin:/usr/local/mysql/bin:$PATH&#8221;<br />
[/cci]</p>
<p>This sets some bash history parameters, and my personal aliases for commonly used commands.  YMMV.  We&#8217;ll continue to tweak the .profile file as we continue, but for now let&#8217;s proceed to installing databases (Next Post)</p>
]]></content:encoded>
			<wfw:commentRss>http://sandeepghael.com/2009/10/ultimate-snow-leopard-web-development-machine-part-2-command-line-tweaks/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Ultimate Snow Leopard Web Development machine (part 1)</title>
		<link>http://sandeepghael.com/2009/10/ultimate-snow-leopard-web-development-machine-part-1/</link>
		<comments>http://sandeepghael.com/2009/10/ultimate-snow-leopard-web-development-machine-part-1/#comments</comments>
		<pubDate>Mon, 12 Oct 2009 20:46:27 +0000</pubDate>
		<dc:creator>sghael</dc:creator>
				<category><![CDATA[MacOSX]]></category>
		<category><![CDATA[GIT]]></category>
		<category><![CDATA[Snow Leopard]]></category>
		<category><![CDATA[Subversion]]></category>

		<guid isPermaLink="false">http://sandeepghael.com/?p=152</guid>
		<description><![CDATA[Like many people, I recently upgraded my MacBook to Snow Leopard. While Mac OS X updates are mostly innocuous, Snow Leopard was a bit trickier.  Moving to the 64bit ready OS meant that some key technologies like Ruby and Python, that I use extensively for my work, didn&#8217;t quite work right after the upgrade cycle.  [...]]]></description>
			<content:encoded><![CDATA[<p><img class="alignleft size-full wp-image-210" title="SL1-photo1" src="http://sandeepghael.com/wp-content/uploads/2009/10/SL1-photo1.jpg" alt="SL1-photo1" width="130" height="175" />Like many people, I recently upgraded my MacBook to Snow Leopard.  While Mac OS X updates are mostly innocuous, Snow Leopard was a bit trickier.   Moving to the 64bit ready OS meant that some key technologies like Ruby and Python, that I use extensively for my work, didn&#8217;t quite work right after the upgrade cycle.  With Snow Leopard,  I found it easier to back up all my info using <a href="http://www.shirt-pocket.com/SuperDuper/SuperDuperDescription.html" target="_blank">SuperDuper</a>,  and perform a clean install (not on top of previous OS) .  Then I compiled and installing many of my tools from scratch.   Several tricky issues, especially related to Python/MySQL bindings, magically started working after this clean install.   It took a few days of Google research and trial/error, but after a few days of annoying hacking at the new OS, I&#8217;ve converged on an &#8220;ultimate&#8221; Snow Leopard web-dev setup.</p>
<p>In the next few post, I&#8217;ll be documenting the steps I took on my box.  FYI, my box is an Intel based (Core 2 Duo) White MacBook 13&#8243;, 2Ghz with 4GB of Ram.  The screen shots are from the same steps done on an iMac with similar specs.</p>
<h1>Step 1:  Backup all data</h1>
<p>Any upgrade process is fraught with catastrophic risk, so please back up all your data.  You should already be using something like Time Machine, so make sure its has done a recent backup of your critical data directories.  For something like an upgrade, I prefer to also use  a tool like &#8220;SuperDuper!&#8221; which makes a clone (entire disk snapshot) to another drive.  This assumes you have an extra drive laying around, but drives are cheap and your work is not.  So take the extra precaution and feel safe knowing you can always just pop in your cloned disk and it&#8217;s as if nothing happened.</p>
<h1>Step 2:  Install Snow Leopard</h1>
<p>Well, duh!  But one key thing to note when doing either a clean, or non-clean install of Snow Leopard is to make sure you are installing all the necessary SDK packages that will allow you to compile other packages like Python or MySQL successfully for 64bit.  We&#8217;ll get to that in a bit.</p>
<p>First the routine stuff:  Pop in the Snow Leopard disk.  Hold down the &#8220;option&#8221; key during the boot to have your Mac ask you what disk to install from.  (see image)</p>
<p>Choose the Mac OS X Install disk.  As mentioned, we are doing a clean install, so we&#8217;ll first format the drive clean (you did do Step 1, right?).  Choose &#8220;Disk Utility&#8221; before the install starts.  Choose your drive, and click on the &#8220;Erase&#8221; tab.  Format the drive &#8220;Mac OS Extended (Journaled), and name the drive whatever (&#8220;iMac2&#8243; is my imaginative name).</p>
<p>Running through the rest of the installer is pretty straightforward.  If you are particular and want to save a few MB, you can click on &#8220;customize&#8221; during the process and un-check all the languages that you are not going to use.  The install process took about 30 mins to complete.</p>
<p><a href="http://sandeepghael.com/wp-content/uploads/2009/10/SL1-photo2.jpg"><img class="alignnone size-full wp-image-211" title="SL1-photo2" src="http://sandeepghael.com/wp-content/uploads/2009/10/SL1-photo2.jpg" alt="SL1-photo2" width="160" height="120" /></a><a href="http://sandeepghael.com/wp-content/uploads/2009/10/SL1-photo3.jpg"><img class="size-full wp-image-212 alignnone" title="SL1-photo3" src="http://sandeepghael.com/wp-content/uploads/2009/10/SL1-photo3.jpg" alt="SL1-photo3" width="160" height="120" /></a><a href="http://sandeepghael.com/wp-content/uploads/2009/10/SL1-photo4.jpg"><img class="alignnone size-full wp-image-213" title="SL1-photo4" src="http://sandeepghael.com/wp-content/uploads/2009/10/SL1-photo4.jpg" alt="SL1-photo4" width="160" height="119" /></a><a href="http://sandeepghael.com/wp-content/uploads/2009/10/SL1-photo5.jpg"><img class="alignnone size-full wp-image-214" title="SL1-photo5" src="http://sandeepghael.com/wp-content/uploads/2009/10/SL1-photo5.jpg" alt="SL1-photo5" width="160" height="120" /></a><a href="http://sandeepghael.com/wp-content/uploads/2009/10/SL1-photo7.jpg"><img class="alignnone size-full wp-image-216" title="SL1-photo7" src="http://sandeepghael.com/wp-content/uploads/2009/10/SL1-photo7.jpg" alt="SL1-photo7" width="160" height="120" /></a></p>
<h1>Step 3:  Update OS X</h1>
<p>The latest version of Mac OS X, as of this entry, is 10.6.1.  Let the OS software update manager do its thing.</p>
<p><a href="http://sandeepghael.com/wp-content/uploads/2009/10/SL1-photo9.jpg"><img class="alignnone size-full wp-image-217" title="SL1-photo9" src="http://sandeepghael.com/wp-content/uploads/2009/10/SL1-photo9.jpg" alt="SL1-photo9" width="160" height="120" /></a></p>
<h1>Step 4:  Install additional XCode SDK packages</h1>
<p>After updating and restarting the computer, open the installer disk and find the &#8220;Optional Installs&#8221; directory.  Open the XCode installer.  XCode will provide some very basic builing tools for 10.6.. stuff like gcc and the appropriate header files to build just about anything from source.  <strong>One thing that is absolutely essential is including &#8220;Mac OS X 10.4 Support&#8221; (see last image).  This is not clicked on by default.</strong> I&#8217;ve found that some source packages won&#8217;t build if they cannot find header files provided by this support package.</p>
<p><a href="http://sandeepghael.com/wp-content/uploads/2009/10/SL1-photo10.jpg"><img class="alignnone size-full wp-image-218" title="SL1-photo10" src="http://sandeepghael.com/wp-content/uploads/2009/10/SL1-photo10.jpg" alt="SL1-photo10" width="160" height="120" /></a><a href="http://sandeepghael.com/wp-content/uploads/2009/10/SL1-photo11.jpg"><img class="alignnone size-full wp-image-219" title="SL1-photo11" src="http://sandeepghael.com/wp-content/uploads/2009/10/SL1-photo11.jpg" alt="SL1-photo11" width="160" height="120" /></a><a href="http://sandeepghael.com/wp-content/uploads/2009/10/SL1-photo12.jpg"><img class="alignnone size-full wp-image-220" title="SL1-photo12" src="http://sandeepghael.com/wp-content/uploads/2009/10/SL1-photo12.jpg" alt="SL1-photo12" width="160" height="120" /></a></p>
<h1>Step 5:  Install Git and Subversion</h1>
<p>If you do any serious development, of any sort, you&#8217;re going to be using version control (if you are sane).</p>
<p>The new school kids are using Git, but there are still plenty of things hosted with Subversion.  So besides needing either to source control your own projects, you&#8217;re going to want these tools to grab latest code (i.e. trunk) for lots of cool stuff like Python, Ruby Gems, etc.</p>
<h3>Install Git</h3>
<p>For various reasons, you are probably better off compiling Git from source.  Don&#8217;t worry, it&#8217;s not painful at all, but does assume some familiarity with using the Terminal/ Command line interface.  If you can follow instructions, you can do this.</p>
<p>Follow these awesomely documented instructions from HiveLogic: <a href="http://hivelogic.com/articles/compiling-git-on-snow-leopard/" target="_blank">http://hivelogic.com/articles/compiling-git-on-snow-leopard/</a></p>
<p>One thing to note:  you can grab the most recent stable version of Git, instead of the exact one mentioned in the HiveLogic article.  Check the repo here and find the latest version  <a href="http://kernel.org/pub/software/scm/git/ " target="_blank">http://kernel.org/pub/software/scm/git/ </a> (use the .tar.bz file).  Also since you just clean installed this computer, you probably don&#8217;t have TextMate, so use &#8220;nano&#8221; from terminal to edit your .profile file as needed</p>
<p>After you confirm that Git is installed correctly, you can also installed GitX which is a great Git &#8220;repo browser&#8221; specifically for OS X.  You can grab it here:  <a href="http://gitx.frim.nl/" target="_blank">http://gitx.frim.nl/</a> .  You probably won&#8217;t use it anytime soon, but you&#8217;ll thank me later.</p>
<h3>Install Subversion</h3>
<p>This is the easiest step here, because it&#8217;s already done for you!  Mac OS comes with Subversion, and while it&#8217;s not the very latest version, it will work just fine.  Type &#8220;which svn&#8221; to see that Subversion lives at &#8220;/usr/bin/svn&#8221;.   &#8220;svn &#8211;version&#8221; shows &#8220;version 1.6.2&#8243;</p>
<p>&#8212;</p>
<p>In &#8220;Ultimate Snow Leopard Web Development machine&#8221; Part 2, we&#8217;ll cover setting up your CLI and installing databases!</p>
]]></content:encoded>
			<wfw:commentRss>http://sandeepghael.com/2009/10/ultimate-snow-leopard-web-development-machine-part-1/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Backup script for MySQL (onsite and offsite)</title>
		<link>http://sandeepghael.com/2009/08/backup-script-for-mysql-onsite-and-offsite/</link>
		<comments>http://sandeepghael.com/2009/08/backup-script-for-mysql-onsite-and-offsite/#comments</comments>
		<pubDate>Mon, 24 Aug 2009 20:11:32 +0000</pubDate>
		<dc:creator>sghael</dc:creator>
				<category><![CDATA[Databases]]></category>

		<guid isPermaLink="false">http://sandeepghael.com/?p=132</guid>
		<description><![CDATA[In the previous post, I provide a backup script for PostgreSQL. As promised, here is the same script written for MySQL. Below are the setup instructions. Copy the contents below into a file named mysql-backup.sh placed in the root account home (on Ubuntu it&#8217;s &#8216;/root&#8217;). For simplicity, I&#8217;m using the root account, but on your [...]]]></description>
			<content:encoded><![CDATA[<p>In the previous post, I provide a backup script for PostgreSQL.  As promised, here is the same script written for MySQL.</p>
<p>Below are the setup instructions.</p>
<ol>
<li>
        Copy the contents below into a file named mysql-backup.sh placed in the root account home (on Ubuntu it&#8217;s &#8216;/root&#8217;).  For simplicity, I&#8217;m using the root account, but on your production servers, you may want to create a dedicated &#8220;backupuser&#8221; account that executes these actions.<br />
        <br/></p>
<div style="background-color: #F1F1F1;">
        [cci lang="bash"]</p>
<p>#!/bin/sh</p>
<p>DATE=`date &#8220;+%Y-%m-%d-%I-%M-%S&#8221;`<br />
BACKUP_DIR=&#8221;mysqlbackups&#8221;</p>
<p>#DO NOT CHANGE ANYTHING BELOW THIS LINE<br />
DUMP=&#8221;mysqldump&#8221;<br />
REMOTE=&#8221;"</p>
<p>usage() {<br />
echo Usage:<br />
echo &#8220;\t$0 [options]&#8221;<br />
echo<br />
echo &#8220;Where options is as follows:&#8221;<br />
echo &#8220;\t-r\t&#8211;remote\t\tThe remote host to copy the DB backup to (mandatory)&#8221;<br />
echo &#8220;\t-u\t&#8211;user\t\t\tDatabase user name; defaults to the current linux user&#8221;<br />
echo &#8220;\t-d\t&#8211;database\t\tDatabase name; defaults to the database user name&#8221;<br />
echo &#8220;\t-p\t&#8211;password\t\tDatabase password; if none is specified you will be prompted to enter one&#8221;<br />
echo &#8220;\t-R\t&#8211;remote&#8211;destination\tThe directory on the remote host to upload the DB backup to; defaults to ~/$BACKUP_DIR&#8221;<br />
echo &#8220;\t-L\t&#8211;local&#8211;destination\tThe directory on local host to save the DB backup; defaults to $HOME/$BACKUP_DIR&#8221;<br />
}</p>
<p>while [ "$1" != "" ]; do<br />
	case $1 in<br />
		-h | &#8211;help ) 		usage<br />
					exit<br />
					;;<br />
		-u | &#8211;user ) 		shift<br />
					USER=$1<br />
					;;<br />
		-d | &#8211;database ) 	shift<br />
					DB=$1<br />
					;;<br />
		-p | &#8211;password ) 	shift<br />
					PASS=$1<br />
					;;<br />
		-r | &#8211;remote ) 	shift<br />
					REMOTE=$1<br />
					;;<br />
		-R | &#8211;remote-destination ) shift<br />
					REMOTE_DIR=$1<br />
					;;<br />
		-L | &#8211;local-destination ) shift<br />
					LOCAL_DIR=$1<br />
					;;<br />
	esac<br />
	shift<br />
done<br />
if [ "$REMOTE" = "" ]; then<br />
	usage<br />
	exit<br />
fi</p>
<p>LOCAL_DIR=${LOCAL_DIR:=&#8221;$HOME/$BACKUP_DIR&#8221;}<br />
REMOTE_DIR=${REMOTE_DIR:=&#8221;~/$BACKUP_DIR&#8221;}<br />
USER=${USER:=`whoami`}<br />
DB=${DB:=$USER}<br />
DEST=&#8221;$LOCAL_DIR/$DB.$DATE.bz2&#8243;</p>
<p>mkdir -p $LOCAL_DIR 2>/dev/null<br />
if [ "$PASS" ]; then<br />
	`$DUMP -u$USER -p$PASS $DB | bzip2 > $DEST`<br />
else<br />
	`$DUMP -u $USER -p $DB | bzip2 > $DEST`<br />
fi<br />
scp $DEST $REMOTE:$REMOTE_DIR</p>
<p>	[/cci]
</p></div>
<p>   	<br/>
   </li>
<li>
        Make the file executable:<br/><br />
[cci lang="bash"]<br />
chmod 755 mysql-backup.sh<br />
[/cci]<br />
        <br/>
   </li>
<li>
	If you don&#8217;t have one already, create an ssh key pair for the account that will be running the cron (&#8216;root&#8217; in mycase).  I set the key type to RSA / 2048 bits.  When prompted for a password by the keygen creator, leave it blank.<br/><br />
[cci lang="bash"]<br />
ssh-keygen -t rsa -b 2048<br />
[/cci]<br />
   	<br/>
   </li>
<li>
	Now take the id_rsa.pub key that was created in the previous step ( by default, it is placed in ~/.ssh/ ) and put it on the remote machine that we want to copy the backups to.  It will need to be placed in a specific file called &#8220;authorized_keys&#8221; in the &#8216;.ssh&#8217; directory of the user you will be ssh-ing in as.  Thus, if you are ssh-ing into the remote machine as &#8220;jsmith&#8221;, put the key in a file called &#8220;authorized_keys&#8221; in /home/jsmith/.ssh/.  The key should be placed into &#8216;authorized_keys&#8217; as one line.  Make sure to also enable passwordless ssh on the remote box, else the script won&#8217;t automatically be able to scp the file over.<br/><br />
	You can test this users ability to ssh into the remote machine without being prompted by a password using simple ssh test:<br/><br />
[cci lang="bash"]<br />
ssh jsmith@[remoteserver.com]<br />
[/cci]<br/><br />
Only continue to the next step when you get password-less ssh working!<br />
<br/>
   </li>
<li>
        Next, create a cronjob to execute the script periodically.  As root, you can use the &#8220;crontab -e&#8221; command to edit/create a crontab account.  I&#8217;ve chosen every hour, on the 15th minute:<br/><br />
[cci lang="bash"]<br />
15 */1 * * * sh /root/mysql-backup.sh -r [username]@[offsite.com] -u [dbusr] -p [dbpass] -d [dbname] -R [remote server path]<br />
[/cci]<br />
   	<br/>
   </li>
<li>
        You may want to confirm that your server time is correct, as the backup file names will be date stamped with their time of creation.   Also confirm that the backup files made it into the local and remote machine directories.  And ofcourse, test the integrity of the created backup files by untaring/ un-bzipping them and restoreing them in a scratch database.<br />
   	<br/>
   </li>
</ol>
<p><br/></p>
]]></content:encoded>
			<wfw:commentRss>http://sandeepghael.com/2009/08/backup-script-for-mysql-onsite-and-offsite/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Backup script for PostgreSQL (onsite and offsite)</title>
		<link>http://sandeepghael.com/2009/08/backup-script-for-postgresql-onsite-and-offsite/</link>
		<comments>http://sandeepghael.com/2009/08/backup-script-for-postgresql-onsite-and-offsite/#comments</comments>
		<pubDate>Fri, 14 Aug 2009 21:14:48 +0000</pubDate>
		<dc:creator>sghael</dc:creator>
				<category><![CDATA[Databases]]></category>

		<guid isPermaLink="false">http://sandeepghael.com/?p=79</guid>
		<description><![CDATA[Somethings never change. Needing to backup data is one of those things. Since webapps are constantly in development, and new features often means changes to the database schema, I find it super useful (and comforting) to do hourly backups of my applications database. If I make some boneheaed mistake, at most I&#8217;ll only lose 1 [...]]]></description>
			<content:encoded><![CDATA[<p>Somethings never change.  Needing to backup data is one of those things.  Since webapps are constantly in development, and new features often means changes to the database schema, I find it super useful (and comforting) to do hourly backups of my applications database.  If I make some boneheaed mistake, at most I&#8217;ll only lose 1 hour of work.  And because I&#8217;m super paranoid, I put the output of the backup in two locations&#8230; the local server and a physically remote server (incase of massive datacenter failure at one location).  </p>
<p>To save disk space, this script will automatically bzip the sql dump file.  Your mileage will vary depending on how big your database is, but on a small webapp I can usually run this thing hourly, 24/7 and only use 1% of the available drive space after a year.  Point being that you can backup as often as your disk space allocation will allow&#8230; and usually it will allow for more than you can imagine.  One thing to note is that this script may not be the best strategy for high-volume sites, since it assumes you will lock the database while doing the dump.</p>
<p>Below are the setup instructions.</p>
<ol>
<li>
        Copy the contents below into a file named pg-backup.sh placed in the root account home (on Ubuntu it&#8217;s &#8216;/root&#8217;).  For simplicity, I&#8217;m using the root account, but on your production servers, you may want to create a dedicated &#8220;backupuser&#8221; account that executes these actions.<br />
        <br/></p>
<div style="background-color: #F1F1F1;">
        [cci lang="bash"]</p>
<p>#!/bin/sh</p>
<p>DATE=`date &#8220;+%Y-%m-%d-%H-%M-%S&#8221;`<br />
BACKUP_DIR=&#8221;pgbackups&#8221;</p>
<p>#DO NOT CHANGE ANYTHING BELOW THIS LINE<br />
DUMP=&#8221;pg_dump&#8221;<br />
REMOTE=&#8221;"</p>
<p>usage() {<br />
echo Usage:<br />
echo &#8220;\t$0 [options]&#8221;<br />
echo<br />
echo &#8220;Where options is as follows:&#8221;<br />
echo &#8220;\t-r\t&#8211;remote\t\tThe remote host to copy the DB backup to (mandatory)&#8221;<br />
echo &#8220;\t-u\t&#8211;user\t\t\tDatabase user name; defaults to the current linux user&#8221;<br />
echo &#8220;\t-d\t&#8211;database\t\tDatabase name; defaults to the database user name&#8221;<br />
echo &#8220;\t-p\t&#8211;password\t\tDatabase password; if none is specified you will be prompted to enter one&#8221;<br />
echo &#8220;\t-R\t&#8211;remote&#8211;destination\tThe directory on the remote host to upload the DB backup to; defaults to ~/$BACKUP_DIR&#8221;<br />
echo &#8220;\t-L\t&#8211;local&#8211;destination\tThe directory on local host to save the DB backup; defaults to $HOME/$BACKUP_DIR&#8221;<br />
}</p>
<p>while [ "$1" != "" ]; do<br />
	case $1 in<br />
		-h | &#8211;help ) 		usage<br />
					exit<br />
					;;<br />
		-u | &#8211;user ) 		shift<br />
					USER=$1<br />
					;;<br />
		-d | &#8211;database ) 	shift<br />
					DB=$1<br />
					;;<br />
		-p | &#8211;password ) 	shift<br />
					export PGPASSWORD=$1<br />
					;;<br />
		-r | &#8211;remote ) 	shift<br />
					REMOTE=$1<br />
					;;<br />
		-R | &#8211;remote-destination ) shift<br />
					REMOTE_DIR=$1<br />
					;;<br />
		-L | &#8211;local-destination ) shift<br />
					LOCAL_DIR=$1<br />
					;;<br />
	esac<br />
	shift<br />
done<br />
if [ "$REMOTE" = "" ]; then<br />
	usage<br />
	exit<br />
fi</p>
<p>LOCAL_DIR=${LOCAL_DIR:=&#8221;$HOME/$BACKUP_DIR&#8221;}<br />
REMOTE_DIR=${REMOTE_DIR:=&#8221;~/$BACKUP_DIR&#8221;}<br />
USER=${USER:=`whoami`}<br />
DB=${DB:=$USER}<br />
DEST=&#8221;$LOCAL_DIR/$DB.$DATE.bz2&#8243;</p>
<p>mkdir -p $LOCAL_DIR 2>/dev/null<br />
`$DUMP -U $USER $DB | bzip2 > $DEST`<br />
ssh $REMOTE mkdir -p $REMOTE_DIR 2>/dev/null<br />
scp $DEST $REMOTE:$REMOTE_DIR</p>
<p>	[/cci]
</p></div>
<p>   	<br/>
   </li>
<li>
        Make the file executable:<br/><br />
[cci lang="bash"]<br />
chmod 755 pg-backup.sh<br />
[/cci]<br />
        <br/>
   </li>
<li>
	If you don&#8217;t have one already, create an ssh key pair for the account that will be running the cron (&#8216;root&#8217; in mycase).  I set the key type to RSA / 2048 bits.  When prompted for a password by the keygen creator, leave it blank.<br/><br />
[cci lang="bash"]<br />
ssh-keygen -t rsa -b 2048<br />
[/cci]<br />
   	<br/>
   </li>
<li>
	Now take the id_rsa.pub key that was created in the previous step ( by default, it is placed in ~/.ssh/ ) and put it on the remote machine that we want to copy the backups to.  It will need to be placed in a specific file called &#8220;authorized_keys&#8221; in the &#8216;.ssh&#8217; directory of the user you will be ssh-ing in as.  Thus, if you are ssh-ing into the remote machine as &#8220;jsmith&#8221;, put the key in a file called &#8220;authorized_keys&#8221; in /home/jsmith/.ssh/.  The key should be placed into &#8216;authorized_keys&#8217; as one line.  Make sure to also enable passwordless ssh on the remote box, else the script won&#8217;t automatically be able to scp the file over.<br/><br />
	You can test this users ability to ssh into the remote machine without being prompted by a password using simple ssh test:<br/><br />
[cci lang="bash"]<br />
ssh jsmith@[remoteserver.com]<br />
[/cci]<br/><br />
Only continue to the next step when you get password-less ssh working!<br />
<br/>
   </li>
<li>
        Next, create a cronjob to execute the script periodically.  As root, you can use the &#8220;crontab -e&#8221; command to edit/create a crontab account.  I&#8217;ve chosen every hour, on the 15th minute:<br/><br />
[cci lang="bash"]<br />
15 */1 * * * sh /root/pg-backup.sh -r [username]@[offsite.com] -u [dbusr] -p [dbpass] -d [dbname] -R [remote server path]<br />
[/cci]<br />
   	<br/>
   </li>
<li>
        You may want to confirm that your server time is correct, as the backup file names will be date stamped with their time of creation.   Also confirm that the backup files made it into the local and remote machine directories.  And ofcourse, test the integrity of the created backup files by untaring/ un-bzipping them and restoreing them in a scratch database.<br />
   	<br/>
   </li>
</ol>
<p>Hope this was helpful!  I&#8217;ll be back next week with a similar script solution for MySQL users.<br />
<br/></p>
]]></content:encoded>
			<wfw:commentRss>http://sandeepghael.com/2009/08/backup-script-for-postgresql-onsite-and-offsite/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

