Files
resatennis_tyroltenn/IDrive/.config/cleanup_cli.php

497 lines
15 KiB
PHP
Executable File

<?php
include_once 'path.php';
$is_periodic = false;
$_SESSION["progress_stat"] = 0;
$pidPathForSearch = "";
$backupset_display = "";
$archive_per = '';
$selected_backupset = '';
$total_perc = 0;
$archive = 0;
$periodic_perc_error = 0;
$emptyFoldersList = '';
$currentPath = getcwd();
if($userName == '') {
$userName = getUserName($currentPath);
$nasUsername = getNasUserName($currentPath);
Scheduler::SetSchedulerPath($webDir, $userName, $nasUsername, $machine);
}
unset($_SESSION['serverAddr']);
getEVSServerAddr();
$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)
}
# This runs in background, so session will be invalid. Re-assign the values so that the scripts can pick the user from Scheduler
# This change helps in logging also
$archive_dir = Scheduler::$archiveclean_dir;
if(!empty($argv[2])) {
$backupset_display = $argv[2];
$_SESSION['archive_per'] = $argv[1];
$archive_per = $_SESSION['archive_per'];
$selected_backupset = Scheduler::$manual_dir;
$jobName = $selected_backupset;
$pidPathForSearch = "{$archive_dir}{$jobName}/{$pidPath}";
} else {
$splitPath = explode("/", $currentPath);
$currentDir = $splitPath[count($splitPath) - 1];
$jobName = startPeriodicCleanup($currentDir);
$is_periodic = true;
if(empty($jobName) || empty($userName)) {
Scheduler::Log("Jobname or user name is empty. jobname: $jobName Username: $userName");
fclose($handle);
return;
}
}
Scheduler::Log(AR_PERIODIC . "pidpath" . $pidPathForSearch);
@chmod($archive_dir,$permissionVal);
@chmod($jobName,$permissionVal);
$handle = NULL;
if(!($handle = fopen($pidPathForSearch, "w"))){
Scheduler::Log(AR_PERIODIC . "[$jobName]" . AR_FAILED . AR_PID_MISS);
exit;
}
else{
@chmod($pidPathForSearch,$permissionVal);
if(!flock($handle, LOCK_EX|LOCK_NB)){
Scheduler::Log("Not able to lock " . $pidPathForSearch);
}
fwrite($handle, ARCHIVE_JOB);
}
$restoreList = Scheduler::$restore_list_dir."/";
$searchDir = Scheduler::$user_dir."Restore/.+searchList";
$cleanupStatusFile = "$archive_dir$jobName/".CLEANUP_STATUS;
Scheduler::Log("Cleanup status File [$cleanupStatusFile]");
$resultFile = "$archive_dir$jobName/".ARCHIVERESULT;
$emptyFoldersList = "$archive_dir$jobName/".EMPTY_FOLDERS_LIST;
$backupLocation = Scheduler::GetHost($userName);
Scheduler::Log("Cleanup[backupLocation] $backupLocation");
if(isset($jobName)){
cleanupOperation();
}
else{
$_SESSION["progress_stat"] = 1;
}
/****************************************************************************************************
* Function Name : cleanupOperation
* Objective : Function for doing cleanup operation.
* Added By : Sajana K
*****************************************************************************************************/
function cleanupOperation()
{
GLOBAL $backupset_display;
GLOBAL $excludePartialFilePath;
GLOBAL $pidPathForSearch;
GLOBAL $backupLocation;
GLOBAL $resultFile;
GLOBAL $archive_per;
GLOBAL $archive_dir;
GLOBAL $jobName;
GLOBAL $OutOutputFilePath;
GLOBAL $is_periodic;
GLOBAL $cleanupStatusFile;
GLOBAL $selected_backupset;
GLOBAL $archive, $filesOnly, $countFile;
GLOBAL $emptyFoldersList,$permissionVal,$dedupDetailFile;
@unlink($cleanupStatusFile);
$errStr = "";
$filesOnly = "$archive_dir$jobName/".FILESONLY;
$backup_set = array();
$archive = 1;
$countFile = "$archive_dir$jobName/".ARCHIVECOUNT;
if(file_exists($backupset_display) && customFileSize($backupset_display)>0)
{
$total_count = 0;
$not_exit_count = 0;
$hiddenFlag = false;
$_SESSION['operation_status'] = "scanning";
$_SESSION['total_count'] = $total_count;
$_SESSION['files_scanned'] = "";
$backupset_display_content = file_get_contents($backupset_display);
$backup_set = explode("\n",$backupset_display_content);
$backup_set_count = count($backup_set)-1;
$count = 0;
$ret = showHiddenStatus();
if($ret == "Hide")
$hiddenFlag = true;
session_write_close();
for($m = 0; $m < $backup_set_count; $m = $m+4) {
if($hiddenFlag === true) {
if(preg_match("/\/\./",$backup_set[$m],$matches))
continue;
}
if($backup_set[$m+1]=="file")
{
$count++;
if(($count % 10 == 0) && !file_exists($pidPathForSearch)) {
$errStr= 'ERR_STR_VAL';
session_start();
errorcleanup($errStr);
}
if(file_exists($dedupDetailFile))
$file_name = $backup_set[$m] . "\n";
else
$file_name = $backupLocation . $backup_set[$m] . "\n";
file_put_contents($filesOnly, $file_name, FILE_APPEND);
@chmod($filesOnly,$permissionVal);
} else {
if(!file_exists($pidPathForSearch)){
$errStr= 'ERR_STR_VAL';
session_start();
errorcleanup($errStr);
}
if(file_exists($dedupDetailFile))
$searchPath=$backup_set[$m]."/";
else
$searchPath=$backupLocation.$backup_set[$m]."/";
$errStr = getArchiveSearchResult($searchPath,$jobName);
if($errStr != ""){
session_start();
errorcleanup($errStr);
}
}
}
# checking file status in remote
if(file_exists($filesOnly)) {
$filestatusflag = fileStatusRemote(FILESTATUSOP,$filesOnly);
if($filestatusflag === true){
$OutOutputFilePath = "$archive_dir$jobName/".OUTPUTFILE;
rename($filesOnly, $OutOutputFilePath);
$cmd = getCleanupCmd($jobName);
exec($cmd);
}
}
session_start();
$_SESSION["progress_stat"] = 1;
if(file_exists($countFile)) {
$values = file_get_contents($countFile);
$tempval=explode("\n",$values);
$total_count = isset($tempval[2])?$tempval[2]:0;
$not_exit_count = isset($tempval[3])?$tempval[3]:0;
}
if($not_exit_count > 0 && $total_count > 0){
$errStr = calcArchivePerc($not_exit_count, $total_count);
if($is_periodic == true){
$deleteFile = Scheduler::$archiveclean_dir.$selected_backupset."/".ARCHIVERESULT;
$archiveDelStatus = deleteItems(PERIODICARCHIVEOP,$deleteFile, "", $not_exit_count);
/* checking delete status for periodic cleanup and writing the error msgs to log file.*/
if($archiveDelStatus == 'AR_DEL_LOG_DIR')
$delLog = AR_FAILED.AR_DEL_LOG_DIR;
else if($archiveDelStatus == 'AR_DEL_LOG')
$delLog = AR_FAILED.AR_DEL_LOG;
else if($archiveDelStatus == 'AR_PID')
$delLog = AR_FAILED.AR_PID;
else
$delLog = $archiveDelStatus;
Scheduler::Log(AR_PERIODIC . "[$jobName]" . $delLog);
}
}
else
$errStr = 'AR_NO_ITEM';
}
else
$errStr = 'AR_FILE_MISS';
errorcleanup($errStr);
}
/****************************************************************************************************
* Function Name : errorcleanup
* Objective :
* Added By :
*****************************************************************************************************/
function errorcleanup($errStr = '')
{
GLOBAL $is_periodic, $jobName, $cleanupStatusFile, $pidPathForSearch, $countFile, $filesOnly, $OutOutputFilePath, $permissionVal, $handle, $archive_dir, $selected_backupset;
fclose($handle);
if($errStr != ""){
if($is_periodic==true){
/* changed by Avinash 04-07-2016 */
//Scheduler::debug_Log(AR_PERIODIC . "[$jobName]" . AR_FAILED . $errStr);
Scheduler::Log(AR_PERIODIC . "[$jobName]" . AR_FAILED . AR_NO_ITEM);
} else{
file_put_contents($cleanupStatusFile,"ERR=$errStr\n",FILE_APPEND);
}
}
$_SESSION["progress_stat"] = 1;
file_put_contents($cleanupStatusFile,"progress_stat=1\n",FILE_APPEND);
@chmod($cleanupStatusFile,$permissionVal);
if($is_periodic == true || $errStr != 'AR_DATA_READY'){
@unlink($pidPathForSearch);
}
if(file_exists($countFile) && ($errStr != 'AR_PERC_ERR' || $is_periodic == true)){
// unlink($countFile);
}
if($errStr == 'AR_PERC_ERR'){
$temp = $archive_dir."/$selected_backupset/temp/";
if(is_dir($temp))
removeItems($temp);
}
if(file_exists($filesOnly))
unlink($filesOnly);
if(file_exists($OutOutputFilePath))
unlink($OutOutputFilePath);
exit;
}
/****************************************************************************************************
* Function Name : calcArchivePerc
* Objective : Calculating archive percentage for Cleanup
* Added By : Sajana K
*****************************************************************************************************/
//function calcArchivePerc($not_exit_count, $total_count ,$resultFile)
function calcArchivePerc($not_exit_count, $total_count )
{
GLOBAL $jobName;
GLOBAL $is_periodic;
GLOBAL $cleanupStatusFile;
GLOBAL $archive_per;
GLOBAL $total_perc;
GLOBAL $periodic_perc_error;
$error = "";
$per_count = ($not_exit_count/$total_count)*100;
$per_count_rnd = round($per_count,2);
$total_perc = $per_count_rnd;
if($is_periodic == false){
if($archive_per >= $per_count)
$error = 'AR_DATA_READY';
else
$error = 'AR_PERC_ERR';
}
else{
if($archive_per >= $per_count){
$periodic_perc_error = 1;
}
else{
$periodic_perc_error = 0;
Scheduler::Log(AR_PERIODIC . "[$jobName]" . AR_FAILED . AR_PERC_ERR);
}
}
return $error;
}
/****************************************************************************************************
* Function Name : getArchiveSearchResult
* Objective : Function for getting the remoteList for Cleanup.
* Added By : Sajana K
# Modified By : Sabin Cheruvattil
*****************************************************************************************************/
function getArchiveSearchResult($searchPath,$jobName)
{
GLOBAL $idevsutilBinaryPath, $whiteSpace, $idevsutilArgument, $assignmentOperator;
GLOBAL $archive_dir, $pidPathForSearch, $OutErrorFilePath, $OutOutputFilePath;
GLOBAL $permissionVal, $appPath, $is_periodic, $emptyFoldersList, $archiveUtfFile;
#GLOBAL $dedupDetailFile;
$searchItem = "*";
$errStr = "";
/* to get the php error message in $php_errormsg variable. */
ini_set('track_errors', 1);
/* get the utf8 file for search operation for archive cleanup */
$archiveUtfFile = getOperationFile(SEARCHOP, $searchItem, $searchPath);
@chmod($archiveUtfFile,$permissionVal);
if(!file_exists($archiveUtfFile)){
$errStr= 'AR_START_ERR';
userCancel($errStr);
return $errStr;
}
/* running the evs search command */
$idevsutilCommandLine = $idevsutilBinaryPath.$whiteSpace.$idevsutilArgument.$assignmentOperator."\"$archiveUtfFile\"";
pclose(popen("$idevsutilCommandLine &","r"));
/* check till output or error file have some content */
while(!@customFileSize($OutOutputFilePath) and !@customFileSize($OutErrorFilePath)){
if(!file_exists($pidPathForSearch)){
$errStr= 'ERR_STR_VAL';
userCancel($errStr);
return $errStr;
}
sleep(1);
}
// added by anil on 6sep2017 for archive cleanup issue fix
if(@customFileSize($OutErrorFilePath) > 0) {
$evsError = file_get_contents($OutErrorFilePath);
if(stristr($evsError,'ERROR'))
{
if(stristr($evsError,'failed to get the device information') || stristr($evsError,'encryption verification failed'))
{
@unlink($dedupDetailFile);
}
$errStrArray = explode("\n",$evsError);
$matches = preg_grep("/@ERROR/", $errStrArray);
$errStr = str_replace("@ERROR:","",$matches[0]);
$evsError = ucfirst(trim($errStr)).". ".ERROR_RELOGIN;
return $evsError;
}
}
/* run cleanup list perl only if process is still alive */
if(file_exists($pidPathForSearch)){
$cmd = getCleanupCmd($jobName);
$isFolderEmpty = shell_exec($cmd);
$isFolderEmpty = trim($isFolderEmpty);
if($isFolderEmpty=='true'){
file_put_contents($emptyFoldersList,"$searchPath\n",FILE_APPEND);
@chmod($emptyFoldersList,$permissionVal);
}
}
userCancel($errStr);
return $errStr;
}
/****************************************************************************************************
* Function Name : userCancel
* Objective :
* Added By :
*****************************************************************************************************/
function userCancel($errStr = '')
{
GLOBAL $archiveUtfFile, $OutOutputFilePath, $OutErrorFilePath;
if(file_exists($archiveUtfFile))
unlink($archiveUtfFile);
if(file_exists($OutOutputFilePath))
unlink($OutOutputFilePath);
if(file_exists($OutErrorFilePath))
unlink($OutErrorFilePath);
}
/****************************************************************************************************
* Function Name : startPeriodicCleanup
* Objective : Function for starting periodic Cleanup.
* Added By : Sajana K
*****************************************************************************************************/
function startPeriodicCleanup($periodicBackupset)
{
GLOBAL $pidPath;
GLOBAL $archive_dir;
GLOBAL $pidPathForSearch;
GLOBAL $backupset_display;
GLOBAL $is_periodic;
GLOBAL $permissionVal;
GLOBAL $archive_per;
GLOBAL $selected_backupset;
GLOBAL $emptyFoldersList;
$jobs = Scheduler::ReadJobs(); //call from command line
$is_periodic = true;
$selected_backupset = $periodicBackupset;
$jobName = $periodicBackupset;
$jobName_type = "Backup_".$jobName;
$archive_per = $jobs[$jobName_type]["periodic"]["perc"];
$backupset_display = Scheduler::$backup_dir.$jobName."/".BACKUPSETFILEDISPLAY;
$pidPathForSearch = "$archive_dir$jobName/$pidPath";
if(file_exists($pidPathForSearch))
return;
$countFile = "$archive_dir$jobName/".ARCHIVECOUNT;
$resultFile = "$archive_dir$jobName/".ARCHIVERESULT;
$jobcheckpath = Scheduler::$backup_dir;
$jobRunning = checkIfJobRunning($jobcheckpath); // checking any job is running
if($jobRunning == 1)
Scheduler::Log(AR_PERIODIC . "[$jobName]Delaying cleanup operation. Reason: Backup is in progress.");
session_write_close();
while($jobRunning == 1){
sleep(60);
$jobcheckpath = Scheduler::$backup_dir;
$jobRunning = checkIfJobRunning($jobcheckpath); // checking any job is running
}
session_start();
if(file_exists($countFile))
unlink($countFile);
if(file_exists($resultFile))
unlink($resultFile);
$temp = $archive_dir."/$selected_backupset/temp/";
if(is_dir($temp)){
removeItems($temp);
}
$pidDir = "$archive_dir/$jobName";
if(!is_dir($archive_dir)){
if(!mkdir($archive_dir, $permissionVal)){
echo "AR_DIR";
Scheduler::Log(AR_PERIODIC . "[$jobName]file:startarchive" . AR_DIR);
exit;
}
@chmod($archive_dir,$permissionVal);
}
if(!is_dir($pidDir)){
if(!mkdir($pidDir, $permissionVal)){
echo "JOB_DIR";
Scheduler::Log(AR_PERIODIC . "[$jobName]" . "file:startarchive" . AR_JOB);
exit;
}
@chmod($pidDir,$permissionVal);
}
return $jobName;
}
#****************************************************************************************************
# 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 $pidPathForSearch, $pidPath;;
if(file_exists($pidPathForSearch)) {
@unlink($pidPathForSearch);
}
if(file_exists($pidPath)) {
@unlink($pidPath);
}
}
exit;
?>