Shell Script to Backup Files Locally Using Rsync
Written by BinnyVA on May 9, 2008 – 11:39 pm -
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.
Posted in Programming, Scripting, Shell Scripts | No Comments »
Shell Script Language - Use Perl, Not Bash
May 2, 2008 – 11:40 pmTo me, a shell script is a script that automates ...
5 Comments »MPD - Music Player Daemon
April 14, 2008 – 11:48 pmMPD is not for everyone. Before continuing into the article, a ...
1 Comment »Frees - Hard Disk Drives Free Space Viewer
April 6, 2008 – 1:43 amI am taking a break from the regular MP3 Players ...
2 Comments »Listen - Gtk MP3 Player
March 22, 2008 – 11:27 pmI expected my MP3 player series will end with the ...
4 Comments »Top 10 Linux MP3 Players
March 14, 2008 – 11:11 amThere are no shortage of audio players in Linux. It ...
35 Comments »mpg123/mpg321 - The Command Line MP3 Players
February 25, 2008 – 1:25 amSo far we looked at the GUI MP3 Players for ...
4 Comments »Fedora 8/KDE Font Bug for Gnome Applications
February 19, 2008 – 12:08 amIf you are using Fedora 8 with KDE you may ...
No Comments »Exaile - Music Player for Gtk+
January 7, 2008 – 11:12 pmExaile an amarok clone for Gnome - and I have ...
7 Comments »Songbird - The Firefox of MP3 Players
December 11, 2007 – 10:39 pmI just installed Songbird - an MP3 player built on ...
5 Comments »