<?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>Lindesk &#187; automate</title>
	<atom:link href="http://lindesk.com/tag/automate/feed/" rel="self" type="application/rss+xml" />
	<link>http://lindesk.com</link>
	<description>Linux - on the Desktop</description>
	<lastBuildDate>Tue, 23 Jun 2009 14:03:08 +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>Shell Script to Backup Files Locally Using Rsync</title>
		<link>http://lindesk.com/2008/05/shell-script-to-backup-files-locally-using-rsync/</link>
		<comments>http://lindesk.com/2008/05/shell-script-to-backup-files-locally-using-rsync/#comments</comments>
		<pubDate>Fri, 09 May 2008 18:21:35 +0000</pubDate>
		<dc:creator>BinnyVA</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[Scripting]]></category>
		<category><![CDATA[Shell Scripts]]></category>
		<category><![CDATA[automate]]></category>
		<category><![CDATA[backup]]></category>
		<category><![CDATA[perl]]></category>
		<category><![CDATA[rsync]]></category>
		<category><![CDATA[script]]></category>
		<category><![CDATA[shell]]></category>

		<guid isPermaLink="false">http://lindesk.com/?p=108</guid>
		<description><![CDATA[
All programmers have their own customized backup solutions. I have six. Yes, six! Five to backup files and one to backup database tables. And I am not counting version control or other backup systems built into the tools I use. Anyway, in the first post of the shell scripts series, let me introduce you to [...]]]></description>
			<content:encoded><![CDATA[<p><img src="http://lindesk.com/wp-content/uploads/2008/02/terminal.png" alt="Terminal" title="Terminal" width="128" height="128" class="intro size-full wp-image-96" align="right" /></p>
<p class="intro">All programmers have their own customized backup solutions. I have six. Yes, six! Five to backup files and one to backup database tables. And I am not counting version control or other backup systems built into the tools I use. Anyway, in the first post of the <a href="http://lindesk.com/2008/05/shell-script-language-use-perl-not-bash/">shell scripts series</a>, let me introduce you to my Rsync based local backup solution.</p>
<h2>Configuration File</h2>
<p>This script <strong>reads a configuration file named &#8216;rsyncbackup.config&#8217;</strong> and backups all the folders specified in that file to <strong>another location in the same system</strong>. This configuration file must be in the same folder as the perl script. The configuration file format is given below&#8230;</p>
<pre><code class="text">#Notes - do NOT include the last '/' at the end of the source folders
~/Scripts
~/Documents

#################### Web files ####################
# My Sites
#Folder to backup		#Folder to which it should be backuped to
/var/www/html/Sites		Htdocs/
/var/www/html/Projects	Htdocs/</code></pre>
<p>If there is just one column in a line, that folder will be backuped to &#8220;<code>&lt;backup folder&gt;/&lt;folder name&gt;</code>&#8220;. Let say that my backup destination folder is &#8216;<code>/var/Backup/Rsync</code>&#8216;. So the first line, &#8216;<code>~/Scripts</code>&#8216; will copy the contents of &#8216;<code>~/Scripts</code>&#8216; to &#8216;<code>/var/Backup/Rsync/Scripts</code>&#8216;</p>
<p>If a line in the configuration file has two columns, then an extra folder will be created with the name provided in the second column. For example, the line &#8216;<code>/var/www/html/Sites &nbsp; &nbsp; Htdocs/</code>&#8216; will create a backup of &#8216;<code>/var/www/html/Sites</code>&#8216; in &#8216;<code>/var/Backup/Rsync/Htdocs/Sites</code>&#8216;</p>
<p>And if you have not guessed it already, all lines that begin in a # are comments and will be ignored.</p>
<h2>The Script</h2>
<p>There is the perl script that automates the rsync calls&#8230;</p>
<pre><code class="perl">#!/usr/bin/perl

#EDIT THIS LINE
$backup_folder = '/var/Backup/Rsync'; #Final '/' must NOT be there.

use File::Basename;
my $config_file = dirname($0) . "/rsyncbackup.config";
my @all_locations = removeComments(getFileContents($config_file));

chdir($backup_folder) or die("Cannot go to folder '$backup_folder'");

foreach my $folder_locations (@all_locations) {
	my($folder,$backup_location) = split(/\s+/,$folder_locations);

	print "Backing up $folder to $backup_location ... ";
	`rsync -a $folder $backup_folder/$backup_location`;
	print "Done\n";
}

sub getFileContents {
	my $file = shift;
	my @lines;
	if(!open (FILE,$file)) {
		die("Can't open '$file': $!");
	} else {
		@lines=&lt;FILE&gt;;
		close(FILE);
	}
	return @lines;
}

sub removeComments {
	my @lines = @_;

	@cleaned = grep(!/^\s*#/, @lines); #Remove Comments
	@cleaned = grep(!/^\s*$/, @cleaned); #Remove Empty lines

	return @cleaned;
}</code></pre>
<p>Execute this script using the command &#8216;perl RsyncBackup.pl&#8217;. In my system I have created an alias &#8216;bk&#8217; for this script. I recommend that you make a similar alias if you take backups regularly(extremely recommended).</p>
<p>Backing up is done using rsync &#8211; so its faster than a simple &#8216;cp&#8217; as only the modified and new files are copied.</p>
]]></content:encoded>
			<wfw:commentRss>http://lindesk.com/2008/05/shell-script-to-backup-files-locally-using-rsync/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
	</channel>
</rss>
