<?xml version="1.0" encoding="iso-8859-1"?>
        <?xml-stylesheet type="text/css" href="http://www.miek.nl/blog/"?>
<rss version="2.0"
 xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
 xmlns:dc="http://purl.org/dc/elements/1.1/"
 xmlns:admin="http://webns.net/mvcb/"
 xmlns:atom="http://www.w3.org/2005/Atom"
>
<channel>
<title>Filed under: perl | Miek</title>
<atom:link href="http://www.miek.nl/blog/archives/perl/index-rss.xml" rel="self" type="application/rss+xml" />
<link>http://www.miek.nl/blog</link>
<description>Thoughts on (technical) stuff</description>
<dc:language>en-us</dc:language>
<dc:creator>Miek Gieben</dc:creator>
<dc:date>2012-02-04T04:15:11+01:00</dc:date>
<admin:generatorAgent rdf:resource="http://nanoblogger.sourceforge.net" />

<item>
<link>http://www.miek.nl/blog/archives/2009/08/10/string_multiplication_in_zsh/index.html</link>
<guid isPermaLink="true">http://www.miek.nl/blog/archives/2009/08/10/string_multiplication_in_zsh/index.html</guid>
<title>String multiplication in zsh</title>
<dc:date>2009-08-10T19:46:31+01:00</dc:date>
<dc:creator>Miek Gieben</dc:creator>
<dc:subject> perl, zsh</dc:subject>
<description><![CDATA[<p>In Perl you have this:</p>

<pre><code>% perl -e 'print "a" x 5, "\n"'
aaaaa
</code></pre>

<p>With that you can easily create a separator string
consisting out of 60 spaces.</p>

<p>I always missed this in my shell - until now.</p>

<p>In Zsh have the following expansion:</p>

<blockquote>
  <p>l:expr::string1::string2:</p>

<pre><code>Pad the resulting words on the left. Each word will be truncated if
required and placed in a field expr characters wide.
</code></pre>
</blockquote>

<p>See <a href="http://zsh.dotsrc.org/Doc/Release/Expansion.html">zsh.dotsrc.org</a>.
There is also a <code>r:</code> variant which operates in the same way.</p>

<p>And lo and behold:</p>

<pre><code>% echo ${(r:40::-:)A}
----------------------------------------
</code></pre>

<p>No more:</p>

<pre><code>A="------------------------------------"
</code></pre>]]></description>

</item>
<item>
<link>http://www.miek.nl/blog/archives/2009/07/17/a_library_at_home/index.html</link>
<guid isPermaLink="true">http://www.miek.nl/blog/archives/2009/07/17/a_library_at_home/index.html</guid>
<title>A library at home</title>
<dc:date>2009-07-17T20:20:44+01:00</dc:date>
<dc:creator>Miek Gieben</dc:creator>
<dc:subject> perl, latex, linux</dc:subject>
<description><![CDATA[<p>I've told a few 
<a href="http://www.miek.nl/s/61ba93ac0b/">times about how</a>
<a href="http://www.miek.nl/s/b3021c7414/">to build a library</a>.</p>

<p>But there a some extra things that must be done before you can call 
yourself a complete librarian.</p>

<ol>
<li>ID-ing the books you have</li>
<li>Storing the information </li>
<li>Marking the books</li>
<li>Sorting and shelving the books</li>
<li>Remembering who borrowed what</li>
</ol>

<h1>ID-ing books</h1>

<p>For this I bought a barcode reader that reads the ISBN number
of the book's backcover. With <code>book_get</code> 
(<a href="http://www.miek.nl/downloads/2009/book_get">Perl code</a>)
I can retrieve the author, title, genre and year published from Google.</p>

<pre><code>% book_get 9789024546497
Cryptonomicon
Neal Stephenson
none
2005
#Cryptonomicon&amp;#8206;by Neal Stephenson, Irene Ketman - 2005
</code></pre>

<p>Getting the genre right is a bit of a problem as Google will
not always report the correct genre (or just slightly different,
i.e. sometimes 'science' and sometimes 'computer science'). I
solved this by explicitly asking for the genre in the next step.</p>

<p>Also the html returned is awful, so sometimes this script looses
its way.</p>

<h1>Storing the information</h1>

<p>Getting the information is the first step, now it needs to be stored
somewhere. In a SQLite database for instance. I've made a minimal
database with only one table which holds all information: title,
genre, author, isbn, and a few more items.</p>

<p>With <code>book_sql</code> 
(<a href="http://www.miek.nl/downloads/2009/book_sql">perl code</a>)
the information retrieved with <code>book_get</code> is 
normalized (correct the genre) and put into the SQLite db.</p>

<h1>Marking the books</h1>

<p>To help with this 
<a href="http://www.miek.nl/s/b3021c7414/">I needed a label writer</a> to
print the author, genre and title on a small label. Currently I am
not confident enough to automate the printing of the labels so 
I manually query the database and feed the rows to <code>labelfmt</code>
(<a href="http://www.miek.nl/downloads/2009/labelfmt">Perl code</a>). This
little scripts uses LaTeX to format the labels and will print the
resulting <code>pdf</code> on the labelwriter.</p>

<pre><code>% sqlite3 biblio.db
sqlite&gt; select * from books where title like '%crypto%';
146|9789024546497|Cryptonomicon|Neal Stephenson, Irene Ketman|science fiction|2005|1201339561|
</code></pre>

<p>Now I give this text to <code>labelfmt</code> and it will print the label</p>

<pre><code>% ./labelfmt
146|9789024546497|Cryptonomicon|Neal Stephenson, Irene Ketman|science fiction|2005|1201339561|
....
</code></pre>

<p>And the labels gets printed:</p>

<p><img src="/gfx/2009/label.jpg" alt="label with print" title="Label with print" /></p>

<p>Next you will needed:</p>

<ul>
<li>little pieces of thick paper/carton; this is where you sticker the 
labels on. Like this:</li>
</ul>

<p><img src="/gfx/2009/label-on-paper.jpg" alt="paper with label" title="Paper with label" /></p>

<ul>
<li>little (120 x 120 mm) plastic triangles; which can be glued in each book, so that
the book's label can be put inside.</li>
</ul>

<p><img src="/gfx/2009/plastic.jpg" alt="plastic in book" title="Plastic in book" /></p>

<p>And insert the label:</p>

<p><img src="/gfx/2009/label-in-book.jpg" alt="label in book" title="Label in book" /></p>

<p>And another one is ready :-)</p>

<h1>Sorting and storing</h1>

<p>We decided to catalog our books based on genre. So I made
a map to tell which genres are placed where. Sorta like</p>

<ul>
<li>COMPUTER - left shelve in the living room </li>
<li>SCIENCE  - middle shelve in the living room</li>
<li>... - somewhere else</li>
</ul>

<p>So there you have it a full blown library setup for you books
at home. </p>

<h1>Borrowing</h1>

<p>Now if somebody want to read one of our book it is simply a
matter of: locating the book, getting the label out, writing
the person's name on the label and storing the label in a
safe place until the book is returned.</p>

<p>This last step could also be automated by using the barcode reader,
but the desktop computer in our living room is not powered on
100% of the time. Booting the machine takes too long and makes
it too cumbersome if someone just wants to borrow a book.</p>]]></description>

</item>
<item>
<link>http://www.miek.nl/blog/archives/2008/05/25/using_s3_with_rdup/index.html</link>
<guid isPermaLink="true">http://www.miek.nl/blog/archives/2008/05/25/using_s3_with_rdup/index.html</guid>
<title>Using S3 with rdup</title>
<dc:date>2008-05-25T17:12:51+01:00</dc:date>
<dc:creator>Miek Gieben</dc:creator>
<dc:subject> rdup, perl</dc:subject>
<description><![CDATA[<p>Well after some playing (and signing up) with 
<a href="http://www.amazon.com/s3">S3</a>, I'm now confident that
rdup can be used with S3. The basic mode of operation is as follows:</p>

<ol>
<li>Create a backup with <code>rdup -c</code></li>
<li>Save the output somewhere</li>
<li>Upload it to Amazon</li>
</ol>

<p>Backup:</p>

<pre><code>rdup -c /dev/null dirs/to/backup &gt; rdup-bin-file
s3-upload rdup-bin-file
</code></pre>

<ol>
<li>Download the backup file</li>
<li>Put it through <code>rdup-snap -c</code></li>
</ol>

<p>Restore:</p>

<pre><code>s3-down rdup-bin-file
cat rdup-bin-file | rdup-snap -c -b /tmp/restore
</code></pre>

<h1>How this works under Ubuntu/Debian with Perl</h1>

<p>The Amazon Perl API is already available with Ubuntu. Just</p>

<pre><code>apt-get install libnet-amazon-s3-perl libwww-perl libxml-simple-perl
</code></pre>

<p>and you will get all the stuff you need to start developing. Note that
<code>perldoc Net::Amazon::S3</code> is something you <em>really</em> need to read. Also
be sure to read the S3 docs from Amazon.</p>

<h1>Generic code</h1>

<p>S3 works with buckets in which you can place objects (which should
be smaller than 5 GB).
So before you can do anything you will need to create a bucket.</p>

<p>All Perl files that follow need to have the following code at the start</p>

<pre><code>#!/usr/bin/perl

use strict;
use warnings;
use Net::Amazon::S3;
</code></pre>

<p>Next you will need to create a connection with Amazon, this done with
your 'access-key-id' and your 'secret-access-key' which I'm not going
to tell of course. Code look like this</p>

<pre><code>my $s3 = Net::Amazon::S3-&gt;new( 
{   
aws_access_key_id =&gt; "my_public_key"
aws_secret_access_key =&gt; "my_private_key",
}   
);
</code></pre>

<h1>Create a bucket</h1>

<p>This step needs to be done once. In code:</p>

<pre><code>my $response = $s3-&gt;add_bucket( { bucket =&gt; "rdup-test1" } ) or 
    die $s3-&gt;err . ": " . $s3-&gt;errstr;
</code></pre>

<h1>Upload a file to Amazon</h1>

<p>Code:</p>

<pre><code>my $localname = $ARGV[0];
my $bucket = $s3-&gt;bucket("rdup-test1");
$bucket-&gt;add_key_filename('rdup-backup-200805-23.bin', $localname,
{ content_type =&gt; 'application/binary', },
) or die $s3-&gt;err . ": " . $s3-&gt;errstr;
</code></pre>

<p>The arguments for <code>add_key_filename</code> are</p>

<ol>
<li>remote filename</li>
<li>local filename</li>
<li>hash reference; in this case with the <code>content_type</code></li>
</ol>

<h1>Download a file from Amazon</h1>

<p>Code:</p>

<pre><code>my $localname = $ARGV[0];
my $bucket = $s3-&gt;bucket("rdup-test1");
$response = $bucket-&gt;get_key_filename('rdup-backup-200805-23.bin',
'GET', $localname) or
    die $s3-&gt;err . ": " . $s3-&gt;errstr;
</code></pre>

<p>The arguments for <code>get_key_filename</code> are:</p>

<ol>
<li>remote filename</li>
<li>method, any else than <code>GET</code> allowed?</li>
<li>local filename</li>
</ol>

<h1>Webinterfaces</h1>

<p>There are also S3 web interfaces out there which will let you point and
click to manage your files. I have no experience with any of them, but
they seem like a nice idea. You can at least manage your backup files
and buckets from your browser; rdup will never have fancy support for
this as it has nothing to do with backing up.</p>

<h1>Conclusion</h1>

<p>You want cheap, off-site backups? With encryption. You can have to now.
For rdup-0.6.1 I will add an <code>rdup-s3</code> tool to automate some of these
things. For now you will need to write your own stuff and use the
following commands to backup</p>

<pre><code>rdup -c /dev/null ~/mydir | rdup-crypt my-key &gt; rdup-crypted.output
&lt;upload to amazon&gt;
&lt;ready&gt;
</code></pre>

<p>And these to restore</p>

<pre><code>&lt;download from amazon&gt;
cat rdup-crypted.output | rdup-crypt -d my-key | rdup-snap -c -b \
/tmp/restore
</code></pre>

<blockquote>
  <p>Ain't that cool?!</p>
</blockquote>]]></description>

</item>
</channel>
</rss>

