Subject nbackup+php = around-clock+around-global
Author lee_domain
Following is a 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.

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) ftp nbackup file to target_ftp_addr

// dis-conncet from database


// Global Variables
$dbname = "employee.fdb ";
$dbpath = "/home/myname/";
$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/myname/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;
}
} // 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";
}

if (is_file($backup_fullname))
{
echo "<br> nbackup file successful: ".$backup_fullname."<br>";
}
else
{
echo "<br> nbackup file failed <br>";
}


// 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>";
}

// 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>";
}


?>