<?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; database</title>
	<atom:link href="http://lindesk.com/tag/database/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>
	</channel>
</rss>
