Perl Script to Backup MySQL Databases

Written by BinnyVA on June 19, 2008 – 11:13 pm -

So far, we covered backing up files locally and over a network. Now let let see how to backup databases(only mysql supported – yet).

The script uses ‘mysqldump’ command to backup the data. That means that the backups are in the SQL dump format. 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 – YYYY-MM-DD.tar.gz.

Configuration File

This script reads a configuration file named ‘dbbackup.config’ and backups all the databases specified in that file to another location in the same system. This configuration file must be in the same folder as the perl script. The configuration file format is given below…

Data
Project_Nexty
App_activecollab
# Unwanted_DB - commented - will not be backedup
binco
binnyva

The Perl Script


#!/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<10);
$mon = '0'.$mon if ($mon<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 > $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=<FILE>;
	close(FILE);
	
	return @lines;
}

sub removeComments {
	my @lines = @_;

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

	return @cleaned;
}

If you need, you can set this script as a cron job – this will make sure that you don’t have to worry about the backup.


Tags: , , , ,
Posted in Programming, Scripting, Shell Scripts | 17 Comments »

Shell Script to Backup Files Locally Using Rsync

Written by BinnyVA on May 9, 2008 – 11:39 pm -

Terminal

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 my Rsync based local backup solution.

Configuration File

This script reads a configuration file named ‘rsyncbackup.config’ and backups all the folders specified in that file to another location in the same system. This configuration file must be in the same folder as the perl script. The configuration file format is given below…

#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/

If there is just one column in a line, that folder will be backuped to “<backup folder>/<folder name>“. Let say that my backup destination folder is ‘/var/Backup/Rsync‘. So the first line, ‘~/Scripts‘ will copy the contents of ‘~/Scripts‘ to ‘/var/Backup/Rsync/Scripts

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 ‘/var/www/html/Sites     Htdocs/‘ will create a backup of ‘/var/www/html/Sites‘ in ‘/var/Backup/Rsync/Htdocs/Sites

And if you have not guessed it already, all lines that begin in a # are comments and will be ignored.

The Script

There is the perl script that automates the rsync calls…

#!/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=<FILE>;
		close(FILE);
	}
	return @lines;
}

sub removeComments {
	my @lines = @_;

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

	return @cleaned;
}

Execute this script using the command ‘perl RsyncBackup.pl’. In my system I have created an alias ‘bk’ for this script. I recommend that you make a similar alias if you take backups regularly(extremely recommended).

Backing up is done using rsync – so its faster than a simple ‘cp’ as only the modified and new files are copied.


Tags: , , , , ,
Posted in Programming, Scripting, Shell Scripts | 4 Comments »

Shell Script Language – Use Perl, Not Bash

Written by BinnyVA on May 2, 2008 – 11:40 pm -

To me, a shell script is a script that automates repetitive tasks. But that is not the ‘official’ definition. Wikipedia has this definition

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 – while the others prefer a definition that defines the technology used. I am not going to claim that my definition is better than the other definition – that’s pointless. Besides, even I think that the ‘other’ definition is the right one. But I will try to show you the advantages of my approach.

The Purpose

I like to automate things(in other words, I’m lazy). So I have a nice little collection of custom shell scripts. But there is a huge barrier to writing shell scripts – the language used. Traditionally shell scripts are written in a language provided by the shell – like bash or tcsh. That is the problem – these languages are Bad – with a capital ‘B’.

The Problem

To people who are accustomed to decent languages, these shell languages will seem clunky – or even evil. But since most shell scripts are small, most people don’t mind the torture.

In bash, the control flow commands seem to be thrown in as a after thought rather than something that’s built into the language. If you don’t believe me, compare the ‘if’ loop of bash and Perl.

Bash

This code checks wether the variable ‘$value’ has the value 0 – if so it prints the message ‘No Value’

if [ $value -eq 0 ] ; then
	echo "No Value"
fi

Perl

The same code in Perl…

if($value == 0) {
	print "No Value";
}

Of course, Perl experts will go for if(!$value) or even unless($value) – but that’s not the point. See how better the code looks in Perl. Yeah, even I am surprised to hear those words – Perl is considered by many to be an ‘ugly’ language. But when compared to shell languages, Perl is a gem(sorry about the pun – couldn’t resist).

The Solution

The solution is simple – don’t use a shell language to write your shell scripts – use a high level scripting language like Perl. Or Python, Ruby, PHP, Tcl or even JavaScript.

I still use bash to write shell scripts – but if the shell script has an if condition(other than a simple argument check), I use a higher language – usually Perl.

Advantages of using a Shell Language

  • Its supported in even the tinest linux distros – even in embedded systems.
  • Its the method preferred by the majority – I hope this will change soon.
  • Command calls look more natural in a shell language.

Advantages of using a High Level Language

  • Better Code
  • Easy to maintain
  • Faster development
  • Libraries provide a lot of functionality
  • Easier to port to different platforms.

So why am I telling you all this? Two reasons…

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.

The second reason is that now that my series on Linux MP3 Players are over, I am going to take a small break from desktop posts and write on more ‘linuxy’ 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’t want you to get confused .

Update: This article is translated to Serbo-Croatian language.


Tags: , , , , ,
Posted in Command Line, Programming, Scripting | 42 Comments »

Using Konqueror as an FTP Client

Written by BinnyVA on May 5, 2007 – 4:38 pm -

Konqueror Logo

As a web developer good FTP clients are a necessity for me. I have to upload the scripts to the server. I used to use gFTP for this. Good client – clean interface, easy to use. But not as powerful as I wanted. Then I found FireFTP – the extension for Firefox – it is a good client. But recently I found that my favorite application, Konqueror, can do it much better than all the other clients.

Didn’t know konqueror could be used as a FTP client? Just copy and paste this FTP URL into the address bar of Konqueror and see for yourself…
ftp://checksoftware.com/

Advantages

The main advantage of using Konqueror as the FTP client is that you are very familiar with it. All the shortcuts, the tricks, you use when browsing through your files are available when you connect to a remote server as well. Ctrl+C and Ctrl+V? The FTP client has got it. What if you want to browse two folders in the remote server at the same time? Konqueror can do it.

2 Pane View

Like many other FTP Clients, you can split Konqueror into two panes(Window->Split View Left and Right). This is perhaps the best way to use Konqueror as an FTP Client.

Konqueror using Split Window Interface

X Pane View

Unlike other FTP Clients, Konqueror goes further than just 2 panes. You can keep on splitting a window to make it ‘just perfect’ for you.

Tabbed View

If paned view is not enough, just open up a new tab – and continue splitting it.

Multiple Protocols – FTP/SFTP/FISH

Konqueror supports many protocols – among which these three are the most important to us…

fish
You can use fish if you have a shell account on the remote server. For example, fish://admin@linkdesk.com would open a connection for user binnyva to a remote server ‘linkdesk.com’.
ftp
File Transfer Protocol – you should be knowing this if you have come this far 😉
sftp
Secure FTP.

Problems

So far I have found only one issue with it – I was not able to configure Konqueror to act as an FTP client properly when I was behind a proxy. I have reasons to believe that it was my fault – but I did spend some time trying. Could someone else confirm this?

Conclusion

Konqueror is the best FTP Client there is. Period.


Posted in Applications, KDE, Tools, Tools, Web Development | 7 Comments »