Subject | Re: nbackup+php = around-clock+around-global |
---|---|
Author | lee_domain |
Post date | 2007-10-11T01:05:30Z |
Following is the updated PHP script to run nbackup on FB2.1
Put this script in a place where your system can call it
automatically/regularly in back-ground
Or the normal way: http://localhost/nbackups.php
I am afraid if the Level 0 file is too large, it is not practical to
send it through e-mail attachement, nor on ftp, unless you've got a
solid exclusive line. ( don't test the tolerance of your ISP, it's
just another business)
It will
1) make the nbackup,
2) read the RDB$BACKUP_HISTORY information and put it into
the e-mail as message
3) compress the nbackup file (bz2)
4) send it together with the e-mail as attachment
I believe you must have figured out what the receiving side will do.
Yes, you are right. An around-clock, around-global database mirro
site ( that's all or only as the first step ?)
Would you like to write a restore program for it ? It can be in any
languages, not necessary with php.
Here is it, test it, and expecting your reply.
<!-- connect to a firebird database and do the nbackup nbackups.php --->
<?php
// nbackup (24x7), around-clock, around-global
// 1) connect to the daabase to be nbackuped
// 2) open table RDB$BACKUP_HISTORY
// 3) check if Level 0 ~ level 2 nbackup files already made
// 4) if already made, go to next step
// 5) if not, do the nbackup
// next step
// 6) e-mail nbackup file as an attachmnet to target_email_addr
// 7) or ftp nbackup file to target_ftp_addr
// dis-conncet from database
// Global Variables
$dbname = "employee.fdb ";
$dbpath = "/home/litaoman/";
$backup_name = "employee";
$backup_path = "/tmp/bak/";
$back_ext = ".nbk";
$backup_fullname ;
$level_0 = "_0-";
$level_1 = "_1-";
$level_2 = "_2-";
$level_3 = "_3-";
$target_email_addr = "daomannlee@...";
$target_ftp_addr = "123.123.12.12";
$tmprcd = 0;
$tmptbl = 0;
$tmplvl = 0;
$tmplvl_0 = 0;
$tmplvl_1 = 0;
$tmplvl_2 = 0;
$tmplvl_3 = 0;
$tmperror = 0;
$tmpnbkup = 0;
$tmpmkcmd = 0;
// set this var to 1 will set a silent mode
$tmpsilent = 0;
// make sure it will not generate the SAME time
sleep(1);
$time_ext = date("Y-m-d-H-i-s");
if ( $tmpsilent == 0)
echo "$time_ext.<br>";
// 1) connect to a database server
$srv_db = 'localhost:/home/litaoman/employee.fdb';
$usrname = 'sysdba';
$pswd = 'masterkey';
// get and report connection result
$conn = fbird_connect($srv_db, $usrname, $pswd);
echo $conn."<br>";
// 2+3) open RDB$BACKUP_HISTORY and check if Level 0 already made
$sql = "SELECT RDB\$BACKUP_LEVEL FROM RDB\$BACKUP_HISTORY";
if ( $tmpsilent == 0)
echo $sql."<br>";
// 2+3) do the query
$res = fbird_query($conn, $sql);
while ( $row = fbird_fetch_object($res) )
{
$bklv = "RDB\$BACKUP_LEVEL";
$lv = trim($row->$bklv);
//echo $lv;
if ($lv == "0")
{
if ( $tmpsilent == 0)
echo "level_0 primary nbackup found <br>";
GLOBAL $tmptbl ;
$tmptbl = 1; // yes, exists
GLOBAL $tmplvl ;
$tmplvl = 1;
GLOBAL $tmplvl_0 ;
$tmplvl_0 = 1;
}
if ($lv == "1")
{
if ( $tmpsilent == 0)
echo "level_1 nbackup file found <br>";
GLOBAL $tmptbl ;
$tmptbl = 1; // yes, exists
GLOBAL $tmplvl ;
$tmplvl = 1;
GLOBAL $tmplvl_1 ;
$tmplvl_1 = 1;
}
if ($lv == "2")
{
if ( $tmpsilent == 0)
echo "level_2 nbackup file found <br>";
GLOBAL $tmptbl ;
$tmptbl = 1; // yes, exists
GLOBAL $tmplvl ;
$tmplvl = 1;
GLOBAL $tmplvl_2 ;
$tmplvl_2 = 1;
}
if ($lv == "3")
{
if ( $tmpsilent == 0)
echo "level_3 nbackup file found <br>";
GLOBAL $tmplvl_3 ;
$tmplvl_3 = 1;
// if found ANY level 3 file, that's enough.
// terminate the while loop at once.
break;
}
} // end of while do query
// relaese the results
fbird_free_result($res);
fbird_commit($conn);
fbird_close($conn);
if ( $tmplvl_0 == 0)
{
if ($tmplvl_1 == 1)
{
echo "error: level 1 nbackup file found, but no level 0 file";
GLOBAL $tmperror ;
$tmperror = 1; //set error flag
}
if ($tmplvl_2 == 1)
{
echo "error: level 2 nbackup file found, but no level 0 file";
GLOBAL $tmperror ;
$tmperror = 1; //set error flag
}
if ($tmplvl_3 == 1)
{
echo "error: level 3 nbackup file found, but no level 0 file";
GLOBAL $tmperror ;
$tmperror = 1; //set error flag
}
}
if ( $tmplvl_1 == 0)
{
if ($tmplvl_2 == 1)
{
echo "error: level 2 nbackup file found, but no level 1 file";
GLOBAL $tmperror ;
$tmperror = 1; //set error flag
}
if ($tmplvl_3 == 1)
{
echo "error: level 3 nbackup file found, but no level 1 file";
GLOBAL $tmperror ;
$tmperror = 1; //set error flag
}
}
if ( $tmplvl_2 == 0)
{
if ($tmplvl_3 == 1)
{
echo "error: level 3 nbackup file found, but no level 2 file";
GLOBAL $tmperror ;
$tmperror = 1; //set error flag
}
}
if ($tmperror == 1 )
exit;
GLOBAL $tmpnbkup ;
$tmpnbkup = 1;
if ( $tmpsilent == 0)
echo "<br> now begin to do the nbackups <br>";
$dir = getcwd();
if ( $tmpsilent == 0)
echo "<br> the current directory:".$dir."<br>";
if ( $tmpsilent == 0)
echo "change directory to /opt/firebird/bin <br>";
chdir("/opt/firebird/bin");
$dirs = getcwd();
if ( $tmpsilent == 0)
echo "the current directory:".$dirs."<br>";
//each run of script can make only one nbackup
//after each nbackup, set the make flag = 1
if ($tmplvl_0 == 0 and $tmpnbkup == 1 and $tmpmkcmd == 0)
{
$backup_fullname =
$backup_path.$backup_name.$level_0.$time_ext.'.nbk';
$nbackup_cmd = "./nbackup -U sysdba -P masterkey -B 0
".$dbpath.$dbname.$backup_fullname;
if ( $tmpsilent == 0)
echo $nbackup_cmd."<br>";
GLOBAL $tmpmkcmd ;
$tmpmkcmd = 1;
}
//there must be a time gap between
//now make the second file, level 1
//each run of script can make only one nbackup
//after each nbackup, set the make flag = 1
if ($tmplvl_1 == 0 and $tmpnbkup == 1 and $tmpmkcmd == 0)
{
$backup_fullname =
$backup_path.$backup_name.$level_1.$time_ext.'.nbk';
$nbackup_cmd = "./nbackup -U sysdba -P masterkey -B 1
".$dbpath.$dbname.$backup_fullname;
if ( $tmpsilent == 0)
echo $nbackup_cmd."<br>";
GLOBAL $tmpmkcmd ;
$tmpmkcmd = 1;
}
// there must be a time gap betwen
// now make the third file, level 2
//each run of script can make only one nbackup
//after each nbackup, set the make flag = 1
if ($tmplvl_2 == 0 and $tmpnbkup == 1 and $tmpmkcmd == 0)
{
$backup_fullname =
$backup_path.$backup_name.$level_2.$time_ext.'.nbk';
$nbackup_cmd = "./nbackup -U sysdba -P masterkey -B 2
".$dbpath.$dbname.$backup_fullname;
if ( $tmpsilent == 0)
echo $nbackup_cmd."<br>";
GLOBAL $tmpmkcmd ;
$tmpmkcmd = 1;
}
// there must be a time gap betwen
// now make the level 3 files
// if nothing made of the previous level files..
if ($tmpmkcmd == 0 )
{
$backup_fullname =
$backup_path.$backup_name.$level_3.$time_ext.'.nbk';
$nbackup_cmd = "./nbackup -U sysdba -P masterkey -B 3
".$dbpath.$dbname.$backup_fullname;
if ( $tmpsilent == 0)
echo $nbackup_cmd."<br>";
}
// make the nbackup file finally
$outcome = exec($nbackup_cmd);
if ( $tmpsilent == 0)
echo "nbackup result: ".$outcome."<br>";
if ( $tmpsilent == 0)
echo "<br> now change back to starting directory <br>";
chdir($dir);
$dirs = getcwd();
if ( $tmpsilent == 0)
echo "the current directory:".$dirs."<br>";
//system way to check if nbackup is successful
if ( $tmpsilent == 0)
echo "<br> system re-check if all nbackup files in position <br>";
$dirsrc = opendir("/tmp/bak");
if (dirsrc)
{
while(( $file = readdir ($dirsrc)) != false)
{
if ( $tmpsilent == 0)
echo "file name: $file <br>";
}
}
else
{
echo "open directory failed";
GLOBAL $tmperror ;
$tmperror = 1; //set error flag
}
if (is_file($backup_fullname))
{
echo "<br> nbackup file successful: ".$backup_fullname."<br>";
}
else
{
echo "<br> nbackup file failed <br>";
GLOBAL $tmperror ;
$tmperror = 1; //set error flag
}
// no nbackup file found
if ( $tmperror == 1 )
exit;
GLOBAL $tmperror ;
$tmperror = 1; //manually set error flag, for testing
// re-check if nbackup is successful
if ( $tmpsilent == 0)
echo "<br> confirm if nbackup is successful by re-connect the
database <br>";
$sql = "SELECT RDB\$FILE_NAME FROM RDB\$BACKUP_HISTORY";
if ( $tmpsilent == 0)
echo $sql."<br>";
// get and report connection result
$conn = fbird_connect($srv_db, $usrname, $pswd);
if ( $tmpsilent == 0)
echo $conn."<br>";
$res = fbird_query($conn, $sql);
while ( $row = fbird_fetch_object($res) )
{
$bkid = "RDB\$FILE_NAME";
$id = trim($row->$bkid);
if ( $tmpsilent == 0)
echo $id."<br>";
if ($id == $backup_fullname)
{
echo "<br> nbackup file confirmed: ".$backup_fullname."<br>";
GLOBAL $tmperror ;
$tmperror = 0; //set down error flag
break;
}
}
// relaese the results
fbird_free_result($res);
// no nbackup file name found in RDB$BACKUP_HISTORY
if ($tmperror == 1) //error flag set
{
// close the connected database, and report result
$closedb = fbird_close($conn);
if ($closedb == 1)
{
echo "<br> disconnect from database OK ! <br>";
} else {
echo "<br> disconnect from database failed ! <br>";
}
echo "no nbackup file name found in RDB$BACKUP_HISTORY";
exit;
}
// get all information from RDB$BACKUP_HISTORY for this nbackup file
if ( $tmpsilent == 0)
echo "<br>get all information from RDB\$BACKUP_HISTORY for this
nbackup file <br>";
$sql = "SELECT * FROM RDB\$BACKUP_HISTORY WHERE RDB\$FILE_NAME =
'".$backup_fullname."'";
if ( $tmpsilent == 0)
echo $sql."<br>";
$res = fbird_query($conn, $sql);
while ( $row = fbird_fetch_object($res) )
{
$bkid = "RDB\$BACKUP_ID";
$id = trim($row->$bkid);
$bkts = "RDB\$TIMESTAMP";
$ts = trim($row->$bkts);
$bklv = "RDB\$BACKUP_LEVEL";
$lv = trim($row->$bklv);
$bkgd = "RDB\$GUID";
$gd = trim($row->$bkgd);
$bksn = "RDB\$SCN";
$sn = trim($row->$bksn);
$bkfn = "RDB\$FILE_NAME";
$fn = trim($row->$bkfn);
$delma = ";;"; // just a mark to seperate each value
$bkinfo =
$delma.$id.$delma.$ts.$delma.$lv.$delma.$gd.$delma.$sn.$delma.$fn.$delma;
//echo $bkinfo;
}
echo $bkinfo;
// relaese the results
fbird_free_result($res);
// close the connected database, and report result
$closedb = fbird_close($conn);
if ($closedb == 1)
{
echo "<br> disconnect from database OK ! <br>";
} else {
echo "<br> disconnect from database failed ! <br>";
}
$bzip_cmd = "bzip2 ".$backup_fullname;
$outcome = exec($bzip_cmd);
if ( $tmpsilent == 0)
echo "bzip2 result: ".$outcome."<br>";
if (is_file($backup_fullname.".bz2"))
{
echo "compress file successful: ".$backup_fullname.".bz2 <br>";
}
else
{
echo "<br> compress file failed <br>";
GLOBAL $tmperror ;
$tmperror = 1; //set error flag
}
if ($tmperror == 1) //error flag set
exit;
$attachment = $backup_fullname.".bz2";
echo "begin to send nbackup file as e-mail-attachment <br>";
/*******************************
* phpmailer is an open source project at:
* http://sourceforge.net
*******************************/
require("class.phpmailer.php");
$mail = new PHPMailer();
$mail->From = "source@...";
$mail->FromName = "Sender's Name";
$mail->AddAddress("receiver@...");
// optional, more target e-mail address/replyto add here
// $mail->AddAddress("other-addr@...");
// $mail->AddReplyTo("info@...","Information");
$mail->AddAttachment($attachment); // attachment
// optional, more attachment to add here
// $mail->AddAttachment($attachment);
echo "$backup_name.$level_3.$time_ext.".nbk.bz2."<br>";
$mail->Subject = $backup_name.$level_3.$time_ext.".nbk.bz2";
$mail->Body = $bkinfo;
if(!$mail->Send())
{
echo "<br> Message was not sent.<br>";
echo "Mailer error: " . $mail->ErrorInfo;
}
else
{
echo "<br> Message has been sent.<br>";
/**
IMPORTANT WARNING
In case that your Mail Server is not installed/configured
correctly, even if this message is displaying here, your
e-mail may NEVER get to its target in the real world.
All transferred data is not ciphered, you may want to
cipher the data before sending out.
Better require an automatic confirmation of receipt from
the receiving side.
*/
}
$bzip_cmd = "bzip2 -d ".$backup_fullname.".bz2";
$outcome = exec($bzip_cmd);
if ( $tmpsilent == 0)
echo "<br> unbzip2 result: ".$outcome."<br>";
if (is_file($backup_fullname))
{
echo "<br>de-compress file successful:".$backup_fullname."<br>";
}
else
{
echo "<br> de-compress file failed <br>";
GLOBAL $tmperror ;
$tmperror = 1; //set error flag
}
?>
Put this script in a place where your system can call it
automatically/regularly in back-ground
Or the normal way: http://localhost/nbackups.php
I am afraid if the Level 0 file is too large, it is not practical to
send it through e-mail attachement, nor on ftp, unless you've got a
solid exclusive line. ( don't test the tolerance of your ISP, it's
just another business)
It will
1) make the nbackup,
2) read the RDB$BACKUP_HISTORY information and put it into
the e-mail as message
3) compress the nbackup file (bz2)
4) send it together with the e-mail as attachment
I believe you must have figured out what the receiving side will do.
Yes, you are right. An around-clock, around-global database mirro
site ( that's all or only as the first step ?)
Would you like to write a restore program for it ? It can be in any
languages, not necessary with php.
Here is it, test it, and expecting your reply.
<!-- connect to a firebird database and do the nbackup nbackups.php --->
<?php
// nbackup (24x7), around-clock, around-global
// 1) connect to the daabase to be nbackuped
// 2) open table RDB$BACKUP_HISTORY
// 3) check if Level 0 ~ level 2 nbackup files already made
// 4) if already made, go to next step
// 5) if not, do the nbackup
// next step
// 6) e-mail nbackup file as an attachmnet to target_email_addr
// 7) or ftp nbackup file to target_ftp_addr
// dis-conncet from database
// Global Variables
$dbname = "employee.fdb ";
$dbpath = "/home/litaoman/";
$backup_name = "employee";
$backup_path = "/tmp/bak/";
$back_ext = ".nbk";
$backup_fullname ;
$level_0 = "_0-";
$level_1 = "_1-";
$level_2 = "_2-";
$level_3 = "_3-";
$target_email_addr = "daomannlee@...";
$target_ftp_addr = "123.123.12.12";
$tmprcd = 0;
$tmptbl = 0;
$tmplvl = 0;
$tmplvl_0 = 0;
$tmplvl_1 = 0;
$tmplvl_2 = 0;
$tmplvl_3 = 0;
$tmperror = 0;
$tmpnbkup = 0;
$tmpmkcmd = 0;
// set this var to 1 will set a silent mode
$tmpsilent = 0;
// make sure it will not generate the SAME time
sleep(1);
$time_ext = date("Y-m-d-H-i-s");
if ( $tmpsilent == 0)
echo "$time_ext.<br>";
// 1) connect to a database server
$srv_db = 'localhost:/home/litaoman/employee.fdb';
$usrname = 'sysdba';
$pswd = 'masterkey';
// get and report connection result
$conn = fbird_connect($srv_db, $usrname, $pswd);
echo $conn."<br>";
// 2+3) open RDB$BACKUP_HISTORY and check if Level 0 already made
$sql = "SELECT RDB\$BACKUP_LEVEL FROM RDB\$BACKUP_HISTORY";
if ( $tmpsilent == 0)
echo $sql."<br>";
// 2+3) do the query
$res = fbird_query($conn, $sql);
while ( $row = fbird_fetch_object($res) )
{
$bklv = "RDB\$BACKUP_LEVEL";
$lv = trim($row->$bklv);
//echo $lv;
if ($lv == "0")
{
if ( $tmpsilent == 0)
echo "level_0 primary nbackup found <br>";
GLOBAL $tmptbl ;
$tmptbl = 1; // yes, exists
GLOBAL $tmplvl ;
$tmplvl = 1;
GLOBAL $tmplvl_0 ;
$tmplvl_0 = 1;
}
if ($lv == "1")
{
if ( $tmpsilent == 0)
echo "level_1 nbackup file found <br>";
GLOBAL $tmptbl ;
$tmptbl = 1; // yes, exists
GLOBAL $tmplvl ;
$tmplvl = 1;
GLOBAL $tmplvl_1 ;
$tmplvl_1 = 1;
}
if ($lv == "2")
{
if ( $tmpsilent == 0)
echo "level_2 nbackup file found <br>";
GLOBAL $tmptbl ;
$tmptbl = 1; // yes, exists
GLOBAL $tmplvl ;
$tmplvl = 1;
GLOBAL $tmplvl_2 ;
$tmplvl_2 = 1;
}
if ($lv == "3")
{
if ( $tmpsilent == 0)
echo "level_3 nbackup file found <br>";
GLOBAL $tmplvl_3 ;
$tmplvl_3 = 1;
// if found ANY level 3 file, that's enough.
// terminate the while loop at once.
break;
}
} // end of while do query
// relaese the results
fbird_free_result($res);
fbird_commit($conn);
fbird_close($conn);
if ( $tmplvl_0 == 0)
{
if ($tmplvl_1 == 1)
{
echo "error: level 1 nbackup file found, but no level 0 file";
GLOBAL $tmperror ;
$tmperror = 1; //set error flag
}
if ($tmplvl_2 == 1)
{
echo "error: level 2 nbackup file found, but no level 0 file";
GLOBAL $tmperror ;
$tmperror = 1; //set error flag
}
if ($tmplvl_3 == 1)
{
echo "error: level 3 nbackup file found, but no level 0 file";
GLOBAL $tmperror ;
$tmperror = 1; //set error flag
}
}
if ( $tmplvl_1 == 0)
{
if ($tmplvl_2 == 1)
{
echo "error: level 2 nbackup file found, but no level 1 file";
GLOBAL $tmperror ;
$tmperror = 1; //set error flag
}
if ($tmplvl_3 == 1)
{
echo "error: level 3 nbackup file found, but no level 1 file";
GLOBAL $tmperror ;
$tmperror = 1; //set error flag
}
}
if ( $tmplvl_2 == 0)
{
if ($tmplvl_3 == 1)
{
echo "error: level 3 nbackup file found, but no level 2 file";
GLOBAL $tmperror ;
$tmperror = 1; //set error flag
}
}
if ($tmperror == 1 )
exit;
GLOBAL $tmpnbkup ;
$tmpnbkup = 1;
if ( $tmpsilent == 0)
echo "<br> now begin to do the nbackups <br>";
$dir = getcwd();
if ( $tmpsilent == 0)
echo "<br> the current directory:".$dir."<br>";
if ( $tmpsilent == 0)
echo "change directory to /opt/firebird/bin <br>";
chdir("/opt/firebird/bin");
$dirs = getcwd();
if ( $tmpsilent == 0)
echo "the current directory:".$dirs."<br>";
//each run of script can make only one nbackup
//after each nbackup, set the make flag = 1
if ($tmplvl_0 == 0 and $tmpnbkup == 1 and $tmpmkcmd == 0)
{
$backup_fullname =
$backup_path.$backup_name.$level_0.$time_ext.'.nbk';
$nbackup_cmd = "./nbackup -U sysdba -P masterkey -B 0
".$dbpath.$dbname.$backup_fullname;
if ( $tmpsilent == 0)
echo $nbackup_cmd."<br>";
GLOBAL $tmpmkcmd ;
$tmpmkcmd = 1;
}
//there must be a time gap between
//now make the second file, level 1
//each run of script can make only one nbackup
//after each nbackup, set the make flag = 1
if ($tmplvl_1 == 0 and $tmpnbkup == 1 and $tmpmkcmd == 0)
{
$backup_fullname =
$backup_path.$backup_name.$level_1.$time_ext.'.nbk';
$nbackup_cmd = "./nbackup -U sysdba -P masterkey -B 1
".$dbpath.$dbname.$backup_fullname;
if ( $tmpsilent == 0)
echo $nbackup_cmd."<br>";
GLOBAL $tmpmkcmd ;
$tmpmkcmd = 1;
}
// there must be a time gap betwen
// now make the third file, level 2
//each run of script can make only one nbackup
//after each nbackup, set the make flag = 1
if ($tmplvl_2 == 0 and $tmpnbkup == 1 and $tmpmkcmd == 0)
{
$backup_fullname =
$backup_path.$backup_name.$level_2.$time_ext.'.nbk';
$nbackup_cmd = "./nbackup -U sysdba -P masterkey -B 2
".$dbpath.$dbname.$backup_fullname;
if ( $tmpsilent == 0)
echo $nbackup_cmd."<br>";
GLOBAL $tmpmkcmd ;
$tmpmkcmd = 1;
}
// there must be a time gap betwen
// now make the level 3 files
// if nothing made of the previous level files..
if ($tmpmkcmd == 0 )
{
$backup_fullname =
$backup_path.$backup_name.$level_3.$time_ext.'.nbk';
$nbackup_cmd = "./nbackup -U sysdba -P masterkey -B 3
".$dbpath.$dbname.$backup_fullname;
if ( $tmpsilent == 0)
echo $nbackup_cmd."<br>";
}
// make the nbackup file finally
$outcome = exec($nbackup_cmd);
if ( $tmpsilent == 0)
echo "nbackup result: ".$outcome."<br>";
if ( $tmpsilent == 0)
echo "<br> now change back to starting directory <br>";
chdir($dir);
$dirs = getcwd();
if ( $tmpsilent == 0)
echo "the current directory:".$dirs."<br>";
//system way to check if nbackup is successful
if ( $tmpsilent == 0)
echo "<br> system re-check if all nbackup files in position <br>";
$dirsrc = opendir("/tmp/bak");
if (dirsrc)
{
while(( $file = readdir ($dirsrc)) != false)
{
if ( $tmpsilent == 0)
echo "file name: $file <br>";
}
}
else
{
echo "open directory failed";
GLOBAL $tmperror ;
$tmperror = 1; //set error flag
}
if (is_file($backup_fullname))
{
echo "<br> nbackup file successful: ".$backup_fullname."<br>";
}
else
{
echo "<br> nbackup file failed <br>";
GLOBAL $tmperror ;
$tmperror = 1; //set error flag
}
// no nbackup file found
if ( $tmperror == 1 )
exit;
GLOBAL $tmperror ;
$tmperror = 1; //manually set error flag, for testing
// re-check if nbackup is successful
if ( $tmpsilent == 0)
echo "<br> confirm if nbackup is successful by re-connect the
database <br>";
$sql = "SELECT RDB\$FILE_NAME FROM RDB\$BACKUP_HISTORY";
if ( $tmpsilent == 0)
echo $sql."<br>";
// get and report connection result
$conn = fbird_connect($srv_db, $usrname, $pswd);
if ( $tmpsilent == 0)
echo $conn."<br>";
$res = fbird_query($conn, $sql);
while ( $row = fbird_fetch_object($res) )
{
$bkid = "RDB\$FILE_NAME";
$id = trim($row->$bkid);
if ( $tmpsilent == 0)
echo $id."<br>";
if ($id == $backup_fullname)
{
echo "<br> nbackup file confirmed: ".$backup_fullname."<br>";
GLOBAL $tmperror ;
$tmperror = 0; //set down error flag
break;
}
}
// relaese the results
fbird_free_result($res);
// no nbackup file name found in RDB$BACKUP_HISTORY
if ($tmperror == 1) //error flag set
{
// close the connected database, and report result
$closedb = fbird_close($conn);
if ($closedb == 1)
{
echo "<br> disconnect from database OK ! <br>";
} else {
echo "<br> disconnect from database failed ! <br>";
}
echo "no nbackup file name found in RDB$BACKUP_HISTORY";
exit;
}
// get all information from RDB$BACKUP_HISTORY for this nbackup file
if ( $tmpsilent == 0)
echo "<br>get all information from RDB\$BACKUP_HISTORY for this
nbackup file <br>";
$sql = "SELECT * FROM RDB\$BACKUP_HISTORY WHERE RDB\$FILE_NAME =
'".$backup_fullname."'";
if ( $tmpsilent == 0)
echo $sql."<br>";
$res = fbird_query($conn, $sql);
while ( $row = fbird_fetch_object($res) )
{
$bkid = "RDB\$BACKUP_ID";
$id = trim($row->$bkid);
$bkts = "RDB\$TIMESTAMP";
$ts = trim($row->$bkts);
$bklv = "RDB\$BACKUP_LEVEL";
$lv = trim($row->$bklv);
$bkgd = "RDB\$GUID";
$gd = trim($row->$bkgd);
$bksn = "RDB\$SCN";
$sn = trim($row->$bksn);
$bkfn = "RDB\$FILE_NAME";
$fn = trim($row->$bkfn);
$delma = ";;"; // just a mark to seperate each value
$bkinfo =
$delma.$id.$delma.$ts.$delma.$lv.$delma.$gd.$delma.$sn.$delma.$fn.$delma;
//echo $bkinfo;
}
echo $bkinfo;
// relaese the results
fbird_free_result($res);
// close the connected database, and report result
$closedb = fbird_close($conn);
if ($closedb == 1)
{
echo "<br> disconnect from database OK ! <br>";
} else {
echo "<br> disconnect from database failed ! <br>";
}
$bzip_cmd = "bzip2 ".$backup_fullname;
$outcome = exec($bzip_cmd);
if ( $tmpsilent == 0)
echo "bzip2 result: ".$outcome."<br>";
if (is_file($backup_fullname.".bz2"))
{
echo "compress file successful: ".$backup_fullname.".bz2 <br>";
}
else
{
echo "<br> compress file failed <br>";
GLOBAL $tmperror ;
$tmperror = 1; //set error flag
}
if ($tmperror == 1) //error flag set
exit;
$attachment = $backup_fullname.".bz2";
echo "begin to send nbackup file as e-mail-attachment <br>";
/*******************************
* phpmailer is an open source project at:
* http://sourceforge.net
*******************************/
require("class.phpmailer.php");
$mail = new PHPMailer();
$mail->From = "source@...";
$mail->FromName = "Sender's Name";
$mail->AddAddress("receiver@...");
// optional, more target e-mail address/replyto add here
// $mail->AddAddress("other-addr@...");
// $mail->AddReplyTo("info@...","Information");
$mail->AddAttachment($attachment); // attachment
// optional, more attachment to add here
// $mail->AddAttachment($attachment);
echo "$backup_name.$level_3.$time_ext.".nbk.bz2."<br>";
$mail->Subject = $backup_name.$level_3.$time_ext.".nbk.bz2";
$mail->Body = $bkinfo;
if(!$mail->Send())
{
echo "<br> Message was not sent.<br>";
echo "Mailer error: " . $mail->ErrorInfo;
}
else
{
echo "<br> Message has been sent.<br>";
/**
IMPORTANT WARNING
In case that your Mail Server is not installed/configured
correctly, even if this message is displaying here, your
e-mail may NEVER get to its target in the real world.
All transferred data is not ciphered, you may want to
cipher the data before sending out.
Better require an automatic confirmation of receipt from
the receiving side.
*/
}
$bzip_cmd = "bzip2 -d ".$backup_fullname.".bz2";
$outcome = exec($bzip_cmd);
if ( $tmpsilent == 0)
echo "<br> unbzip2 result: ".$outcome."<br>";
if (is_file($backup_fullname))
{
echo "<br>de-compress file successful:".$backup_fullname."<br>";
}
else
{
echo "<br> de-compress file failed <br>";
GLOBAL $tmperror ;
$tmperror = 1; //set error flag
}
?>