[Linux-Discussion] rsync your machine for backups

  • From: John Madden <weez@xxxxxxxxxxxxx>
  • To: linux-discussion@xxxxxxxxxxxxx
  • Date: Sun, 29 Jul 2001 19:48:56 -0500

I just implemented this on FreeLists to ensure that the outage earlier 
this month won't have such grave consequences, were it to ever happen 
again.  Here's how I did it, in case anyone else feels like implementing 
it.  I think it's a pretty sweet setup. :)

What you need: two separate machines (could be done with one, but that's 
not the point here), perl, rsync installed on both, plus DSA 
authentication for whatever user you're using on the "local" machine to 
whatever user you're using on the "remote" machine.  

The only downside here is that this isn't an incremental backup (although 
it could certainly be modified to make one), it only gives you the 
beginnings of a "hot failover" setup to keep two machines in sync.  
Comments and improvements are welcome. :)

John


#!/usr/bin/perl -w
use strict;

# Script to ensure that some stuff gets backed up every <> minutes or so,
# depending on system load.

# #           # #
# Configuration #
# #           # #

# Remote machine/IP and username on that machine
my $REMOTEBOX = "192.168.0.10";
my $REMOTEUSER= "root";

# Destination directory for backups on the remote box
my $REMOTEDIR = "/backup";

# Locations of system binaries
my $SSH       = "/usr/bin/ssh";
my $RSYNC     = "/usr/local/bin/rsync";

# Extra options for rsync
my $OPTS = "-azu";

# Time (in seconds) we wait before doing another backup listed in @DIRS.  
my $SMALLWAIT      = "60";

# Time (in seconds between backup runs.
my $BIGWAIT        = "3600";

# Load average at which delay doing another backup.
my $GOODLOAD  = "1.00";

# List of directories we're going to backup.  DON'T put a trailing slash on
# directory names.
my @DIRS      = ("/web", "/htdig/conf", "/var/lib", "/home", "/etc", 
"/root");

# #               # #
# End Configuration #
# #               # #




# Startup: start ssh-agent and cache the passphrase for our DSA key
`/usr/bin/ssh-agent > /root/scripts/current-ssh-agent.sh`;
`/bin/bash /root/scripts/current-ssh-agent.sh`;
`/usr/bin/ssh-add ~/.ssh/id_dsa`;

print "\n*** Please note that if the passphrase on this key is changed, 
backupd.pl must be stopped/restarted. ***\n";

# Run infinitely
for(;;)
{
        # Run through each of the backups that we wish to perform
        foreach(@DIRS)
        {
                CheckSystem();
#               print "\nDoing $_...";
                system("$RSYNC $OPTS -e $SSH $_ 
$REMOTEUSER\@$REMOTEBOX:$REMOTEDIR");
#               print "\nSleeping $SMALLWAIT...";
                sleep $SMALLWAIT;
        }
#       print "\nBackups done, sleeping $BIGWAIT...";
        sleep $BIGWAIT;
}

# Check the load average on the system, sleep for 5 seconds if it's too
# high, rinse, repeat.  Keeps things from getting completely thrashed 
# while other stuff is going on.
sub CheckSystem
{
        my $test = 1;

        open(L, "/proc/loadavg");
        $test = <L>;
        $test = (split(/ /, $test))[0];
        close L;

        while($test >= $GOODLOAD)
        {
                print STDERR "Load average is too high ($test), sleeping for 5 
seconds...\n";
                sleep 5;

                # Again
                open(L, "/proc/loadavg");
                $test = <L>;
                $test = (split(/ /, $test))[0];
                close L;
        }
}


=============================================================
Avenir Web's Linux Discussion List

List info: //www.freelists.org/cgi-bin/webpage?webpage_id=13
To unsubscribe: email linux-discussion-request@xxxxxxxxxxxxx
with 'unsubscribe' in the Subject line.

Administrative contact: weez@xxxxxxxxxxxxx
=============================================================

Other related posts:

  • » [Linux-Discussion] rsync your machine for backups