Script to Backup Files Over a Network Using Rsync

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

9 comments

  1. 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.

  2. 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

  3. 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.

  4. 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 Reply to Amir Watad Cancel reply

Your email address will not be published. Required fields are marked *