<?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>My Wushu Blog &#187; Backups</title>
	<atom:link href="http://www.mywushublog.com/tag/backups/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.mywushublog.com</link>
	<description></description>
	<lastBuildDate>Wed, 28 Jul 2010 05:05:19 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0</generator>
		<item>
		<title>Using Amazon&#8217;s S3 for Backups</title>
		<link>http://www.mywushublog.com/2009/04/using-amazon-s3-for-backups/</link>
		<comments>http://www.mywushublog.com/2009/04/using-amazon-s3-for-backups/#comments</comments>
		<pubDate>Sun, 26 Apr 2009 01:47:31 +0000</pubDate>
		<dc:creator>mike</dc:creator>
				<category><![CDATA[Geekyness]]></category>
		<category><![CDATA[Backups]]></category>
		<category><![CDATA[FreeBSD]]></category>
		<category><![CDATA[MySQL]]></category>
		<category><![CDATA[PostgreSQL]]></category>
		<category><![CDATA[S3]]></category>

		<guid isPermaLink="false">http://www.mywushublog.com/?p=337</guid>
		<description><![CDATA[I don&#8217;t have a backup system for home (which is where this site, and others are located), and I have generally relied on duplicating enough of my important stuff between friends and other computers. That, and I have a RAID5 setup for my large storage, and then home directories and website stuff is on a [...]]]></description>
			<content:encoded><![CDATA[<p>I don&#8217;t have a backup system for home (which is where this site, and others are located), and I have generally relied on duplicating enough of my important stuff between friends and other computers. That, and I have a RAID5 setup for my large storage, and then home directories and website stuff is on a RAID1 ZFS volume. This doesn&#8217;t prevent accidental &#8220;oh-no&#8221;s, but it does protect me from some hardware failures.</p>
<p>Last year when I upgraded to the new server, I lost a lot of data because I forgot to backup all of my MySQL databases. I like to think I can learn from my mistakes, so a full year later I finally did something about it and signed up for Amazon&#8217;s S3 service.</p>
<p>The pricing is pretty nice, and I don&#8217;t have all that much data to backup. I figure, I&#8217;ll use up a few GB in total, and keep the monthly price around $1 &#8211; $2. That seems worth the price for off-site backup&#8217;s.</p>
<p>Now, I have 3 main websites that I need to backup, and one test site that I like to play around with:</p>
<ul>
<li><a href="http://www.m87-blackhole.org/">http://www.m87-blackhole.org/</a> &lt;- This is the first domain that I owned, and its the site where my family checks out new photos</li>
<li><a href="http://www.willowoakboarding.com/">http://www.willowoakboarding.com/</a> &lt;- My parent&#8217;s site for their boarding ranch. I&#8217;m glad they have no concept of a SLA, or that things need to be backed up <img src='http://www.mywushublog.com/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </li>
<li><a href="http://www.mywushublog.com/">http://www.mywushublog.com/</a> &lt;- This site of course, where I claim my own identity on the internet</li>
<li><a href="http://www.evil-genius-network.com/">http://www.evil-genius-network.com/</a> &lt;- a test domain, but now I run a little OpenID service, For one&#8230;</li>
</ul>
<p>After a quick &#8220;FreeBSD s3 backup&#8221; Google search, I found Gary Dalton&#8217;s blog post: <a href="http://dvector.com/oracle/2008/10/18/backing-up-to-amazon-s3/">http://dvector.com/oracle/2008/10/18/backing-up-to-amazon-s3/</a>. After reading this post, I formulated my plan of attack:</p>
<ul>
<li>Sign up for S3, create a &#8220;bucket&#8221; for each site</li>
<li>Use something to interface with S3 ( <a href="http://duplicity.nongnu.org/">duplicity</a> )</li>
<li>Automate MySQL and PostgreSQL backups</li>
<li>Create a service account to run both s3 and db backup scripts as</li>
<li>Set up a cron job for backups</li>
</ul>
<p>So, after I signed up for S3, I had to create the buckets. I couldn&#8217;t find a way to do this though my Amazon account settings, so I created a little ruby script.<br />
<code><br />
$ sudo gem install aws-s3<br />
$ vim make-bucket.rb<br />
</code></p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;"><span style="color:#008000; font-style:italic;">#!/usr/local/bin/ruby</span>
&nbsp;
<span style="color:#CC0066; font-weight:bold;">require</span> <span style="color:#996600;">'aws/s3'</span>
&nbsp;
<span style="color:#6666ff; font-weight:bold;">AWS::S3::Base</span>.<span style="color:#9900CC;">establish_connection</span>!<span style="color:#006600; font-weight:bold;">&#40;</span>
<span style="color:#ff3333; font-weight:bold;">:access_key_id</span>     <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#996600;">'my-s3-key-id'</span>,
<span style="color:#ff3333; font-weight:bold;">:secret_access_key</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#996600;">'my-s3-secret-access-key'</span>
<span style="color:#006600; font-weight:bold;">&#41;</span>
<span style="color:#6666ff; font-weight:bold;">AWS::S3::Bucket</span>.<span style="color:#9900CC;">create</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#996600;">'mywushublog'</span><span style="color:#006600; font-weight:bold;">&#41;</span>
<span style="color:#6666ff; font-weight:bold;">AWS::S3::Bucket</span>.<span style="color:#9900CC;">create</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#996600;">'willowoak'</span><span style="color:#006600; font-weight:bold;">&#41;</span>
<span style="color:#6666ff; font-weight:bold;">AWS::S3::Bucket</span>.<span style="color:#9900CC;">create</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#996600;">'m87-blackhole'</span><span style="color:#006600; font-weight:bold;">&#41;</span>
<span style="color:#6666ff; font-weight:bold;">AWS::S3::Bucket</span>.<span style="color:#9900CC;">create</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#996600;">'evil-genius-network'</span><span style="color:#006600; font-weight:bold;">&#41;</span></pre></div></div>

<p><code>$ ./make-bucket.rb</code><br />
Next, I had to install duplicity and py-boto<br />
<code>[root@server ~] cd /usr/ports/sysutils/duplicity<br />
[root@server duplicity] make install<br />
...<br />
[root@server duplicity] cd ../../devel/py-boto<br />
[root@server py-boto] make install clean<br />
...<br />
[root@server py-boto]</code><br />
Next step, create a user (with access to shared data, and website data) to run the backups with the adduser command&#8230;<br />
<code>[root@server py-boto] adduser -g shared-data -G www -s /bin/tcsh -w random s3backupuser<br />
...<br />
[roott@server py-boto] su - s3backupuser<br />
In tcsh, you can `set autolist' to have the shell automatically show<br />
all the possible matches when doing filename/directory expansion.<br />
%</code><br />
I&#8217;ll have to set my Access ID and Access Key in the s3backupuser&#8217;s environment, as well as a GnuPG passphrase so the backups are encrypted (and compressed). I mean, I trust Amazon, but not THAT much <img src='http://www.mywushublog.com/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> <br />
<code>% vim .cshrc<br />
setenv AWS_ACCESS_KEY_ID my-s3-key-id<br />
setenv AWS_SECRET_ACCESS_KEY my-s3-secrect-access-key<br />
setenv PASSPRASE AVeryRandonPassphraseForGnuPG</code><br />
Next, I copied the very useful automysqlbackup.sh script into a separate script for each website. I could have just dumped every database that was running, but I wanted to segregate each site&#8217;s databases into a different directory. So, I&#8217;m complicating my cron job by running multiple backup scripts, but I really want to make the end result easily readable and identifiable by me. So for each site, I create a directoy under /u01/backups:<br />
<code>%ll /u01/backups/<br />
total 8<br />
drwxr-x---  5 s3-backupuser  mysql  5 Apr 25 15:46 evil-genius-network<br />
drwxr-x---  5 s3-backupuser  mysql  5 Apr 25 15:47 m87-blackhole<br />
drwxr-x---  5 s3-backupuser  mysql  5 Apr 25 15:46 mywushublog<br />
drwxr-x---  5 s3-backupuser  mysql  5 Apr 25 15:47 willowoak</code><br />
Next was the s3-backups.sh script, which is very crude and simple. If I&#8217;m really motivated, I&#8217;ll make it nicer but I&#8217;m lazy and if I don&#8217;t need anymore functionality then I&#8217;ll just leave it. One thing I initially forgot was that I set my Amazon S3 variables in the users .cshrc profile. This is not a good place to have those things, it was just handy as I was running the duplicity commands manually. So I had to add those in, otherwise the cron job would fail.</p>
<p><strong>~/bin/s3-backups.sh</strong>:</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #666666; font-style: italic;">#!/bin/sh</span>
<span style="color: #007800;">PATH</span>=<span style="color: #000000; font-weight: bold;">/</span>sbin:<span style="color: #000000; font-weight: bold;">/</span>bin:<span style="color: #000000; font-weight: bold;">/</span>usr<span style="color: #000000; font-weight: bold;">/</span>sbin:<span style="color: #000000; font-weight: bold;">/</span>usr<span style="color: #000000; font-weight: bold;">/</span>bin:<span style="color: #000000; font-weight: bold;">/</span>usr<span style="color: #000000; font-weight: bold;">/</span>games:<span style="color: #000000; font-weight: bold;">/</span>usr<span style="color: #000000; font-weight: bold;">/</span>local<span style="color: #000000; font-weight: bold;">/</span>sbin:<span style="color: #000000; font-weight: bold;">/</span>usr<span style="color: #000000; font-weight: bold;">/</span>local<span style="color: #000000; font-weight: bold;">/</span>bin:<span style="color: #000000; font-weight: bold;">/</span>home<span style="color: #000000; font-weight: bold;">/</span>s3<span style="color: #000000; font-weight: bold;">/</span>bin
&nbsp;
<span style="color: #666666; font-style: italic;"># Amazon S3 keys, and GnuPG keys</span>
<span style="color: #007800;">AWS_ACCESS_KEY_ID</span>=
<span style="color: #007800;">AWS_SECRET_ACCESS_KEY</span>=
<span style="color: #007800;">PASSPHRASE</span>=
<span style="color: #7a0874; font-weight: bold;">export</span> AWS_ACCESS_KEY_ID
<span style="color: #7a0874; font-weight: bold;">export</span> AWS_SECRET_ACCESS_KEY
<span style="color: #7a0874; font-weight: bold;">export</span> PASSPHRASE
&nbsp;
<span style="color: #7a0874; font-weight: bold;">echo</span> <span style="color: #ff0000;">&quot;*************************************************&quot;</span>
<span style="color: #7a0874; font-weight: bold;">echo</span> <span style="color: #ff0000;">&quot;*   Backing up Website content....              *&quot;</span>
<span style="color: #7a0874; font-weight: bold;">echo</span> <span style="color: #ff0000;">&quot;*                                               *&quot;</span>
<span style="color: #7a0874; font-weight: bold;">echo</span> <span style="color: #ff0000;">&quot;*     www.willowoakboarding.com...              *&quot;</span>
duplicity <span style="color: #000000; font-weight: bold;">/</span>www<span style="color: #000000; font-weight: bold;">/</span>www.willowoakboarding.com s3+http:<span style="color: #000000; font-weight: bold;">//</span>s3.amazon.com<span style="color: #000000; font-weight: bold;">/</span>willowoak<span style="color: #000000; font-weight: bold;">/</span>www
<span style="color: #7a0874; font-weight: bold;">echo</span> <span style="color: #ff0000;">&quot;*     www.mywushublog.com...                    *&quot;</span>
duplicity <span style="color: #000000; font-weight: bold;">/</span>www<span style="color: #000000; font-weight: bold;">/</span>www.mywushublog.com s3+http:<span style="color: #000000; font-weight: bold;">//</span>s3.amazon.com<span style="color: #000000; font-weight: bold;">/</span>mywushublog<span style="color: #000000; font-weight: bold;">/</span>www
<span style="color: #7a0874; font-weight: bold;">echo</span> <span style="color: #ff0000;">&quot;*     www.m87-blackhole.org...                  *&quot;</span>
duplicity <span style="color: #000000; font-weight: bold;">/</span>www<span style="color: #000000; font-weight: bold;">/</span>www.m87-blackhole.org s3+http:<span style="color: #000000; font-weight: bold;">//</span>s3.amazon.com<span style="color: #000000; font-weight: bold;">/</span>m87-blackhole<span style="color: #000000; font-weight: bold;">/</span>www
<span style="color: #7a0874; font-weight: bold;">echo</span> <span style="color: #ff0000;">&quot;*************************************************&quot;</span>
<span style="color: #7a0874; font-weight: bold;">echo</span> <span style="color: #ff0000;">&quot;*   Backing up databases....                    *&quot;</span>
<span style="color: #7a0874; font-weight: bold;">echo</span> <span style="color: #ff0000;">&quot;*                                               *&quot;</span>
<span style="color: #7a0874; font-weight: bold;">echo</span> <span style="color: #ff0000;">&quot;*     www.willowoakboard.com...                 *&quot;</span>
duplicity <span style="color: #000000; font-weight: bold;">/</span>u01<span style="color: #000000; font-weight: bold;">/</span>backups<span style="color: #000000; font-weight: bold;">/</span>willowoak s3+http:<span style="color: #000000; font-weight: bold;">//</span>s3.amazon.com<span style="color: #000000; font-weight: bold;">/</span>willowoak<span style="color: #000000; font-weight: bold;">/</span>db
<span style="color: #7a0874; font-weight: bold;">echo</span> <span style="color: #ff0000;">&quot;*     www.mywushublog.com...                    *&quot;</span>
duplicity <span style="color: #000000; font-weight: bold;">/</span>u01<span style="color: #000000; font-weight: bold;">/</span>backups<span style="color: #000000; font-weight: bold;">/</span>mywushublog s3+http:<span style="color: #000000; font-weight: bold;">//</span>s3.amazon.com<span style="color: #000000; font-weight: bold;">/</span>mywushublog<span style="color: #000000; font-weight: bold;">/</span>db
<span style="color: #7a0874; font-weight: bold;">echo</span> <span style="color: #ff0000;">&quot;*     www.m87-blackhole.org...                  *&quot;</span>
duplicity <span style="color: #000000; font-weight: bold;">/</span>u01<span style="color: #000000; font-weight: bold;">/</span>backups<span style="color: #000000; font-weight: bold;">/</span>m87-blackhole s3+http:<span style="color: #000000; font-weight: bold;">//</span>s3.amazon.com<span style="color: #000000; font-weight: bold;">/</span>m87-blackhole<span style="color: #000000; font-weight: bold;">/</span>db
<span style="color: #7a0874; font-weight: bold;">echo</span> <span style="color: #ff0000;">&quot;*************************************************&quot;</span></pre></div></div>

<p>And last but not least, a cronjob to tie it all together:<br />
<code>% crontab -e<br />
@weekly ~/bin/s3-backups.sh<br />
@weekly ~/bin/mywushublog-mysql-backup.sh<br />
@weekly ~/bin/willowoak-mysql-backup.sh<br />
@weekly ~/bin/m87-blackhole-mysql-backup.sh<br />
@weekly ~/bin/evil-genius-network-mysql-backup.sh</code><br />
I can check the status of a backup by running <strong>duplicity</strong> with the &#8216;<em>collection-status</em>&#8216; flag:<br />
<code>%duplicity collection-status s3+http://s3.amazon.com/mywushublog/db<br />
Last full backup date: Sat Apr 25 15:08:02 2009<br />
Collection Status<br />
-----------------<br />
Connecting with backend: BotoBackend<br />
Archive dir: None<br />
Found 0 backup chains without signatures.<br />
Found a complete backup chain with matching signature chain:<br />
-------------------------<br />
Chain start time: Sat Apr 25 15:08:02 2009<br />
Chain end time: Sat Apr 25 15:08:02 2009<br />
Number of contained backup sets: 1<br />
Total number of contained volumes: 1<br />
Type of backup set:                            Time:      Num volumes:<br />
Full         Sat Apr 25 15:08:02 2009                 1<br />
-------------------------<br />
No orphaned or incomplete backup sets found.</code><br />
I can also list the files:<br />
<code>%duplicity list-current-files s3+http://s3.amazon.com/mywushublog/db<br />
Last full backup date: Sat Apr 25 15:08:02 2009<br />
Sat Apr 25 15:05:11 2009 .<br />
Sat Apr 25 15:05:10 2009 daily<br />
Sat Apr 25 15:05:10 2009 daily/mywushublog<br />
Sat Apr 25 15:05:10 2009 monthly<br />
Sat Apr 25 15:05:10 2009 weekly<br />
Sat Apr 25 15:05:11 2009 weekly/mywushublog<br />
Sat Apr 25 15:05:11 2009 weekly/mywushublog/mywushublog_week.17.2009-04-25_15h05m.sql.gz</code><br />
Pretty sweet automated backup process. It is a lot cheaper than tapes or additional disk storage. With S3, I also don&#8217;t have to worry about buying additional hardware, the maintenance of a library or tape drive (which is what I had a few years ago, what a headache).</p>
]]></content:encoded>
			<wfw:commentRss>http://www.mywushublog.com/2009/04/using-amazon-s3-for-backups/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
