#!/usr/local/bin/perl -w # do_backup.pl - Run Backups of remote and local systems # Written by Ralph Bolton, May 29th, 2001 # ralph.bolton@pre-emptive.net # This script uses ssh and ufsdump to backup local and remote systems. # The filesystems we backup are specified in a config file. To backup # a local filesystem, we just run ufsdump. To backup a remote one, # we ssh to it, and then run ufsdump on the remote machine. That ufsdump # then uses REXEC to get back to us and our tape device. It's not secure, # so don't be fooled just because it's using ssh! # To get this to work: # 1) edit this file just below here and get it looking right for your # system. Mainly, just change the file locations, and the hostname. # 2) Create a backup.conf file, roughly of the format: # machine:/dev/rdsk/device Comment # # For example: # # My backup.conf file... # empire:/dev/rdsk/c0t0d0s0 Root # empire:/dev/rdsk/c0t0d0s4 Dynamic Data area # rebel:/dev/rdsk/c0t0d0s0 Root # # 3) Make sure that you can ssh to the remote machines without a password. # You may prefer to create a user (in group 3, so it can access tape devices) # and then run this script as that user (certainly that's got to be better # than doing it all as root!) # 4) Ensure you could manually log on to the remote and run ufsdump # with a remote tape device. # 5) Run this script and get your full backups flying! # Specify this machine's hostname so that we can figure out # which filesystems are local, and which are remote $my_hostname="curry.brokat.uk"; # Where is the backup configuration file? $config_file="backup.conf"; # Some semi-pre-defined things. You should be okay with these... $ufsdump="/usr/sbin/ufsdump"; $dump_opts="0luf"; $backup_device="/dev/rmt/0cbn"; $ssh="/usr/local/bin/ssh"; # Internals (don't mess) @config_lines=(); $SN="do_backup.pl"; # read_config() - Read in the configuration file sub read_config { my ($config)=@_; if(open(CONFIG, "< $config")) { print "$SN: Reading config...\n"; while() { chop; $line=$_; $line=~s/#.*$//g; $line=~s/^\s*//g; unless($line=~/^\s*$/) { @config_lines=(@config_lines, $line); } } close(CONFIG); } else { print STDERR "$SN: Could not open $config: $!\n"; exit 1; } } # get_time() - friendly version of localtime()!! sub get_time { my ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) =localtime(time); my @months=("Jan","Feb","Mar","Apr","May","Jun","Jul","Aug", "Sep","Oct","Nov","Dec"); my $month=$months[$mon]; $year=$year+1900; $min=sprintf("%2d",$min); $min=~s/\s/0/g; $hour=sprintf("%2d",$hour); $hour=~s/\s/0/g; return("$hour:$min $month $mday, $year"); } # execute_local_backup() - run ufsdump on a local filesystem sub execute_local_backup { my ($device,$comment)=@_; $time=&get_time; if($comment=~/^\s*$/) { $detail="$device"; } else { $detail="$comment ($device)"; } print "$SN: Backing up local $detail at $time\n"; system($ufsdump,$dump_opts,$backup_device,$device); } # execute_remote_backup() - run ssh/ufsdump on a remote filesystem sub execute_remote_backup { my ($hostname,$device,$comment)=@_; $time=&get_time; if($comment=~/^\s*$/) { $detail="$hostname:$device\n"; } else { $detail="$comment on $hostname ($device)"; } print "$SN: Backing up $detail at $time\n"; system($ssh,$hostname,$ufsdump,$dump_opts,"$my_hostname:$backup_device",$device); } sub do_local_backups { my @locals=(); @locals=grep(/^\s*$my_hostname/,@config_lines); foreach $device (@locals) { #print "Got device: $device\n"; (undef,$out)=split(/:/,$device); ($device,$comment)=split(/\s+/,$out); #print ">$device< >$comment<\n"; &execute_local_backup($device,$comment); } } sub do_remote_backups { my @remotes=(); @remotes=grep(!/^\s*$my_hostname/,@config_lines); foreach $device (@remotes) { ($hostname,$out)=split(/:/,$device); ($device,$comment)=split(/\s+/,$out); #print ">$hostname< >$device< >$comment<\n"; &execute_remote_backup($hostname,$device,$comment); } } sub print_start_banner { my $time=&get_time; print "*******************************************\n"; printf("* Starting Backup at %20s *\n",$time); print "*******************************************\n"; } sub print_end_banner { my $time=&get_time; print "*******************************************\n"; printf("* Finishing Backup at %19s *\n",$time); print "*******************************************\n"; } MAIN: { &print_start_banner; &read_config($config_file); $|=1; &do_local_backups; &do_remote_backups; &print_end_banner; } # end of script