1013 lines
33 KiB
PHP
Executable File
1013 lines
33 KiB
PHP
Executable File
<?php
|
|
define('BACKUP_SUCCESS','13');
|
|
//define('BACKUPSETFORDISPLAY','BackupsetDisplayFile.txt');
|
|
define('ENCRYPTIONVERIFICATIONFAILED','encryption verification failed');
|
|
include('sqliteDB.php');
|
|
|
|
# new code added for backward compatability of backupset file content change.
|
|
$needToLogout = "$webDir/".APPDIR.PATHSEPARATOR."logout.txt";
|
|
if(file_exists($needToLogout))
|
|
{
|
|
$oldPath = $jobDir."BackupsetFileForDisplay.txt";
|
|
$newPath = $jobDir.BACKUPSETFORDISPLAY;
|
|
if(file_exists($oldPath) && !file_exists($newPath))
|
|
updateBackupSetFilesData($oldPath,$newPath);
|
|
//@unlink($needToLogout);
|
|
}
|
|
|
|
include('generateBackupSetFiles.php');
|
|
$traceRetrycount = 0;
|
|
$evspid = 0;
|
|
$opprocpid = 0;
|
|
|
|
$issignal = FALSE;
|
|
$isDbService = 0;
|
|
# check if not last day for schedule job then exit
|
|
if(isset($argv[1]) && "lastDay" === $argv[1] && !checkLastDayOfMonth()){
|
|
traceLog("Cannot start Schedule job, Reason : last day of the month.");
|
|
exit;
|
|
}
|
|
$pcntlProcess = 0;
|
|
# Subroutine that processes SIGINT and SIGTERM signal received by the script during backup
|
|
if( extension_loaded('pcntl') && function_exists('pcntl_signal') ){
|
|
declare(ticks = 1);
|
|
pcntl_signal(SIGTERM, 'process_term');// Termination ('kill' was called)
|
|
pcntl_signal(SIGINT, 'process_term'); // Interrupted (Ctrl-C is pressed)
|
|
$pcntlProcess = 1;
|
|
}
|
|
|
|
|
|
# create pid.txt if required.
|
|
createPid($taskType);
|
|
$operationName = BACKUP;
|
|
# pre-cleanup for all intermediate files and folders.
|
|
removeBackupTempFiles();
|
|
|
|
# To get the Operation type as mirror/relative
|
|
getBackupOperationType();
|
|
|
|
# Define output user log file name and create LOG and ERROR directories
|
|
createLogFiles($outputFileName);
|
|
|
|
# check if binary exist and have execute permission
|
|
checkBinaryExists();
|
|
|
|
# To load the partial and full Exclude array from Exclude file
|
|
#loadExcludeArray();
|
|
|
|
writeLogHeader();
|
|
|
|
$backupSetEmpty = checkIfSetEmpty(BACKUPSETFORDISPLAY);
|
|
if($backupSetEmpty) {
|
|
if(!($logOutputHandle = fopen($logOutputFilePath,"a"))){
|
|
traceLog("Could not open file $logOutputFilePath, Reason:".customErrorMsg().LINEFEED);
|
|
exit_cleanup();
|
|
}
|
|
fwrite($logOutputHandle, "Your backup set is empty.".LINEFEED);
|
|
fclose($logOutputHandle);
|
|
exit_cleanup();
|
|
}
|
|
|
|
if(!preg_match("/$varManualTask/", $jobDir)){
|
|
waitforArchiveJobtoFinish($ArchiveJobPath);
|
|
waitforDeleteJobtoFinish($DeleteJobPath);
|
|
}
|
|
|
|
if(!($ERRORFAILEDFILE = fopen($errorFile, "w"))){
|
|
$errStr = "Could not open file $errorFile to write, Reason:$!";
|
|
traceLog($errStr);
|
|
exit_cleanup();
|
|
}
|
|
|
|
|
|
|
|
startBackup();
|
|
waitForEnginesToFinish();
|
|
|
|
if($errStr != ""){
|
|
traceLog($errStr);
|
|
writeOperationErrorToErrorFile($errStr);
|
|
}
|
|
|
|
exit_cleanup();
|
|
|
|
|
|
#******************************************************************************************************************************************
|
|
# Subroutine Name : startBackup.
|
|
# Objective : Start backup operation by reading the content of info_file. The info_file is getting created and written by
|
|
# generateBackupsetFiles() subroutine. Check the retry condition and perform retry to backup, if needed.
|
|
# Added By : Pooja Havaldar
|
|
#******************************************************************************************************************************************
|
|
function startBackup()
|
|
{
|
|
GLOBAL $FD_WRITE, $BACK_FILE, $FD_READ, $errStr, $pid_generate, $failedFilesCount;
|
|
GLOBAL $retrycount, $failedfiles_index, $jobrunningDir, $phpPath, $isLocalBackup;
|
|
GLOBAL $bacSync, $totalFiles, $webDir, $APPDIR, $logOutputFilePath,$BackupForkchilds,$dbIndexCount;
|
|
GLOBAL $localDB_pid_generate, $totalEngineBackup, $processName, $traceRetrycount, $needToLogout;
|
|
$failedfiles_index = 0;
|
|
|
|
if($isLocalBackup == 1){
|
|
#need to verify PVT key.
|
|
$verification = validatePVTKey();
|
|
if(!$verification){
|
|
traceLog("logout trigger".$needToLogout);
|
|
$errDetails = ENCRYPTIONVERIFICATIONFAILED;
|
|
writeOperationErrorToErrorFile($errDetails);
|
|
traceLog($errDetails);
|
|
if(file_put_contents($needToLogout, ENCRYPTIONVERIFICATIONFAILED))
|
|
chmod($needToLogout,0777);
|
|
exit_cleanup();
|
|
}
|
|
|
|
createUpdateDbPaths();
|
|
}
|
|
# To udpate backupset files to sync with the root(/) directory.
|
|
autoUpdateBackupset();
|
|
$engineID = 1;
|
|
|
|
#opening info file for generateBackupsetFiles function to write backup set information and for main process to read that information
|
|
if(!($FD_WRITE = fopen(INFOFILE,"w"))){
|
|
$errStr = "Could not open file ".INFOFILE." to write, Reason:".customErrorMsg();
|
|
return;
|
|
}
|
|
|
|
if( extension_loaded('pcntl') && function_exists('pcntl_fork') ){
|
|
$pid_generate = pcntl_fork();
|
|
if($pid_generate == -1){
|
|
$errStr = "Unable to start generateBackupsetFiles operation. Reason:".customErrorMsg();
|
|
return;
|
|
}
|
|
|
|
# this child process will create all backup set files with 1000-1000 files set
|
|
if($pid_generate == 0){
|
|
generateBackupsetFiles();
|
|
exit;
|
|
}
|
|
}
|
|
else {
|
|
$forkProcessScriptPath = $webDir.PATHSEPARATOR.APPDIR.PATHSEPARATOR.SCRIPTS.PATHSEPARATOR.FORKPROCESSFILE;
|
|
$cmd = "$phpPath $forkProcessScriptPath 'backup' '$logOutputFilePath'";
|
|
$pid_generate = exec("$cmd > /dev/null 2>".EXEC_ERRORFILE." & echo $!");
|
|
}
|
|
|
|
fclose($FD_WRITE);
|
|
fclose($BACK_FILE);
|
|
|
|
$exec_cores = getSystemCpuCores();
|
|
file_put_contents(ENGINE_LOCKE_FILE,"");
|
|
@chmod(ENGINE_LOCKE_FILE,PERMISSIONVAL);
|
|
|
|
if($isLocalBackup == 1){
|
|
|
|
$fileChildProcessPath = $webDir.PATHSEPARATOR.APPDIR.PATHSEPARATOR.SCRIPTS.PATHSEPARATOR.DB_SERVICE_FILE;
|
|
$cmd = "$phpPath $fileChildProcessPath";
|
|
$localDB_pid_generate = exec("$cmd > /dev/null & echo $!");
|
|
}
|
|
|
|
do
|
|
{
|
|
if(!($FD_READ = fopen(INFOFILE,"r"))){
|
|
$errStr = "Could not open file ".INFOFILE." to read, Reason:".customErrorMsg();
|
|
return;
|
|
}
|
|
#this loop will read info file and for every record it will execute doBackupOperation for backing up the data
|
|
while (1){
|
|
if(!file_exists(PIDFILE))
|
|
break;
|
|
|
|
if(empty($line)){
|
|
$line = fgets($FD_READ);
|
|
$line = trim($line);
|
|
}
|
|
|
|
if(empty($line)){
|
|
sleep(1);
|
|
fseek($FD_READ, 0, SEEK_CUR); #to clear eof flag
|
|
continue;
|
|
}
|
|
|
|
if(preg_match("/^BAC_SYNC/", $line)){
|
|
$lineSplit = preg_split("/ /", $line,2);
|
|
$bacSync = trim($lineSplit[1]);
|
|
$line = "";
|
|
continue;
|
|
}
|
|
|
|
if(preg_match("/^TOTALFILES/", $line)){
|
|
$lineSplit = preg_split("/ /", $line,2);
|
|
$totalFiles = trim($lineSplit[1]);
|
|
$line = "";
|
|
continue;
|
|
}
|
|
if(preg_match("/^FAILEDCOUNT/", $line)){
|
|
$lineSplit = preg_split("/ /", $line,2);
|
|
$failedFilesCount = trim($lineSplit[1]);
|
|
sleep(2);
|
|
break;
|
|
}
|
|
|
|
if($totalEngineBackup > 1 && extension_loaded('pcntl') && function_exists('pcntl_fork') ){
|
|
$retVal = isEngineRunning(PIDFILE."_$engineID");
|
|
if($retVal === false){
|
|
if(isAnyEngineRunning(ENGINE_LOCKE_FILE))
|
|
{
|
|
#check if any engine is running if running then check down code block or skip
|
|
while(1){
|
|
if(!file_exists(PIDFILE) or !isAnyEngineRunning(ENGINE_LOCKE_FILE))
|
|
break;
|
|
|
|
$exec_loads = sys_getloadavg();
|
|
if($exec_loads[1] > $exec_cores){
|
|
sleep(20);
|
|
continue;
|
|
}
|
|
break;
|
|
}
|
|
}
|
|
|
|
if(!file_exists(PIDFILE))
|
|
break;
|
|
|
|
array_key_exists( $engineID, $dbIndexCount ) ? $dbIndexCount[$engineID]++ : $dbIndexCount[$engineID] = 1;
|
|
|
|
if($failedfiles_index != -1) {
|
|
$failedfiles_index++;
|
|
if($failedfiles_index > 2000000000) {
|
|
$failedfiles_index = 0;
|
|
}
|
|
}
|
|
|
|
$pid_generate_doBackup = pcntl_fork();
|
|
if($pid_generate_doBackup == -1){
|
|
$errStr = "Unable to start doBackupOperation. Reason:".customErrorMsg();
|
|
exit_cleanup();
|
|
}
|
|
else if ($pid_generate_doBackup == 0){ # this child process will create all backup set files with 1000-1000 files set
|
|
$processName = "dobackup";
|
|
doBackupOperation($line,$engineID,$dbIndexCount[$engineID],$failedfiles_index);
|
|
exit;
|
|
}
|
|
else
|
|
{
|
|
$BackupForkchilds[] = $pid_generate_doBackup;
|
|
if(isset($exec_loads[1]) && ($exec_loads[1] > $exec_cores))
|
|
sleep(2);
|
|
else
|
|
sleep(1);
|
|
}
|
|
$line = "";
|
|
}
|
|
|
|
$engineID++;
|
|
if($engineID > $totalEngineBackup){
|
|
$engineID = 1;
|
|
sleep(2);
|
|
}
|
|
|
|
foreach($BackupForkchilds as $key => $pid) {
|
|
$res = pcntl_waitpid($pid, $status, WNOHANG);
|
|
|
|
// If the process has already exited
|
|
if($res == -1 || $res > 0){
|
|
unset($BackupForkchilds[$key]);
|
|
}
|
|
}
|
|
|
|
}
|
|
else{
|
|
if($failedfiles_index != -1) {
|
|
$failedfiles_index++;
|
|
if($failedfiles_index > 2000000000) {
|
|
$failedfiles_index = 0;
|
|
}
|
|
}
|
|
|
|
doBackupOperation($line,1,1,$failedfiles_index);
|
|
$line = "";
|
|
}
|
|
}
|
|
|
|
fclose($FD_READ);
|
|
|
|
waitForProcess($pid_generate, NOSCANERRORFILE);
|
|
waitForEnginesToFinish();
|
|
|
|
if($totalFiles == 0){
|
|
$filecontent = file(INFOFILE);
|
|
|
|
for($i = count($filecontent) - 1; $i >= 0; $i--){
|
|
|
|
if(preg_match("/^FAILEDCOUNT/", $filecontent[$i])){
|
|
$lineSplit = preg_split("/ /", $filecontent[$i],2);
|
|
$failedFilesCount = trim($lineSplit[1]);
|
|
}
|
|
elseif(preg_match("/^TOTALFILES/", $filecontent[$i])){
|
|
$lineSplit = preg_split("/ /", $filecontent[$i],2);
|
|
$totalFiles = trim($lineSplit[1]);
|
|
}
|
|
elseif(preg_match("/^BAC_SYNC/", $filecontent[$i])){
|
|
$lineSplit = preg_split("/ /", $filecontent[$i],2);
|
|
$bacSync = trim($lineSplit[1]);
|
|
break;
|
|
}
|
|
}
|
|
|
|
#loop and traverse from last
|
|
if($totalFiles == 0)
|
|
traceLog("Unable to get total files count");
|
|
}
|
|
|
|
$retry = 0;
|
|
|
|
if(($retrycount < MAX_RETRY) and file_exists(RETRY_INFOFILE) and file_exists(PIDFILE) and customFileSize(RETRY_INFOFILE) > 0){
|
|
if(!rename(RETRY_INFOFILE, INFOFILE)){
|
|
traceLog("Unable to rename ".RETRY_INFOFILE." to ".INFOFILE.", Reason: ".customErrorMsg());
|
|
break;
|
|
}
|
|
|
|
if(!($FD_WRITE = fopen(INFOFILE,"a"))){
|
|
$errStr = "Could not open file ".INFOFILE." to write, Reason:".customErrorMsg();
|
|
break;
|
|
}
|
|
|
|
fwrite($FD_WRITE, "BAC_SYNC $bacSync\n");
|
|
fwrite($FD_WRITE, "TOTALFILES $totalFiles\n");
|
|
fwrite($FD_WRITE, "FAILEDCOUNT $failedFilesCount");
|
|
fclose($FD_WRITE);
|
|
// need to pass engine id value here.
|
|
updateRetryCount($engineID);
|
|
$traceRetrycount++;
|
|
|
|
if(file_exists("LimitedRetry.txt") && ($retrycount == LIMITED_RETRY - 1)){
|
|
unlink("LimitedRetry.txt");
|
|
$failedfiles_index = -1;
|
|
}
|
|
else{
|
|
# checking for last retry to instruct child process not to create any more retry info file
|
|
if($retrycount == MAX_RETRY - 1)
|
|
$failedfiles_index = -1;
|
|
}
|
|
$retry = 1;
|
|
sleep(5);
|
|
$engineID = 1;
|
|
}
|
|
$line = "";
|
|
|
|
}while($retry == 1);
|
|
|
|
}
|
|
|
|
#****************************************************************************************************
|
|
# Subroutine Name : doBackupOperation.
|
|
# Objective : This subroutine performs the actual task of backing up files. It creates
|
|
# a child process which executes the backup command. It also creates a process
|
|
# which continuously monitors the temporary output file. At the end of backup,
|
|
# it inspects the temporary error file if present. It then deletes the temporary
|
|
# output file, temporary error file and the temporary directory created by
|
|
# idevsutil binary.
|
|
# Modified By : Deepak Chaurasia, Pooja Havaldar
|
|
#*****************************************************************************************************/
|
|
function doBackupOperation($parameters,$engineID, $dbIndexCount,$failedfiles_index)
|
|
{
|
|
GLOBAL $isLocalBackup, $idevsutilBinaryPath, $webDir, $logOutputFilePath, $backupLocation, $operationStartTime;
|
|
GLOBAL $backupUtfFile, $errStr, $machine, $phpPath, $isDbService, $failedFilesCount, $evspid, $pid_OutputProcess, $opprocpid, $userDir, $IDPVTContent, $enctype, $needToLogout;
|
|
|
|
$startPidFileLock = fopen(ENGINE_LOCKE_FILE, 'a+');
|
|
$locked = flock($startPidFileLock, LOCK_SH);
|
|
if(!$locked){
|
|
fclose($startPidFileLock);
|
|
traceLog("Failed to lock engine file\n");
|
|
return 0;
|
|
}
|
|
|
|
|
|
// Creating PID based on engine
|
|
$engineFp = fopen(PIDFILE."_$engineID", 'w+');
|
|
$isLocked = flock($engineFp, LOCK_EX|LOCK_NB);
|
|
if(!$isLocked){
|
|
flock($startPidFileLock, LOCK_UN);
|
|
fclose($startPidFileLock);
|
|
fclose($engineFp);
|
|
traceLog("Failed to lock engine pid\n");
|
|
return 0;
|
|
}
|
|
|
|
@chmod(PIDFILE."_$engineID",PERMISSIONVAL);
|
|
|
|
$parameter_list = array();
|
|
$parameter_list = preg_split("/ /", $parameters,3);
|
|
$opType = BACKUP;
|
|
|
|
if($isLocalBackup == 1){
|
|
$opType = LOCALBACKUP;
|
|
|
|
/* #validate encType
|
|
if(!encTypeValidation())
|
|
$errDetails = ENCRYPTIONVERIFICATIONFAILED;
|
|
if($enctype != "DEFAULT") {
|
|
if(!pvtKeyValidation())
|
|
$errDetails = ENCRYPTIONVERIFICATIONFAILED;
|
|
|
|
}
|
|
*/
|
|
|
|
#validate encType and pvtkey if it is
|
|
if((!encTypeValidation()) || (($enctype != "DEFAULT") && (!pvtKeyValidation()))) {
|
|
$errDetails = ENCRYPTIONVERIFICATIONFAILED;
|
|
writeOperationErrorToErrorFile($errDetails);
|
|
traceLog($errDetails);
|
|
if(file_put_contents($needToLogout, ENCRYPTIONVERIFICATIONFAILED))
|
|
chmod($needToLogout,0777);
|
|
flock($startPidFileLock, LOCK_UN);
|
|
fclose($startPidFileLock);
|
|
fclose($engineFp);
|
|
exit_cleanup();
|
|
}
|
|
}
|
|
|
|
$backupUtfFile = getOperationFile(array("opType" => $opType,
|
|
"backupsetFile" => $parameter_list[0],
|
|
"backupType" => $parameter_list[1],
|
|
"source" => $parameter_list[2],
|
|
"engineID" => $engineID));
|
|
# EVS command to execute for backup
|
|
$idevsutilCommandLine = $idevsutilBinaryPath.WHITESPACE.IDEVSUTIL_ARG.ASSIGNMENTOP."\"$backupUtfFile\"";
|
|
if(file_exists(PIDFILE)){
|
|
$pid = exec("$idevsutilCommandLine > /dev/null & echo $!");
|
|
$evspid = $pid;
|
|
|
|
// backup child process for output file parsing
|
|
$backupLocationSource = base64_encode($backupLocation);
|
|
$fileChildProcessPath = $webDir.PATHSEPARATOR.APPDIR.PATHSEPARATOR.SCRIPTS.PATHSEPARATOR.BACKUP_CHILDPROCESSFILE;
|
|
$cmd = "$phpPath $fileChildProcessPath \"$logOutputFilePath\" \"$parameter_list[0]\" \"$parameter_list[1]\" '".base64_encode($parameter_list[2])."' '$backupLocationSource' \"$operationStartTime\" \"$engineID\" \"$dbIndexCount\" \"$failedfiles_index\"";
|
|
$pid_OutputProcess = exec("$cmd > /dev/null 2>".EXEC_ERRORFILE." & echo $!");
|
|
$opprocpid = $pid_OutputProcess;
|
|
}
|
|
else{
|
|
@unlink(UTFFILE."_$engineID");
|
|
fclose($engineFp);
|
|
@unlink(PIDFILE."_$engineID");
|
|
flock($startPidFileLock, LOCK_UN);
|
|
fclose($startPidFileLock);
|
|
return 0;
|
|
}
|
|
|
|
waitForEVS($pid, SCANERRORFILE, $engineID);
|
|
$evspid = 0;
|
|
file_put_contents(OUTPUTFILE."_$engineID","CHILD_PROCESS_COMPLETED\n",FILE_APPEND);
|
|
|
|
waitForEVS($pid_OutputProcess, NOSCANERRORFILE);
|
|
|
|
if($isLocalBackup == 1) {
|
|
$dbInfoHandle = fopen(TEMP_DB_FILES.LOCALDBINFOFILE, 'a+');
|
|
flock($dbInfoHandle, LOCK_EX);
|
|
fwrite($dbInfoHandle, "textdb_".$engineID."_".$dbIndexCount.".xml\n");
|
|
flock($dbInfoHandle, LOCK_UN);
|
|
fclose($dbInfoHandle);
|
|
}
|
|
|
|
# check for failed file index -1, then compare error
|
|
if($failedfiles_index === -1) {
|
|
$finalErrContents = file_get_contents(ERRORFILE . '_' . $engineID);
|
|
if(trim($finalErrContents) != '') {
|
|
file_put_contents(RETRY_ABORT_CAUSE, processUncaughtServerError($finalErrContents) . "\n", FILE_APPEND);
|
|
return;
|
|
}
|
|
}
|
|
|
|
# update server address if cmd failed due to wrong evs server address
|
|
if($engineID == 1)
|
|
checkErrorAndUpdateServerAddr("_$engineID");
|
|
|
|
@unlink(UTFFILE."_$engineID");
|
|
fclose($engineFp);
|
|
@unlink(PIDFILE."_$engineID");
|
|
flock($startPidFileLock, LOCK_UN);
|
|
fclose($startPidFileLock);
|
|
return BACKUP_SUCCESS;
|
|
}
|
|
|
|
function processUncaughtServerError($serverError) {
|
|
GLOBAL $ErrorArgumentsRetry;
|
|
foreach($ErrorArgumentsRetry as $retryError) {
|
|
if(preg_match("/$retryError/", $serverError)) {
|
|
return 'Error Occurred: ' . $retryError;
|
|
}
|
|
}
|
|
|
|
return '';
|
|
}
|
|
|
|
#**********************************************************************************************************
|
|
# Subroutine Name : checkErrorFile
|
|
# Objective : This function will compare error.txt content with error msgs and terminate the job if required.
|
|
# Added By : Pooja Havaldar
|
|
#**********************************************************************************************************/
|
|
function checkErrorFile(&$prevErrorSize, $engineID)
|
|
{
|
|
GLOBAL $ErrorArgumentsExit, $fileJobTerminationPath, $phpPath, $errStr;
|
|
$currentErrorSize = 0;
|
|
|
|
if(!file_exists(ERRORFILE."_$engineID"))
|
|
return 0;
|
|
else{
|
|
$currentErrorSize = customFileSize(ERRORFILE."_$engineID");
|
|
if($currentErrorSize == 0) #checking if error.txt has size
|
|
return 0;
|
|
}
|
|
|
|
if($currentErrorSize == $prevErrorSize)
|
|
return 0;
|
|
|
|
$linesBackupErrorFile = "";
|
|
$linesBackupErrorFile = file_get_contents(ERRORFILE."_$engineID");
|
|
|
|
for($j=0; $j <= count($ErrorArgumentsExit)-1; $j++){
|
|
if(preg_match("/$ErrorArgumentsExit[$j]/", $linesBackupErrorFile)){
|
|
checkDedupDeviceError($linesBackupErrorFile);
|
|
$errStr = "Operation could not be completed. Reason : $ErrorArgumentsExit[$j] \n";
|
|
traceLog($errStr);
|
|
$cmd = "$phpPath $fileJobTerminationPath 2 '".$ErrorArgumentsExit[$j]."'";
|
|
`$cmd`;
|
|
@unlink(ERRORFILE."_$engineID");
|
|
return 1;
|
|
}
|
|
}
|
|
|
|
$prevErrorSize = $currentErrorSize;
|
|
return 0;
|
|
}
|
|
|
|
#****************************************************************************************************
|
|
# Subroutine Name : waitforArchiveJobtoFinish.
|
|
# Objective : This subroutine will wait till Archive Job finishes
|
|
# Modified By : Pooja Havaldar
|
|
#*****************************************************************************************************/
|
|
function waitforArchiveJobtoFinish($ArchiveJobPath)
|
|
{
|
|
GLOBAL $logOutputFilePath, $jobDir;
|
|
|
|
$arch_flag = 0;
|
|
//$jobScheduledTime = getCutOffDetailsOfBakcupJob($jobDir);
|
|
|
|
while(checkIfJobRunning($ArchiveJobPath)){
|
|
if(!file_exists(PIDFILE)){
|
|
$errMsg = PIDFILE." does not exist for backup.";
|
|
traceLog($errMsg);
|
|
exit_cleanup();
|
|
}
|
|
|
|
/* $currentTimeVal = time();
|
|
if(($jobScheduledTime >= $currentTimeVal) ) {
|
|
if(!($logOutputHandle = fopen($logOutputFilePath,"a"))){
|
|
traceLog("Could not open file $logOutputFilePath, Reason:".customErrorMsg().LINEFEED);
|
|
sleep(5);
|
|
continue;
|
|
}
|
|
fwrite($logOutputHandle, "Your cut off time reached while backup is delayed, as Archive Cleanup is in progress.".LINEFEED);
|
|
fclose($logOutputHandle);
|
|
}*/
|
|
if($arch_flag == 0){
|
|
if(!($logOutputHandle = fopen($logOutputFilePath,"a"))){
|
|
traceLog("Could not open file $logOutputFilePath, Reason:".customErrorMsg().LINEFEED);
|
|
sleep(5);
|
|
continue;
|
|
}
|
|
fwrite($logOutputHandle, "Your scheduled job has been delayed, as Archive Cleanup is in progress.".LINEFEED);
|
|
fclose($logOutputHandle);
|
|
traceLog("Your scheduled job has been delayed, as Archive Cleanup is in progress.");
|
|
$arch_flag = 1;
|
|
}
|
|
sleep(5);
|
|
}
|
|
}
|
|
|
|
#****************************************************************************************************
|
|
# Subroutine Name : getCutOffDetailsOfBakcupJob.
|
|
# Objective : This subroutine is used to get the cut of details of the backup job as per request.
|
|
# {Not been used as of now, Can use in feature if needed.}
|
|
# Modified By : Anil Kumar
|
|
#*****************************************************************************************************/
|
|
function getCutOffDetailsOfBakcupJob($jobDirPath)
|
|
{
|
|
GLOBAL $userDir;
|
|
$jobInfo = 0;
|
|
$jobName = explode("Backup/", $jobDirPath);
|
|
$jobDir = str_replace('/', '', $jobName[1]);
|
|
$jobDir = "Backup_".$jobDir;
|
|
$fileName = $userDir.PATHSEPARATOR.".popUpList.txt";
|
|
$array_data = array();
|
|
if(file_exists($fileName)) {
|
|
$content = file_get_contents($fileName);
|
|
$array_data = json_decode($content, true);
|
|
}
|
|
|
|
if ($array_data[$jobDir] && $array_data[$jobDir]["scheduler"] && $array_data[$jobDir]["scheduler"]["cut_off_time"])
|
|
{
|
|
$cutOffStatus = $array_data[$jobDir]["scheduler"]["cut_off_time"];
|
|
|
|
if ($cutOffStatus == "on"){
|
|
$cutoff_hour = $array_data[$jobDir]["scheduler"]["cutoff_hour"] ;
|
|
$cutoff_minute = $array_data[$jobDir]["scheduler"]["cutoff_minute"] ;
|
|
$cutoff_meridian = $array_data[$jobDir]["scheduler"]["cutoff_meridian"] ;
|
|
$cutoff_hour.":".$cutoff_minute.":"."00"." ".$cutoff_meridian;
|
|
$jobInfo = strtotime(date("Y-m-d") . $currentTime);
|
|
}
|
|
}
|
|
|
|
return $jobInfo;
|
|
|
|
}
|
|
|
|
#****************************************************************************************************
|
|
# Subroutine Name : waitforDeleteJobtoFinish.
|
|
# Objective : This subroutine will wait till Delete Job finishes
|
|
# Modified By : Vijay Vinoth
|
|
#*****************************************************************************************************/
|
|
function waitforDeleteJobtoFinish($DeleteJobPath)
|
|
{
|
|
GLOBAL $logOutputFilePath;
|
|
$flag = 0;
|
|
while(checkIfDeleteJobRunning($DeleteJobPath)){
|
|
if(!file_exists(PIDFILE)){
|
|
$errMsg = PIDFILE." is not there in job path.";
|
|
traceLog($errMsg);
|
|
exit_cleanup();
|
|
}
|
|
|
|
if($flag == 0){
|
|
if(!($logOutputHandle = fopen($logOutputFilePath,"a"))){
|
|
traceLog("Could not open file $logOutputFilePath, Reason:".customErrorMsg().LINEFEED);
|
|
sleep(5);
|
|
continue;
|
|
}
|
|
fwrite($logOutputHandle, "Your scheduled job has been delayed, as Delete is in progress.");
|
|
fclose($logOutputHandle);
|
|
traceLog("Your scheduled job has been delayed, as Delete is in progress.", __FILE__, __LINE__);
|
|
$flag = 1;
|
|
}
|
|
sleep(5);
|
|
}
|
|
}
|
|
|
|
|
|
#****************************************************************************************************
|
|
# Subroutine Name : process_term.
|
|
# Objective : The signal handler invoked when SIGINT or SIGTERM signal is received by the script
|
|
# Added By : Arnab Gupta
|
|
# Modified by : Pooja Havaldar, Sabin Cheruvattil
|
|
#*****************************************************************************************************/
|
|
function process_term($signal)
|
|
{
|
|
GLOBAL $processName, $pid_generate, $pid_OutputProcess, $errStr, $dbh, $FD_WRITE, $issignal;
|
|
GLOBAL $bacSync, $totalFiles, $failedFilesCount, $ERRORFAILEDFILE, $BACK_FILE, $evspid, $opprocpid;
|
|
$issignal = TRUE;
|
|
|
|
if($processName == "main") {
|
|
if(file_exists(PIDFILE)) {
|
|
@unlink(PIDFILE);
|
|
}
|
|
|
|
if(file_exists(STARTPIDFILE)) {
|
|
@unlink(STARTPIDFILE);
|
|
}
|
|
|
|
waitForProcess($pid_generate, NOSCANERRORFILE);
|
|
waitForEnginesToFinish();
|
|
|
|
$filecontent = file(INFOFILE);
|
|
for($i = count($filecontent) - 1; $i >= 0; $i--) {
|
|
if(preg_match("/^FAILEDCOUNT/", $filecontent[$i])) {
|
|
$lineSplit = preg_split('/ /', $filecontent[$i], 2);
|
|
$failedFilesCount = trim($lineSplit[1]);
|
|
}
|
|
|
|
if(preg_match("/^TOTALFILES/", $filecontent[$i])) {
|
|
$split_totalFiles = preg_split('/ /', $filecontent[$i], 2);
|
|
$totalFiles = trim($split_totalFiles[1]);
|
|
}
|
|
|
|
if(preg_match("/^BAC_SYNC/", $filecontent[$i])) {
|
|
$lineSplit = preg_split('/ /', $filecontent[$i], 2);
|
|
$bacSync = trim($lineSplit[1]);
|
|
break;
|
|
}
|
|
}
|
|
|
|
$errStr = "Operation could not be completed. Reason: Process has been killed due to restart/shutdown.";
|
|
exit_cleanup();
|
|
} else if($processName == "dobackup") {
|
|
if($evspid) {
|
|
`kill $evspid`;
|
|
}
|
|
|
|
if($opprocpid) {
|
|
waitForEVS($opprocpid, NOSCANERRORFILE);
|
|
}
|
|
|
|
waitForEnginesToFinish();
|
|
# engines lock will release
|
|
if(file_exists(ENGINE_LOCKE_FILE)) {
|
|
@unlink(ENGINE_LOCKE_FILE);
|
|
}
|
|
} else if($processName == "generate") {
|
|
fwrite($FD_WRITE, "BAC_SYNC $bacSync\nTOTALFILES $totalFiles\nFAILEDCOUNT $failedFilesCount");
|
|
fclose($FD_WRITE);
|
|
fclose($ERRORFAILEDFILE);
|
|
fclose($BACK_FILE);
|
|
|
|
if("" != $errStr){
|
|
traceLog($errStr);
|
|
writeOperationErrorToErrorFile($errStr);
|
|
}
|
|
|
|
# newly added if dbh is defined then only close.
|
|
if($dbh){
|
|
unset($dbh);
|
|
}
|
|
exit;
|
|
}
|
|
}
|
|
|
|
#****************************************************************************************************
|
|
# Subroutine Name : waitForEnginesToFinish.
|
|
# Objective : wait for fork process to complete
|
|
# Added By : Vijay Vinoth
|
|
#*****************************************************************************************************/
|
|
function waitForEnginesToFinish()
|
|
{
|
|
GLOBAL $BackupForkchilds;
|
|
while(count($BackupForkchilds) > 0) {
|
|
foreach($BackupForkchilds as $key => $pid) {
|
|
$res = pcntl_waitpid($pid, $status, WNOHANG);
|
|
|
|
// If the process has already exited
|
|
if($res == -1 || $res > 0){
|
|
unset($BackupForkchilds[$key]);
|
|
}
|
|
}
|
|
|
|
sleep(1);
|
|
}
|
|
|
|
while(isAnyEngineRunning(ENGINE_LOCKE_FILE))
|
|
sleep(1);
|
|
|
|
return;
|
|
}
|
|
|
|
#****************************************************************************************************
|
|
# Subroutine Name : createUpdateDbPaths.
|
|
# Objective : Update DB path in dbPath.xml file
|
|
# Added By : Vijay Vinoth
|
|
#*****************************************************************************************************/
|
|
function createUpdateDbPaths()
|
|
{
|
|
GLOBAL $backupLocation,$dedupDetailFile,$username,$dedupRestoreDeviceID;
|
|
if(file_exists($dedupDetailFile)){
|
|
$deDup = true;
|
|
$deviceJSON = file_get_contents($dedupDetailFile);
|
|
$deviceArray = json_decode($deviceJSON, true);
|
|
$deviceID = trim($deviceArray['deviceID']);
|
|
$deviceID = preg_replace("/^5c0b/", '', preg_replace("/4b5z$/", '', $deviceID, 1), 1);
|
|
$serverRoot = trim($deviceArray['serverRoot']);
|
|
$nickName = trim($deviceArray['nick_name']);
|
|
|
|
}
|
|
$dbpaths = $backupLocation.'/'.$username.'/dbpaths.xml';
|
|
|
|
if(!file_exists($dbpaths))
|
|
{
|
|
$simple_xml = new SimpleXMLElement("<?xml version=\"1.0\" encoding=\"UTF-8\" ?><dbpaths></dbpaths>", null, false);
|
|
$simple_xml->addChild('dbpathinfo');
|
|
$simple_xml->dbpathinfo[0]->addAttribute('serverroot', "$serverRoot");
|
|
$simple_xml->dbpathinfo[0]->addAttribute('deviceid', "$deviceID");
|
|
$simple_xml->dbpathinfo[0]->addAttribute('nickname', "$nickName");
|
|
$simple_xml->dbpathinfo[0]->addAttribute('dbpath', "/LDBNEW/$serverRoot/$username.ibenc");
|
|
$simple_xml->asXML($dbpaths);
|
|
}
|
|
else
|
|
{
|
|
$exist = 0;
|
|
$xml = simplexml_load_file($dbpaths);
|
|
|
|
foreach($xml->children() as $child) {
|
|
$attr = $child->attributes();
|
|
foreach($attr as $key => $value) {
|
|
if($key == "serverroot" && $value == "$serverRoot")
|
|
{
|
|
$exist = 1;
|
|
return;
|
|
}
|
|
}
|
|
}
|
|
|
|
if(!$exist){
|
|
$sxe = new SimpleXMLElement($xml->asXML());
|
|
$newItem = $sxe->addChild("dbpathinfo");
|
|
$newItem->addAttribute('serverroot', "$serverRoot");
|
|
$newItem->addAttribute('deviceid', "$deviceID");
|
|
$newItem->addAttribute('nickname', "$nickName");
|
|
$newItem->addAttribute('dbpath', "/LDBNEW/$serverRoot/$username.ibenc");
|
|
$sxe->asXML($dbpaths);
|
|
}
|
|
}
|
|
|
|
if(file_exists($dbpaths)) {
|
|
$xmlContent = file_get_contents($dbpaths);
|
|
$xmlContent = str_replace("><", ">\n<", $xmlContent);
|
|
file_put_contents($dbpaths, $xmlContent);
|
|
}
|
|
}
|
|
|
|
#****************************************************************************************************
|
|
# Subroutine Name : writeLogHeader.
|
|
# Objective : This function will write the Log header details in the the output file
|
|
# Added By : Pooja Havaldar
|
|
# Modified by :
|
|
#*****************************************************************************************************/
|
|
function writeLogHeader()
|
|
{
|
|
GLOBAL $relative, $logOutputFilePath, $isLocalBackup, $nasUsername;
|
|
GLOBAL $headerMsg, $username, $hostName, $hidden, $backupLocation;
|
|
|
|
$temp = "";
|
|
$logheader = "";
|
|
if($relative == 1)
|
|
$temp = "Mirror";
|
|
else
|
|
$temp = "Relative";
|
|
|
|
# added by anil for log related issues
|
|
verifyLastUserLog(LOGPIDFILE);
|
|
/*
|
|
if(file_exists(LOGPIDFILE)) {
|
|
if($handle = fopen(LOGPIDFILE,'r+'))
|
|
{
|
|
$buffer = fread($handle, LIMIT);
|
|
fclose($handle);
|
|
if(file_exists($buffer)) {
|
|
if(strpos($buffer,"_SUCCESS") || strpos($buffer,"_ABORTED") || strpos($buffer,"_FAILURE")){
|
|
# file_put_contents("/volume1/web/idriveIt/test1.txt","760 in backup :: already deleted \n ** ", FILE_APPEND);
|
|
}
|
|
else{
|
|
$statusToUpdate = "FAILURE";
|
|
|
|
$newName = "$buffer"."_".$statusToUpdate;
|
|
$resCmd = "mv \"$buffer\" \"$newName\"";
|
|
$res = `$resCmd`;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
*/
|
|
############End of new code
|
|
|
|
# require to open log file to show job in progress as well as to log exclude details
|
|
$logOutputHandle = fopen($logOutputFilePath, 'a+');
|
|
if($logOutputHandle){
|
|
# As log file is opened so create logpid file as well
|
|
$currPath = trim(getcwd());
|
|
if(file_put_contents(LOGPIDFILE,"$currPath/$logOutputFilePath")){
|
|
chmod(LOGPIDFILE,PERMISSIONVAL);
|
|
/*$logFileHandle = fopen(LOGPIDFILE, 'a+');
|
|
$logPidHandler = flock($logFileHandle, LOCK_EX|LOCK_NB);
|
|
fclose($logFileHandle);*/
|
|
}
|
|
else {
|
|
$errStr = "Unable to open file ".LOGPIDFILE.", Reason:".customErrorMsg();
|
|
fwrite($logOutputHandle,$errStr.PHP_EOL);
|
|
fclose($logOutputHandle);
|
|
traceLog($errStr);
|
|
exit_cleanup();
|
|
}
|
|
@chmod($logOutputFilePath,PERMISSIONVAL);
|
|
}
|
|
else {
|
|
$errStr = "Failed to create $logOutputFilePath, Reason:".customErrorMsg();
|
|
traceLog($errStr);
|
|
exit_cleanup();
|
|
}
|
|
|
|
# construct log header.
|
|
//$headerMsg = "Backup Start Time: ".WHITESPACE.date("D M d H:i:s Y").LINEFEED;
|
|
$headerMsg = "Backup Start Time: ".WHITESPACE.trim(shell_exec('date +"%a %b %d %T %Y"')).LINEFEED;
|
|
$logheader = $headerMsg.LINEFEED."NAS User name: ".$nasUsername.LINEFEED;
|
|
$logheader .= "IDrive User name: ".$username.LINEFEED;
|
|
|
|
$headerMsg .= getHeaderMsg($temp, $hidden);
|
|
$logheader .= getHeaderMsg($temp, $hidden);
|
|
$headerMsg .= "Backup Location: ".$backupLocation.LINEFEED;
|
|
$logheader .= "Backup Location: ".$backupLocation.LINEFEED.LINEFEED;
|
|
|
|
# write log header.
|
|
fwrite($logOutputHandle,$logheader.PHP_EOL);
|
|
fclose($logOutputHandle);
|
|
|
|
}
|
|
|
|
#****************************************************************************************************
|
|
# Subroutine Name : exit_cleanup.
|
|
# Objective : This function will execute the major functions required at the time of exit
|
|
# Added By : Deepak Chaurasia
|
|
# Modified by : Pooja Havaldar, Sabin Cheruvattil
|
|
#*****************************************************************************************************/
|
|
function exit_cleanup()
|
|
{
|
|
GLOBAL $successFiles, $syncedFiles, $failedFiles, $exit_flag, $cancelFlag, $errStr, $status;
|
|
GLOBAL $localDB_pid_generate, $isLocalBackup, $processName, $issignal, $traceRetrycount, $userDir;
|
|
$evsTemp = $userDir.PATHSEPARATOR.EVS_TEMP;
|
|
traceLog("Total Retry Attempts:$traceRetrycount");
|
|
list($successFiles,$syncedFiles,$failedFiles,$exit_flag) = getParameterValueFromStatusFile(array('FILES_COUNT_INDEX', 'SYNC_COUNT_FILES_INDEX', 'FAILED_COUNT_FILES_INDEX', 'EXIT_FLAG_INDEX'));
|
|
|
|
if(($processName == "main") && ($isLocalBackup == 1)){
|
|
$tempFileDetaild = TEMP_DB_FILES.LOCALDBINFOFILE;
|
|
|
|
$dbInfoHandle = fopen($tempFileDetaild, 'a+');
|
|
flock($dbInfoHandle, LOCK_EX);
|
|
fwrite($dbInfoHandle, "PROCESS_COMPLETED");
|
|
flock($dbInfoHandle, LOCK_UN);
|
|
fclose($dbInfoHandle);
|
|
|
|
waitForEVS($localDB_pid_generate, NOSCANERRORFILE);
|
|
createUpdateDbPaths();
|
|
}
|
|
$exit = preg_split('/-/', $exit_flag,2);
|
|
if($exit[0] && $exit[1] != "")
|
|
$errStr = $exit[1];
|
|
|
|
if(file_exists(PIDFILE)) {
|
|
@unlink(PIDFILE);
|
|
} else if(!$issignal) {
|
|
# In childprocess, if we exit due to some exit scenario, then this exit_flag will be true with error msg
|
|
$cancelFlag = true;
|
|
if(!$exit[0])
|
|
$errStr = readFromUserCancel();
|
|
traceLog("$errStr");
|
|
} else if($issignal) {
|
|
$cancelFlag = TRUE;
|
|
}
|
|
|
|
writeOperationSummary(BACKUP);
|
|
getOperationStatus();
|
|
if(($status == "SUCCESS") && (file_exists($evsTemp)))
|
|
removeItems($evsTemp);
|
|
updateLogFileName();
|
|
|
|
if($isLocalBackup != 1) {
|
|
updateBackupSummaryToServer();
|
|
updateLogListXml();
|
|
uploadLogListXml();
|
|
updateBackupLogToServer();
|
|
}
|
|
|
|
sendMail();
|
|
removeBackupTempFiles();
|
|
@unlink(STARTPIDFILE);
|
|
removeItems("LimitedRetry.txt*");
|
|
//flock(LOGPIDFILE, LOCK_UN);
|
|
exit;
|
|
}
|
|
|
|
/*****************************************************************************************************
|
|
# Function : pvtKeyValidation
|
|
# In Param : ---
|
|
# Out Param : SUCCESS/FAILURE
|
|
# Objective : This function is to validate private key content from local IDPVT file with variable content
|
|
# Added By : Anil Kumar [03-12-2019]
|
|
# Modified By : <name> [Date]
|
|
#****************************************************************************************************/
|
|
function pvtKeyValidation()
|
|
{
|
|
GLOBAL $userDir, $IDPVTContent;
|
|
$encPvtkeyPath = $userDir.PATHSEPARATOR."IDPVT";
|
|
$encKeyContent = file_get_contents($encPvtkeyPath);
|
|
//traceLog("FILE_CONTENT: ".$encKeyContent." variable content: ".$IDPVTContent );
|
|
if($IDPVTContent != $encKeyContent){
|
|
return 0;
|
|
}
|
|
return 1;
|
|
}
|
|
|
|
/*****************************************************************************************************
|
|
# Function : encTypeValidation
|
|
# In Param : ---
|
|
# Out Param : SUCCESS/FAILURE
|
|
# Objective : This function is to validate encType from login cgi to account settings file info.
|
|
# Added By : Anil Kumar [03-12-2019]
|
|
# Modified By : <name> [Date]
|
|
#****************************************************************************************************/
|
|
function encTypeValidation()
|
|
{
|
|
GLOBAL $enctype;
|
|
$key = "encType";
|
|
$encTypeFomfile = "";
|
|
getParameterValueFromAccountSettingsFile($key,$encTypeFomfile);
|
|
|
|
if (strcasecmp($encTypeFomfile, $enctype) != 0)
|
|
return 0;
|
|
else
|
|
return 1;
|
|
}
|
|
|
|
?>
|