#!/usr/bin/perl
# rpm_backup.pl 6/21/08 Meryll Larkin
# 7/22/08 refactored for clarity
# moved version into a subroutine
# changed names of subs to reflect return value
# make_date to make_date_string
# 3/15/09 corrected find & remove old backups command
# so that it starts search from / directory
# rpm_backup.pl
# Remotely back up the RPM database on multiple remote servers
# Tested on RHE3, RHE4, RHE5
# Use this script before running yum or up2date
# to make sure the rpm database has been backed up
# Requires/assumes the ability to ssh (as a user) using a key without
# the need to supply a password
# Requires/assumes the user has sudo privileges on each server
# ********************************************************
use strict;
my @HOSTS = qw(server1.fqdn.com server2.fqdn.com server3.fqdn.com);
# number of months to keep old rpm backup files
my $MO_LIMIT = 6;
# the following numbers should be numbers representing
# the lowest and highest version of RHE to be serviced with this script
my $LOWEST_VERSION_NO = 3;
my $HIGHEST_VERSION_NO = 5.9;
# my $date = &make_date_string;
my $date = `date +%Y%m%d`; # this works fine on Linux
for (@HOSTS) {
my $host = $_;
print "\nWorking on $host\n";
my $version = &get_version_number($host,$LOWEST_VERSION_NO,$HIGHEST_VERSION_NO);
my $ssh;
# if the version is prior to RHE5, then ssh command is ssh -f otherwise ssh -t
$version < 5 ? $ssh = "ssh -f " : $ssh = "ssh -t ";
# next line will create directory only if it doesn't already exist
`$ssh $host 'sudo mkdir -p /root/UPDATES'`;
`$ssh $host 'cd /var/lib ; sudo tar -cf rpm$date.tar rpm'`;
`$ssh $host 'sudo gzip /var/lib/rpm$date.tar'`;
`$ssh $host 'sudo mv /var/lib/rpm$date.tar.gz /root/UPDATES'`;
# remove backup files older than $MO_LIMIT old to save space
print "Removing tar files more than $MO_LIMIT months old.\n";
# example of what command looks like on command line:
# /usr/bin/find /root/UPDATES/ -mtime +180 -name "*.tar" -exec /bin/rm -f {} \;
# multiply $MO_LIMIT by 30 to get days
my $DAY_LIMIT = $MO_LIMIT * 30;
`$ssh $host 'cd / && sudo /usr/bin/find /root/UPDATES/ -mtime +$DAY_LIMIT -name "*.tar.gz" -exec /bin/rm -f {} \\;'`;
}
exit 0;
# *************************************
sub make_date_string {
# retrieve current time as a long integer, put it into $t
my $t = time();
my ($sec, $min, $hour, $dom, $mon, $year) = localtime($t);
$year += 1900;
# for $mon, January is zero correct for that
$mon++;
# force month to be expressed as 2 digits
$mon = "0" . $mon if ($mon<10);
# force day to be expressed as 2 digits
$dom = "0" . $dom if ($dom<10);
# get format 20080613 for June 13 2008
my $date = $year . $mon . $dom;
return $date;
}
# *************************************
sub get_version_number {
my ($host, $LOWEST_VERSION_NO, $HIGHEST_VERSION_NO) = @_;
my $version = `ssh -n $host 'cat /etc/redhat-release'`;
# reduce the full release string to a number
$version =~ s/^.*release\s*//;
$version =~ s/^\D*//;
$version =~ s/\s*\(.*$//;
# error checking
if (($version !~ /\d/) || ($version < $LOWEST_VERSION_NO) ||
($version > $HIGHEST_VERSION_NO)) {
print "Unexpected value in version number: '$version'\n";
exit (1); # exit with error code
}
return $version;
}
# **********************************END
|
|