<?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>Guy Rutenberg &#187; Bash</title>
	<atom:link href="http://www.guyrutenberg.com/category/bash/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.guyrutenberg.com</link>
	<description>Keeping track of what I do</description>
	<lastBuildDate>Wed, 16 Jun 2010 19:53:40 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9.1</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Improved FTP Backup for WordPress</title>
		<link>http://www.guyrutenberg.com/2010/02/28/improved-ftp-backup-for-wordpress/</link>
		<comments>http://www.guyrutenberg.com/2010/02/28/improved-ftp-backup-for-wordpress/#comments</comments>
		<pubDate>Sun, 28 Feb 2010 06:38:44 +0000</pubDate>
		<dc:creator>Guy</dc:creator>
				<category><![CDATA[Bash]]></category>
		<category><![CDATA[Projects]]></category>
		<category><![CDATA[Wordpress]]></category>
		<category><![CDATA[backup]]></category>

		<guid isPermaLink="false">http://www.guyrutenberg.com/?p=637</guid>
		<description><![CDATA[This script backups both the database and files of a WordPress blog into a remote FTP server (while keeping a local copy). It&#8217;s an update of my WordPress Backup to FTP script. The main changes are auto-detecting database settings and better support for caching plugins (specifically WP-Cache). The new version makes it easier to backup [...]]]></description>
			<content:encoded><![CDATA[<p>This script backups both the database and files of a WordPress blog into a remote FTP server (while keeping a local copy). It&#8217;s an update of my <a href="/2009/01/06/wordpress-backup-to-ftp/">WordPress Backup to FTP</a> script. The main changes are auto-detecting database settings and better support for caching plugins (specifically WP-Cache). The new version makes it easier to backup multiple WordPress blogs to the same FTP server.<br />
<span id="more-637"></span><br />
Usage is pretty simple after a short initial configuration. First, save the the script and make it executable:</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #c20cb9; font-weight: bold;">chmod</span> +x wp-backup</pre></div></div>

<p>(assuming you saved it under the name <code>wp-backup</code>). After saving it edit the file with your favorite editor and set the 5 configuration variable to whatever is appropriate for you. <code>BACKUP_DIR</code> is the folder to save the local backups to. <code>FTP_HOST</code>, <code>FTP_USER</code>, <code>FTP_PASS</code> control the FTP host, username and password, respectively, for the remote backup server. <code>FTP_BACKUP_DIR</code> sets the folder on the FTP server to save the remote backup to.</p>
<p>Now that the initial configuration is done, all you need to do is execute the script and give the path to the blog as an argument. For example:</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;">.<span style="color: #000000; font-weight: bold;">/</span>wp-backup <span style="color: #000000; font-weight: bold;">/</span>home<span style="color: #000000; font-weight: bold;">/</span>someuser<span style="color: #000000; font-weight: bold;">/</span>myblog</pre></div></div>

<p>And that it, the script will backup your files (excluding cache) and database to both a local and remote locations. This allows using the same script to backup multiple WordPress blogs, unlike the previous script which had to be modified for each blog.</p>
<p>And now the script itself:</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #666666; font-style: italic;">#!/bin/bash</span>
&nbsp;
<span style="color: #666666; font-style: italic;"># Copyright 2008, 2010 Guy Rutenberg &lt;http://www.guyrutenberg.com/contact-me&gt;</span>
<span style="color: #666666; font-style: italic;"># WordPress FTP backup 2.0</span>
<span style="color: #666666; font-style: italic;">#</span>
<span style="color: #666666; font-style: italic;"># Easily backup wordpress instances via ftp.</span>
<span style="color: #666666; font-style: italic;">#</span>
<span style="color: #666666; font-style: italic;"># Change Log:</span>
<span style="color: #666666; font-style: italic;"># ===========</span>
<span style="color: #666666; font-style: italic;"># 2.0:</span>
<span style="color: #666666; font-style: italic;">#  - Auto-detect database settings.</span>
<span style="color: #666666; font-style: italic;">#  - Exclude cache data from backups.</span>
&nbsp;
<span style="color: #007800;">BACKUP_DIR</span>=
<span style="color: #007800;">FTP_HOST</span>=
<span style="color: #007800;">FTP_USER</span>=
<span style="color: #007800;">FTP_PASS</span>=
<span style="color: #007800;">FTP_BACKUP_DIR</span>=
&nbsp;
<span style="color: #666666; font-style: italic;"># end of configuration - you probably don't need to touch anything bellow</span>
&nbsp;
<span style="color: #007800;">PROG</span>=<span style="color: #000000; font-weight: bold;">`</span><span style="color: #c20cb9; font-weight: bold;">basename</span> <span style="color: #ff0000;">&quot;$0&quot;</span><span style="color: #000000; font-weight: bold;">`</span>
print_usage <span style="color: #7a0874; font-weight: bold;">&#40;</span><span style="color: #7a0874; font-weight: bold;">&#41;</span> <span style="color: #7a0874; font-weight: bold;">&#123;</span>
    <span style="color: #7a0874; font-weight: bold;">echo</span> <span style="color: #ff0000;">&quot;USAGE: <span style="color: #007800;">${PROG}</span> [options] BLOG_ROOT&quot;</span>
    <span style="color: #7a0874; font-weight: bold;">echo</span> <span style="color: #ff0000;">&quot;Backup a WordPress blog&quot;</span>
<span style="color: #7a0874; font-weight: bold;">&#125;</span>
&nbsp;
print_help <span style="color: #7a0874; font-weight: bold;">&#40;</span><span style="color: #7a0874; font-weight: bold;">&#41;</span>  <span style="color: #7a0874; font-weight: bold;">&#123;</span>
    print_usage
    <span style="color: #c20cb9; font-weight: bold;">cat</span> <span style="color: #cc0000; font-style: italic;">&lt;&lt; EOF
&nbsp;
Options:
    -h, --help          show this help message and exit
&nbsp;
EOF</span>
<span style="color: #7a0874; font-weight: bold;">&#125;</span>
&nbsp;
<span style="color: #007800;">TEMP</span>=<span style="color: #000000; font-weight: bold;">`</span><span style="color: #c20cb9; font-weight: bold;">getopt</span> <span style="color: #660033;">-o</span> h <span style="color: #660033;">--long</span> <span style="color: #7a0874; font-weight: bold;">help</span> <span style="color: #660033;">-n</span> <span style="color: #ff0000;">&quot;<span style="color: #007800;">$PROG</span>&quot;</span> <span style="color: #660033;">--</span> <span style="color: #ff0000;">&quot;$@&quot;</span><span style="color: #000000; font-weight: bold;">`</span>
<span style="color: #000000; font-weight: bold;">if</span> <span style="color: #7a0874; font-weight: bold;">&#40;</span><span style="color: #7a0874; font-weight: bold;">&#40;</span><span style="color: #007800;">$?</span><span style="color: #7a0874; font-weight: bold;">&#41;</span><span style="color: #7a0874; font-weight: bold;">&#41;</span>; <span style="color: #000000; font-weight: bold;">then</span>
    print_usage
    <span style="color: #7a0874; font-weight: bold;">exit</span> <span style="color: #000000;">1</span>
<span style="color: #000000; font-weight: bold;">fi</span>
&nbsp;
<span style="color: #7a0874; font-weight: bold;">eval</span> <span style="color: #000000; font-weight: bold;">set</span> <span style="color: #660033;">--</span> <span style="color: #ff0000;">&quot;<span style="color: #007800;">$TEMP</span>&quot;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">while</span> <span style="color: #c20cb9; font-weight: bold;">true</span> ; <span style="color: #000000; font-weight: bold;">do</span>
    <span style="color: #000000; font-weight: bold;">case</span> <span style="color: #ff0000;">&quot;$1&quot;</span> <span style="color: #000000; font-weight: bold;">in</span>
        -h<span style="color: #000000; font-weight: bold;">|</span>--help<span style="color: #7a0874; font-weight: bold;">&#41;</span> print_help; <span style="color: #7a0874; font-weight: bold;">exit</span> <span style="color: #000000; font-weight: bold;">;;</span>
        --<span style="color: #7a0874; font-weight: bold;">&#41;</span> <span style="color: #7a0874; font-weight: bold;">shift</span>; <span style="color: #7a0874; font-weight: bold;">break</span><span style="color: #000000; font-weight: bold;">;;</span>
    <span style="color: #000000; font-weight: bold;">esac</span>
<span style="color: #000000; font-weight: bold;">done</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">if</span> <span style="color: #7a0874; font-weight: bold;">&#91;</span> <span style="color: #660033;">-z</span> <span style="color: #ff0000;">&quot;$1&quot;</span> <span style="color: #7a0874; font-weight: bold;">&#93;</span>
<span style="color: #000000; font-weight: bold;">then</span>
 print_usage <span style="color: #000000; font-weight: bold;">&gt;</span> <span style="color: #000000; font-weight: bold;">/</span>dev<span style="color: #000000; font-weight: bold;">/</span>stderr
 <span style="color: #7a0874; font-weight: bold;">exit</span> <span style="color: #000000;">1</span>
<span style="color: #000000; font-weight: bold;">fi</span>
&nbsp;
<span style="color: #007800;">BLOG_DIR</span>=$<span style="color: #000000;">1</span>
<span style="color: #007800;">DB_NAME</span>=<span style="color: #000000; font-weight: bold;">`</span><span style="color: #7a0874; font-weight: bold;">echo</span> <span style="color: #ff0000;">&quot;&lt;?php require_once(<span style="color: #000099; font-weight: bold;">\&quot;</span><span style="color: #007800;">${BLOG_DIR}</span>/wp-config.php<span style="color: #000099; font-weight: bold;">\&quot;</span>); echo DB_NAME;&quot;</span> <span style="color: #000000; font-weight: bold;">|</span> php<span style="color: #000000; font-weight: bold;">`</span>
<span style="color: #007800;">DB_USER</span>=<span style="color: #000000; font-weight: bold;">`</span><span style="color: #7a0874; font-weight: bold;">echo</span> <span style="color: #ff0000;">&quot;&lt;?php require_once(<span style="color: #000099; font-weight: bold;">\&quot;</span><span style="color: #007800;">${BLOG_DIR}</span>/wp-config.php<span style="color: #000099; font-weight: bold;">\&quot;</span>); echo DB_USER;&quot;</span> <span style="color: #000000; font-weight: bold;">|</span> php<span style="color: #000000; font-weight: bold;">`</span>
<span style="color: #007800;">DB_PASS</span>=<span style="color: #000000; font-weight: bold;">`</span><span style="color: #7a0874; font-weight: bold;">echo</span> <span style="color: #ff0000;">&quot;&lt;?php require_once(<span style="color: #000099; font-weight: bold;">\&quot;</span><span style="color: #007800;">${BLOG_DIR}</span>/wp-config.php<span style="color: #000099; font-weight: bold;">\&quot;</span>); echo DB_PASSWORD;&quot;</span> <span style="color: #000000; font-weight: bold;">|</span> php<span style="color: #000000; font-weight: bold;">`</span>
<span style="color: #007800;">DB_HOST</span>=<span style="color: #000000; font-weight: bold;">`</span><span style="color: #7a0874; font-weight: bold;">echo</span> <span style="color: #ff0000;">&quot;&lt;?php require_once(<span style="color: #000099; font-weight: bold;">\&quot;</span><span style="color: #007800;">${BLOG_DIR}</span>/wp-config.php<span style="color: #000099; font-weight: bold;">\&quot;</span>); echo DB_HOST;&quot;</span> <span style="color: #000000; font-weight: bold;">|</span> php<span style="color: #000000; font-weight: bold;">`</span>
&nbsp;
<span style="color: #007800;">BLOG_DIR</span>=<span style="color: #000000; font-weight: bold;">`</span><span style="color: #c20cb9; font-weight: bold;">dirname</span> <span style="color: #ff0000;">&quot;<span style="color: #007800;">$BLOG_DIR</span>&quot;</span><span style="color: #000000; font-weight: bold;">`/`</span><span style="color: #c20cb9; font-weight: bold;">basename</span> <span style="color: #ff0000;">&quot;<span style="color: #007800;">$BLOG_DIR</span>&quot;</span><span style="color: #000000; font-weight: bold;">`</span>
<span style="color: #007800;">BACKUP_DIR</span>=<span style="color: #000000; font-weight: bold;">`</span><span style="color: #c20cb9; font-weight: bold;">dirname</span> <span style="color: #ff0000;">&quot;<span style="color: #007800;">$BACKUP_DIR</span>&quot;</span><span style="color: #000000; font-weight: bold;">`/`</span><span style="color: #c20cb9; font-weight: bold;">basename</span> <span style="color: #ff0000;">&quot;<span style="color: #007800;">$BACKUP_DIR</span>&quot;</span><span style="color: #000000; font-weight: bold;">`</span>
&nbsp;
<span style="color: #7a0874; font-weight: bold;">echo</span> <span style="color: #660033;">-n</span> <span style="color: #ff0000;">&quot;dumping database... &quot;</span>
<span style="color: #007800;">DUMP_NAME</span>=<span style="color: #800000;">${DB_NAME}</span>-$<span style="color: #7a0874; font-weight: bold;">&#40;</span><span style="color: #c20cb9; font-weight: bold;">date</span> +<span style="color: #000000; font-weight: bold;">%</span>Y<span style="color: #000000; font-weight: bold;">%</span>m<span style="color: #000000; font-weight: bold;">%</span>d<span style="color: #7a0874; font-weight: bold;">&#41;</span>.sql.bz2
mysqldump <span style="color: #660033;">--user</span>=<span style="color: #800000;">${DB_USER}</span> <span style="color: #660033;">--password</span>=<span style="color: #800000;">${DB_PASS}</span> <span style="color: #660033;">--host</span>=<span style="color: #800000;">${DB_HOST}</span> \
 <span style="color: #660033;">--databases</span> <span style="color: #800000;">${DB_NAME}</span> \
 <span style="color: #000000; font-weight: bold;">|</span> <span style="color: #c20cb9; font-weight: bold;">bzip2</span> <span style="color: #660033;">-c</span> <span style="color: #000000; font-weight: bold;">&gt;</span> <span style="color: #800000;">${BACKUP_DIR}</span><span style="color: #000000; font-weight: bold;">/</span><span style="color: #800000;">${DUMP_NAME}</span>
<span style="color: #000000; font-weight: bold;">if</span> <span style="color: #7a0874; font-weight: bold;">&#91;</span> <span style="color: #ff0000;">&quot;$?&quot;</span> <span style="color: #660033;">-ne</span> <span style="color: #ff0000;">&quot;0&quot;</span> <span style="color: #7a0874; font-weight: bold;">&#93;</span>; <span style="color: #000000; font-weight: bold;">then</span>
	<span style="color: #7a0874; font-weight: bold;">echo</span> <span style="color: #ff0000;">&quot;failed!&quot;</span>
	<span style="color: #7a0874; font-weight: bold;">exit</span> <span style="color: #000000;">1</span>
<span style="color: #000000; font-weight: bold;">fi</span>
<span style="color: #7a0874; font-weight: bold;">echo</span> <span style="color: #ff0000;">&quot;done&quot;</span>
&nbsp;
<span style="color: #7a0874; font-weight: bold;">echo</span> <span style="color: #660033;">-n</span> <span style="color: #ff0000;">&quot;Creating tarball... &quot;</span>
<span style="color: #007800;">TAR_NAME</span>=<span style="color: #800000;">${BLOG_DIR##*/}</span>-$<span style="color: #7a0874; font-weight: bold;">&#40;</span><span style="color: #c20cb9; font-weight: bold;">date</span> +<span style="color: #000000; font-weight: bold;">%</span>Y<span style="color: #000000; font-weight: bold;">%</span>m<span style="color: #000000; font-weight: bold;">%</span>d<span style="color: #7a0874; font-weight: bold;">&#41;</span>.tar.bz2
<span style="color: #c20cb9; font-weight: bold;">tar</span> <span style="color: #660033;">-cjf</span> <span style="color: #800000;">${BACKUP_DIR}</span><span style="color: #000000; font-weight: bold;">/</span><span style="color: #800000;">${BLOG_DIR##*/}</span>-$<span style="color: #7a0874; font-weight: bold;">&#40;</span><span style="color: #c20cb9; font-weight: bold;">date</span> +<span style="color: #000000; font-weight: bold;">%</span>Y<span style="color: #000000; font-weight: bold;">%</span>m<span style="color: #000000; font-weight: bold;">%</span>d<span style="color: #7a0874; font-weight: bold;">&#41;</span>.tar.bz2 <span style="color: #660033;">--exclude</span> cache <span style="color: #800000;">${BLOG_DIR}</span>
<span style="color: #000000; font-weight: bold;">if</span> <span style="color: #7a0874; font-weight: bold;">&#91;</span> <span style="color: #ff0000;">&quot;$?&quot;</span> <span style="color: #660033;">-ne</span> <span style="color: #ff0000;">&quot;0&quot;</span> <span style="color: #7a0874; font-weight: bold;">&#93;</span>; <span style="color: #000000; font-weight: bold;">then</span>
	<span style="color: #7a0874; font-weight: bold;">echo</span> <span style="color: #ff0000;">&quot;failed!&quot;</span>
	<span style="color: #7a0874; font-weight: bold;">exit</span> <span style="color: #000000;">2</span>
<span style="color: #000000; font-weight: bold;">fi</span>
<span style="color: #7a0874; font-weight: bold;">echo</span> <span style="color: #ff0000;">&quot;done&quot;</span>
&nbsp;
<span style="color: #7a0874; font-weight: bold;">echo</span> <span style="color: #660033;">-n</span> <span style="color: #ff0000;">&quot;Uploading SQL dump and tarball to FTP... &quot;</span>
lftp <span style="color: #660033;">-u</span> <span style="color: #800000;">${FTP_USER}</span>,<span style="color: #800000;">${FTP_PASS}</span> <span style="color: #800000;">${FTP_HOST}</span> <span style="color: #cc0000; font-style: italic;">&lt;&lt;EOF
cd &quot;${FTP_BACKUP_DIR}&quot;
put &quot;${BACKUP_DIR}/${DUMP_NAME}&quot;
put &quot;${BACKUP_DIR}/${TAR_NAME}&quot;
&nbsp;
EOF</span>
<span style="color: #000000; font-weight: bold;">if</span> <span style="color: #7a0874; font-weight: bold;">&#91;</span> <span style="color: #ff0000;">&quot;$?&quot;</span> <span style="color: #660033;">-ne</span> <span style="color: #ff0000;">&quot;0&quot;</span> <span style="color: #7a0874; font-weight: bold;">&#93;</span>; <span style="color: #000000; font-weight: bold;">then</span>
	<span style="color: #7a0874; font-weight: bold;">echo</span> <span style="color: #ff0000;">&quot;failed!&quot;</span>
	<span style="color: #7a0874; font-weight: bold;">exit</span> <span style="color: #000000;">3</span>
<span style="color: #000000; font-weight: bold;">fi</span>
<span style="color: #7a0874; font-weight: bold;">echo</span> <span style="color: #ff0000;">&quot;done&quot;</span></pre></div></div>

]]></content:encoded>
			<wfw:commentRss>http://www.guyrutenberg.com/2010/02/28/improved-ftp-backup-for-wordpress/feed/</wfw:commentRss>
		<slash:comments>13</slash:comments>
		</item>
		<item>
		<title>Batch Renaming Using sed</title>
		<link>http://www.guyrutenberg.com/2009/01/12/batch-renaming-using-sed/</link>
		<comments>http://www.guyrutenberg.com/2009/01/12/batch-renaming-using-sed/#comments</comments>
		<pubDate>Mon, 12 Jan 2009 13:10:22 +0000</pubDate>
		<dc:creator>Guy</dc:creator>
				<category><![CDATA[Bash]]></category>
		<category><![CDATA[Linux]]></category>
		<category><![CDATA[Tips]]></category>
		<category><![CDATA[sed]]></category>

		<guid isPermaLink="false">http://www.guyrutenberg.com/?p=229</guid>
		<description><![CDATA[I was reorganizing my music library and decided to change the naming convention I&#8217;ve used. This task is just asking to be automated. Since the filename change could be described using regular expression, I looked for a way to use sed for the renaming process.
The files I had, had the following pattern as filename ARTIST [...]]]></description>
			<content:encoded><![CDATA[<p>I was reorganizing my music library and decided to change the naming convention I&#8217;ve used. This task is just asking to be automated. Since the filename change could be described using regular expression, I looked for a way to use <code>sed</code> for the renaming process.</p>
<p>The files I had, had the following pattern as filename ARTIST &#8211; SONG &#8211; TRACK &#8211; ALBUM</p>

<div class="wp_syntax"><div class="code"><pre class="text" style="font-family:monospace;">James Brown - I Got You (I Feel Good).ogg  - 01 - Classic James Brown</pre></div></div>

<p>I wanted to rename it to ARTIST &#8211; ALBUM &#8211; TRACK &#8211; NAME</p>

<div class="wp_syntax"><div class="code"><pre class="text" style="font-family:monospace;">James Brown - Classic James Brown - 01 - I Got You (I Feel Good).ogg</pre></div></div>

<p>Describing the change as a <code>sed</code> program is easy:</p>

<div class="wp_syntax"><div class="code"><pre class="text" style="font-family:monospace;">s/\(.*\) - \(.*\) - \(.*\) - \(.*\).ogg/\1 - \4 - \3 - \2.ogg/</pre></div></div>

<p>Now all that has to be done is to pass each filename to mv and pass it again after it went through the <code>sed</code> script. This could be done like this:</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">for</span> i <span style="color: #000000; font-weight: bold;">in</span> <span style="color: #000000; font-weight: bold;">*</span>; <span style="color: #000000; font-weight: bold;">do</span>
  <span style="color: #c20cb9; font-weight: bold;">mv</span> <span style="color: #ff0000;">&quot;<span style="color: #007800;">$i</span>&quot;</span> <span style="color: #ff0000;">&quot;<span style="color: #780078;">`echo $i | sed &quot;s/\(.*\) - \(.*\) - \(.*\) - \(.*\).ogg/\1 - \4 - \3 - \2.ogg/&quot;`</span>&quot;</span>;
<span style="color: #000000; font-weight: bold;">done</span></pre></div></div>

<p>The important part is the</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">`</span><span style="color: #7a0874; font-weight: bold;">echo</span> <span style="color: #007800;">$i</span> <span style="color: #000000; font-weight: bold;">|</span> <span style="color: #c20cb9; font-weight: bold;">sed</span> <span style="color: #ff0000;">&quot;s/\(.*\) - \(.*\) - \(.*\) - \(.*\).ogg/\1 - \4 - \3 - \2.ogg/&quot;</span><span style="color: #000000; font-weight: bold;">`</span></pre></div></div>

<p>which pipes the filename to <code>sed</code> and returns it as an argument for <code>mv</code>.</p>
<p>To see what renaming will be done one can alter a bit the above command, and get</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">for</span> i <span style="color: #000000; font-weight: bold;">in</span> <span style="color: #000000; font-weight: bold;">*</span>; <span style="color: #000000; font-weight: bold;">do</span>
  <span style="color: #7a0874; font-weight: bold;">echo</span> <span style="color: #ff0000;">&quot;<span style="color: #007800;">$i</span>&quot;</span> <span style="color: #ff0000;">&quot;-&gt;&quot;</span> <span style="color: #ff0000;">&quot;<span style="color: #780078;">`echo $i | sed &quot;s/\(.*\) - \(.*\) - \(.*\) - \(.*\).ogg/\1 - \4 - \3 - \2.ogg/&quot;`</span>&quot;</span>;
<span style="color: #000000; font-weight: bold;">done</span></pre></div></div>

<p>While will effectively  print a list of lines of the form <code>oldname -> newname</code>.</p>
<p>Of course this technique isn&#8217;t limited to the renaming I&#8217;ve done. By changing the pattern given to <code>sed</code>, one can do any kind of renaming that can be described as a regular expression replacement. Also one can change the globbing (the *) in the for loop to operate only on specific files, that match a given pattern, in the directory instead of all of them. </p>
]]></content:encoded>
			<wfw:commentRss>http://www.guyrutenberg.com/2009/01/12/batch-renaming-using-sed/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>WordPress Backup to FTP</title>
		<link>http://www.guyrutenberg.com/2009/01/06/wordpress-backup-to-ftp/</link>
		<comments>http://www.guyrutenberg.com/2009/01/06/wordpress-backup-to-ftp/#comments</comments>
		<pubDate>Tue, 06 Jan 2009 13:46:33 +0000</pubDate>
		<dc:creator>Guy</dc:creator>
				<category><![CDATA[Bash]]></category>
		<category><![CDATA[Wordpress]]></category>
		<category><![CDATA[backup]]></category>
		<category><![CDATA[FTP]]></category>

		<guid isPermaLink="false">http://www.guyrutenberg.com/?p=187</guid>
		<description><![CDATA[Update: A newer version of the script is available.
This script allows you to easily backup your WordPress blog to an FTP server. It&#8217;s actually a modification of my Wordpress Backup to Amazon S3 Script, but instead of saving the backup to Amazon S3 it uploads it to an FTP server. Another update is that now [...]]]></description>
			<content:encoded><![CDATA[<p><strong>Update:</strong> A newer version of the script is <a href="/2010/02/28/improved-ftp-backup-for-wordpress/">available</a>.</p>
<p>This script allows you to easily backup your WordPress blog to an FTP server. It&#8217;s actually a modification of my <a href="/2008/10/11/wordpress-backup-to-amazon-s3-script/">Wordpress Backup to Amazon S3 Script</a>, but instead of saving the backup to Amazon S3 it uploads it to an FTP server. Another update is that now the SQL dump includes the database creation instructions so you don&#8217;t need to create it manually before restoring from the backup.</p>
<p>Although I&#8217;ve written it with WordPress in mind (to creates backups of my blog), it isn&#8217;t WordPress specific. It can be used to backup any website that consists of a MySQL database and files. I&#8217;ve successfully used it to backup MediaWiki installation.<br />
<span id="more-187"></span></p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #666666; font-style: italic;">#!/bin/bash</span>
&nbsp;
<span style="color: #666666; font-style: italic;"># (C) 2008 Guy Rutenberg</span>
<span style="color: #666666; font-style: italic;"># This is a script that creates backups of my blog.</span>
&nbsp;
<span style="color: #007800;">DB_NAME</span>=guy_blog
<span style="color: #007800;">DB_USER</span>=<span style="color: #000000; font-weight: bold;">******</span>
<span style="color: #007800;">DB_PASS</span>=<span style="color: #000000; font-weight: bold;">******</span>
<span style="color: #007800;">DB_HOST</span>=localhost
&nbsp;
<span style="color: #007800;">BLOG_DIR</span>=<span style="color: #000000; font-weight: bold;">/</span>home<span style="color: #000000; font-weight: bold;">/</span>guyru<span style="color: #000000; font-weight: bold;">/</span>guyrutenberg.com<span style="color: #000000; font-weight: bold;">/</span>
<span style="color: #007800;">BACKUP_DIR</span>=<span style="color: #000000; font-weight: bold;">/</span>home<span style="color: #000000; font-weight: bold;">/</span>guyru<span style="color: #000000; font-weight: bold;">/</span>backups<span style="color: #000000; font-weight: bold;">/</span>
&nbsp;
<span style="color: #007800;">FTP_HOST</span>=88.198.42.4
<span style="color: #007800;">FTP_USER</span>=<span style="color: #000000; font-weight: bold;">******</span>
<span style="color: #007800;">FTP_PASS</span>=<span style="color: #000000; font-weight: bold;">******</span>
<span style="color: #007800;">FTP_BACKUP_DIR</span>=guyru<span style="color: #000000; font-weight: bold;">/</span>
&nbsp;
<span style="color: #666666; font-style: italic;"># end of configuration - you probably don't need to touch anything bellow</span>
<span style="color: #007800;">BLOG_DIR</span>=<span style="color: #000000; font-weight: bold;">`</span><span style="color: #c20cb9; font-weight: bold;">dirname</span> <span style="color: #ff0000;">&quot;<span style="color: #007800;">$BLOG_DIR</span>&quot;</span><span style="color: #000000; font-weight: bold;">`/`</span><span style="color: #c20cb9; font-weight: bold;">basename</span> <span style="color: #ff0000;">&quot;<span style="color: #007800;">$BLOG_DIR</span>&quot;</span><span style="color: #000000; font-weight: bold;">`</span>
<span style="color: #007800;">BACKUP_DIR</span>=<span style="color: #000000; font-weight: bold;">`</span><span style="color: #c20cb9; font-weight: bold;">dirname</span> <span style="color: #ff0000;">&quot;<span style="color: #007800;">$BACKUP_DIR</span>&quot;</span><span style="color: #000000; font-weight: bold;">`/`</span><span style="color: #c20cb9; font-weight: bold;">basename</span> <span style="color: #ff0000;">&quot;<span style="color: #007800;">$BACKUP_DIR</span>&quot;</span><span style="color: #000000; font-weight: bold;">`</span>
&nbsp;
<span style="color: #7a0874; font-weight: bold;">echo</span> <span style="color: #660033;">-n</span> <span style="color: #ff0000;">&quot;dumping database... &quot;</span>
<span style="color: #007800;">DUMP_NAME</span>=<span style="color: #800000;">${DB_NAME}</span>-$<span style="color: #7a0874; font-weight: bold;">&#40;</span><span style="color: #c20cb9; font-weight: bold;">date</span> +<span style="color: #000000; font-weight: bold;">%</span>Y<span style="color: #000000; font-weight: bold;">%</span>m<span style="color: #000000; font-weight: bold;">%</span>d<span style="color: #7a0874; font-weight: bold;">&#41;</span>.sql.bz2
mysqldump <span style="color: #660033;">--user</span>=<span style="color: #800000;">${DB_USER}</span> <span style="color: #660033;">--password</span>=<span style="color: #800000;">${DB_PASS}</span> <span style="color: #660033;">--host</span>=<span style="color: #800000;">${DB_HOST}</span> \
 <span style="color: #660033;">--databases</span> <span style="color: #800000;">${DB_NAME}</span> \
 <span style="color: #000000; font-weight: bold;">|</span> <span style="color: #c20cb9; font-weight: bold;">bzip2</span> <span style="color: #660033;">-c</span> <span style="color: #000000; font-weight: bold;">&gt;</span> <span style="color: #800000;">${BACKUP_DIR}</span><span style="color: #000000; font-weight: bold;">/</span><span style="color: #800000;">${DUMP_NAME}</span>
<span style="color: #000000; font-weight: bold;">if</span> <span style="color: #7a0874; font-weight: bold;">&#91;</span> <span style="color: #ff0000;">&quot;$?&quot;</span> <span style="color: #660033;">-ne</span> <span style="color: #ff0000;">&quot;0&quot;</span> <span style="color: #7a0874; font-weight: bold;">&#93;</span>; <span style="color: #000000; font-weight: bold;">then</span>
	<span style="color: #7a0874; font-weight: bold;">echo</span> <span style="color: #ff0000;">&quot;failed!&quot;</span>
	<span style="color: #7a0874; font-weight: bold;">exit</span> <span style="color: #000000;">1</span>
<span style="color: #000000; font-weight: bold;">fi</span>
<span style="color: #7a0874; font-weight: bold;">echo</span> <span style="color: #ff0000;">&quot;done&quot;</span>
&nbsp;
<span style="color: #7a0874; font-weight: bold;">echo</span> <span style="color: #660033;">-n</span> <span style="color: #ff0000;">&quot;Creating tarball... &quot;</span>
<span style="color: #007800;">TAR_NAME</span>=<span style="color: #800000;">${BLOG_DIR##*/}</span>-$<span style="color: #7a0874; font-weight: bold;">&#40;</span><span style="color: #c20cb9; font-weight: bold;">date</span> +<span style="color: #000000; font-weight: bold;">%</span>Y<span style="color: #000000; font-weight: bold;">%</span>m<span style="color: #000000; font-weight: bold;">%</span>d<span style="color: #7a0874; font-weight: bold;">&#41;</span>.tar.bz2
<span style="color: #c20cb9; font-weight: bold;">tar</span> <span style="color: #660033;">-cjf</span> <span style="color: #800000;">${BACKUP_DIR}</span><span style="color: #000000; font-weight: bold;">/</span><span style="color: #800000;">${BLOG_DIR##*/}</span>-$<span style="color: #7a0874; font-weight: bold;">&#40;</span><span style="color: #c20cb9; font-weight: bold;">date</span> +<span style="color: #000000; font-weight: bold;">%</span>Y<span style="color: #000000; font-weight: bold;">%</span>m<span style="color: #000000; font-weight: bold;">%</span>d<span style="color: #7a0874; font-weight: bold;">&#41;</span>.tar.bz2 <span style="color: #800000;">${BLOG_DIR}</span>
<span style="color: #000000; font-weight: bold;">if</span> <span style="color: #7a0874; font-weight: bold;">&#91;</span> <span style="color: #ff0000;">&quot;$?&quot;</span> <span style="color: #660033;">-ne</span> <span style="color: #ff0000;">&quot;0&quot;</span> <span style="color: #7a0874; font-weight: bold;">&#93;</span>; <span style="color: #000000; font-weight: bold;">then</span>
	<span style="color: #7a0874; font-weight: bold;">echo</span> <span style="color: #ff0000;">&quot;failed!&quot;</span>
	<span style="color: #7a0874; font-weight: bold;">exit</span> <span style="color: #000000;">2</span>
<span style="color: #000000; font-weight: bold;">fi</span>
<span style="color: #7a0874; font-weight: bold;">echo</span> <span style="color: #ff0000;">&quot;done&quot;</span>
&nbsp;
<span style="color: #7a0874; font-weight: bold;">echo</span> <span style="color: #660033;">-n</span> <span style="color: #ff0000;">&quot;Uploading SQL dump and tarball to FTP... &quot;</span>
lftp <span style="color: #660033;">-u</span> <span style="color: #800000;">${FTP_USER}</span>,<span style="color: #800000;">${FTP_PASS}</span> <span style="color: #800000;">${FTP_HOST}</span> <span style="color: #cc0000; font-style: italic;">&lt;&lt;EOF
cd &quot;${FTP_BACKUP_DIR}&quot;
put &quot;${BACKUP_DIR}/${DUMP_NAME}&quot;
put &quot;${BACKUP_DIR}/${TAR_NAME}&quot;
&nbsp;
EOF</span>
<span style="color: #000000; font-weight: bold;">if</span> <span style="color: #7a0874; font-weight: bold;">&#91;</span> <span style="color: #ff0000;">&quot;$?&quot;</span> <span style="color: #660033;">-ne</span> <span style="color: #ff0000;">&quot;0&quot;</span> <span style="color: #7a0874; font-weight: bold;">&#93;</span>; <span style="color: #000000; font-weight: bold;">then</span>
	<span style="color: #7a0874; font-weight: bold;">echo</span> <span style="color: #ff0000;">&quot;failed!&quot;</span>
	<span style="color: #7a0874; font-weight: bold;">exit</span> <span style="color: #000000;">3</span>
<span style="color: #000000; font-weight: bold;">fi</span>
<span style="color: #7a0874; font-weight: bold;">echo</span> <span style="color: #ff0000;">&quot;done&quot;</span></pre></div></div>

<p>The configuration is done using the variables in the beginning of the script (until the &#8220;end of configuration&#8221; line). They are pretty self explanatory, and you should adjust them for your site before using the script. If there are variables you don&#8217;t understand what they do, see the post about the <a href="/2008/10/11/wordpress-backup-to-amazon-s3-script">previous script</a>, and don&#8217;t hesitate to comment.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.guyrutenberg.com/2009/01/06/wordpress-backup-to-ftp/feed/</wfw:commentRss>
		<slash:comments>21</slash:comments>
		</item>
		<item>
		<title>Backup a SourceForge hosted SVN repository &#8211; sf-svn-backup</title>
		<link>http://www.guyrutenberg.com/2008/12/12/backup-a-sourceforge-hosted-svn-repository-sf-svn-backup/</link>
		<comments>http://www.guyrutenberg.com/2008/12/12/backup-a-sourceforge-hosted-svn-repository-sf-svn-backup/#comments</comments>
		<pubDate>Fri, 12 Dec 2008 17:49:30 +0000</pubDate>
		<dc:creator>Guy</dc:creator>
				<category><![CDATA[Bash]]></category>
		<category><![CDATA[backup]]></category>
		<category><![CDATA[svn]]></category>

		<guid isPermaLink="false">http://www.guyrutenberg.com/?p=175</guid>
		<description><![CDATA[SourceForge urges their users to backup the code repositories of their projects. As I have several projects hosted with SourceForge, I should do it too. Making the backups isn&#8217;t complicated at all, but because it isn&#8217;t automated properly, I&#8217;ve been lazy with it.
sf-svn-backup was written in order to simply automate the process. The script is [...]]]></description>
			<content:encoded><![CDATA[<p>SourceForge urges their users to backup the code repositories of their projects. As I have several projects hosted with SourceForge, I should do it too. Making the backups isn&#8217;t complicated at all, but because it isn&#8217;t automated properly, I&#8217;ve been lazy with it.</p>
<p><a href="/wp-content/uploads/2008/12/sf-svn-backup.gz"><code>sf-svn-backup</code></a> was written in order to simply automate the process. The script is pretty simple to use, just pass as the first argument the project name and the script will write down to <code>stdout</code> the dump file.</p>
<p>For example:</p>

<div class="wp_syntax"><div class="code"><pre class="text" style="font-family:monospace;">sf-svn-backup openyahtzee &gt; openyahtzee.dump</pre></div></div>

<p>The project name should be it&#8217;s UNIX name (e.g. openyahtzee and not Open Yahtzee). Because the script writes the dump file directly to <code>stdout</code> it&#8217;s easy to pipe the output first through a compression program such as <code>gzip</code> to create compressed SVN dump files.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.guyrutenberg.com/2008/12/12/backup-a-sourceforge-hosted-svn-repository-sf-svn-backup/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
		<item>
		<title>tarsum &#8211; Calculate Checksum for Files inside Tar Archive</title>
		<link>http://www.guyrutenberg.com/2008/10/24/tarsum-calculate-checksum-for-files-inside-tar-archive/</link>
		<comments>http://www.guyrutenberg.com/2008/10/24/tarsum-calculate-checksum-for-files-inside-tar-archive/#comments</comments>
		<pubDate>Fri, 24 Oct 2008 20:02:05 +0000</pubDate>
		<dc:creator>Guy</dc:creator>
				<category><![CDATA[Bash]]></category>
		<category><![CDATA[Projects]]></category>
		<category><![CDATA[tarsum]]></category>

		<guid isPermaLink="false">http://www.guyrutenberg.com/?p=149</guid>
		<description><![CDATA[Update: I&#8217;ve released tarsum-0.2, a new version of tarsum.
Some time ago, I got back a hard disk back from data recovery. One of the annoying issues I encountered with the recovered data was corrupted files. Some files looked like they were recovered successfully but their content was corrupted. The ones that were configuration files, where [...]]]></description>
			<content:encoded><![CDATA[<p><b>Update</b>: I&#8217;ve released <a href="/2009/04/29/tarsum-02-a-read-only-version-of-tarsum/">tarsum-0.2</a>, a new version of <code>tarsum</code>.</p>
<p>Some time ago, I got back a hard disk back from data recovery. One of the annoying issues I encountered with the recovered data was corrupted files. Some files looked like they were recovered successfully but their content was corrupted. The ones that were configuration files, where usually easy to detect, as it raised errors in programs that tried to use them. But when such error occurs in some general text file, (or inside the data of an SQL dump), the file may seem correctly fine unless closely inspected.</p>
<p>I have an habit of storing old backups on CDs (they are initially made to online storage), I do it in order to reduce backup costs. But the recovered/corrupted data issue raised some concerns about my ability to recover using this disks. Assuming that I have a disk failure, and I couldn&#8217;t recover from my online backups for reason, how can I check the integrity of my CD backups?</p>
<p>Only storing and comparing hash signature for the whole archive, is almost useless. It allows you to validate whether all the files are probably fine, but it can&#8217;t tell apart one corrupted file in the archive from a completed corrupted archive. My idea was to calculate checksum (hash) for each file in the data and store the signature in a way that would allow me to see which individual files are corrupted.</p>
<p>This is where <code>tarsum</code> comes to the rescue. As it&#8217;s name applies it calculate checksum for each file in the archive. You can download <code>tarsum</code> from <a href="/wp-content/uploads/2008/10/tarsum.gz">here</a>.</p>
<p>Using tarsum is pretty straight forward.</p>

<div class="wp_syntax"><div class="code"><pre class="text" style="font-family:monospace;">tarsum backup.tar &gt; backup.tar.md5</pre></div></div>

<p>Calculates the MD5 checksums of the files. You can specify other hashes as well, by passing a tool that calculates it (it must work like <code>md5sum</code>).</p>

<div class="wp_syntax"><div class="code"><pre class="text" style="font-family:monospace;">tarsum --checksum=sha256sum backup.tar &gt; backup.tar.sha256</pre></div></div>

<p>To verify the integrity of the files inside the archive we use the <code>diff</code> command:</p>

<div class="wp_syntax"><div class="code"><pre class="text" style="font-family:monospace;">tarsum backup.tar | diff backup.tar.md5 -</pre></div></div>

<p>where <code>backup.tar.md5</code> is the original signature file we created. This is possible because the signatures are sorted alphabetically by the file name inside the archive, so it the order of the files is always the same.</p>
<p>Note that if you use an updated version of GNU tar, <code>tarsum</code> can also operate directly on compressed archives (e.g. tar.bz2, tar.gz).</p>
]]></content:encoded>
			<wfw:commentRss>http://www.guyrutenberg.com/2008/10/24/tarsum-calculate-checksum-for-files-inside-tar-archive/feed/</wfw:commentRss>
		<slash:comments>6</slash:comments>
		</item>
		<item>
		<title>s3backup &#8211; Easy backups of Folders to Amazon S3</title>
		<link>http://www.guyrutenberg.com/2008/10/21/s3backup-easy-backups-of-folders-to-amazon-s3/</link>
		<comments>http://www.guyrutenberg.com/2008/10/21/s3backup-easy-backups-of-folders-to-amazon-s3/#comments</comments>
		<pubDate>Tue, 21 Oct 2008 19:24:49 +0000</pubDate>
		<dc:creator>Guy</dc:creator>
				<category><![CDATA[Bash]]></category>
		<category><![CDATA[Amazon AWS]]></category>
		<category><![CDATA[backup]]></category>

		<guid isPermaLink="false">http://www.guyrutenberg.com/?p=142</guid>
		<description><![CDATA[This is an updated version of my previous backups script &#8211; Backup Directories to Amazon S3 Script. The new script works much better and safer. Unlike the old script, the new one creates the tarballs in a temporary file under /tmp, and allows more control over the backup process.


#! /bin/bash
&#160;
# Copyright (C) 2008 by Guy [...]]]></description>
			<content:encoded><![CDATA[<p>This is an updated version of my previous backups script &#8211; <a href="/2008/03/01/backup-directories-to-amazon-s3-script/">Backup Directories to Amazon S3 Script</a>. The new script works much better and safer. Unlike the old script, the new one creates the tarballs in a temporary file under <code>/tmp</code>, and allows more control over the backup process.</p>
<p><span id="more-142"></span></p>

<div class="wp_syntax"><div class="code"><pre class="text" style="font-family:monospace;">#! /bin/bash
&nbsp;
# Copyright (C) 2008 by Guy Rutenberg
&nbsp;
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the
# Free Software Foundation, Inc.,
# 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
&nbsp;
PROG=s3backup
DESCRIPTION=&quot;Backup directories to Amazon S3&quot;
SYNOPSIS=&quot;${PROG} [options] DIR BUCKET&quot;
VERSION=0.2
&nbsp;
print_usage () {
    echo &quot;Usage: ${SYNOPSIS}&quot;
    echo &quot;Save a backup of DIR to Amazon S3 bucket BUCKET&quot;
}
print_help ()  {
    print_usage
    cat &lt;&lt; EOF
&nbsp;
Options:
    -h, --help          show this help message and exit
    --version           show program's version number and exit
    -f, --full-path     use the path to dir in archive instead of only dir
    -v, --verbose       be verbose about progress
&nbsp;
Example:
    s3backup ~/Documents guyru-backup/
EOF
}
&nbsp;
print_version () {
    echo &quot;$PROG $VERSION&quot;
    echo &quot;Copyright (C) 2008 Guy Rutenberg &lt;http://www.guyrutenberg.com&gt;&quot;
}
&nbsp;
# print only if verbose
vecho () {
    if (( &quot;$VERBOSE&quot; &gt;= &quot;$1&quot; )) ; then
        echo &quot;$2&quot;
    fi
}
VERBOSE=0
TEMP=`getopt -o hvf --long help,version,verbose,full-path -n '${PROG}' -- &quot;$@&quot;`
if [ $? != 0 ] ; then
    print_usage
    exit 1
fi
&nbsp;
eval set -- &quot;$TEMP&quot;
&nbsp;
while true ; do
    case &quot;$1&quot; in
        -h|--help) print_help; exit ;;
        --version) print_version; exit ;;
        -f|--full-path) FULL_PATH=1; shift ;; 
        -v|--verbose) let VERBOSE++ ; shift ;;
        --) shift; break;;
    esac
done
&nbsp;
# no directory passed
if [ -z &quot;$1&quot; ]; then
    echo &quot;$PROG: missing DIR&quot;
    echo &quot;&quot;
    print_usage
    exit 1
fi
&nbsp;
# no bucket passed
if [ -z &quot;$2&quot; ]; then
    echo &quot;$PROG: missing BUCKET&quot;
    echo &quot;&quot;
    print_usage
    exit 2
fi
BUCKET=$2
&nbsp;
&nbsp;
DIR=$(basename &quot;$1&quot;)
DIR_PATH=$(dirname &quot;$1&quot;)
&nbsp;
DATE=$(date +%Y%m%d)
&nbsp;
&nbsp;
if [ -z &quot;$FULL_PATH&quot; ] ; then
    cd &quot;$DIR_PATH&quot;
    TAR_PATH=&quot;$DIR&quot;
else
    TAR_PATH=&quot;$1&quot;
fi
&nbsp;
vecho 1 &quot;Creating tarball&quot;
TARNAME=`mktemp`
TAR_OPTIONS=&quot;-c&quot;
if (( $VERBOSE &gt; 0 )); then
    TAR_OPTIONS+=&quot;v&quot;
fi
tar $TAR_OPTIONS &quot;$TAR_PATH&quot; | bzip2 -9 -c &gt; &quot;${TARNAME}&quot;
&nbsp;
vecho 1 &quot;Sending to Amazon S3&quot;
S3_OPTIONS=&quot;&quot;
if (( $VERBOSE &gt; 0 )); then
    S3_OPTIONS+=&quot;v&quot;
fi
s3cmd put $S3_OPTIONS &quot;$TARNAME&quot; &quot;s3://${BUCKET}$DIR-$DATE.tar.bz2&quot;
&nbsp;
vecho 1 &quot;Deleting intermediate files&quot;
rm &quot;$TARNAME&quot;
&nbsp;
# vim:sw=4:softtabstop=4:expandtab</pre></div></div>

<p>The script can also be downloaded from <a href="/wp-content/uploads/2008/10/s3backup.gz">s3backup.gz</a>.</p>
<p>Script usage is pretty straight forward, just pass the folder to backup as the first argument to the script, and an S3 bucket name, to which the backup will be saved as the second. You can also consult <code>s3backup --help</code>.</p>
<p>Note that you can add a prefix to your backups by adding it to the S3 bucket. For example</p>

<div class="wp_syntax"><div class="code"><pre class="text" style="font-family:monospace;">s3backup ~/Documents bucketname/prefix/</pre></div></div>

]]></content:encoded>
			<wfw:commentRss>http://www.guyrutenberg.com/2008/10/21/s3backup-easy-backups-of-folders-to-amazon-s3/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Wordpress Backup to Amazon S3 Script</title>
		<link>http://www.guyrutenberg.com/2008/10/11/wordpress-backup-to-amazon-s3-script/</link>
		<comments>http://www.guyrutenberg.com/2008/10/11/wordpress-backup-to-amazon-s3-script/#comments</comments>
		<pubDate>Sat, 11 Oct 2008 09:19:22 +0000</pubDate>
		<dc:creator>Guy</dc:creator>
				<category><![CDATA[Bash]]></category>
		<category><![CDATA[Wordpress]]></category>
		<category><![CDATA[Amazon AWS]]></category>
		<category><![CDATA[backup]]></category>

		<guid isPermaLink="false">http://www.guyrutenberg.com/?p=110</guid>
		<description><![CDATA[This is an updated version of my Wordpress Backup Script. The new version basically does the same thing: backup up a wordpress blog (actually any site that consists of files and a MySQL database). The new thing about the script is that instead of only saving the backup locally, it also uploads it to Amazon [...]]]></description>
			<content:encoded><![CDATA[<p>This is an updated version of my <a href="/2008/05/07/wordpress-backup-script/">Wordpress Backup Script</a>. The new version basically does the same thing: backup up a wordpress blog (actually any site that consists of files and a MySQL database). The new thing about the script is that instead of only saving the backup locally, it also uploads it to Amazon S3.</p>
<p><span id="more-110"></span></p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #666666; font-style: italic;">#!/bin/bash</span>
&nbsp;
<span style="color: #666666; font-style: italic;"># (C) 2008 Guy Rutenberg</span>
<span style="color: #666666; font-style: italic;"># This is a script that creates backups of my blog.</span>
&nbsp;
<span style="color: #007800;">DB_NAME</span>=database_name
<span style="color: #007800;">DB_USER</span>=database_user
<span style="color: #007800;">DB_PASS</span>=<span style="color: #000000; font-weight: bold;">****</span>
<span style="color: #007800;">DB_HOST</span>=localhost
&nbsp;
<span style="color: #666666; font-style: italic;">#no trailing slash</span>
<span style="color: #007800;">BLOG_DIR</span>=<span style="color: #000000; font-weight: bold;">/</span>path<span style="color: #000000; font-weight: bold;">/</span>to<span style="color: #000000; font-weight: bold;">/</span>blog
<span style="color: #007800;">BACKUP_DIR</span>=<span style="color: #000000; font-weight: bold;">/</span>path<span style="color: #000000; font-weight: bold;">/</span>to<span style="color: #000000; font-weight: bold;">/</span>backups
<span style="color: #007800;">S3BUCKET</span>=s3:<span style="color: #000000; font-weight: bold;">//</span>bucket-name<span style="color: #000000; font-weight: bold;">/</span>
&nbsp;
<span style="color: #666666; font-style: italic;"># end of configuration - you probably don't need to touch anything bellow</span>
<span style="color: #007800;">DUMP_NAME</span>=<span style="color: #800000;">${DB_NAME}</span>-$<span style="color: #7a0874; font-weight: bold;">&#40;</span><span style="color: #c20cb9; font-weight: bold;">date</span> +<span style="color: #000000; font-weight: bold;">%</span>Y<span style="color: #000000; font-weight: bold;">%</span>m<span style="color: #000000; font-weight: bold;">%</span>d<span style="color: #7a0874; font-weight: bold;">&#41;</span>.sql.bz2
&nbsp;
<span style="color: #7a0874; font-weight: bold;">echo</span> <span style="color: #660033;">-n</span> <span style="color: #ff0000;">&quot;dumping database... &quot;</span>
mysqldump <span style="color: #660033;">--user</span>=<span style="color: #800000;">${DB_USER}</span> <span style="color: #660033;">--password</span>=<span style="color: #800000;">${DB_PASS}</span> <span style="color: #660033;">--host</span>=<span style="color: #800000;">${DB_HOST}</span> <span style="color: #800000;">${DB_NAME}</span> \
 <span style="color: #000000; font-weight: bold;">|</span> <span style="color: #c20cb9; font-weight: bold;">bzip2</span> <span style="color: #660033;">-c</span> <span style="color: #000000; font-weight: bold;">&gt;</span> <span style="color: #800000;">${BACKUP_DIR}</span><span style="color: #000000; font-weight: bold;">/</span><span style="color: #800000;">${DUMP_NAME}</span>
<span style="color: #000000; font-weight: bold;">if</span> <span style="color: #7a0874; font-weight: bold;">&#91;</span> <span style="color: #ff0000;">&quot;$?&quot;</span> <span style="color: #660033;">-ne</span> <span style="color: #ff0000;">&quot;0&quot;</span> <span style="color: #7a0874; font-weight: bold;">&#93;</span>; <span style="color: #000000; font-weight: bold;">then</span>
	<span style="color: #7a0874; font-weight: bold;">echo</span> <span style="color: #ff0000;">&quot;failed!&quot;</span>
	<span style="color: #7a0874; font-weight: bold;">exit</span> <span style="color: #000000;">1</span>
<span style="color: #000000; font-weight: bold;">fi</span>
<span style="color: #7a0874; font-weight: bold;">echo</span> <span style="color: #ff0000;">&quot;done&quot;</span>
&nbsp;
<span style="color: #007800;">TAR_NAME</span>=<span style="color: #800000;">${BLOG_DIR##*/}</span>-$<span style="color: #7a0874; font-weight: bold;">&#40;</span><span style="color: #c20cb9; font-weight: bold;">date</span> +<span style="color: #000000; font-weight: bold;">%</span>Y<span style="color: #000000; font-weight: bold;">%</span>m<span style="color: #000000; font-weight: bold;">%</span>d<span style="color: #7a0874; font-weight: bold;">&#41;</span>.tar.bz2
<span style="color: #7a0874; font-weight: bold;">echo</span> <span style="color: #660033;">-n</span> <span style="color: #ff0000;">&quot;Creating tarball... &quot;</span>
<span style="color: #c20cb9; font-weight: bold;">tar</span> <span style="color: #660033;">-cjf</span> <span style="color: #800000;">${BACKUP_DIR}</span><span style="color: #000000; font-weight: bold;">/</span><span style="color: #800000;">${BLOG_DIR##*/}</span>-$<span style="color: #7a0874; font-weight: bold;">&#40;</span><span style="color: #c20cb9; font-weight: bold;">date</span> +<span style="color: #000000; font-weight: bold;">%</span>Y<span style="color: #000000; font-weight: bold;">%</span>m<span style="color: #000000; font-weight: bold;">%</span>d<span style="color: #7a0874; font-weight: bold;">&#41;</span>.tar.bz2 <span style="color: #800000;">${BLOG_DIR}</span>
<span style="color: #000000; font-weight: bold;">if</span> <span style="color: #7a0874; font-weight: bold;">&#91;</span> <span style="color: #ff0000;">&quot;$?&quot;</span> <span style="color: #660033;">-ne</span> <span style="color: #ff0000;">&quot;0&quot;</span> <span style="color: #7a0874; font-weight: bold;">&#93;</span>; <span style="color: #000000; font-weight: bold;">then</span>
	<span style="color: #7a0874; font-weight: bold;">echo</span> <span style="color: #ff0000;">&quot;failed!&quot;</span>
	<span style="color: #7a0874; font-weight: bold;">exit</span> <span style="color: #000000;">1</span>
<span style="color: #000000; font-weight: bold;">fi</span>
<span style="color: #7a0874; font-weight: bold;">echo</span> <span style="color: #ff0000;">&quot;done&quot;</span>
&nbsp;
<span style="color: #7a0874; font-weight: bold;">echo</span> <span style="color: #660033;">-n</span> <span style="color: #ff0000;">&quot;Uploading SQL dump to Amazon S3... &quot;</span>
s3cmd put <span style="color: #800000;">${BACKUP_DIR}</span><span style="color: #000000; font-weight: bold;">/</span><span style="color: #800000;">${DUMP_NAME}</span> <span style="color: #800000;">${S3BUCKET}</span><span style="color: #800000;">${DUMP_NAME}</span>
<span style="color: #000000; font-weight: bold;">if</span> <span style="color: #7a0874; font-weight: bold;">&#91;</span> <span style="color: #ff0000;">&quot;$?&quot;</span> <span style="color: #660033;">-ne</span> <span style="color: #ff0000;">&quot;0&quot;</span> <span style="color: #7a0874; font-weight: bold;">&#93;</span>; <span style="color: #000000; font-weight: bold;">then</span>
	<span style="color: #7a0874; font-weight: bold;">echo</span> <span style="color: #ff0000;">&quot;failed!&quot;</span>
	<span style="color: #7a0874; font-weight: bold;">exit</span> <span style="color: #000000;">1</span>
<span style="color: #000000; font-weight: bold;">fi</span>
<span style="color: #7a0874; font-weight: bold;">echo</span> <span style="color: #ff0000;">&quot;done&quot;</span>
&nbsp;
<span style="color: #7a0874; font-weight: bold;">echo</span> <span style="color: #660033;">-n</span> <span style="color: #ff0000;">&quot;Uploading tarball to Amazon S3... &quot;</span>
s3cmd put <span style="color: #800000;">${BACKUP_DIR}</span><span style="color: #000000; font-weight: bold;">/</span><span style="color: #800000;">${TAR_NAME}</span> <span style="color: #800000;">${S3BUCKET}</span><span style="color: #800000;">${TAR_NAME}</span>
<span style="color: #000000; font-weight: bold;">if</span> <span style="color: #7a0874; font-weight: bold;">&#91;</span> <span style="color: #ff0000;">&quot;$?&quot;</span> <span style="color: #660033;">-ne</span> <span style="color: #ff0000;">&quot;0&quot;</span> <span style="color: #7a0874; font-weight: bold;">&#93;</span>; <span style="color: #000000; font-weight: bold;">then</span>
	<span style="color: #7a0874; font-weight: bold;">echo</span> <span style="color: #ff0000;">&quot;failed!&quot;</span>
	<span style="color: #7a0874; font-weight: bold;">exit</span> <span style="color: #000000;">1</span>
<span style="color: #000000; font-weight: bold;">fi</span>
<span style="color: #7a0874; font-weight: bold;">echo</span> <span style="color: #ff0000;">&quot;done&quot;</span></pre></div></div>

<p>As with the older <a href="/2008/03/01/backup-directories-to-amazon-s3-script/">Amazon S3 backup script</a>, this one also required you to install the <a href="http://s3tools.logix.cz/s3cmd">s3cmd</a> package.</p>
<p>The first six variables:</p>
<ul>
<li><code>DB_NAME</code></li>
<li><code>DB_USER</code></li>
<li><code>DB_PASS</code></li>
<li><code>DB_HOST</code></li>
<li><code>BLOG_DIR</code></li>
<li><code>BACKUP_DIR</code></li>
</ul>
<p>Are the pretty self explanatory and have the same meaning as in the <a href="/2008/05/07/wordpress-backup-script/">old script</a>.</p>
<p>The script has one new variable: <code>S3BUCKET</code>. This should be set to the S3 bucket to which the backups should be saved. If you want to add some prefix to the backups files on S3 this is the place.</p>
<p>The script keeps the backups both in the local backup directory and the Amazon S3.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.guyrutenberg.com/2008/10/11/wordpress-backup-to-amazon-s3-script/feed/</wfw:commentRss>
		<slash:comments>17</slash:comments>
		</item>
		<item>
		<title>WordPress Backup Script</title>
		<link>http://www.guyrutenberg.com/2008/05/07/wordpress-backup-script/</link>
		<comments>http://www.guyrutenberg.com/2008/05/07/wordpress-backup-script/#comments</comments>
		<pubDate>Wed, 07 May 2008 08:15:52 +0000</pubDate>
		<dc:creator>Guy</dc:creator>
				<category><![CDATA[Bash]]></category>
		<category><![CDATA[Tips]]></category>
		<category><![CDATA[Wordpress]]></category>
		<category><![CDATA[backup]]></category>

		<guid isPermaLink="false">http://www.guyrutenberg.com/?p=54</guid>
		<description><![CDATA[This is a small script I&#8217;ve written to automate my server-side backups of my blogs. It creates a backup of both the database and the actual WordPress files.

#!/bin/bash
&#160;
# (C) 2008 Guy Rutenberg - http://www.guyrutenberg.com
# This is a script that creates backups of blogs.
&#160;
DB_NAME=guy_blog
DB_USER=guy_root
DB_PASS=****
DB_HOST=localhost
&#160;
#no trailing slash
BLOG_DIR=/home/guyru/guyrutenberg.com
BACKUP_DIR=/home/guyru/backups
&#160;
&#160;
echo -n &#34;dumping database... &#34;
mysqldump --user=${DB_USER} --password=${DB_PASS} --host=${DB_HOST} ${DB_NAME} \
 &#124; [...]]]></description>
			<content:encoded><![CDATA[<p>This is a small script I&#8217;ve written to automate my server-side backups of my blogs. It creates a backup of both the database and the actual WordPress files.</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #666666; font-style: italic;">#!/bin/bash</span>
&nbsp;
<span style="color: #666666; font-style: italic;"># (C) 2008 Guy Rutenberg - http://www.guyrutenberg.com</span>
<span style="color: #666666; font-style: italic;"># This is a script that creates backups of blogs.</span>
&nbsp;
<span style="color: #007800;">DB_NAME</span>=guy_blog
<span style="color: #007800;">DB_USER</span>=guy_root
<span style="color: #007800;">DB_PASS</span>=<span style="color: #000000; font-weight: bold;">****</span>
<span style="color: #007800;">DB_HOST</span>=localhost
&nbsp;
<span style="color: #666666; font-style: italic;">#no trailing slash</span>
<span style="color: #007800;">BLOG_DIR</span>=<span style="color: #000000; font-weight: bold;">/</span>home<span style="color: #000000; font-weight: bold;">/</span>guyru<span style="color: #000000; font-weight: bold;">/</span>guyrutenberg.com
<span style="color: #007800;">BACKUP_DIR</span>=<span style="color: #000000; font-weight: bold;">/</span>home<span style="color: #000000; font-weight: bold;">/</span>guyru<span style="color: #000000; font-weight: bold;">/</span>backups
&nbsp;
&nbsp;
<span style="color: #7a0874; font-weight: bold;">echo</span> <span style="color: #660033;">-n</span> <span style="color: #ff0000;">&quot;dumping database... &quot;</span>
mysqldump <span style="color: #660033;">--user</span>=<span style="color: #800000;">${DB_USER}</span> <span style="color: #660033;">--password</span>=<span style="color: #800000;">${DB_PASS}</span> <span style="color: #660033;">--host</span>=<span style="color: #800000;">${DB_HOST}</span> <span style="color: #800000;">${DB_NAME}</span> \
 <span style="color: #000000; font-weight: bold;">|</span> <span style="color: #c20cb9; font-weight: bold;">bzip2</span> <span style="color: #660033;">-c</span> <span style="color: #000000; font-weight: bold;">&gt;</span> <span style="color: #800000;">${BACKUP_DIR}</span><span style="color: #000000; font-weight: bold;">/</span><span style="color: #800000;">${DB_NAME}</span>-$<span style="color: #7a0874; font-weight: bold;">&#40;</span><span style="color: #c20cb9; font-weight: bold;">date</span> +<span style="color: #000000; font-weight: bold;">%</span>Y<span style="color: #000000; font-weight: bold;">%</span>m<span style="color: #000000; font-weight: bold;">%</span>d<span style="color: #7a0874; font-weight: bold;">&#41;</span>.sql.bz2
<span style="color: #000000; font-weight: bold;">if</span> <span style="color: #7a0874; font-weight: bold;">&#91;</span> <span style="color: #ff0000;">&quot;$?&quot;</span> <span style="color: #660033;">-ne</span> <span style="color: #ff0000;">&quot;0&quot;</span> <span style="color: #7a0874; font-weight: bold;">&#93;</span>; <span style="color: #000000; font-weight: bold;">then</span>
	<span style="color: #7a0874; font-weight: bold;">echo</span> <span style="color: #660033;">-e</span> <span style="color: #ff0000;">&quot;<span style="color: #000099; font-weight: bold;">\n</span>mysqldump failed!&quot;</span>
	<span style="color: #7a0874; font-weight: bold;">exit</span> <span style="color: #000000;">1</span>
<span style="color: #000000; font-weight: bold;">fi</span>
<span style="color: #7a0874; font-weight: bold;">echo</span> <span style="color: #ff0000;">&quot;done&quot;</span>
&nbsp;
&nbsp;
<span style="color: #7a0874; font-weight: bold;">echo</span> <span style="color: #660033;">-n</span> <span style="color: #ff0000;">&quot;Creating tarball... &quot;</span>
<span style="color: #c20cb9; font-weight: bold;">tar</span> <span style="color: #660033;">-cjf</span> <span style="color: #800000;">${BACKUP_DIR}</span><span style="color: #000000; font-weight: bold;">/</span><span style="color: #800000;">${BLOG_DIR##*/}</span>-$<span style="color: #7a0874; font-weight: bold;">&#40;</span><span style="color: #c20cb9; font-weight: bold;">date</span> +<span style="color: #000000; font-weight: bold;">%</span>Y<span style="color: #000000; font-weight: bold;">%</span>m<span style="color: #000000; font-weight: bold;">%</span>d<span style="color: #7a0874; font-weight: bold;">&#41;</span>.tar.bz2 <span style="color: #800000;">${BLOG_DIR}</span>
<span style="color: #000000; font-weight: bold;">if</span> <span style="color: #7a0874; font-weight: bold;">&#91;</span> <span style="color: #ff0000;">&quot;$?&quot;</span> <span style="color: #660033;">-ne</span> <span style="color: #ff0000;">&quot;0&quot;</span> <span style="color: #7a0874; font-weight: bold;">&#93;</span>; <span style="color: #000000; font-weight: bold;">then</span>
	<span style="color: #7a0874; font-weight: bold;">echo</span> <span style="color: #660033;">-e</span> <span style="color: #ff0000;">&quot;<span style="color: #000099; font-weight: bold;">\n</span>tarball creation failed!&quot;</span>
	<span style="color: #7a0874; font-weight: bold;">exit</span> <span style="color: #000000;">1</span>
<span style="color: #000000; font-weight: bold;">fi</span>
<span style="color: #7a0874; font-weight: bold;">echo</span> <span style="color: #ff0000;">&quot;done&quot;</span></pre></div></div>

<p><span id="more-54"></span><br />
Copy the above code and save it to a file called <code>backup-blog</code>. Before you can use it there are some configurations to be done. At the beginning of the script (right after the copyright notice), you will find some variable you need to set.</p>
<p>The first set of variables is the database configuration. Set <code>DB_NAME</code> to the name of your database, <code>DB_USER</code> to your database username, <code>DB_PASS</code> to the user&#8217;s password and <code>DB_HOST</code> to the hostname of the database server. For most users the <code>DB_HOST</code> should be set for <code>localhost</code> (but some users need to specify a different value here).</p>
<p>The next variable is <code>BLOG_DIR</code>. Set it to the full path of the directory where you installed WordPress. <code>BACKUP_DIR</code> should be set to the full path of the directory you to keep the backups in, make sure the user which will run the script has write access to it. Notice that both paths shouldn&#8217;t end with a trailing &#8216;/&#8217; (slash).</p>
<p>Now that the configuration is over, make the script executable:<br />
<code>chmod +x backup-blog</code><br />
Now you can test that everything works ok by running the script: <code>./backup-blog</code>. If the script failed for some reason make sure your configuration is correct.</p>
<p>The next step is to automate the backup process using <code>cron</code>. Run <code>crontab -e</code> to add an entry to the backup script. Append the following line to the <code>crontab</code>:<br />
<code>30 1 * * 4 /path/to/backup-blog</code><br />
This will run the script automatically for you once a week, on 01:30 AM every Friday. Be sure to check up the backup folder from time to time to make sure everything is running smooth.</p>
<p>Note that while the script was originally intended to create backups of WordPress blogs it can also be used to backup any kind of website that needs both files and database backups, like MediaWiki, Drupal, or almost any kind of CMS and blogging platform.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.guyrutenberg.com/2008/05/07/wordpress-backup-script/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Convert PNMs to DjVu</title>
		<link>http://www.guyrutenberg.com/2008/03/11/convert-pnms-to-djvu/</link>
		<comments>http://www.guyrutenberg.com/2008/03/11/convert-pnms-to-djvu/#comments</comments>
		<pubDate>Tue, 11 Mar 2008 20:47:38 +0000</pubDate>
		<dc:creator>Guy</dc:creator>
				<category><![CDATA[Bash]]></category>
		<category><![CDATA[Projects]]></category>
		<category><![CDATA[DjVu]]></category>

		<guid isPermaLink="false">http://www.guyrutenberg.com/2008/03/11/convert-pnms-to-djvu/</guid>
		<description><![CDATA[I&#8217;ve decided to scan some notebooks. After researching a bit, I&#8217;ve decided to use DjVu (instead of PDF which I normally use). I&#8217;ve chose to use DjVu because it offered great quality with very good compression rate (~26KB per page) in lineart (black and white). 
While XSane can natively save a multipage project into PDF [...]]]></description>
			<content:encoded><![CDATA[<p>I&#8217;ve decided to scan some notebooks. After researching a bit, I&#8217;ve decided to use <a href="http://www.djvu.org">DjVu</a> (instead of PDF which I normally use). I&#8217;ve chose to use DjVu because it offered great quality with very good compression rate (~26KB per page) in lineart (black and white). </p>
<p>While XSane can natively save a multipage project into PDF it can&#8217;t do so for DjVu. So, the solution is to use the PNMs generated by XSane and convert them using the command line tools offered by <a href="http://djvu.sourceforge.net/">DjVuLibre</a> to bundle them together to a DjVu file. As you can guess doing this manually is pretty hard work. To make this task easier I&#8217;ve written a small bash script to automate the process.<br />
<span id="more-45"></span></p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #666666; font-style: italic;">#!/bin/bash</span>
<span style="color: #666666; font-style: italic;"># pnm2djvu </span>
<span style="color: #666666; font-style: italic;">#(C) 2008 Guy Rutenberg</span>
<span style="color: #666666; font-style: italic;">#pnm2djvu - A small script that takes a directory with PNMs and converts them to </span>
<span style="color: #666666; font-style: italic;">#monochrome (bitonal) djvu file with the PNMs ordered alphabetically.</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">if</span> <span style="color: #7a0874; font-weight: bold;">&#91;</span> <span style="color: #660033;">-z</span> <span style="color: #ff0000;">&quot;$1&quot;</span> <span style="color: #7a0874; font-weight: bold;">&#93;</span>; <span style="color: #000000; font-weight: bold;">then</span>
	<span style="color: #7a0874; font-weight: bold;">echo</span> <span style="color: #ff0000;">&quot;Directory of PNMs reguired&quot;</span>
	<span style="color: #7a0874; font-weight: bold;">exit</span>
<span style="color: #000000; font-weight: bold;">fi</span>
<span style="color: #000000; font-weight: bold;">if</span> <span style="color: #7a0874; font-weight: bold;">&#91;</span> <span style="color: #660033;">-z</span> <span style="color: #ff0000;">&quot;$2&quot;</span> <span style="color: #7a0874; font-weight: bold;">&#93;</span>; <span style="color: #000000; font-weight: bold;">then</span>
	<span style="color: #7a0874; font-weight: bold;">echo</span> <span style="color: #ff0000;">&quot;filename for output required&quot;</span>
	<span style="color: #7a0874; font-weight: bold;">exit</span>
<span style="color: #000000; font-weight: bold;">fi</span>
&nbsp;
<span style="color: #007800;">pnms</span>=<span style="color: #000000; font-weight: bold;">`</span><span style="color: #c20cb9; font-weight: bold;">ls</span> <span style="color: #ff0000;">&quot;$1/&quot;</span> <span style="color: #000000; font-weight: bold;">|</span> <span style="color: #c20cb9; font-weight: bold;">grep</span> <span style="color: #ff0000;">&quot;.pnm$&quot;</span><span style="color: #000000; font-weight: bold;">`</span>
<span style="color: #007800;">pagenum</span>=<span style="color: #000000;">1</span>
<span style="color: #000000; font-weight: bold;">for</span> i <span style="color: #000000; font-weight: bold;">in</span> <span style="color: #800000;">${pnms}</span>; <span style="color: #000000; font-weight: bold;">do</span>
	<span style="color: #7a0874; font-weight: bold;">echo</span> <span style="color: #ff0000;">&quot;Processing page number <span style="color: #007800;">${pagenum}</span>&quot;</span>
	cjb2 <span style="color: #ff0000;">&quot;$1/<span style="color: #007800;">$i</span>&quot;</span>  <span style="color: #ff0000;">&quot;page<span style="color: #007800;">$i</span>.djvu&quot;</span>
	<span style="color: #000000; font-weight: bold;">if</span> <span style="color: #7a0874; font-weight: bold;">&#91;</span> <span style="color: #660033;">-f</span> <span style="color: #ff0000;">&quot;$2&quot;</span> <span style="color: #7a0874; font-weight: bold;">&#93;</span>; <span style="color: #000000; font-weight: bold;">then</span>
		djvm <span style="color: #660033;">-i</span> <span style="color: #ff0000;">&quot;$2&quot;</span> <span style="color: #ff0000;">&quot;page<span style="color: #007800;">$i</span>.djvu&quot;</span>
	<span style="color: #000000; font-weight: bold;">else</span>
		djvm <span style="color: #660033;">-c</span> <span style="color: #ff0000;">&quot;$2&quot;</span> <span style="color: #ff0000;">&quot;page<span style="color: #007800;">$i</span>.djvu&quot;</span>
	<span style="color: #000000; font-weight: bold;">fi</span>
	<span style="color: #c20cb9; font-weight: bold;">rm</span> <span style="color: #ff0000;">&quot;page<span style="color: #007800;">$i</span>.djvu&quot;</span>
	<span style="color: #007800;">pagenum</span>=<span style="color: #000000; font-weight: bold;">`</span><span style="color: #c20cb9; font-weight: bold;">expr</span> <span style="color: #007800;">$pagenum</span> + <span style="color: #000000;">1</span><span style="color: #000000; font-weight: bold;">`</span>
<span style="color: #000000; font-weight: bold;">done</span>;</pre></div></div>

<p>Using the script is pretty simple. Pass as the first argument a path to a directory holding the PNMs (it can also be &#8220;.&#8221;), and the name of the output DjVu file. If the file already exist the script will append all the PNMs to it, otherwise it will just create a new one.</p>
<p>I&#8217;ve released this script following the &#8220;release early&#8221; practice in the open-source community. This script is far from perfect and I plan t improve when I&#8217;ve some more time. Currently this script has some major drawbacks such as fixed DPI (300) and it only support bitonal (black and white) compression. As I wrote above, I plan to address this issues and make the script more flexible in the future.</p>
<p>Despite its drawbacks I hope you will still find this script useful. If you have any questions or suggestions please comment.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.guyrutenberg.com/2008/03/11/convert-pnms-to-djvu/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Backup Directories To Amazon S3 Script</title>
		<link>http://www.guyrutenberg.com/2008/03/01/backup-directories-to-amazon-s3-script/</link>
		<comments>http://www.guyrutenberg.com/2008/03/01/backup-directories-to-amazon-s3-script/#comments</comments>
		<pubDate>Sat, 01 Mar 2008 20:12:27 +0000</pubDate>
		<dc:creator>Guy</dc:creator>
				<category><![CDATA[Bash]]></category>
		<category><![CDATA[Linux]]></category>
		<category><![CDATA[Projects]]></category>
		<category><![CDATA[Tips]]></category>
		<category><![CDATA[Amazon AWS]]></category>
		<category><![CDATA[backup]]></category>

		<guid isPermaLink="false">http://www.guyrutenberg.com/2008/03/01/backup-directories-to-amazon-s3-script/</guid>
		<description><![CDATA[This is a small script I wrote today, to automate my backups, which I do on Amazon S3. This is fairly short, yet useful bash script that utilize the s3cmd to do the actual sending of the files.


#! /bin/bash
&#160;
# 2008 (C) Guy Rutenberg
#This is a script to backup a given folder and send them to [...]]]></description>
			<content:encoded><![CDATA[<p>This is a small script I wrote today, to automate my backups, which I do on <a href="http://aws.amazon.com/s3">Amazon S3</a>. This is fairly short, yet useful bash script that utilize the <a href="http://s3tools.logix.cz/s3cmd">s3cmd</a> to do the actual sending of the files.<br />
<span id="more-43"></span></p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #666666; font-style: italic;">#! /bin/bash</span>
&nbsp;
<span style="color: #666666; font-style: italic;"># 2008 (C) Guy Rutenberg</span>
<span style="color: #666666; font-style: italic;">#This is a script to backup a given folder and send them to Amazon S3</span>
&nbsp;
<span style="color: #666666; font-style: italic;">#string trailing slash (if exists)</span>
<span style="color: #007800;">DIR</span>=<span style="color: #ff0000;">&quot;<span style="color: #007800;">${1/%\//}</span>&quot;</span>
<span style="color: #007800;">TARNAME</span>=<span style="color: #ff0000;">&quot;<span style="color: #007800;">${DIR}</span>-<span style="color: #007800;">$(date +%Y%m%d)</span>.tar.bz2&quot;</span>
<span style="color: #000000; font-weight: bold;">if</span> <span style="color: #7a0874; font-weight: bold;">&#91;</span> <span style="color: #660033;">-e</span> <span style="color: #800000;">${TARNAME}</span> <span style="color: #7a0874; font-weight: bold;">&#93;</span>
<span style="color: #000000; font-weight: bold;">then</span>
	<span style="color: #7a0874; font-weight: bold;">echo</span> <span style="color: #ff0000;">&quot;Refusing to overwrite existing file - <span style="color: #007800;">${TARNAME}</span>&quot;</span>
	<span style="color: #7a0874; font-weight: bold;">exit</span>
<span style="color: #000000; font-weight: bold;">fi</span>
<span style="color: #7a0874; font-weight: bold;">echo</span> <span style="color: #ff0000;">&quot;Creating tarball&quot;</span>
<span style="color: #c20cb9; font-weight: bold;">tar</span> <span style="color: #660033;">-vc</span> <span style="color: #ff0000;">&quot;$1&quot;</span> <span style="color: #000000; font-weight: bold;">|</span> <span style="color: #c20cb9; font-weight: bold;">bzip2</span> <span style="color: #660033;">-9</span> <span style="color: #660033;">-c</span> <span style="color: #000000; font-weight: bold;">&gt;</span> <span style="color: #ff0000;">&quot;<span style="color: #007800;">${TARNAME}</span>&quot;</span>
&nbsp;
<span style="color: #7a0874; font-weight: bold;">echo</span> <span style="color: #ff0000;">&quot;Sending to Amazon S3&quot;</span>
s3cmd put <span style="color: #660033;">-v</span> <span style="color: #ff0000;">&quot;<span style="color: #007800;">${TARNAME}</span>&quot;</span> s3:<span style="color: #000000; font-weight: bold;">//</span>guyru-backup<span style="color: #000000; font-weight: bold;">/</span>
&nbsp;
<span style="color: #7a0874; font-weight: bold;">echo</span> <span style="color: #ff0000;">&quot;Deleting intermediate files&quot;</span>
<span style="color: #c20cb9; font-weight: bold;">rm</span> <span style="color: #ff0000;">&quot;<span style="color: #007800;">${TARNAME}</span>&quot;</span></pre></div></div>

<p>To use the script just save it somewhere and make it executable. Remember to substitute <code>guyru-backup</code> with your own bucket name. Notice that you must have s3cmd installed (for Gentoo it&#8217;s available under <code>net-misc/s3cmd</code>). s3cmd is one of the best interfaces to the S3 service I&#8217;ve seen, so you&#8217;ll probably want to install it anyway.</p>
<p>The script usage instructions are pretty simple. Just call the script and pass the path to the folder you want to backup as the first argument.</p>
<p>I intend to use this script to fully automate my backups using cron (maybe this way I&#8217;ll remember to the backups regularly).</p>
<p>I hope you will find this script useful. If you have any suggestions or comment please send them.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.guyrutenberg.com/2008/03/01/backup-directories-to-amazon-s3-script/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

<!-- Dynamic Page Served (once) in 1.406 seconds -->
