Script to Backup Files Over a Network Using Rsync

Written by BinnyVA on June 4, 2008 – 9:47 pm -

Script

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;
}

del.icio.us | Digg it | reddit | StumbleUpon

Tags: , , , , ,
Posted in Networking, Scripting, Shell Scripts | 9 Comments »

9 Comments to “Script to Backup Files Over a Network Using Rsync”

  1. [...] 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 – [...]

  2. soul_krasty says:

    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.

  3. [...] rely on premade solutions, even if I like hacking with small scripts running in cron directorys This site provides a rsync [...]

  4. Amir Watad says:

    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.

  5. BinnyVA says:

    @Amir Watad
    Nice – and publish the script as well. It will be useful for others too.

  6. phil says:

    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

  7. Christos says:

    Any idea on how to combine this with a webbased ssh-client like http://electrica-ms.mures.rdsnet.ro/ ?

  8. Dogue says:

    how do i get a password to work over the network on sql. When i put a password in for a variable and past it to the rysnc it seems to run the command too fast and when sql askes for the password the script has already ran.

  9. Horace says:

    Help “how to write” backup.cgi with perl on Linux Debian 2.4, Samba 3.0x connects to Win XP pro peer-to-peer LAN. Crontab runs daily backup.cgi of file.csv (4MB) to a local folder on the Linux box. Instead, I want to copy this file to a share on the LAN. fstab mounts the share. I’m brain dead in writing the cgi to copy to the share. Currently: copy($bak_this, $to_that) or die “Cannot backup $bak_this.”; How do I define $to_that = “???”; Thanks for the help.

Leave a Comment