<?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>Computer Support &#187; Remote archive</title>
	<atom:link href="http://www.xiitec.com/blog/tag/remote-archive/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.xiitec.com/blog</link>
	<description></description>
	<lastBuildDate>Wed, 30 Dec 2009 08:40:28 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9.2</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Avoiding Disaster with a Remote Archive</title>
		<link>http://www.xiitec.com/blog/2008/02/08/avoiding-disaster-with-a-remote-archive/</link>
		<comments>http://www.xiitec.com/blog/2008/02/08/avoiding-disaster-with-a-remote-archive/#comments</comments>
		<pubDate>Sat, 09 Feb 2008 00:24:02 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[Shell scripts]]></category>
		<category><![CDATA[Remote archive]]></category>

		<guid isPermaLink="false">http://www.xiitec.com/blog/?p=105</guid>
		<description><![CDATA[Whether or not you have a good backup strategy, with tape rotation and so forth, it&#8217;s still a nice insurance policy to identify a half-dozen critical files and have them sent to a separate off-site archive system. Even if it&#8217;s just that one key file that contains customer addresses, invoices, or even email from your [...]]]></description>
			<content:encoded><![CDATA[<p>Whether or not you have a good backup strategy, with tape rotation and so forth, it&#8217;s still a nice insurance policy to identify a half-dozen critical files and have them sent to a separate off-site archive system. Even if it&#8217;s just that one key file that contains customer addresses, invoices, or even email from your sweetheart, having an occasional off-site archive can save your life when you least expect it.</p>
<p><span id="more-105"></span></p>
<p>This sounds more complex than it really is, because as you&#8217;ll see in this script, the archive is just a file emailed to a remote mailbox and could even be pointed to a Yahoo! or Hotmail mailbox. The list of files is kept in a separate data file, with shell wildcards allowed therein. Filenames can contain spaces too, something that rather complicates the script, as you&#8217;ll see.</p>
<blockquote><p>#!/bin/sh</p>
<p># remotebackup &#8211; Takes a list of files and directories,<br />
#    builds a single archive, compressed, then emails it off to a<br />
#    remote archive site for safekeeping. It&#8217;s intended to be run<br />
#    every night for critical user files, but not intended to<br />
#    replace a more rigorous backup scheme. You should strongly<br />
#    consider using unpacker, Script #88, on the remote end too.</p>
<p>uuencode=&#8221;/usr/bin/uuencode&#8221;<br />
outfile=&#8221;/tmp/rb.$$.tgz&#8221;<br />
outfname=&#8221;backup.$(date +%y%m%d).tgz&#8221;<br />
infile=&#8221;/tmp/rb.$$.in&#8221;</p>
<p>trap &#8220;/bin/rm -f $outfile $infile&#8221; 0</p>
<p>if [ $# -ne 2 -a $# -ne 3 ] ; then<br />
echo &#8220;Usage: $(basename $0) backup-file-list remoteaddr {targetdir}&#8221; &gt;&amp;2<br />
exit 1<br />
fi</p>
<p>if [ ! -s "$1" ] ; then<br />
echo &#8220;Error: backup list $1 is empty or missing&#8221; &gt;&amp;2<br />
exit 1<br />
fi</p>
<p># Scan entries and build fixed infile list. This expands wildcards<br />
# and escapes spaces in filenames with a backslash, producing a<br />
# change: &#8220;this file&#8221; becomes this\ file so quotes are not needed.</p>
<p>while read entry; do<br />
echo &#8220;$entry&#8221; | sed -e &#8217;s/ /\\ /g&#8217; &gt;&gt; $infile<br />
done &lt; &#8220;$1&#8243;</p>
<p># The actual work of building the archive, encoding it, and sending it</p>
<p>tar czf &#8211; $(cat $infile) | \<br />
$uuencode $outfname | \<br />
mail -s &#8220;${3:-Backup archive for $(date)}&#8221; &#8220;$2&#8243;</p>
<p>echo &#8220;Done. $(basename $0) backed up the following files:&#8221;<br />
sed &#8217;s/^/   /&#8217; $infile<br />
echo -n &#8220;and mailed them to $2 &#8221;<br />
if [ ! -z "$3" ] ; then<br />
echo &#8220;with requested target directory $3&#8243;<br />
else<br />
echo &#8220;&#8221;<br />
fi</p>
<p>exit 0</p></blockquote>
<p>After the basic validity checks, the script processes the file containing the list of critical files, which is supplied as the first command argument, to ensure that spaces embedded in its filenames will work in the <code>while </code>loop (remember, by default spaces delimit arguments, so without some additional help, the shell will think that &#8220;test file&#8221; is two arguments, not one). It does this by prefacing every space with a backslash. Then it builds the archive with the primitive but useful tar command, which lacks the ability to read standard input for its file list and thus must be fed the filenames via a cat invocation.</p>
<blockquote><p>tar czf &#8211; $(cat $infile)</p></blockquote>
<p>The <code>tar </code>invocation automatically compresses the archive, and <code>uuencode </code>is then utilized to ensure that the resultant archive data file can be successfully emailed without corruption. The end result is that the remote address receives an email message with the uuencoded tar archive as an attachment. This should be a straightforward script.</p>
<p>This script expects two arguments: the name of a file that contains a list of files to archive and back up, and the destination email address for the compressed, uuencoded archive file. The file list can be as simple as</p>
<blockquote><p>$ cat filelist<br />
*.sh<br />
*.html</p></blockquote>
]]></content:encoded>
			<wfw:commentRss>http://www.xiitec.com/blog/2008/02/08/avoiding-disaster-with-a-remote-archive/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

