1123 lines
35 KiB
PHP
Executable File
1123 lines
35 KiB
PHP
Executable File
<?php
|
|
ini_set('track_errors', 1);
|
|
$backupUtfFile = "";
|
|
$current_source = "";
|
|
$rel_Data = "";
|
|
$BackupFile = "";
|
|
$Backupfilecount = 0;
|
|
$relative = 0;
|
|
$filesOnlyFile_count = 0;
|
|
$noRelIndex = 0;
|
|
$excludeParArray = "";
|
|
$quotemeta = "";
|
|
$source = array();
|
|
$pid = "";
|
|
$pid_generate = "";
|
|
$pid_OutputProcess = "";
|
|
$localDB_pid_generate = -1;
|
|
$checkCPUUsage = 0;
|
|
|
|
$processName = "main";
|
|
$hidden = "Enabled";
|
|
$varManualTask = '\+ManualTask\/';
|
|
$outputFileName = "BACKUP";
|
|
|
|
$jobDir = $jobrunningDir.PATHSEPARATOR;
|
|
$excludeFullPath = $userDir.PATHSEPARATOR.FULL_PATH_EXCLUDE_FILE;
|
|
$excludePartialPath = $userDir.PATHSEPARATOR.EXCLUDE_PARTIALPATH;
|
|
$ArchiveJobPath = $userDir.PATHSEPARATOR.ARCHIVE.PATHSEPARATOR;
|
|
$DeleteJobPath = $userDir.PATHSEPARATOR.DELETE.PATHSEPARATOR;
|
|
$BackupsetFile_relative = TEMP_FILES.PATHSEPARATOR.BACKUPSETFILE_RELATIVE;
|
|
$BackupsetFile_norelative = TEMP_FILES.PATHSEPARATOR.BACKUPSETFILE_NORELATIVE;
|
|
$BackupsetFile_filesOnly = TEMP_FILES.PATHSEPARATOR.BACKUPSETFILE_ONLYFILES;
|
|
$BackupExcludesetFile = TEMP_FILES.PATHSEPARATOR.EXCLUDE_FILE;
|
|
$fileOnlySetName = $BackupsetFile_filesOnly;
|
|
$fileJobTerminationPath = $webDir.PATHSEPARATOR.APPDIR.PATHSEPARATOR.SCRIPTS.PATHSEPARATOR.JOBTERMINATIONFILE;
|
|
|
|
$operationStartTime = time();
|
|
# Errors encountered during backup operation for which the script should not retry the backup operation
|
|
$ErrorArgumentsExit = array( "encryption verification failed",
|
|
"some files could not be transferred due to quota over limit",
|
|
"skipped-over limit",
|
|
"account is under maintenance",
|
|
"account has been cancelled",
|
|
"account has been expired",
|
|
"protocol version mismatch",
|
|
"password mismatch",
|
|
"not enough free space on device",
|
|
"No route to host",
|
|
"bad response from proxy",
|
|
"out of memory",
|
|
"failed to get the device information",
|
|
"Device not configured",
|
|
"error in file write",
|
|
);
|
|
|
|
# Errors encountered during backup operation for which the script should retry the backup operation
|
|
$ErrorArgumentsRetry = array("io timeout",
|
|
"Operation timed out",
|
|
"nodename nor servname provided, or not known",
|
|
"failed to connect",
|
|
"connection unexpectedly closed",
|
|
"Connection refused",
|
|
//"failed verification -- update",
|
|
"Input/output error",
|
|
"unauthorized user" /* To Test Retry ,
|
|
"No such file or directory"*/
|
|
);
|
|
|
|
# Checking whether current job is ManualTask or Scheduled
|
|
if(! preg_match("/$varManualTask/", $jobDir))
|
|
$taskType = SCHEDULED;
|
|
|
|
# Read backup location
|
|
if($isLocalBackup == 1)
|
|
$backupLocation = $hostNameLB;
|
|
else
|
|
$backupLocation = readBackupLocation();
|
|
|
|
#****************************************************************************************************
|
|
# Subroutine Name : generateBackupsetFiles
|
|
# Objective : Generating backupset file/files based on backup set content based on exclude
|
|
# and relative/no-relative and the required data is entered to info_file.
|
|
# Added By : Pooja Havaldar
|
|
#*****************************************************************************************************/
|
|
function generateBackupsetFiles()
|
|
{
|
|
GLOBAL $BackupsetFile, $BackupArray, $source, $errStr, $backupLocation;
|
|
GLOBAL $isLocalBackup, $hidden, $relative, $dedupDetailFile;
|
|
GLOBAL $ERRORFAILEDFILE, $filehandle, $EXTN, $FD_WRITE, $BACK_FILE, $dbh;
|
|
GLOBAL $BackupFile, $totalSize, $filesonlycount, $fileOnlySetName, $bacSync, $failedFilesCount;
|
|
GLOBAL $Backupfilecount, $noRelIndex, $BackupsetFile_norelative, $filecount, $current_source, $totalFiles, $processName;
|
|
GLOBAL $dbh_insert, $dbh_update, $logOutputFilePath, $enumerateFileSize, $BackupExcludesetFile;
|
|
$backupArrayCount = 0;
|
|
|
|
$totalSize = 0;
|
|
$processName = "generate";
|
|
$bacSync = 0;
|
|
$filesonlycount = 0;
|
|
$retVal = 0;
|
|
|
|
$BackupArrayInitial = array();
|
|
$BackupArrayInitial = file(BACKUPSETFORDISPLAY);
|
|
|
|
$BackupArray = array();
|
|
$BackupArraySize = array();
|
|
$BackupArrayLMod = array();
|
|
for($i = 0; $i < count($BackupArrayInitial); $i+=4){
|
|
$BackupArrayInitial[$i] = trim($BackupArrayInitial[$i],"\n");
|
|
if(!empty($BackupArrayInitial[$i]))
|
|
{
|
|
array_push($BackupArray,$BackupArrayInitial[$i]);
|
|
array_push($BackupArraySize,$BackupArrayInitial[$i+2]);
|
|
array_push($BackupArrayLMod,$BackupArrayInitial[$i+3]);
|
|
}
|
|
}
|
|
$backupArrayCount = count($BackupArray);
|
|
if(!$isLocalBackup){
|
|
if(!createDB()){
|
|
$errStr = "Create DB failed.";
|
|
traceLog($errStr);
|
|
generateBackupsetCleanup($errStr);
|
|
return;
|
|
}
|
|
|
|
if(!prepareSelectStmt()){
|
|
$errStr = "Prepare select statement failed.";
|
|
traceLog($errStr);
|
|
generateBackupsetCleanup($errStr);
|
|
return;
|
|
}
|
|
if(file_exists($dedupDetailFile)){
|
|
try{
|
|
$dbh_insert = $dbh->prepare("INSERT INTO IDriveLocalTable (FILEPATH, MOD_DT, FILESIZE, LEVELCOUNT) values (?,?,?,?)") ;
|
|
$dbh_update = $dbh->prepare("UPDATE IDriveLocalTable SET MOD_DT = ?, FILESIZE = ? , PRESENTDATE = ? , LEVELCOUNT = ? WHERE FILEPATH = ?");
|
|
}
|
|
catch(PDOException $e){
|
|
$errStr = "Error: Prepare statement failed for insert/update. Reason: ".$e->getMessage();
|
|
traceLog($errStr);
|
|
writeOperationErrorToErrorFile($errStr);
|
|
unset($dbh);
|
|
exit;
|
|
}
|
|
}
|
|
}
|
|
//$localtime = date("D M d H:i:s Y");
|
|
$localtime = trim(shell_exec('date +"%a %b %d %T %Y"'));
|
|
|
|
$pos = 0;
|
|
$i = 0;
|
|
|
|
do{
|
|
$item = $BackupArray[$i];
|
|
$itemSize = trim($BackupArraySize[$i],"\n");
|
|
$itemLMod = trim($BackupArrayLMod[$i],"\n");
|
|
|
|
$type = filetype($item);
|
|
if($type == 'link' || $type == 'socket' || $type == 'fifo' || $type == 'block' || $type == 'char') {
|
|
$i++;
|
|
continue;
|
|
/*
|
|
This is removed to fix the issue Snigdha_2.04.06_03_16 by discussing with deepak
|
|
|
|
$printItem = getPrintItem($item);
|
|
$content = "[$localtime] [EXCLUDED] [$printItem]. Reason: Not a regular file.".LINEFEED;
|
|
if(! ($logOutputHandle = fopen($BackupExcludesetFile, "a")))
|
|
traceLog("Could not open file $logOutputFilePath, Reason : ".customErrorMsg());
|
|
|
|
fwrite($logOutputHandle,$content);
|
|
fclose($logOutputHandle);
|
|
$i++;
|
|
continue;*/
|
|
}
|
|
|
|
if(($hidden == 'Disabled') && preg_match('/\/\./',$item)){
|
|
$i++;
|
|
continue;
|
|
}
|
|
|
|
$printItem = getPrintItem($item);
|
|
if(!is_readable($item)){
|
|
if(!file_exists($item))
|
|
fwrite($ERRORFAILEDFILE, "[".$localtime."] [FAILED] [$printItem] Reason: No such file or directory".LINEFEED);
|
|
else
|
|
fwrite($ERRORFAILEDFILE, "[".$localtime."] [FAILED] [$printItem] Reason: Permission denied".LINEFEED);
|
|
|
|
$failedFilesCount++;
|
|
$totalFiles++;
|
|
$i++;
|
|
continue;
|
|
}
|
|
loadExcludeArray();
|
|
$ret = excludeCheck($item, $printItem);
|
|
if($ret == 1){
|
|
$i++;
|
|
continue;
|
|
}
|
|
elseif($ret == 2){
|
|
generateBackupsetCleanup("");
|
|
return;
|
|
}
|
|
|
|
if(is_dir($item)){
|
|
if(!file_exists(PIDFILE))
|
|
break;
|
|
|
|
if($relative == 0){
|
|
$Backupfilecount = 0;
|
|
$noRelIndex++;
|
|
$BackupFile = $BackupsetFile_norelative."$noRelIndex";
|
|
$filecount = 0;
|
|
|
|
$pos = strrpos($item, "/");
|
|
$source[$noRelIndex] = substr($item,0,$pos);
|
|
if($source[$noRelIndex] == "")
|
|
$source[$noRelIndex] = "/";
|
|
$current_source = $source[$noRelIndex];
|
|
|
|
if(!($filehandle = fopen($BackupFile,"w"))){
|
|
$errStr = "Unable to open $BackupFile to write, Reason: $!";
|
|
generateBackupsetCleanup($errStr);
|
|
return;
|
|
}
|
|
}
|
|
|
|
if(!$isLocalBackup){
|
|
$retVal = getSearchResultfromDB($item, 0);
|
|
if(!$retVal){
|
|
$errStr = "Unable to open ExtendedBackupList file to write.";
|
|
generateBackupsetCleanup($errStr);
|
|
return;
|
|
}
|
|
elseif($retVal == 2){
|
|
$i++;
|
|
continue;
|
|
}
|
|
}
|
|
|
|
if(PHP_INT_MAX <= 2147483647){
|
|
if(!enumerateFromLS($item)){
|
|
$errStr = "List Enumerate failed for item : $item \n";
|
|
$i++;
|
|
continue;
|
|
//generateBackupsetCleanup($errStr);
|
|
//return;
|
|
}
|
|
|
|
}
|
|
else{
|
|
if(!enumerate($item)){
|
|
$errStr = "Enumerate failed for item : $item \n";
|
|
generateBackupsetCleanup($errStr);
|
|
return;
|
|
}
|
|
}
|
|
|
|
if($relative == 0 && $filecount>0)
|
|
fwrite($FD_WRITE, "$BackupFile ".RELATIVE." $current_source\n");
|
|
}
|
|
else{
|
|
|
|
# added new code to to fix Snigdha_2.04.06_09_7
|
|
$temp = "";
|
|
$temp = trim($item, "\n");
|
|
|
|
if($isLocalBackup === 1) {
|
|
if(strpos($temp, $backupLocation) !== false) {
|
|
$localtime = trim(shell_exec('date +"%a %b %d %T %Y"'));
|
|
$content = "[$localtime] [EXCLUDED] [$item]. Reason: Item belongs to same express device.".LINEFEED;
|
|
if(!($logOutputHandle = fopen($BackupExcludesetFile, 'a'))){
|
|
traceLog("Could not open file $logOutputFilePath, Reason : " . customErrorMsg());
|
|
}
|
|
fwrite($logOutputHandle, $content);
|
|
fclose($logOutputHandle);
|
|
$failedFilesCount++;
|
|
$totalFiles++;
|
|
$i++;
|
|
continue;
|
|
}
|
|
}
|
|
|
|
fwrite($BACK_FILE, $printItem.LINEFEED);
|
|
$customFileData = customFileSizeFromLS($item);
|
|
$totalSize += $customFileData;
|
|
$enumerateFileSize += $customFileData;
|
|
$current_source = getSource();
|
|
|
|
if($relative == 0){
|
|
$filesonlycount++;
|
|
$filecount = $filesonlycount;
|
|
}
|
|
else
|
|
$filecount++;
|
|
|
|
$totalFiles++;
|
|
if($filecount == FILE_MAX_COUNT || $enumerateFileSize >= BACKUP_SET_MAX_SIZE){
|
|
$filesonlycount = 0;
|
|
if(!createBackupSetFiles1k("FILESONLY")){
|
|
$errStr = "\nCouldnot open new Backupsetfile for files only to write.";
|
|
generateBackupsetCleanup($errStr);
|
|
return;
|
|
}
|
|
}
|
|
}
|
|
$i++;
|
|
|
|
if(!$isLocalBackup and ($backupArrayCount == $i)){
|
|
$BackupArray = array();
|
|
loadExtendedList();
|
|
$i = 0;
|
|
$backupArrayCount = count($BackupArray);
|
|
}
|
|
}while($i <= $backupArrayCount-1);
|
|
|
|
# create totalsize.txt
|
|
file_put_contents(TOTAL_SIZE_PATH, $totalSize);
|
|
@chmod(TOTAL_SIZE_PATH, PERMISSIONVAL);
|
|
|
|
if(!$isLocalBackup)
|
|
readRemainingResultsFromDB();
|
|
|
|
if($relative == 1 && $filecount == 0)
|
|
@unlink($BackupFile);
|
|
elseif($filesonlycount == 0)
|
|
@unlink($fileOnlySetName);
|
|
|
|
$mdtCheck = false;
|
|
if($relative == 1 && $filecount > 0){
|
|
//if(file_exists($dedupDetailFile) && file_exists($BackupFile))
|
|
//$mdtCheck = createAndVerifyCheckSum($BackupFile);
|
|
$current_source = getSource();
|
|
fwrite($FD_WRITE, "$BackupFile ".RELATIVE." $current_source $mdtCheck\n");
|
|
}
|
|
elseif($filesonlycount > 0){
|
|
if(file_exists($dedupDetailFile) && file_exists($fileOnlySetName))
|
|
$mdtCheck = createAndVerifyCheckSum($fileOnlySetName);
|
|
|
|
$current_source = "/";
|
|
fwrite($FD_WRITE, "$fileOnlySetName ".NORELATIVE." $current_source $mdtCheck\n");
|
|
}
|
|
|
|
if(is_resource($EXTN))
|
|
fclose($EXTN);
|
|
|
|
generateBackupsetCleanup("");
|
|
}
|
|
|
|
|
|
#****************************************************************************************************
|
|
# Subroutine Name : generateBackupsetCleanup
|
|
# Objective : To complete the error cleanup and exit out of generateBackupset Function
|
|
# Added By : Pooja Havaldar
|
|
#*****************************************************************************************************/
|
|
function generateBackupsetCleanup($errStr = '')
|
|
{
|
|
GLOBAL $FD_WRITE, $bacSync, $totalFiles, $failedFilesCount, $ERRORFAILEDFILE, $BACK_FILE, $dbh;
|
|
|
|
fwrite($FD_WRITE, "BAC_SYNC $bacSync\n");
|
|
fwrite($FD_WRITE, "TOTALFILES $totalFiles\n");
|
|
fwrite($FD_WRITE, "FAILEDCOUNT $failedFilesCount");
|
|
fclose($FD_WRITE);
|
|
fclose($ERRORFAILEDFILE);
|
|
|
|
if(is_resource($BACK_FILE))
|
|
fclose($BACK_FILE);
|
|
|
|
if("" != $errStr)
|
|
writeOperationErrorToErrorFile($errStr);
|
|
|
|
# newly added if dbh is defined then only close.
|
|
if($dbh)
|
|
unset($dbh);
|
|
}
|
|
|
|
#**********************************************************************************************************
|
|
# Subroutine Name : enumerate
|
|
# Objective : This function recursively enumerates a directly and enters data in the created
|
|
# backupset files.
|
|
# Added By : Pooja Havaldar
|
|
#**********************************************************************************************************/
|
|
function enumerate($item)
|
|
{
|
|
GLOBAL $failedFilesCount, $jobDir;
|
|
GLOBAL $totalFiles, $filecount;
|
|
GLOBAL $isLocalBackup;
|
|
GLOBAL $backupLocation;
|
|
GLOBAL $relative;
|
|
GLOBAL $totalSize, $enumerateFileSize;
|
|
GLOBAL $current_source;
|
|
GLOBAL $hidden;
|
|
GLOBAL $ERRORFAILEDFILE, $filehandle, $BACK_FILE, $logOutputFilePath,$BackupExcludesetFile;
|
|
|
|
GLOBAL $checkCPUUsage;
|
|
/*if(!file_exists(PIDFILE))
|
|
break;*/
|
|
|
|
if(++$checkCPUUsage%100 === 0)
|
|
usleep(1000); // 1000 == 1 millisecond
|
|
|
|
$backupLocation1 = $backupLocation;
|
|
if(file_exists($dedupDetailFile))
|
|
$backupLocation1 = '';
|
|
|
|
//$localtime = date("D M d H:i:s Y");
|
|
$localtime = trim(shell_exec('date +"%a %b %d %T %Y"'));
|
|
$retVal = 1;
|
|
$errStr = "";
|
|
if (substr($item, -1, 1) != "/")
|
|
$item .= "/";
|
|
|
|
# get path after 1st level
|
|
$printItem = getPrintItem($item);
|
|
|
|
if($DIR = opendir($item)) {
|
|
while (($file = readdir($DIR)) !== false) {
|
|
if(!file_exists(PIDFILE))
|
|
break;
|
|
|
|
if ($file == "." or $file == "..")
|
|
continue;
|
|
|
|
$temp = "";
|
|
$temp = trim($item.$file,"\n");
|
|
|
|
$type = filetype($temp);
|
|
|
|
if($type == 'link' || $type == 'socket' || $type == 'fifo' || $type == 'block' || $type == 'char') {
|
|
continue;
|
|
}
|
|
|
|
if(($hidden === 'Disabled') && preg_match("/\/\./", $temp))
|
|
continue;
|
|
if($isLocalBackup === 1){
|
|
if(strpos($temp,$backupLocation)!==false){
|
|
$content = "[$localtime] [EXCLUDED] [{$printItem}{$file}]. Reason: Item belongs to same express device.".LINEFEED;
|
|
if(! ($logOutputHandle = fopen($BackupExcludesetFile, "a"))){
|
|
traceLog("Could not open file $logOutputFilePath, Reason : ".customErrorMsg());
|
|
}
|
|
fwrite($logOutputHandle,$content);
|
|
fclose($logOutputHandle);
|
|
continue;
|
|
}
|
|
}
|
|
|
|
if(!is_readable($temp)){
|
|
if(!file_exists($temp))
|
|
fwrite($ERRORFAILEDFILE, "[".$localtime."] [FAILED] [$printItem$file] Reason : No such file or directory".LINEFEED);
|
|
else
|
|
fwrite($ERRORFAILEDFILE, "[".$localtime."] [FAILED] [$printItem$file] Reason : Permission denied".LINEFEED);
|
|
|
|
$failedFilesCount++;
|
|
$totalFiles++;
|
|
continue;
|
|
}
|
|
|
|
$ret = excludeCheck($temp, $printItem.$file);
|
|
if($ret == 1)
|
|
continue;
|
|
elseif($ret == 2){
|
|
$retVal = 0;
|
|
break;
|
|
}
|
|
|
|
if(is_dir($temp)){
|
|
if(!enumerate($temp)){
|
|
$errStr = "Enumerate failed for item : $temp \n";
|
|
$retVal = 0;
|
|
break;
|
|
}
|
|
}
|
|
else{
|
|
/* To Test Retry
|
|
if($filecount%2 != 0)
|
|
$file = $file."_ODD";
|
|
*/
|
|
|
|
$totalFiles++;
|
|
if(!$isLocalBackup){
|
|
if(checkForSync($backupLocation1.$printItem.$file, $temp, 0)) #This subroutine checks for sync and remaining entries are written to PartialSyncFile
|
|
continue;
|
|
}
|
|
|
|
$customFileSizeData = sprintf("%u",filesize($temp));
|
|
if($relative == 0){
|
|
$item_orig = $item;
|
|
$totalSize += $customFileSizeData;
|
|
$enumerateFileSize += $customFileSizeData;
|
|
if($current_source != "/"){
|
|
$current_source_quote = preg_quote($current_source, '/');
|
|
$item_orig = preg_replace("/$current_source_quote/","",$item_orig,1);
|
|
}
|
|
$temp = $item_orig.$file;
|
|
fwrite($filehandle, "$temp".LINEFEED);
|
|
}
|
|
else{
|
|
$current_source = getSource();
|
|
fwrite($BACK_FILE, $printItem.$file.LINEFEED);
|
|
$totalSize += $customFileSizeData;
|
|
$enumerateFileSize += $customFileSizeData;
|
|
}
|
|
|
|
$filecount++;
|
|
|
|
if($filecount == FILE_MAX_COUNT || $enumerateFileSize >= BACKUP_SET_MAX_SIZE){
|
|
if(!file_exists(PIDFILE))
|
|
break;
|
|
|
|
if(!createBackupSetFiles1k("")){
|
|
$errStr = "\nCouldnot open new Backupsetfile to write.";
|
|
$retVal = 0;
|
|
break;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
closedir($DIR);
|
|
}
|
|
else{
|
|
$reason = customErrorMsg();
|
|
$errStr = "Unable to open directory $item, Reason:$reason\n";
|
|
if(strpos($reason,'Permission denied')>=0)
|
|
fwrite($ERRORFAILEDFILE, "[".$localtime."] [FAILED] [$printItem] Reason : Permission denied.".LINEFEED);
|
|
else
|
|
fwrite($ERRORFAILEDFILE, "[".$localtime."] [FAILED] [$printItem] Reason : No such file or directory.".LINEFEED);
|
|
$failedFilesCount++;
|
|
$totalFiles++;
|
|
}
|
|
|
|
if("" != $errStr){
|
|
traceLog($errStr);
|
|
writeOperationErrorToErrorFile($errStr);
|
|
}
|
|
|
|
return $retVal;
|
|
}
|
|
|
|
#****************************************************************************************************
|
|
# Subroutine Name : createBackupSetFiles1k.
|
|
# Objective : Construct the backup set file/files based on relative or no-relative.
|
|
# Added By : Pooja Havaldar
|
|
#*****************************************************************************************************/
|
|
function createBackupSetFiles1k($filesOnly_flag)
|
|
{
|
|
GLOBAL $Backupfilecount, $filecount, $enumerateFileSize;
|
|
GLOBAL $relative, $fileOnlySetName, $current_source, $BackupsetFile_filesOnly ;
|
|
GLOBAL $filesOnlyFile_count, $BackupFile, $noRelIndex, $BackupsetFile_norelative, $BackupsetFile_relative;
|
|
GLOBAL $FD_WRITE, $BACK_FILE, $filehandle, $dedupDetailFile;
|
|
|
|
$filecount = 0;
|
|
$enumerateFileSize = 0;
|
|
$Backupfilecount++;
|
|
$mdtCheck = false;
|
|
//if(file_exists($dedupDetailFile))
|
|
//$mdtCheck = createAndVerifyCheckSum($BackupFile);
|
|
|
|
if($relative == 0){
|
|
if($filesOnly_flag == "FILESONLY"){
|
|
$filesOnlyFile_count++;
|
|
fwrite($FD_WRITE, "$fileOnlySetName ".NORELATIVE." $current_source $mdtCheck\n");
|
|
$fileOnlySetName = $BackupsetFile_filesOnly."_".$filesOnlyFile_count;
|
|
fclose($BACK_FILE);
|
|
if(!($BACK_FILE = fopen($fileOnlySetName,"w"))){
|
|
traceLog("Unable to open $fileOnlySetName to write, Reason: ".customErrorMsg());
|
|
return 0;
|
|
}
|
|
}
|
|
else{
|
|
fwrite($FD_WRITE, "$BackupFile ".RELATIVE." $current_source $mdtCheck\n");
|
|
$BackupFile = $BackupsetFile_norelative."$noRelIndex"."_$Backupfilecount";
|
|
fclose($filehandle);
|
|
if(!($filehandle = fopen($BackupFile,"w"))){
|
|
traceLog("Unable to open file $BackupFile to write, Reason: ".customErrorMsg());
|
|
return 0;
|
|
}
|
|
}
|
|
}
|
|
else{
|
|
fwrite($FD_WRITE, "$BackupFile ".RELATIVE." $current_source $mdtCheck\n");
|
|
$BackupFile = $BackupsetFile_relative."_$Backupfilecount";
|
|
fclose($BACK_FILE);
|
|
if(!($BACK_FILE = fopen($BackupFile,"w"))){
|
|
traceLog("Unable to open file $BackupFile to write, Reason: ".customErrorMsg());
|
|
return 0;
|
|
}
|
|
}
|
|
|
|
return 1;
|
|
}
|
|
|
|
#**************************************************************************************************************************
|
|
# Subroutine Name : getBackupOperationType.
|
|
# Objective : This subroutine reads the .relative.txt to get the type of Backup Operation - mirror/relative
|
|
# and also open the intermediate files required for backup based on mirror/relative
|
|
# Added By : Pooja Havaldar
|
|
#**************************************************************************************************************************/
|
|
function getBackupOperationType()
|
|
{
|
|
GLOBAL $isLocalBackup;
|
|
GLOBAL $rel_Data;
|
|
GLOBAL $relative;
|
|
GLOBAL $current_source;
|
|
GLOBAL $BackupFile;
|
|
GLOBAL $BackupsetFile_relative;
|
|
GLOBAL $BackupsetFile_filesOnly;
|
|
GLOBAL $BACK_FILE;
|
|
GLOBAL $userDir;
|
|
|
|
# get the backup operation type mirror/relative
|
|
if(($isLocalBackup == 1) or !file_exists("$userDir/".RELATIVE_FILEPATH)){
|
|
$rel_Data = RELATIVE;
|
|
$relative = 1;
|
|
$current_source = "/";
|
|
}
|
|
else{
|
|
$rel_Data = file_get_contents("$userDir/".RELATIVE_FILEPATH);
|
|
$rel_Data = trim($rel_Data);
|
|
if($rel_Data === RELATIVE){
|
|
$relative = 1;
|
|
$current_source = "/";
|
|
}
|
|
else
|
|
$relative = 0;
|
|
}
|
|
if(!is_dir(TEMP_FILES)){
|
|
if(! mkdir(TEMP_FILES)){
|
|
traceLog("Unable to create directory ".TEMP_FILES.", Reason:".customErrorMsg());
|
|
unlink(PIDFILE);
|
|
unlink(STARTPIDFILE);
|
|
exit;
|
|
}
|
|
@chmod(TEMP_FILES, PERMISSIONVAL);
|
|
}
|
|
if(!is_dir(TEMP_DB_FILES) && $isLocalBackup == 1){
|
|
if(! mkdir(TEMP_DB_FILES)){
|
|
traceLog("Unable to create directory ".TEMP_DB_FILES.", Reason:".customErrorMsg());
|
|
unlink(PIDFILE);
|
|
unlink(STARTPIDFILE);
|
|
exit;
|
|
}
|
|
@chmod(TEMP_FILES, 0777);
|
|
}
|
|
|
|
#opening the backup set files based on mirror/relative
|
|
if($relative == 1){
|
|
if(!($BACK_FILE = fopen($BackupsetFile_relative,"w"))){
|
|
traceLog("Unable to open file $BackupsetFile_relative to write, Reason: ".customErrorMsg());
|
|
unlink(PIDFILE);
|
|
unlink(STARTPIDFILE);
|
|
exit;
|
|
}
|
|
$BackupFile = $BackupsetFile_relative;
|
|
}
|
|
else {
|
|
if(!($BACK_FILE = fopen($BackupsetFile_filesOnly,"w"))){
|
|
traceLog("Unable to open file $BackupsetFile_filesOnly to write, Reason:".customErrorMsg());
|
|
unlink(PIDFILE);
|
|
unlink(STARTPIDFILE);
|
|
exit;
|
|
}
|
|
$BackupFile = $BackupsetFile_filesOnly;
|
|
}
|
|
}
|
|
|
|
#******************************************************************************************************************
|
|
# Subroutine Name : removeBackupTempFiles.
|
|
# Objective : This subroutine romoves all temporary files and folders. Also cleanup condition based files
|
|
# and folder e.g restore list etc.
|
|
# Added By : Pooja Havaldar
|
|
#******************************************************************************************************************/
|
|
function removeBackupTempFiles()
|
|
{
|
|
GLOBAL $successFiles;
|
|
GLOBAL $backupLocation;
|
|
GLOBAL $backupUtfFile;
|
|
GLOBAL $userDir;
|
|
|
|
$dedupDetailFile = $userDir.PATHSEPARATOR.DEDUP_DETAIL_FILE;
|
|
$deviceID = "";
|
|
$restoreList = "";
|
|
if(file_exists($dedupDetailFile)){
|
|
$deviceJSON = file_get_contents($dedupDetailFile);
|
|
$deviceArray = json_decode($deviceJSON, true);
|
|
$deviceID = trim($deviceArray['deviceID']);
|
|
}
|
|
|
|
#If backup success, then .RestoreList and Search list gets cleaned up
|
|
if(0 < $successFiles){
|
|
$rListPath = "$userDir/.RestoreList";
|
|
$remoteSearchPath = "$userDir/Restore/.+searchList/*";
|
|
$backupLocation = str_replace("\'","'\\''",$backupLocation);
|
|
if($deviceID)
|
|
$restoreList = $rListPath."/".$deviceID;
|
|
else
|
|
$restoreList = $rListPath.$backupLocation;
|
|
if($restoreList!="/" && $remoteSearchPath!="/"){
|
|
$val = array($restoreList,$remoteSearchPath,PROGRESS_PATH."_*");
|
|
removeItems($val);
|
|
}
|
|
}
|
|
|
|
if(file_exists(TOTAL_SIZE_PATH))
|
|
@unlink(TOTAL_SIZE_PATH);
|
|
|
|
if(file_exists(STATUSFILE))
|
|
@unlink(STATUSFILE);
|
|
|
|
if(file_exists($backupUtfFile))
|
|
@unlink($backupUtfFile);
|
|
|
|
if(file_exists(OUTPUTFILE))
|
|
@unlink(OUTPUTFILE);
|
|
|
|
if(file_exists(ERRORFILE))
|
|
@unlink(ERRORFILE);
|
|
|
|
if(file_exists(CANCEL_PATH))
|
|
@unlink(CANCEL_PATH);
|
|
|
|
if(file_exists(INFOFILE))
|
|
@unlink(INFOFILE);
|
|
|
|
if(file_exists(RETRY_ABORT_CAUSE))
|
|
@unlink(RETRY_ABORT_CAUSE);
|
|
|
|
if(file_exists(RETRY_INFOFILE))
|
|
@unlink(RETRY_INFOFILE);
|
|
|
|
if(file_exists(EXEC_ERRORFILE))
|
|
@unlink(EXEC_ERRORFILE);
|
|
|
|
if(file_exists(CHECKSUMOUTPUTFILE))
|
|
@unlink(CHECKSUMOUTPUTFILE);
|
|
|
|
if(file_exists(CHECKSUMERRORFILE))
|
|
@unlink(CHECKSUMERRORFILE);
|
|
|
|
if(file_exists(MDTCHECKOUTPUTFILE))
|
|
@unlink(MDTCHECKOUTPUTFILE);
|
|
|
|
if(file_exists(ENGINE_LOCKE_FILE))
|
|
@unlink(ENGINE_LOCKE_FILE);
|
|
|
|
if(file_exists(MDTCHECKERRORFILE))
|
|
@unlink(MDTCHECKERRORFILE);
|
|
|
|
if(file_exists(OPERATION_SUMMERY_ERROR_FILE))
|
|
@unlink(OPERATION_SUMMERY_ERROR_FILE);
|
|
|
|
|
|
$arrayToRemove = array(TEMP_FILES, ERROR_DIR, UTFFILE."*", ERRORFILE."*", PROGRESS_PATH."*", STATUSFILE."*", OUTPUTFILE."*");
|
|
removeItems($arrayToRemove);
|
|
}
|
|
|
|
|
|
#****************************************************************************************************
|
|
# Subroutine Name : createAndVerifyCheckSum.
|
|
# Objective : This function will create & verify the check sum for Dedup account
|
|
# Added By : Senthil Pandian
|
|
# Modified by :
|
|
#*****************************************************************************************************/
|
|
function createAndVerifyCheckSum($currentBackupSetFile)
|
|
{
|
|
GLOBAL $idevsutilBinaryPath,$jobDir,$bacSync,$filecount;
|
|
|
|
$checksumOutputFile = $jobDir.CHECKSUMOUTPUTFILE;
|
|
$checksumErrorFile = $jobDir.CHECKSUMERRORFILE;
|
|
$mdtCheckOutputFile = $jobDir.MDTCHECKOUTPUTFILE;
|
|
$mdtCheckErrorFile = $jobDir.MDTCHECKERRORFILE;
|
|
|
|
$createCheckSumUtfFile = getOperationFile(array("opType" => CREATECHECKSUM,
|
|
"backupsetFile" => $currentBackupSetFile));
|
|
|
|
# EVS command to execute for Create Check Sum
|
|
$idevsutilCommandLine = $idevsutilBinaryPath.WHITESPACE.IDEVSUTIL_ARG.ASSIGNMENTOP."\"$createCheckSumUtfFile\"";
|
|
if(!file_exists(PIDFILE)){
|
|
return false; //Need to handle error
|
|
}
|
|
$pid = exec("$idevsutilCommandLine");
|
|
@unlink($createCheckSumUtfFile);
|
|
if(file_exists($checksumErrorFile) && filesize($checksumErrorFile)>0){
|
|
$errStr = file_get_contents($checksumErrorFile);
|
|
traceLog($errStr);
|
|
@unlink($checksumErrorFile);
|
|
@unlink($checksumOutputFile);
|
|
//Need to handle error
|
|
return false;
|
|
}
|
|
if(file_exists($checksumOutputFile) && filesize($checksumOutputFile)>0){
|
|
$mdtCheckUtfFile = getOperationFile(array( "opType" => MDTCHECK,
|
|
"backupsetFile" => $checksumOutputFile));
|
|
# EVS command to execute for Create Check Sum
|
|
$idevsutilCommandLine = $idevsutilBinaryPath.WHITESPACE.IDEVSUTIL_ARG.ASSIGNMENTOP."\"$mdtCheckUtfFile\"";
|
|
$pid = exec("$idevsutilCommandLine");
|
|
|
|
@unlink($mdtCheckUtfFile);
|
|
if(file_exists($mdtCheckErrorFile) && filesize($mdtCheckErrorFile)>0){
|
|
$errStr = file_get_contents($mdtCheckErrorFile);
|
|
traceLog($errStr);
|
|
@unlink($mdtCheckErrorFile);
|
|
@unlink($mdtCheckOutputFile);
|
|
return false;
|
|
}
|
|
if(file_exists($mdtCheckOutputFile)){
|
|
traceLog("file_exists:$mdtCheckOutputFile");
|
|
if(!$handle = fopen($mdtCheckOutputFile,'r')){
|
|
@unlink($mdtCheckErrorFile);
|
|
@unlink($mdtCheckOutputFile);
|
|
traceLog("Could not open $mdtCheckOutputFile".customErrorMsg());
|
|
return false;
|
|
}
|
|
$buffer = fread($handle, LIMIT);
|
|
$byteRead = strlen($buffer);
|
|
|
|
if($byteRead == 0){
|
|
if(!file_exists(PIDFILE)){
|
|
return false;
|
|
}
|
|
}
|
|
$resultList = rtrim($buffer);
|
|
traceLog("resultList:$resultList");
|
|
$resultListXML = "<root>$resultList</root>";
|
|
$xml = simplexml_load_string($resultListXML);
|
|
if(!$xml){
|
|
$resultListXML = checkAndConstructXML($resultList);
|
|
$resultListXML = "<root>$resultListXML</root>";
|
|
$xml = simplexml_load_string($resultListXML);
|
|
}
|
|
$backupSet = '';
|
|
foreach ($xml->item as $value)
|
|
{
|
|
$fieldFileName = $value['fname'];
|
|
$checkSum = trim($value['checksum']);
|
|
$fileSeekID = trim($value['fileSeekID']);
|
|
$folderSeekID = trim($value['folderSeekID']);
|
|
$fullPathSeekID = trim($value['fullPathSeekID']);
|
|
$mdt = trim($value['mdt']);
|
|
$fileSize = trim($value['fileSize']);
|
|
$status = trim($value['status']);
|
|
traceLog("fieldFileName:$fieldFileName");
|
|
traceLog("status:$status");
|
|
if($status=="SUCCESS"){
|
|
//File in Sync
|
|
$size = customFileSize($fieldFileName);
|
|
$modtime = filemtime($fieldFileName);
|
|
$levelCount = substr_count($fieldFileName,"/");
|
|
update(1, $modtime, $size, time(), $levelCount-1, $fieldFileName);
|
|
$bacSync++;
|
|
$filecount--;
|
|
} else {
|
|
//Needs to be backed up
|
|
$mdt = str_pad($mdt,20,0,STR_PAD_LEFT);
|
|
$fileSize = str_pad($fileSize,20,0,STR_PAD_LEFT);
|
|
$backupSet .= $fieldFileName.":".$checkSum."$".$mdt."$".$fileSize."$".$fileSeekID."$".$folderSeekID."$".$fullPathSeekID."\n";
|
|
}
|
|
}
|
|
traceLog("currentBackupSetFile:$currentBackupSetFile");
|
|
traceLog("backupSet:$backupSet");
|
|
if($backupSet) {
|
|
file_put_contents($currentBackupSetFile,$backupSet);
|
|
return true;
|
|
}
|
|
return false;
|
|
}
|
|
} else {
|
|
traceLog("$checksumOutputFile file not found");//Need to handle error
|
|
return false;
|
|
}
|
|
}
|
|
|
|
#**********************************************************************************************************
|
|
# Subroutine Name : enumerateFromLS
|
|
# Objective : This function recursively enumerates a directly and enters data in the created
|
|
# backupset files.
|
|
# Added By : Vijay Vinoth
|
|
# Modified By : Sabin Cheruvattil
|
|
#**********************************************************************************************************/
|
|
function enumerateFromLS($item)
|
|
{
|
|
GLOBAL $failedFilesCount, $jobDir, $totalFiles, $filecount, $isLocalBackup, $backupLocation, $relative;
|
|
GLOBAL $totalSize, $enumerateFileSize, $current_source, $hidden, $machine, $appPath, $checkCPUUsage;
|
|
GLOBAL $ERRORFAILEDFILE, $filehandle, $BACK_FILE, $logOutputFilePath,$BackupExcludesetFile, $dedupDetailFile;
|
|
$commandOutput = '';
|
|
|
|
$lastLine = '';
|
|
$resultList = '';
|
|
$toFind = array("/","'"," ");
|
|
$toReplace = array("_","_","_");
|
|
$cmdOPFile = TEMP_FILES . PATHSEPARATOR . LS_TMP_OUTPUT . addslashes(str_replace($toFind, $toReplace, $item));
|
|
$enumErrFile = TEMP_FILES . PATHSEPARATOR . LS_TMP_ERROR . addslashes(str_replace($toFind, $toReplace, $item));
|
|
|
|
if(++$checkCPUUsage % 100 === 0) {
|
|
usleep(1000); // 1000 == 1 millisecond
|
|
}
|
|
|
|
$backupLocation1 = $backupLocation;
|
|
if(file_exists($dedupDetailFile)) {
|
|
$backupLocation1 = '';
|
|
}
|
|
|
|
$localtime = trim(shell_exec('date +"%a %b %d %T %Y"'));
|
|
$retVal = 1;
|
|
$errStr = '';
|
|
if(substr($item, -1, 1) != "/") {
|
|
$item .= "/";
|
|
}
|
|
|
|
# get path after 1st level
|
|
$printItem = getPrintItem($item);
|
|
$itemNew = str_replace("'", "'\''", $item);
|
|
|
|
# add individual pid holder file for logging pid
|
|
if($machine == NETGEAR) {
|
|
$cmd = "$appPath/ls -aln '$itemNew' 2> '$enumErrFile' 1> '$cmdOPFile' & echo $!";
|
|
} else if($machine == QNAP || $machine == SYNOLOGY || $machine == ASUSTOR) {
|
|
$cmd = "ls -aln '$itemNew' 2> '$enumErrFile' 1> '$cmdOPFile' & echo $!";
|
|
} else if($machine == TWOTIER) {
|
|
$cmd = "/bin/ls -aln '$itemNew' 2> '$enumErrFile' 1> '$cmdOPFile' & echo $!";
|
|
}
|
|
|
|
$lsPIDFile = $jobDir . "LSPID";
|
|
|
|
$lsPID = exec($cmd);
|
|
file_put_contents($lsPIDFile, $lsPID);
|
|
|
|
# wait check for ls pid to complete.
|
|
while(checkPIDRunning($lsPID))
|
|
usleep(1000);
|
|
|
|
# Remove individual ls pid file as ls execution got completed
|
|
# if job is terminated, kill ls process from job termination script
|
|
if(file_exists($lsPIDFile)) {
|
|
# removing the pid file as the ls processing got completed
|
|
@unlink($lsPIDFile);
|
|
}
|
|
|
|
$handle = fopen($cmdOPFile, 'r');
|
|
if(!$handle) {
|
|
unlink($cmdOPFile);
|
|
unlink($enumErrFile);
|
|
$errStr = "Unable to open ls output for item: $$item\n";
|
|
$retVal = 0;
|
|
return $retVal;
|
|
}
|
|
|
|
$sizeFile = filesize($enumErrFile);
|
|
if(file_exists($enumErrFile) && ($sizeFile > 0)) {
|
|
$reason = join('', file($enumErrFile));
|
|
$errStr = "Unable to list directory $item.\n";
|
|
if(strpos($reason, 'Permission denied') >= 0) {
|
|
fwrite($ERRORFAILEDFILE, '[' . $localtime . "] [FAILED] [$printItem] Reason : Permission denied." . LINEFEED);
|
|
} else {
|
|
fwrite($ERRORFAILEDFILE, '[' . $localtime . "] [FAILED] [$printItem] Reason : No such file or directory." . LINEFEED);
|
|
}
|
|
$failedFilesCount++;
|
|
$totalFiles++;
|
|
|
|
traceLog($errStr);
|
|
//writeOperationErrorToErrorFile($errStr);
|
|
@unlink($cmdOPFile);
|
|
@unlink($enumErrFile);
|
|
fclose($handle);
|
|
$retVal = 0;
|
|
return $retVal;
|
|
}
|
|
|
|
|
|
while(1) {
|
|
$buffer = fread($handle, 4096*25);
|
|
|
|
if(empty($buffer)) {
|
|
if(!empty($lastLine))
|
|
array_push($commandOutput, $lastLine);
|
|
else
|
|
$commandOutput = '';
|
|
$lastLine = '';
|
|
}
|
|
else{
|
|
$resultList = $lastLine.$buffer;
|
|
$commandOutput = explode("\n", $resultList);
|
|
$lastLine = array_pop($commandOutput);
|
|
}
|
|
unset($buffer);
|
|
|
|
if(empty($commandOutput) or !count($commandOutput))
|
|
break;
|
|
|
|
$i = 0;
|
|
foreach($commandOutput as $outputList) {
|
|
if(!file_exists(PIDFILE))
|
|
break;
|
|
|
|
if(++$i % 100 === 0)
|
|
usleep(1000);
|
|
|
|
if(empty($outputList))
|
|
continue;
|
|
|
|
$parseDetails = preg_split('/\s+/', $outputList, 8);
|
|
if(($parseDetails[0] == 'total')) {
|
|
continue;
|
|
}
|
|
$fileSize = $parseDetails[4];
|
|
$fileType = $parseDetails[0];
|
|
$split1 = preg_split('/\s/', $parseDetails[7], 2);
|
|
$fileName = trim($split1[1],"\n");
|
|
|
|
if(empty($fileName)) {
|
|
traceLog("Skipped Item in enumerate : " . $outputList);
|
|
continue;
|
|
}
|
|
|
|
if($fileName == '.' || $fileName == '..') {
|
|
continue;
|
|
}
|
|
|
|
$temp = $item.$fileName;
|
|
|
|
if(($fileType[0] != 'd') && ($fileType[0] != '-')) {
|
|
continue;
|
|
}
|
|
|
|
if(($hidden === 'Disabled') && preg_match("/\/\./", $temp)) {
|
|
continue;
|
|
}
|
|
|
|
if($isLocalBackup === 1) {
|
|
if(strpos($temp, $backupLocation) !== false) {
|
|
$content = "[$localtime] [EXCLUDED] [{$printItem}{$fileName}]. Reason: Item belongs to same express device.".LINEFEED;
|
|
if(!($logOutputHandle = fopen($BackupExcludesetFile, 'a'))){
|
|
traceLog("Could not open file $logOutputFilePath, Reason : " . customErrorMsg());
|
|
}
|
|
fwrite($logOutputHandle, $content);
|
|
fclose($logOutputHandle);
|
|
continue;
|
|
}
|
|
}
|
|
|
|
if(!is_readable($temp)) {
|
|
if(!file_exists($temp)) {
|
|
fwrite($ERRORFAILEDFILE, '[' . $localtime . "] [FAILED] [$printItem$fileName] Reason : No such file or directory" . LINEFEED);
|
|
} else {
|
|
fwrite($ERRORFAILEDFILE, '[' . $localtime . "] [FAILED] [$printItem$fileName] Reason : Permission denied" . LINEFEED);
|
|
}
|
|
|
|
$failedFilesCount++;
|
|
$totalFiles++;
|
|
continue;
|
|
}
|
|
|
|
$ret = excludeCheck($temp, $printItem . $fileName);
|
|
if($ret == 1) {
|
|
continue;
|
|
} elseif($ret == 2) {
|
|
$retVal = 0;
|
|
break;
|
|
}
|
|
|
|
if($fileType[0] == "d") {
|
|
# @TODO: update to extended backup directory list
|
|
if(!enumerateFromLS($temp)) {
|
|
$errStr = "Enumerate failed for item : $temp \n";
|
|
}
|
|
}
|
|
else if($fileType[0] == '-') {
|
|
$totalFiles++;
|
|
if(!$isLocalBackup) {
|
|
if(checkForSync($backupLocation1. $printItem . $fileName, $temp, 0)) {
|
|
#This subroutine checks for sync and remaining entries are written to PartialSyncFile
|
|
continue;
|
|
}
|
|
}
|
|
|
|
if($relative == 0) {
|
|
$item_orig = $item;
|
|
$totalSize += $fileSize;
|
|
$enumerateFileSize += $fileSize;
|
|
if($current_source != "/") {
|
|
$current_source_quote = preg_quote($current_source, '/');
|
|
$item_orig = preg_replace("/$current_source_quote/", '', $item_orig, 1);
|
|
}
|
|
|
|
$temp = $item_orig . $fileName;
|
|
fwrite($filehandle, $temp . LINEFEED);
|
|
} else {
|
|
$current_source = getSource();
|
|
fwrite($BACK_FILE, $printItem . $fileName . LINEFEED);
|
|
$totalSize += $fileSize;
|
|
$enumerateFileSize += $fileSize;
|
|
}
|
|
|
|
$filecount++;
|
|
|
|
if($filecount == FILE_MAX_COUNT || $enumerateFileSize >= BACKUP_SET_MAX_SIZE) {
|
|
if(!file_exists(PIDFILE)) {
|
|
break;
|
|
}
|
|
|
|
if(!createBackupSetFiles1k('')) {
|
|
$errStr = "\nCouldnot open new Backupsetfile to write.";
|
|
$retVal = 0;
|
|
break;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
}
|
|
|
|
fclose($handle);
|
|
@unlink($cmdOPFile);
|
|
@unlink($enumErrFile);
|
|
return $retVal;
|
|
}
|
|
|
|
#**********************************************************************************************************
|
|
# Subroutine Name : checkPIDRunning
|
|
# Objective : checks whether the process is running or not
|
|
# Modified By : Sabin Cheruvattil
|
|
#**********************************************************************************************************/
|
|
function checkPIDRunning($pid)
|
|
{
|
|
return file_exists("/proc/$pid");
|
|
}
|
|
?>
|