Sample Linux server (web centric) back up script in PERL

Below is a sample, detailed, PERL script for backing up your web centric Linux server. It includes ways to creating tgz of required areas, pushing them to magnetic tapes, other backup servers and even on to desktops –
 
Code:
 
#!/usr/bin/perl
use POSIX qw(strftime);

 

umask 0000; 

##################################################### 

# This Program is a custom made backup script that #

# takes Back Up of all the required areas of a typical linux

# based web server #

#####################################################

#Define logger here

#find logs from /var/log/messages

$LOGGER = “/usr/bin/logger -i -p local0.info -t fullBackUps”;

sub MyLog {

my $msg=sprintf(“%s”,@_);

my $COMMAND=”” ;

chomp($msg);

if ($daemon==0) {

print “$msg\n”;

} else {

$COMMAND=”$LOGGER $msg”;

system($COMMAND);

}

}

if (-t STDIN) {

$daemon=0;

} else {

$daemon=1;

}

#get current timestamp

$tmstmp = strftime “%Y%m%d%H%M”, localtime;

system (“mkdir /backup/$tmstmp”);

###Back up of core VHs/Core websites

chdir (“/”);

$filName=$tmstmp.”virtualHosts.tgz”;

MyLog (“####Starting taking backup of Virtual hosts area\n\n”);

system (“gtar -zcf $filName intranet1_part1/virtualHosts/”);

system (“mv $filName /backup/$tmstmp/”);

###Back up of Lib modules

chdir (“/”);

$filName=$tmstmp.”virtualHostsLib.tgz”;

MyLog (“####Starting taking backup of Virtual hosts lib area\n\n”);

system (“gtar -zcf $filName intranet1_part1/virtualHosts.lib/”);

system (“mv $filName /backup/$tmstmp/”);

###Back up of VH mirrors

$filName=$tmstmp.”virtualHostsMirror.tgz”;

MyLog (“####Starting taking backup of Virtual hosts mirror area\n\n”);

system (“gtar -zcf $filName intranet1_part3/virtualHosts.mirror/”);

system (“mv $filName /backup/$tmstmp/”);

###Back up of Data areas

$filName=$tmstmp.”virtualHostsData.tgz”;

MyLog (“####Starting taking backup of Virtual hosts data area\n\n”);

system (“gtar -zcf $filName intranet1_part3/virtualHosts.data/data/”);

system (“mv $filName /backup/$tmstmp/”);

###Back up of db2 script areas which contains valuable Db2 install and backup scripts

chdir (“/usr/users/”);

$filName=$tmstmp.”allDb2Database.tgz”;

MyLog (“####Starting taking tar of database\n\n”);

system (“gtar -zcf $filName gom/ db2inst1/Db2CreateDatabaseTables/”);

system (“mv $filName /backup/$tmstmp/”);

MyLog (“\n\n####Finished taking backup\n\n”);

##Push the latest backup to Stand by server xx.xx.xx.xx

##currently, data Files are also pushed.. Once they grow larger in size, say in GBs,

##you can exclude it from the push list

##anyways we are pushing full data Files backup every weekend

MyLog (“\n\n####Start Pushing daily backup to Stand by server\n\n”);

system (“scp /backup/$tmstmp/* root\@xx.xx.xx.xx:/backup/backup_xx/.”);

##Every weekend, we push full data File backup to the Stand by server

system(“date > /tmp/date.tmp”);

if(open(datetmp, “/tmp/date.tmp”)) {

while(<datetmp>)

{

$line = $_;

chomp($line);

@chnks = split(/ /,$line);

$dayOfWeek = $chnks[0];

}

}

else{

print “Unable to open datetmp file”;

}

if($dayOfWeek =~ /Sat/){

MyLog (“\n\n####Start Pushing Saturday Backup to Stand by server\n\n”);

##Push the full Data backup to Stand by server xx.xx.xx.xx’s Sat_full area

system (“scp /backup/$tmstmp/”.$tmstmp.”virtualHostsData.tgz root\@xx.xx.xx.xx:/backup/backup_xx/datafiles_Sat_full/.”);

}

#Store daily full backup in magnetic tape here

MyLog (“\n\n####Start Pushing Daily backup to Magnetic Tape\n\n”);

system(“mt -f /dev/nst0 rewind”);

$mtCmd = “tar -cf /dev/nst0 /backup/$tmstmp/”;

$mtCmdFlg = system($mtCmd);

if($mtCmdFlg != 0){

MyLog (“\nsystem_backup_error —>Pushing data to Magnetic Tape failed!\n”);

}

##end of weekend full data File backup

##This code allows you to push backup to any PC on the network

#$mountStatus = system(” mount -t smbfs -o ip=xx.xx.xx.xx,user=backupxx,password=xxxx //xxxxxx/xxSERVERBACK /mnt/backup/”);

#if($mountStatus == 0) {

#MyLog(“\n****Connected to desktop xx.xx.xx.xx to dump backup files”);

#system(“cp /backup/* /mnt/backup/.”);

#system(“rm -f /backup/*”);

#system(“umount /mnt/backup/”);

#MyLog “\n\n****Finished taking backup\n\n”;

#}

#else {

# MyLog(“Connection to desktop Failed”);

# exit 1;

#}

###################End of Program####################################