<?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; Scripting</title>
	<atom:link href="http://lindesk.com/category/command-line/scripting/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>Perl Script to Backup MySQL Databases</title>
		<link>http://lindesk.com/2008/06/perl-script-to-backup-mysql-databases/</link>
		<comments>http://lindesk.com/2008/06/perl-script-to-backup-mysql-databases/#comments</comments>
		<pubDate>Thu, 19 Jun 2008 17:55:59 +0000</pubDate>
		<dc:creator>BinnyVA</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[Scripting]]></category>
		<category><![CDATA[Shell Scripts]]></category>
		<category><![CDATA[backup]]></category>
		<category><![CDATA[database]]></category>
		<category><![CDATA[mysql]]></category>
		<category><![CDATA[perl]]></category>
		<category><![CDATA[script]]></category>

		<guid isPermaLink="false">http://lindesk.com/?p=113</guid>
		<description><![CDATA[A perl script that can be used to backup your databases.]]></description>
			<content:encoded><![CDATA[<p class="intro">So far, we covered <a href="http://lindesk.com/2008/05/shell-script-to-backup-files-locally-using-rsync/">backing up files locally</a> and <a href="http://lindesk.com/2008/06/script-to-backup-files-over-a-network-using-rsync/">over a network</a>. Now let let see how to backup databases(only mysql supported &#8211; yet).</p>
<p>The <strong class="highlight">script uses &#8216;mysqldump&#8217;</strong> command to backup the data. That means that the <strong class="highlight">backups are in the SQL dump format</strong>. The dumps of all the databases that are backed up are compressed and stored in the destination folder. They will be named in this format &#8211; YYYY-MM-DD.tar.gz.</p>
<h2>Configuration File</h2>
<p>This script <strong class="highlight">reads a configuration file named &#8216;dbbackup.config&#8217; and backups all the databases specified in that file</strong> to another location in the same system. This <strong class="highlight">configuration file must be in the same folder as the perl script</strong>. The configuration file format is given below&#8230;</p>
<pre><code class="text">Data
Project_Nexty
App_activecollab
# Unwanted_DB - commented - will not be backedup
binco
binnyva
</code></pre>
<h2>The Perl Script</h2>
<pre><code class="perl">
#!/usr/bin/perl
# Backups all the databases specified in the dbbackup.config file

$backup_folder = '/var/Backup/Special/Databases'; #EDIT THIS LINE

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

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

my ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime(time);
$year += 1900;
$mon++;
#Zero padding
$mday = '0'.$mday if ($mday&lt;10);
$mon = '0'.$mon if ($mon&lt;10);

my $folder = "$year-$mon-$mday";
mkdir($folder) or die("Cannot create a folder called '$folder'");

foreach my $database (@databases) {
	next if ($database eq '');
	chomp($database);

	my $table = '';
	if(index($database,' ')+1) { #Get just 1 table in the database - if there is a ' '(space) in the db name
		my @parts = split(' ',$database);
		$database = $parts[0];
		$table = $parts[1];
	}

	print "Backing up $database ... ";

 	my $file = $database;
 	$file .= '_' . $table if($table ne '');
 	$file .= ".sql";

 	`mysqldump -u root $database $table &gt; $folder/$file`;

	print "Done\n";
}
print "Compressing the folder ... ";
`tar -czf $folder.tar.gz $folder/`;
print "Done\nRemoving Folder ... ";
`rm -rf $folder`;
print "Done\n\n";

sub getFileContents {
	my $file = shift;
	open (FILE,$file) || die("Can't open '$file': $!");
	my @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>If you need, you can set this script as a cron job &#8211; this will make sure that you don&#8217;t have to worry about the backup.</p>
]]></content:encoded>
			<wfw:commentRss>http://lindesk.com/2008/06/perl-script-to-backup-mysql-databases/feed/</wfw:commentRss>
		<slash:comments>15</slash:comments>
		</item>
		<item>
		<title>Script to Backup Files Over a Network Using Rsync</title>
		<link>http://lindesk.com/2008/06/script-to-backup-files-over-a-network-using-rsync/</link>
		<comments>http://lindesk.com/2008/06/script-to-backup-files-over-a-network-using-rsync/#comments</comments>
		<pubDate>Wed, 04 Jun 2008 16:29:42 +0000</pubDate>
		<dc:creator>BinnyVA</dc:creator>
				<category><![CDATA[Networking]]></category>
		<category><![CDATA[Scripting]]></category>
		<category><![CDATA[Shell Scripts]]></category>
		<category><![CDATA[backup]]></category>
		<category><![CDATA[network]]></category>
		<category><![CDATA[perl]]></category>
		<category><![CDATA[remote]]></category>
		<category><![CDATA[rsync]]></category>
		<category><![CDATA[script]]></category>

		<guid isPermaLink="false">http://lindesk.com/?p=112</guid>
		<description><![CDATA[
This script will backup the specified files to another computer on your network. You can also use this to send your files to a remote server. This script compliments the last Rsync Backup script. Its possible to combine both the script together, I prefer to keep them separate.
The Setup
For this to work, you need to [...]]]></description>
			<content:encoded><![CDATA[<p><img src="http://lindesk.com/wp-content/uploads/2008/02/terminal.png" alt="Script" title="Terminal" width="128" height="128" class="alignnone size-full wp-image-96 intro" align="right" /></p>
<p class="intro">This script will backup the specified files to another computer on your network. You can also use this to send your files to a remote server. This script compliments the last <a href="http://lindesk.com/2008/05/shell-script-to-backup-files-locally-using-rsync/">Rsync Backup script</a>. Its possible to combine both the script together, I prefer to keep them separate.</p>
<h2>The Setup</h2>
<p>For this to work, you need to have a <a href="http://www.linuxconfig.org/Passwordless_ssh">password-less login system over ssh</a>. You should configure the remote system to accept your credentials by giving your public key to the remote server. If you are not sure how to do that, just leave a comment and I&#8217;ll make a post on how to set it up.</p>
<p>The configuration file is the same format as the one used in the <a href="http://lindesk.com/2008/05/shell-script-to-backup-files-locally-using-rsync/">last Rsync script</a>. But in this case, the file name will be &#8216;<code>rsyncnetworkbackup.config</code>&#8216;.</p>
<h2>The Code</h2>
<pre><code class="perl">
#!/usr/bin/perl

#The folder on the remote system that must be used to store the data
$backup_folder = '/home/neo/Backup'; #Final '/' must NOT be there.
# The user for whom we have set up the key based login
$backup_user = 'neo';
# The IP address/domain name of the remote system.
$backup_server = '192.168.0.30';

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

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

	print "Backing up $folder to $backup_location ... ";
	`rsync -avze ssh $folder $backup_user\@$backup_server:\"$backup_folder/$backup_location\"`;
	print "Done\n";
}

sub getFileContents {
	my $file = shift;
	my @lines;

	open (FILE,$file) || die("Can't open '$file': $!");

	@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>
]]></content:encoded>
			<wfw:commentRss>http://lindesk.com/2008/06/script-to-backup-files-over-a-network-using-rsync/feed/</wfw:commentRss>
		<slash:comments>7</slash:comments>
		</item>
		<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>
		<item>
		<title>Shell Script Language &#8211; Use Perl, Not Bash</title>
		<link>http://lindesk.com/2008/05/shell-script-language-use-perl-not-bash/</link>
		<comments>http://lindesk.com/2008/05/shell-script-language-use-perl-not-bash/#comments</comments>
		<pubDate>Fri, 02 May 2008 18:22:17 +0000</pubDate>
		<dc:creator>BinnyVA</dc:creator>
				<category><![CDATA[Command Line]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[Scripting]]></category>
		<category><![CDATA[bash]]></category>
		<category><![CDATA[Opinion]]></category>
		<category><![CDATA[perl]]></category>
		<category><![CDATA[script]]></category>
		<category><![CDATA[shell]]></category>

		<guid isPermaLink="false">http://lindesk.com/?p=107</guid>
		<description><![CDATA[
To me, a shell script is a script that automates repetitive tasks. But that is not the &#8216;official&#8217; definition. Wikipedia has this definition&#8230;
A shell script is a script written for the shell, or command line interpreter, of an operating system.
I use a definition that defines the purpose of the script &#8211; while the others prefer [...]]]></description>
			<content:encoded><![CDATA[<p><a href='http://lindesk.com/wp-content/uploads/2008/02/terminal.png'><img src="http://lindesk.com/wp-content/uploads/2008/02/terminal.png" alt="" title="Terminal" width="128" height="128" class="alignnone intro size-full wp-image-96" align="right" /></a></p>
<p class="intro">To me, a shell script is a <strong class="highlight">script that automates repetitive tasks</strong>. But that is not the &#8216;official&#8217; definition. Wikipedia has this <a href="http://en.wikipedia.org/wiki/Shell_scripts">definition</a>&#8230;</p>
<blockquote cite="http://en.wikipedia.org/wiki/Shell_scripts"><p>A shell script is a script written for the shell, or command line interpreter, of an operating system.</p></blockquote>
<p>I use a definition that <strong class="highlight">defines the purpose</strong> of the script &#8211; while the others prefer a definition that <strong class="highlight">defines the technology used</strong>. I am not going to claim that my definition is better than the other definition &#8211; that&#8217;s pointless. Besides, even I think that the &#8216;other&#8217; definition is the right one. But I will try to show you the advantages of my approach.</p>
<h2>The Purpose</h2>
<p>I like to automate things(in other words, I&#8217;m lazy). So I have a nice little collection of custom shell scripts. But there is a huge barrier to writing shell scripts &#8211; the language used. <strong class="highlight">Traditionally shell scripts are written in a language provided by the shell</strong> &#8211; like <a href="http://www.gnu.org/software/bash/bash.html">bash</a> or <a href="http://www.tcsh.org/Welcome">tcsh</a>. That is the problem &#8211; these languages are Bad &#8211; with a capital &#8216;B&#8217;.</p>
<h2>The Problem</h2>
<p>To people who are accustomed to decent languages, these <strong class="highlight">shell languages will seem clunky</strong> &#8211; or even evil. But since most shell scripts are small, most people don&#8217;t mind the torture.</p>
<p>In bash, the control flow commands seem to be thrown in as a after thought rather than something that&#8217;s built into the language. If you don&#8217;t believe me, compare the &#8216;if&#8217; loop of bash and Perl.</p>
<h3>Bash</h3>
<p>This code checks wether the variable &#8216;$value&#8217; has the value 0 &#8211; if so it prints the message &#8216;No Value&#8217;</p>
<pre><code class="bash">if [ $value -eq 0 ] ; then
	echo "No Value"
fi</code></pre>
<h3>Perl</h3>
<p>The same code in Perl&#8230;</p>
<pre><code class="perl">if($value == 0) {
	print "No Value";
}</code></pre>
<p>Of course, Perl experts will go for <code>if(!$value)</code> or even <code>unless($value)</code> &#8211; but that&#8217;s not the point. See how better the <strong class="highlight">code looks in Perl</strong>. Yeah, even I am surprised to hear those words &#8211; Perl is considered by many to be an &#8216;ugly&#8217; language. But when compared to shell languages, Perl is a gem(sorry about the pun &#8211; couldn&#8217;t resist).</p>
<h2>The Solution</h2>
<p>The solution is simple &#8211; <strong class="highlight">don&#8217;t use a shell language to write your shell scripts &#8211; use a high level scripting language</strong> like Perl. Or Python, Ruby, PHP, Tcl or <a href="http://www.openjs.com/articles/javascript_scripting_language.php">even JavaScript</a>.</p>
<p>I still use <a href="http://www.openjs.com/articles/javascript_scripting_language.php" title="Command Line Twitter Client">bash to write shell scripts</a> &#8211; but if the shell script has an if condition(other than a simple argument check), I use a higher language &#8211; usually Perl.</p>
<h2>Advantages of using a Shell Language</h2>
<ul>
<li>Its supported in even the tinest linux distros &#8211; even in embedded systems.</li>
<li>Its the method preferred by the majority &#8211; I hope this will change soon.</li>
<li>Command calls look more natural in a shell language.</li>
</ul>
<h2>Advantages of using a High Level Language</h2>
<ul>
<li>Better Code</li>
<li>Easy to maintain</li>
<li>Faster development</li>
<li>Libraries provide a lot of functionality</li>
<li>Easier to port to different platforms.</li>
</ul>
<p>So why am I telling you all this? Two reasons&#8230;</p>
<p>One, the next time you are going to write a shell script, I want you to choose a high level language rather than using bash.</p>
<p>The second reason is that now that <a href="http://lindesk.com/2008/03/top-10-linux-mp3-players/">my series on Linux MP3 Players</a> are over, I am going to take a small break from desktop posts and write on more &#8216;linuxy&#8217; topics. And one of those topic is Shell Scripting. So in the future posts, I am going to share some of my shell scripts with you. So when I publish a Perl script and call it a shell script, I don&#8217;t want you to get confused .</p>
]]></content:encoded>
			<wfw:commentRss>http://lindesk.com/2008/05/shell-script-language-use-perl-not-bash/feed/</wfw:commentRss>
		<slash:comments>32</slash:comments>
		</item>
	</channel>
</rss>
