Script to Backup Files Over a Network Using Rsync
Written by BinnyVA on June 4, 2008 – 9:47 pm -
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 have a password-less login system over ssh. 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’ll make a post on how to set it up.
The configuration file is the same format as the one used in the last Rsync script. But in this case, the file name will be ‘rsyncnetworkbackup.config‘.
The Code
#!/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=<FILE>;
close(FILE);
return @lines;
}
sub removeComments {
my @lines = @_;
@cleaned = grep(!/^\s*#/, @lines); #Remove Comments
@cleaned = grep(!/^\s*$/, @cleaned); #Remove Empty lines
return @cleaned;
}
Digg it
|
reddit
|
StumbleUpon
Tags: backup, network, perl, remote, rsync, script
Posted in Networking, Scripting, Shell Scripts |

Follow me(@binnyva) on Twitter
June 19th, 2008 at 11:14 pm
[...] 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 - [...]
July 8th, 2008 at 12:27 pm
hello..can you guide me writing a shell script to backup mysql in fedora 8 and then transfers the backup mysql to other LAMP in another machine.
January 12th, 2009 at 7:21 pm
[...] rely on premade solutions, even if I like hacking with small scripts running in cron directorys This site provides a rsync [...]
March 15th, 2009 at 8:39 pm
Great.
Actually I lost about 300GB of data after my external HD broke. It wasn’t backed up and this taught me a lesson.
I’m planning to write a somehow similar script which syncs critical data between local HD, remote PC, and cloud storage like Dropbox, Gmail and others. I’m a little busy in the few coming weeks but I’ll write it as soon as I get some free time. Will inform you for sure.
Great blog btw.
March 15th, 2009 at 9:37 pm
@Amir Watad
Nice - and publish the script as well. It will be useful for others too.
March 21st, 2009 at 11:12 am
i very much need a way to send this data to a remote server, used for this purpose only, i am actually amazed i have this working
i started my own server system last summer after 10 years of leased servers and way too many problems - and have been running with no backup systems in place and i am worried…
any thoughts on how to use this nifty script to do this?
thank you