Files
resatennis_tyroltenn/IDrive/.config/dbReindex_cli.php

298 lines
9.5 KiB
PHP
Executable File

<?php
include_once 'path.php';
session_write_close();
Scheduler::Log("ReIndex started");
$createFolderTableQry = "CREATE TABLE " . IBFOLDER . " (DIRID integer primary key autoincrement, NAME char(1024) not null,FILE_LMD char(256),FILE_SIZE __int64,LEVEL_NO integer DEFAULT 0,SYNC_DATE char(50), UNIQUE(NAME));";
$createFolderIndexQry = "CREATE INDEX ibfolder_indx ON " . IBFOLDER . " (NAME);";
$createFileTableQry = 'CREATE TABLE ' . IBFILE . ' (FILEID integer primary key autoincrement, DIRID integer not null, NAME char(1024) not null,FILE_LMD char(256),FILE_SIZE __int64,FILE_OR_FOLDER integer DEFAULT 0,BKSET_LMD char(50), SYNC_DATE char(50),BACKUP_STATUS integer DEFAULT 0,BKSET_NAME char(50),HASH_VALUE __int64,THUMB integer DEFAULT 0, FOLDER_ID char(1024),MPC char(50),ENC_NAME char(1024), foreign key (DIRID) references ibfolder (DIRID),UNIQUE(DIRID, NAME));';
$createFileIndexQry = 'CREATE INDEX ibfile_indx ON ' . IBFILE . ' (NAME);';
$selectFolderTableQry = "SELECT DIRID FROM " . IBFOLDER . " WHERE NAME=?";
$insertFolderTableQry = "INSERT INTO " . IBFOLDER . " (NAME,FILE_LMD,FILE_SIZE,SYNC_DATE) values (?,?,?,?)";
$insertFileTableQry = "INSERT INTO " . IBFILE . " (DIRID,NAME,FILE_LMD,FILE_SIZE,FILE_OR_FOLDER,BKSET_LMD,SYNC_DATE,BACKUP_STATUS,BKSET_NAME,HASH_VALUE,FOLDER_ID,MPC,ENC_NAME) VALUES (?,?,?,?,?,?,?,?,?,?,?,?,?)";
$hostName = getNAShostname();
$dedupBucket = '';
$dedupServerRoot = '';
$dedupServerRootSlash = '';
if(file_exists($reIndexDirPath . '/' . REINDEXBUCKET)) {
$dedupBucket = file_get_contents($reIndexDirPath . '/' . REINDEXBUCKET);
$dedupServerRoot = getServerRootByDeviceID($dedupBucket);
$dedupServerRootSlash = $dedupServerRoot . '/';
} else if(file_exists($dedupDetailFile)) {
$deviceJSON = file_get_contents($dedupDetailFile);
$deviceArray = json_decode($deviceJSON, true);
$dedupServerRoot = trim($deviceArray['serverRoot']);
$dedupServerRootSlash = $dedupServerRoot . '/';
}
$drive = getLocalBackupLocation();
$dbDriveUserDir = "$drive/$userName";
$dbDriveDir = "$dbDriveUserDir/LDBNEW";
$dbDrivePath = getExpressDBPath($drive, $dedupBucket);
if(!($pidHandle = fopen($pidPath, 'w'))) {
Scheduler::Log('ReIndex: pid file not opened');
$arr = array(false);
exit;
}
if(!flock($pidHandle, LOCK_EX|LOCK_NB)) {
Scheduler::Log("not able to lock pid.txt ReIndex: lockFlag");
$arr = array(false);
fclose($pidHandle);
remove();
}
@unlink(OUTPUTFILE);
@unlink(ERROR_FILE);
if(!file_exists($dbDriveDir)) {
if(!mkdir($dbDriveDir, $permissionVal)) {
Scheduler::Log("Failed to create Directory $dbDriveDir: ".customErrorMsg());
remove();
}
@chmod($dbDriveDir, $permissionVal);
}
$lastLine = "";
$commLine = 0;
$flag = 0;
$reIndexUtfFile = getOperationFile(REINDEX, $dedupServerRoot);
@chmod($reIndexUtfFile, $permissionVal);
if(!file_exists($reIndexUtfFile)) {
Scheduler::Log("ReIndex: $reIndexUtfFile not exists");
print "ReIndex UTF not exists";
$arr = array(false);
fclose($pidHandle);
remove();
}
$idevsutilCommandLine = $idevsutilBinaryPath . $whiteSpace . $idevsutilArgument.$assignmentOperator."'$reIndexUtfFile'";
pclose(popen("$idevsutilCommandLine &", "r"));
while(!customFileSize(OUTPUTFILE) && !customFileSize(ERROR_FILE)){
sleep(2);
}
$handle = fopen(OUTPUTFILE, "r");
if(!$handle) {
if(customFileSize(ERROR_FILE))
Scheduler::Log("ReIndex failed. Reason:".file_get_contents(ERROR_FILE));
Scheduler::Log("Couldn't open output file ".OUTPUTFILE);
$arr = array(false);
fclose($handle);
remove();
}
//Removing existing Database
@unlink($dbDrivePath);
try {
//Creating Database
$db = new PDO("sqlite:$dbDrivePath");
@chmod($dbDrivePath, $permissionVal);
$db->exec('PRAGMA journal_mode = wal;');
$db->exec('PRAGMA synchronous=1;');
$db->exec('PRAGMA read_uncommitted = on;');
//Creating tables & its index
$db->exec($createFolderTableQry);
$db->exec($createFolderIndexQry);
$db->exec($createFileTableQry);
$db->exec($createFileIndexQry);
//Prepare statement to check folder whether it is exists or not
$selectFolder = $db->prepare($selectFolderTableQry);
//Prepare statement to insert entry to the tables "file/folder"
$insertFolder = $db->prepare($insertFolderTableQry);
$insertFile = $db->prepare($insertFileTableQry);
$db->beginTransaction();
//$sharedPath = getFullPathOfSharedFolder();
//Scheduler::Log("sharedPath:$sharedPath");
//Parse output.txt file of reindex EVS command for db recreation
while(1) {
$buffer = fread($handle, LIMIT);
if(0 === strlen($buffer)) {
if(customFileSize(ERROR_FILE)) {
Scheduler::Log("ReIndex failed. Reason:" . file_get_contents(ERROR_FILE));
break;
}
sleep(WAIT_FOR);
fseek($handle, 0, 1); //to clear eof flag
continue;
}
//need to check appending partial record to packet or to first line of packet
if("" != $lastLine) {
$buffer = $lastLine.$buffer;
}
$resultList = explode("\n", $buffer);
$bufIndex = count($resultList);
$bufIndex -= 1;
//keep last line of buffer only when it not ends with newline.
if(!preg_match('/\n$/', $buffer)) {
$lastLine = $resultList[$bufIndex];
} else {
$lastLine = "";
}
if(0 < $bufIndex && preg_match('/End of database/', $resultList[$bufIndex-1])) {
$flag = 1;
$bufIndex -= 1;
}
for($index = 0; $index < $bufIndex; $index++) {
$xml = simplexml_load_string($resultList[$index]);
$fileName = $xml['fname'];
$exp = explode("/", $fileName);
$fileName = array_pop($exp);
$fileFolder = implode('/', $exp);
$fileFolderId = $xml['folder_id'];
$fileMPC = $xml['mpc'];
$fileEncName = $xml['enc_name'];
$fileLMD = $xml['mod_time'];
$fileSize = $xml['size'];
$expDate = explode(' ', $fileLMD);
$fileLMD = $expDate[0] . " " . ampmTimeFormat($expDate[1]);
$selFolderName = (($dedupServerRoot != '')? "'$dedupServerRoot$fileFolder/'" : "'/$fileMPC$fileFolder/'");
if(!$selectFolder->execute(array($selFolderName))) {
$selectFolder->closeCursor();
Scheduler::Log("select failed for folder: $dedupServerRoot$fileFolder");
remove();
}
if($row = $selectFolder->fetch()) {
$dirID = trim($row[0]);
$selectFolder->closeCursor();
} else {
$folderName = $fileFolder;
$exp = explode("/", $folderName);
$startIndex = count($exp);
$date = filemtime("$drive/$userName/$dedupServerRootSlash$fileMPC/$fileFolderId");
$date = date("Y/m/d h:i:s A", $date);
$insertFolderName = (($dedupServerRoot != '')? "'$dedupServerRoot$folderName/'" : "'/$fileMPC$folderName/'");
$folderDetail = array($insertFolderName, $date, '0', 'NA');
if(!$insertFolder->execute($folderDetail)) {
Scheduler::Log("Insert failed for folder: $insertFolderName");
remove();
}
$dirID = $db->lastInsertId();
$insertFolder->closeCursor();
array_pop($exp);
for($i = $startIndex; $i > 1; $i--) {
$folderName = implode('/', $exp);
//Checking the folder whether already present in DB or not
$insertFolderName = (($dedupServerRoot != '')? "'$dedupServerRoot$folderName/'" : "'/$fileMPC$folderName/'");
if(!$selectFolder->execute(array($insertFolderName))) {
$selectFolder->closeCursor();
Scheduler::Log("select failed for folder: $insertFolderName");
remove();
}
array_pop($exp);
//Skip the folder insertion if it is already present in DB
if($row = $selectFolder->fetch()) {
$selectFolder->closeCursor();
break;
}
# close db cursor here, else it wont be able to fetch from above
$selectFolder->closeCursor();
$date = filemtime("$drive/$userName/$dedupServerRootSlash$fileMPC/$fileFolderId");
$date = date("Y/m/d h:i:s A", $date);
$folderDetail = array($insertFolderName, $date, '0', 'NA');
if(!$insertFolder->execute($folderDetail)) {
$insertFolder->closeCursor();
Scheduler::Log("Insert failed for folder: $insertFolderName");
remove();
}
$insertFolder->closeCursor();
}
}
$fileName = "'$fileName'";
$fileDetail = array($dirID, $fileName, $fileLMD, $fileSize, '1', 'NA', 'NA','1','Default Backupset', '0', $fileFolderId, $fileMPC, $fileEncName);
if(!$insertFile->execute($fileDetail)) {
$insertFile->closeCursor();
Scheduler::Log("Insert failed for file: $fileName");
remove();
}
$insertFile->closeCursor();
if($index % 5 == 0) {
# Committing for every 5 insert/update
$db->commit();
$db->beginTransaction();
}
}
if(1 == $flag){
break;
}
}
$db->commit();
unset($db);
Scheduler::Log("ReIndex queries are committed");
Scheduler::Log("ReIndex finished");
remove();
} catch(Exception $e) {
remove();
$db->commit();
unset($db);
Scheduler::Log("Exception: " . $e->getMessage());
}
#****************************************************************************************************
# Subroutine Name : remove
# Objective :
# Added By : Pooja Havaldar
#*****************************************************************************************************/
function remove()
{
GLOBAL $pidHandle, $reIndexDirPath;
if($pidHandle) {
fclose($pidHandle);
}
$liveConnectionsFile = Scheduler::$user_dir."liveConnections.txt";
if(!empty($reIndexDirPath) && $reIndexDirPath!="/") {
#shell_exec("rm -rf $reIndexDirPath");
removeItems($reIndexDirPath);
}
if(file_exists($liveConnectionsFile)) {
#shell_exec("rm -rf $liveConnectionsFile");
removeItems($liveConnectionsFile);
}
exit;
}
?>