[go: up one dir, main page]

Menu

[r3]: / cpy / bkgen  Maximize  Restore  History

Download this file

205 lines (200 with data), 7.2 kB

#!/usr/bin/php5
<? 
/*
VMLMAT: VM Linux Management and Archival Tool.
Copyright (c) 2007, Ronnie Michael, BMC Software Inc.

All rights reserved.

Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:

    * Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
    * Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
    * Neither the name of the BMC Software Inc. nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/ 
#
# lets begin with bkgen process.
#
# Initiated by cron to check for requests
# features Mult-volume support
#
# this function will delete all the related _volyx.tar files of the tarfile prefix
function delarcvols($arcpath,$tarfile) {  
     $path=$arcpath . ".";
     $handle=opendir($path);  
     while ($file = readdir($handle)) {  
     if ($file == "." || $file == "..") { } else { 
         $sln=strlen($file);
# remove the _volyx.tar 
         $name=substr($file,0,$sln-10);
         if ($tarfile == $name) system('rm ' . $arcpath . $file); 
      } 
     } 
     closedir($handle);  
     return;  
} 
?>
<?
#main procedure
#
# get parmfile variables needed
include 'bkinc.php';
#
# lets define our post routine file for CMS control
$pfile = "/tmp/post.txt";
#
# lets get the request info
$rfile = "reqfile.txt";
$prfile="/mnt/request/" . $rfile;
$fh = fopen($prfile, 'r'); 
$rdata = fgets($fh); 
fclose($fh);
$strgs = explode(" ", $rdata);
/* array starts at offset zero */
$done=$strgs[2];
#
# gen script called by root.crontab to process queue requests
#
#
#check for new request only if active reqfile.txt is done.
if ( $done == "1" ) {
system('echo "Checking for new requests"');
system('/var/clone/bkreq');
}
#
#Must re-read if bkreq has copied a new request to reqfile.txt
#
#Check the busy flag if set then exit so no further processing as
#we already have a bkgen processing it
#
$fh = fopen($prfile, 'r'); 
$rdata = fgets($fh); 
fclose($fh);
$strgs = explode(" ", $rdata);
/* array starts at offset zero */
$done=$strgs[2];
$bsy=$strgs[0];
#
if ( $bsy == "1" ) {
#lets check to see if queue has stopped due to improper shutdown
# lets get elapse time in hours...
$elapse = (time() - filemtime($prfile))/60;
#if more time than qwait to process a request has occurred, lets mark this request done and move on  
if ($elapse > $qwait) {
$done="1";
$fh = fopen($prfile, 'w'); 
$strdata =  substr($rdata, 0, 4) . $done . substr($rdata, 5, strlen($rdata));
fwrite($fh, $strdata);
fclose($fh);
$message=30;
$hostname="queue";
system('/var/clone/bkmail ' . $hostname . ' ' . $samail . ' ' . $message . ' x');
}
system('echo "request already in progress"');
exit(1);
}
#
# We are the new active bkgen for this process, so lets process
#
#
# set the busy flag in the request to tell next crontab bkgen do not process
$bsy="1";
$fh = fopen($prfile, 'w'); 
$strdata =  $bsy . ' ' . substr($rdata, 2, strlen($rdata));
fwrite($fh, $strdata);
fclose($fh);
#
# get our request info
$oip=$strgs[10];
$ohost=$strgs[9];
$userid=$strgs[8];
$email=$strgs[7];
$tarfile=$strgs[6];
$hip=$strgs[5];
$hostname=$strgs[4];
$req=$strgs[3];
#
# Lets check to see if request is a shutdown
if ( $req == "2" ) {
# let control vm shutdown and logoff the hostname
system('echo SHUTDOWN '. $hostname);
touch($pfile);chmod($pfile,0777);
system('hcp msg ' . $cvm . ' down ' . $hostname);
#lets sleep-spin until we get the post
        $i=0; /* lets kill the wait if too long and notify SysAdmin as the CMS Control VM may be hung. */
	while (file_exists($pfile)) {$i++;if($i=="120"){unlink($pfile);system('/var/clone/bkmail ' . $hostname . ' ' . $samail . ' 27 x');} system('sleep 1');}
# update up/down status of vm guests
system('/var/clone/bkstat');
$message="10";
#system('echo /var/clone/bkmail ' . $hostname . ' ' . $email . ' ' . $message . ' x');
#system('/var/clone/bkmail ' . $hostname . ' ' . $email . ' ' . $message . ' x');
system('/var/clone/bkdone ' . $message);
exit(1);
}
#
# Lets check to see if request is a bring up
if ( $req == "3" ) {
# let control vm xautolog the hostname
system('echo BRINGUP '. $hostname);
touch($pfile);chmod($pfile,0777);
system('hcp msg ' . $cvm . ' up ' . $hostname);
#lets sleep-spin until we get the post
        $i=0; /* lets kill the wait if too long and notify SysAdmin as the CMS Control VM may be hung. */
	while (file_exists($pfile)) {$i++;if($i=="120"){unlink($pfile);system('/var/clone/bkmail ' . $hostname . ' ' . $samail . ' 27 x');} system('sleep 1');}
# update up/down status of vm guests
system('/var/clone/bkstat');
$message="11";
#system('echo /var/clone/bkmail ' . $hostname . ' ' . $email . ' ' . $message . ' x');
#system('/var/clone/bkmail ' . $hostname . ' ' . $email . ' ' . $message . ' x');
system('/var/clone/bkdone ' . $message);
exit(1);
}
#
# Lets check to see if request is a delete backup
if ( $req == "4" ) {
$message="0";
system('echo delete ' . $hostname);
delarcvols($arcpath,$hostname);
system('/var/clone/bkdone ' . $message);
exit(1);
}
#
# Lets check to see if request is status update
if ( $req == "6" ) {
$message="0";
system('/var/clone/bkstat');
system('/var/clone/bkdone ' . $message);
exit(1);
}
#
#check for correct request before proceeding
# must be either 0 or 1 at this point
if ( $req != "0" && $req != "1" && $req != "5" ) {
# Some type of control error has occurred as request is malformed 
$message="12";
system('echo /var/clone/bkmail ' . $hostname . ' ' . $email . ' ' . $message . ' x');
system('/var/clone/bkmail ' . $hostname . ' ' . $email . ' ' . $message . ' x');
system('/var/clone/bkdone ' . $message);
exit(1);
}
#lets setup the change file for changing the hostname and IP address
$prfile = "/tmp/change.input";
$fh = fopen($prfile, 'w'); 
$nl = "\n";
$strdata =  "/" . $ohost . "/s/" . $ohost . "/" . $hostname . "/g" . $nl;
fwrite($fh, $strdata);
$strdata =  "/" . $oip . "/s/" . $oip . "/" . $hip . "/g" . $nl;
fwrite($fh, $strdata);
fclose($fh);
# lets begin clone (bkup or restore) processing
system('echo /var/clone/bkclone ' . $tarfile); 
system('/var/clone/bkclone ' . $tarfile); 
?>