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.
Excellent piece of work!
Thanks!
Hi,
It my first experience with Perl, and your script is very very good.
Now, I’m trying to do the same thing to a remote server via a ftp or ssh.
Thank you very much for your help.
P/S: I tried to do the same thing with bash, but now I prefere the Perl