462 lines
16 KiB
PHP
Executable File
462 lines
16 KiB
PHP
Executable File
<?php
|
|
include('sqliteDB.php');
|
|
$localBackupHash = array();
|
|
$selectFolderCount = '';
|
|
$current_pos = 0;
|
|
|
|
if(function_exists('pcntl_signal')){
|
|
declare(ticks = 1); //To handle signals (Kill/Term/..etc)
|
|
pcntl_signal(SIGINT, 'process_term');
|
|
pcntl_signal(SIGTERM, 'process_term');
|
|
}
|
|
|
|
if(!createLBDB()){
|
|
$errStr = "Create LBDB failed.";
|
|
traceDBLog($errStr);
|
|
writeOperationErrorToErrorFile($errStr);
|
|
exit;
|
|
}
|
|
|
|
$ibFolder = "ibfolder";
|
|
$ibFile = "ibfile";
|
|
try{
|
|
$selectFolderCount = $dbh_LB->prepare("SELECT DIRID FROM $ibFolder WHERE NAME=?");
|
|
$dbh_ibFolder_insert = $dbh_LB->prepare("INSERT INTO $ibFolder (NAME,FILE_LMD,FILE_SIZE,SYNC_DATE) values (?,?,?,?)");
|
|
$dbh_ibFile_insert = $dbh_LB->prepare("INSERT INTO $ibFile (DIRID,NAME,FILE_LMD,FILE_SIZE,BKSET_LMD,SYNC_DATE,BKSET_NAME,HASH_VALUE,FOLDER_ID,MPC,ENC_NAME,FILE_OR_FOLDER,BACKUP_STATUS) VALUES (?,?,?,?,?,?,?,?,?,?,?,?,?)");
|
|
$dbh_ibFolder_update = $dbh_LB->prepare("UPDATE $ibFolder SET FILE_LMD=? WHERE NAME=?");
|
|
$dbh_ibFile_update = $dbh_LB->prepare("UPDATE $ibFile SET FILE_LMD=?,FILE_SIZE=? WHERE NAME=? AND FOLDER_ID=?") ;
|
|
}
|
|
catch (PDOException $e){
|
|
$errStr = "Error: Prepare statement failed for insert/update for Local Backup, ".customErrorMsg();
|
|
traceDBLog($errStr);
|
|
writeOperationErrorToErrorFile($errStr);
|
|
|
|
if($dbh_LB)
|
|
unset($dbh_LB);
|
|
exit;
|
|
}
|
|
|
|
$db_info_maualTask = $userDir.PATHSEPARATOR.LOCALBACKUP.PATHSEPARATOR.MANUALTASK.PATHSEPARATOR.TEMP_DB_FILES;
|
|
$db_info_file = $db_info_maualTask.LOCALDBINFOFILE;
|
|
$db_info_retry_file = $db_info_maualTask."db_info_retry.txt";
|
|
$old_data = "";
|
|
|
|
while(1){
|
|
if(!file_exists($db_info_file))
|
|
sleep(2);
|
|
else
|
|
break;
|
|
}
|
|
|
|
commitXmlData();
|
|
$dbh_LB->commit();
|
|
|
|
# Retry scenario
|
|
if(file_exists($db_info_retry_file)){
|
|
file_put_contents($db_info_retry_file,"PROCESS_COMPLETED",FILE_APPEND);
|
|
rename($db_info_retry_file,$db_info_file);
|
|
@chmod($db_info_file, PERMISSIONVAL);
|
|
commitXmlData(1);
|
|
$dbh_LB->commit();
|
|
}
|
|
exit;
|
|
|
|
#************************************************************************************************************************
|
|
# Subroutine Name : commitXmlData
|
|
# Objective : This subroutine helps to read xml data and pass the details to insert/update process
|
|
# Added By : Vijay Vinoth [23/01/2018]
|
|
# Modified By :
|
|
#*************************************************************************************************************************
|
|
function commitXmlData($retry = 0)
|
|
{
|
|
GLOBAL $dbh_LB, $db_info_file, $db_info_maualTask, $db_info_retry_file, $fileCount, $databaseLB;
|
|
$handle = fopen($db_info_file,'r');
|
|
if(!$handle){
|
|
traceDBLog("Unable to open $db_info_file\n");
|
|
exit;
|
|
}
|
|
$extendedAsciiChars = getExtendedASCIIChrOrd();
|
|
while (1)
|
|
{
|
|
$dbXmlFileName = '';
|
|
$error = 0;
|
|
|
|
$dbXmlFileName = trim(fgets($handle));
|
|
if($dbXmlFileName == ''){
|
|
sleep(WAIT_FOR);
|
|
fseek($handle, 0, SEEK_CUR); //to clear eof flag
|
|
continue;
|
|
}
|
|
|
|
if($dbXmlFileName == 'PROCESS_COMPLETED'){
|
|
break;
|
|
}
|
|
|
|
$xml=simplexml_load_file($db_info_maualTask.$dbXmlFileName);
|
|
|
|
if(!$xml) {
|
|
$resultListXML = str_replace($extendedAsciiChars['chr'], $extendedAsciiChars['ord'], $xml);
|
|
$xml = simplexml_load_string($resultListXML);
|
|
if(!$xml){
|
|
|
|
$xmlResult = file_get_contents($db_info_maualTask.$dbXmlFileName);
|
|
$xml = constructXMLByParsingEachLine($xmlResult);
|
|
}
|
|
}
|
|
if(!$xml)
|
|
{
|
|
$string = file_get_contents($db_info_maualTask.$dbXmlFileName);
|
|
|
|
$search = "/>";
|
|
$string = explode("\n", $string); // \n is the character for a line break
|
|
|
|
foreach($string as $val)
|
|
{
|
|
if(strpos(trim($val),$search) !== false)
|
|
{
|
|
$dbpaths = simplexml_load_string($val);
|
|
if(!$dbpaths){
|
|
traceDBLog("Error: Unable commit :: ".trim($val));
|
|
continue;
|
|
}
|
|
insertUpdatedBbData($dbpaths);
|
|
}
|
|
else if((strpos(trim($val),'dbpaths>') === false) && (strpos(trim($val),'<?xml version="1.0" encoding="UTF-8"?>') === false)){
|
|
traceDBLog("Error: Unable commit :: ".trim($val));
|
|
}
|
|
}
|
|
continue;
|
|
}
|
|
|
|
if($retry == 0) {
|
|
$dbh_LB->beginTransaction();
|
|
@chmod($databaseLB . '-shm', PERMISSIONVAL);
|
|
@chmod($databaseLB . '-wal', PERMISSIONVAL);
|
|
}
|
|
|
|
foreach($xml->children() as $dbpaths)
|
|
insertUpdatedBbData($dbpaths);
|
|
try{
|
|
if($retry == 0) {
|
|
$dbh_LB->commit();
|
|
}
|
|
}
|
|
catch (PDOException $e) {
|
|
traceDBLog('Exception: ' . $e->getMessage());
|
|
$error = 1;
|
|
|
|
if($retry == 1)
|
|
file_put_contents($db_info_retry_file,"$dbXmlFileName \n",FILE_APPEND);
|
|
}
|
|
|
|
if($retry == 0 and $error != 1)
|
|
@unlink($db_info_maualTask.$dbXmlFileName);
|
|
}
|
|
|
|
fclose($handle);
|
|
}
|
|
|
|
|
|
#************************************************************************************************************************
|
|
# Subroutine Name : insertUpdatedBbData
|
|
# Objective : This subroutine inserts/update entry to IbFile/IbFolder
|
|
# Added By : Vijay Vinoth [23/01/2018]
|
|
# Modified By :
|
|
#*************************************************************************************************************************
|
|
function insertUpdatedBbData($dbpaths)
|
|
{
|
|
$operation = trim($dbpaths->attributes()->operation);
|
|
$dbtable = trim($dbpaths->attributes()->dbtable);
|
|
if($operation == 'insert'){
|
|
if($dbtable == 'file')
|
|
insertIbFile(1,0,$dbpaths);
|
|
else if($dbtable == 'folder')
|
|
insertIbFolder(1, $dbpaths->attributes()->modtime, $dbpaths->attributes()->fileName, $dbpaths->attributes()->fieldSize);
|
|
}
|
|
else if($operation == 'update'){
|
|
if($dbtable == 'file')
|
|
updateIbFile(1, $dbpaths);
|
|
else if($dbtable == 'folder')
|
|
updateIbFolder(1, $dbpaths->attributes()->modtime, $dbpaths->attributes()->fileName);
|
|
}
|
|
return;
|
|
}
|
|
|
|
#************************************************************************************************************************
|
|
# Subroutine Name : insertIbFile
|
|
# Objective : This subroutine inserts entry to IbFile
|
|
# Added By : Senthil Pandian [12/01/2016]
|
|
# Modified By :
|
|
#*************************************************************************************************************************
|
|
function insertIbFile($retry,$dirID,$dbpaths)
|
|
{
|
|
GLOBAL $dbh_ibFile_insert;
|
|
$fileDetail = array();
|
|
|
|
if(!$dirID){
|
|
$dirID = folderExistenceinDB($dbpaths);
|
|
if(!$dirID)
|
|
$dirID = insertFolders($retry,$dbpaths);
|
|
}
|
|
|
|
$fileDetail[0] = $dirID;
|
|
$fileDetail[1] = $dbpaths->attributes()->fileName;
|
|
$fileDetail[2] = $dbpaths->attributes()->modtime;
|
|
$fileDetail[3] = $dbpaths->attributes()->fieldSize;
|
|
$fileDetail[4] = $dbpaths->attributes()->bksetLMD;
|
|
$fileDetail[5] = $dbpaths->attributes()->syncDate;
|
|
$fileDetail[6] = $dbpaths->attributes()->bksetName;
|
|
$fileDetail[7] = $dbpaths->attributes()->hashValue;
|
|
$fileDetail[8] = $dbpaths->attributes()->fieldFolderId;
|
|
$fileDetail[9] = $dbpaths->attributes()->fieldMPC;
|
|
$fileDetail[10] = $dbpaths->attributes()->fieldEncName;
|
|
$fileDetail[11] = $dbpaths->attributes()->fileOrFolder;
|
|
$fileDetail[12] = $dbpaths->attributes()->backupStatus;
|
|
|
|
if($retry == 0){
|
|
$fileDetail[4] = 'NA';
|
|
$fileDetail[5] = 'NA';
|
|
$fileDetail[6] = 'Default Backupset';
|
|
$fileDetail[7] = '0';
|
|
$fileDetail[11] = '1';
|
|
$fileDetail[12] = '1';
|
|
}
|
|
|
|
try{
|
|
$rows = $dbh_ibFile_insert->execute($fileDetail);
|
|
|
|
$dbh_ibFile_insert->closeCursor();
|
|
} catch(PDOException $e) {
|
|
traceDBLog("Insert entry to IbFile table has failed for file: $fileName. Reason:".$e->getMessage());
|
|
if($retry === TRY_UPDATE)
|
|
updateIbFile(0, $dbpaths);
|
|
}
|
|
}
|
|
|
|
#************************************************************************************************************************
|
|
# Subroutine Name : insertIbFolder
|
|
# Objective : This subroutine inserts entry to IbFolder
|
|
# Added By : Senthil Pandian [12/01/2016]
|
|
# Modified By :
|
|
#*************************************************************************************************************************
|
|
function insertIbFolder($retry, $modtime, $fileName, $fieldSize)
|
|
{
|
|
GLOBAL $dbh_ibFolder_insert;
|
|
$fileDetail = array();
|
|
|
|
$fileDetail[0] = $fileName;
|
|
$fileDetail[1] = $modtime;
|
|
$fileDetail[2] = $fieldSize;
|
|
$fileDetail[3] = "NA";
|
|
try{
|
|
$rows = $dbh_ibFolder_insert->execute($fileDetail);
|
|
$dbh_ibFolder_insert->closeCursor();
|
|
} catch(PDOException $e) {
|
|
traceDBLog("Insert entry to IbFolder table has failed for file: $fileName. Reason:".$e->getMessage());
|
|
if($retry === TRY_UPDATE){
|
|
updateIbFolder(0, $modtime, $fileName);
|
|
}
|
|
}
|
|
}
|
|
|
|
#************************************************************************************************************************
|
|
# Subroutine Name : updateIbFile
|
|
# Objective : This subroutine updates entry to IbFile
|
|
# Added By : Senthil Pandian [12/01/2016]
|
|
# Modified By :
|
|
#*************************************************************************************************************************
|
|
function updateIbFile($retry, $dbpaths)
|
|
{
|
|
GLOBAL $dbh_ibFile_update;
|
|
$fileDetail = array();
|
|
|
|
$fileDetail[0] = $dbpaths->attributes()->modtime;
|
|
$fileDetail[1] = $dbpaths->attributes()->fieldSize;
|
|
$fileDetail[2] = $dbpaths->attributes()->fileName;
|
|
$fileDetail[3] = $dbpaths->attributes()->fieldFolderId;
|
|
|
|
try{
|
|
$rows = $dbh_ibFile_update->execute($fileDetail);
|
|
$dbh_ibFile_update->closeCursor();
|
|
} catch(PDOException $e) {
|
|
traceDBLog("Update entry to IbFile table has failed for file: $fileName. Reason:".$e->getMessage());
|
|
if($retry === TRY_INSERT){
|
|
insertIbFile(0, 0, $dbpaths);
|
|
}
|
|
}
|
|
}
|
|
|
|
#************************************************************************************************************************
|
|
# Subroutine Name : updateIbFolder
|
|
# Objective : This subroutine updates entry to IbFolder
|
|
# Added By : Senthil Pandian [12/01/2016]
|
|
# Modified By :
|
|
#*************************************************************************************************************************
|
|
function updateIbFolder($retry, $modtime, $fileName)
|
|
{
|
|
GLOBAL $dbh_ibFolder_update;
|
|
$fileDetail = array();
|
|
|
|
$fileDetail[0] = $modtime;
|
|
$fileDetail[1] = $fileName;
|
|
|
|
try{
|
|
$rows = $dbh_ibFolder_update->execute($fileDetail);
|
|
$dbh_ibFolder_update->closeCursor();
|
|
} catch(PDOException $e) {
|
|
traceDBLog("Update entry to IbFolder table has failed for file: $fileName. Reason:".$e->getMessage());
|
|
if($retry === TRY_INSERT){
|
|
insertIbFolder(0, $modtime, $fileName, 0);
|
|
}
|
|
}
|
|
}
|
|
|
|
#********************************************************************************************************
|
|
# Subroutine Name : folderExistenceinDB
|
|
# Objective : This subroutine checks whether the folder path already exists in the DB,
|
|
# and if it does, updates entry and returns the resp dirID
|
|
# Added By : Pooja Havaldar [12/01/2016]
|
|
# Modified By : Senthil Pandian
|
|
#*********************************************************************************************************
|
|
/*function folderExistenceinDB($keyString)
|
|
{
|
|
GLOBAL $localBackupHash;
|
|
$fieldMPC = ($keyString != '/') ? "/".$keyString : "/";
|
|
|
|
$keyString = substr($keyString,0,strrpos($keyString, '/'))."/";
|
|
$searchItem = "'".$fieldMPC.$keyString."'";
|
|
|
|
if(isset($localBackupHash[$searchItem]))
|
|
return $localBackupHash[$searchItem];
|
|
|
|
$dirID = getDirID($keyString, $fieldMPC);
|
|
if($dirID){
|
|
$localBackupHash[$searchItem] = $dirID;
|
|
}
|
|
return $dirID;
|
|
}*/
|
|
|
|
function folderExistenceinDB($dbpaths)
|
|
{
|
|
GLOBAL $localBackupHash, $dedupDetailFile;
|
|
$keyString = $dbpaths->attributes()->keyString;
|
|
$fieldMPC = $dbpaths->attributes()->fieldMPC;
|
|
|
|
//if(!file_exists($dedupDetailFile) && $fieldMPC != '/')
|
|
// $fieldMPC = "/".$fieldMPC;
|
|
$fieldMPC = "/".$keyString;
|
|
|
|
$keyString = substr($keyString,0,strrpos($keyString, '/'))."/";
|
|
$searchItem = "'".$fieldMPC.$keyString."'";
|
|
|
|
if(isset($localBackupHash[$searchItem]))
|
|
return $localBackupHash[$searchItem];
|
|
|
|
$dirID = getDirID($keyString, $fieldMPC);
|
|
if($dirID)
|
|
$localBackupHash[$searchItem] = $dirID;
|
|
|
|
return $dirID;
|
|
}
|
|
|
|
#*****************************************************************************************************************************************
|
|
# Subroutine Name : insertFolders
|
|
# Objective : This subroutine makes level by level folder entry into the DB and returns the dirID of the full folder path
|
|
# Added By : Pooja Havaldar
|
|
# Modified By : Senthil Pandian
|
|
#*****************************************************************************************************************************************/
|
|
function insertFolders($retry,$dbpaths)
|
|
{
|
|
GLOBAL $dbh_LB,$localBackupHash,$fileCount,$dedupDetailFile;
|
|
$keyString = $dbpaths->attributes()->keyString;
|
|
$fieldMPC = $dbpaths->attributes()->fieldMPC;
|
|
|
|
if(!file_exists($dedupDetailFile) && $fieldMPC != '/')
|
|
$fieldMPC = "/".$fieldMPC;
|
|
|
|
$folderPath = substr($keyString,0,strrpos($keyString, '/'))."/";
|
|
$folder_list = explode('/', $folderPath);
|
|
|
|
for($index = count($folder_list)-1; $index >= 0; $index--){
|
|
$keyString = substr($keyString,0,strrpos($keyString, '/'));
|
|
$folderList = $keyString."/";
|
|
$searchItem = "'$fieldMPC$folderList'";
|
|
|
|
if(isset($localBackupHash[$searchItem]))
|
|
break;
|
|
|
|
$localBackupHash[$searchItem] = getDirID($folderList, $fieldMPC);
|
|
if($localBackupHash[$searchItem])
|
|
break;
|
|
|
|
$modtime = date("Y/m/d h:i:s A", filemtime($folderList));
|
|
insertIbFolder(1, $modtime, $searchItem, 0);
|
|
//list($modtime, $fieldSize) = getSizeLMD($folderList);
|
|
/*$fileCount++;
|
|
if($fileCount % $localDBCommitFreq == 0){
|
|
$dbh_LB->commit();
|
|
$dbh_LB->beginTransaction();
|
|
$fileCount = 0;
|
|
}
|
|
if($retry == 0){
|
|
$dbh_LB->commit();
|
|
$dbh_LB->beginTransaction();
|
|
}
|
|
*/
|
|
$localBackupHash[$searchItem] = getDirID($folderList, $fieldMPC);
|
|
}
|
|
$folderPath = "'".$fieldMPC.$folderPath."'";
|
|
return $localBackupHash[$folderPath];
|
|
}
|
|
|
|
#*******************************************************************************************************
|
|
# Subroutine Name : getDirID
|
|
# Objective : This subroutine returns the dirID of full folder path
|
|
# Added By : Senthil Pandian [12/01/2016]
|
|
# Modified By :
|
|
#********************************************************************************************************
|
|
function getDirID($keyString,$fieldMPC)
|
|
{
|
|
GLOBAL $selectFolderCount;
|
|
$finalPath = "'".$fieldMPC.$keyString."'";
|
|
$selectFolderCount->execute(array($finalPath));
|
|
$dir = $selectFolderCount->fetch();
|
|
$selectFolderCount->closeCursor();
|
|
$dirID = $dir['DIRID'];
|
|
|
|
if($dirID == "")
|
|
$dirID = 0;
|
|
|
|
return $dirID;
|
|
}
|
|
|
|
#**************************************************************************************************************************
|
|
# Subroutine Name : getSizeLMD
|
|
# Objective : This subroutine returns the modtime and size of file/folder path using the stat cmd
|
|
# Added By : Senthil Pandian [12/01/2016]
|
|
# Modified By :
|
|
#**************************************************************************************************************************
|
|
function getSizeLMD($filePath)
|
|
{
|
|
$actualPath = getLocalFilePath($filePath);
|
|
$modtime = date("Y/m/d h:i:s A", filemtime($actualPath));
|
|
$size = customFileSize($actualPath);
|
|
return array($modtime, $size);
|
|
}
|
|
|
|
#****************************************************************************************************
|
|
# Subroutine Name : process_term
|
|
# Objective : The signal handler invoked when SIGTERM signal is received by the script
|
|
# Added By : Vijay Vinoth [30/03/2018]
|
|
# Modified By :
|
|
#*****************************************************************************************************
|
|
function process_term()
|
|
{
|
|
GLOBAL $dbh_LB;
|
|
if($dbh_LB){
|
|
$dbh_LB->commit();
|
|
unset($dbh_LB);
|
|
}
|
|
exit;
|
|
}
|
|
?>
|