Fix purge of log files was missing some log files.

This commit is contained in:
Laurent Destailleur 2017-07-12 20:23:35 +02:00
parent 33ceb22b8b
commit a487492a7a
2 changed files with 53 additions and 43 deletions

View File

@ -28,10 +28,10 @@
class Utils class Utils
{ {
var $db; var $db;
var $output; // Used by Cron method to return message var $output; // Used by Cron method to return message
var $result; // Used by Cron method to return data var $result; // Used by Cron method to return data
/** /**
* Constructor * Constructor
* *
@ -47,21 +47,21 @@ class Utils
* Purge files into directory of data files. * Purge files into directory of data files.
* CAN BE A CRON TASK * CAN BE A CRON TASK
* *
* @param string $choice Choice of purge mode ('tempfiles', 'tempfilesold' to purge temp older than 24h, 'allfiles', 'logfiles') * @param string $choice Choice of purge mode ('tempfiles', 'tempfilesold' to purge temp older than 24h, 'allfiles', 'logfile')
* @return int 0 if OK, < 0 if KO (this function is used also by cron so only 0 is OK) * @return int 0 if OK, < 0 if KO (this function is used also by cron so only 0 is OK)
*/ */
function purgeFiles($choice='tempfilesold') function purgeFiles($choice='tempfilesold')
{ {
global $conf, $langs, $dolibarr_main_data_root; global $conf, $langs, $dolibarr_main_data_root;
$langs->load("admin"); $langs->load("admin");
dol_syslog("Utils::purgeFiles choice=".$choice, LOG_DEBUG); dol_syslog("Utils::purgeFiles choice=".$choice, LOG_DEBUG);
require_once DOL_DOCUMENT_ROOT.'/core/lib/files.lib.php'; require_once DOL_DOCUMENT_ROOT.'/core/lib/files.lib.php';
$filesarray=array(); $filesarray=array();
if (empty($choice)) $choice='tempfilesold'; if (empty($choice)) $choice='tempfilesold';
if ($choice=='tempfiles' || $choice=='tempfilesold') if ($choice=='tempfiles' || $choice=='tempfilesold')
{ {
// Delete temporary files // Delete temporary files
@ -78,29 +78,39 @@ class Utils
} }
} }
} }
if ($choice=='allfiles') if ($choice=='allfiles')
{ {
// Delete all files // Delete all files (except install.lock)
if ($dolibarr_main_data_root) if ($dolibarr_main_data_root)
{ {
$filesarray=dol_dir_list($dolibarr_main_data_root,"all",0,'','install\.lock$'); $filesarray=dol_dir_list($dolibarr_main_data_root,"all",0,'','install\.lock$');
} }
} }
if ($choice=='logfile') if ($choice=='logfile')
{ {
// Define filelog to discard it from purge // Define files log
if ($dolibarr_main_data_root)
{
$filesarray=dol_dir_list($dolibarr_main_data_root, "files", 0, '.*\.log$', 'install\.lock$');
}
$filelog=''; $filelog='';
if (! empty($conf->syslog->enabled)) if (! empty($conf->syslog->enabled))
{ {
$filelog=$conf->global->SYSLOG_FILE; $filelog=$conf->global->SYSLOG_FILE;
$filelog=preg_replace('/DOL_DATA_ROOT/i',DOL_DATA_ROOT,$filelog); $filelog=preg_replace('/DOL_DATA_ROOT/i',DOL_DATA_ROOT,$filelog);
}
$filesarray[]=array('fullname'=>$filelog,'type'=>'file'); $alreadyincluded=false;
foreach ($filesarray as $tmpcursor)
{
if ($tmpcursor['fullname'] == $filelog) { $alreadyincluded=true; }
}
if (! $alreadyincluded) $filesarray[]=array('fullname'=>$filelog,'type'=>'file');
}
} }
$count=0; $count=0;
if (count($filesarray)) if (count($filesarray))
{ {
@ -120,7 +130,7 @@ class Utils
} }
} }
} }
// Update cachenbofdoc // Update cachenbofdoc
if (! empty($conf->ecm->enabled) && $choice=='allfiles') if (! empty($conf->ecm->enabled) && $choice=='allfiles')
{ {
@ -129,14 +139,14 @@ class Utils
$result = $ecmdirstatic->refreshcachenboffile(1); $result = $ecmdirstatic->refreshcachenboffile(1);
} }
} }
if ($count > 0) $this->output=$langs->trans("PurgeNDirectoriesDeleted", $count); if ($count > 0) $this->output=$langs->trans("PurgeNDirectoriesDeleted", $count);
else $this->output=$langs->trans("PurgeNothingToDelete"); else $this->output=$langs->trans("PurgeNothingToDelete");
//return $count; //return $count;
return 0; // This function can be called by cron so must return 0 if OK return 0; // This function can be called by cron so must return 0 if OK
} }
/** /**
* Make a backup of database * Make a backup of database
@ -147,16 +157,16 @@ class Utils
* @param int $usedefault 1=Use default backup profile (Set this to 1 when used as cron) * @param int $usedefault 1=Use default backup profile (Set this to 1 when used as cron)
* @param string $file 'auto' or filename to build * @param string $file 'auto' or filename to build
* @param int $keeplastnfiles Keep only last n files (not used yet) * @param int $keeplastnfiles Keep only last n files (not used yet)
* @return int 0 if OK, < 0 if KO (this function is used also by cron so only 0 is OK) * @return int 0 if OK, < 0 if KO (this function is used also by cron so only 0 is OK)
*/ */
function dumpDatabase($compression='none', $type='auto', $usedefault=1, $file='auto', $keeplastnfiles=0) function dumpDatabase($compression='none', $type='auto', $usedefault=1, $file='auto', $keeplastnfiles=0)
{ {
global $db, $conf, $langs, $dolibarr_main_data_root; global $db, $conf, $langs, $dolibarr_main_data_root;
global $dolibarr_main_db_name, $dolibarr_main_db_host, $dolibarr_main_db_user, $dolibarr_main_db_port, $dolibarr_main_db_pass; global $dolibarr_main_db_name, $dolibarr_main_db_host, $dolibarr_main_db_user, $dolibarr_main_db_port, $dolibarr_main_db_pass;
$langs->load("admin"); $langs->load("admin");
dol_syslog("Utils::dumpDatabase type=".$type." compression=".$compression." file=".$file, LOG_DEBUG); dol_syslog("Utils::dumpDatabase type=".$type." compression=".$compression." file=".$file, LOG_DEBUG);
require_once DOL_DOCUMENT_ROOT.'/core/lib/files.lib.php'; require_once DOL_DOCUMENT_ROOT.'/core/lib/files.lib.php';
@ -190,14 +200,14 @@ class Utils
$outputdir = $conf->admin->dir_output.'/backup'; $outputdir = $conf->admin->dir_output.'/backup';
$result=dol_mkdir($outputdir); $result=dol_mkdir($outputdir);
// MYSQL // MYSQL
if ($type == 'mysql' || $type == 'mysqli') if ($type == 'mysql' || $type == 'mysqli')
{ {
$cmddump=$conf->global->SYSTEMTOOLS_MYSQLDUMP; $cmddump=$conf->global->SYSTEMTOOLS_MYSQLDUMP;
$outputfile = $outputdir.'/'.$file; $outputfile = $outputdir.'/'.$file;
// for compression format, we add extension // for compression format, we add extension
$compression=$compression ? $compression : 'none'; $compression=$compression ? $compression : 'none';
@ -205,11 +215,11 @@ class Utils
if ($compression == 'bz') $outputfile.='.bz2'; if ($compression == 'bz') $outputfile.='.bz2';
$outputerror = $outputfile.'.err'; $outputerror = $outputfile.'.err';
dol_mkdir($conf->admin->dir_output.'/backup'); dol_mkdir($conf->admin->dir_output.'/backup');
// Parameteres execution // Parameteres execution
$command=$cmddump; $command=$cmddump;
if (preg_match("/\s/",$command)) $command=escapeshellarg($command); // Use quotes on command if (preg_match("/\s/",$command)) $command=escapeshellarg($command); // Use quotes on command
//$param=escapeshellarg($dolibarr_main_db_name)." -h ".escapeshellarg($dolibarr_main_db_host)." -u ".escapeshellarg($dolibarr_main_db_user)." -p".escapeshellarg($dolibarr_main_db_pass); //$param=escapeshellarg($dolibarr_main_db_name)." -h ".escapeshellarg($dolibarr_main_db_host)." -u ".escapeshellarg($dolibarr_main_db_user)." -p".escapeshellarg($dolibarr_main_db_pass);
$param=$dolibarr_main_db_name." -h ".$dolibarr_main_db_host; $param=$dolibarr_main_db_name." -h ".$dolibarr_main_db_host;
$param.=" -u ".$dolibarr_main_db_user; $param.=" -u ".$dolibarr_main_db_user;
@ -250,16 +260,16 @@ class Utils
$paramcrypted.=' -p"'.preg_replace('/./i','*',$dolibarr_main_db_pass).'"'; $paramcrypted.=' -p"'.preg_replace('/./i','*',$dolibarr_main_db_pass).'"';
$paramclear.=' -p"'.str_replace(array('"','`'),array('\"','\`'),$dolibarr_main_db_pass).'"'; $paramclear.=' -p"'.str_replace(array('"','`'),array('\"','\`'),$dolibarr_main_db_pass).'"';
} }
$errormsg=''; $errormsg='';
// Debut appel methode execution // Debut appel methode execution
$fullcommandcrypted=$command." ".$paramcrypted." 2>&1"; $fullcommandcrypted=$command." ".$paramcrypted." 2>&1";
$fullcommandclear=$command." ".$paramclear." 2>&1"; $fullcommandclear=$command." ".$paramclear." 2>&1";
if ($compression == 'none') $handle = fopen($outputfile, 'w'); if ($compression == 'none') $handle = fopen($outputfile, 'w');
if ($compression == 'gz') $handle = gzopen($outputfile, 'w'); if ($compression == 'gz') $handle = gzopen($outputfile, 'w');
if ($compression == 'bz') $handle = bzopen($outputfile, 'w'); if ($compression == 'bz') $handle = bzopen($outputfile, 'w');
if ($handle) if ($handle)
{ {
$ok=0; $ok=0;
@ -277,11 +287,11 @@ class Utils
elseif (preg_match('/'.preg_quote('SET SQL_NOTES=@OLD_SQL_NOTES').'/i',$read)) $ok=1; elseif (preg_match('/'.preg_quote('SET SQL_NOTES=@OLD_SQL_NOTES').'/i',$read)) $ok=1;
} }
pclose($handlein); pclose($handlein);
if ($compression == 'none') fclose($handle); if ($compression == 'none') fclose($handle);
if ($compression == 'gz') gzclose($handle); if ($compression == 'gz') gzclose($handle);
if ($compression == 'bz') bzclose($handle); if ($compression == 'bz') bzclose($handle);
if (! empty($conf->global->MAIN_UMASK)) if (! empty($conf->global->MAIN_UMASK))
@chmod($outputfile, octdec($conf->global->MAIN_UMASK)); @chmod($outputfile, octdec($conf->global->MAIN_UMASK));
} }
@ -291,7 +301,7 @@ class Utils
dol_syslog("Failed to open file ".$outputfile,LOG_ERR); dol_syslog("Failed to open file ".$outputfile,LOG_ERR);
$errormsg=$langs->trans("ErrorFailedToWriteInDir"); $errormsg=$langs->trans("ErrorFailedToWriteInDir");
} }
// Get errorstring // Get errorstring
if ($compression == 'none') $handle = fopen($outputfile, 'r'); if ($compression == 'none') $handle = fopen($outputfile, 'r');
if ($compression == 'gz') $handle = gzopen($outputfile, 'r'); if ($compression == 'gz') $handle = gzopen($outputfile, 'r');
@ -320,13 +330,13 @@ class Utils
} }
} }
// Fin execution commande // Fin execution commande
$this->output = $errormsg; $this->output = $errormsg;
$this->error = $errormsg; $this->error = $errormsg;
$this->result = array("commandbackuplastdone" => $command." ".$paramcrypted, "commandbackuptorun" => ""); $this->result = array("commandbackuplastdone" => $command." ".$paramcrypted, "commandbackuptorun" => "");
//if (empty($this->output)) $this->output=$this->result['commandbackuplastdone']; //if (empty($this->output)) $this->output=$this->result['commandbackuplastdone'];
} }
// MYSQL NO BIN // MYSQL NO BIN
if ($type == 'mysqlnobin') if ($type == 'mysqlnobin')
{ {
@ -338,7 +348,7 @@ class Utils
if ($compression == 'bz') $outputfile.='.bz2'; if ($compression == 'bz') $outputfile.='.bz2';
$outputerror = $outputfile.'.err'; $outputerror = $outputfile.'.err';
dol_mkdir($conf->admin->dir_output.'/backup'); dol_mkdir($conf->admin->dir_output.'/backup');
if ($compression == 'gz' or $compression == 'bz') if ($compression == 'gz' or $compression == 'bz')
{ {
backup_tables($outputfiletemp); backup_tables($outputfiletemp);
@ -349,16 +359,16 @@ class Utils
{ {
backup_tables($outputfile); backup_tables($outputfile);
} }
$this->output = ""; $this->output = "";
$this->result = array("commandbackuplastdone" => "", "commandbackuptorun" => ""); $this->result = array("commandbackuplastdone" => "", "commandbackuptorun" => "");
} }
// POSTGRESQL // POSTGRESQL
if ($type == 'postgresql') if ($type == 'postgresql')
{ {
$cmddump=$conf->global->SYSTEMTOOLS_POSTGRESQLDUMP; $cmddump=$conf->global->SYSTEMTOOLS_POSTGRESQLDUMP;
$outputfile = $outputdir.'/'.$file; $outputfile = $outputdir.'/'.$file;
// for compression format, we add extension // for compression format, we add extension
$compression=$compression ? $compression : 'none'; $compression=$compression ? $compression : 'none';
@ -366,11 +376,11 @@ class Utils
if ($compression == 'bz') $outputfile.='.bz2'; if ($compression == 'bz') $outputfile.='.bz2';
$outputerror = $outputfile.'.err'; $outputerror = $outputfile.'.err';
dol_mkdir($conf->admin->dir_output.'/backup'); dol_mkdir($conf->admin->dir_output.'/backup');
// Parameteres execution // Parameteres execution
$command=$cmddump; $command=$cmddump;
if (preg_match("/\s/",$command)) $command=escapeshellarg($command); // Use quotes on command if (preg_match("/\s/",$command)) $command=escapeshellarg($command); // Use quotes on command
//$param=escapeshellarg($dolibarr_main_db_name)." -h ".escapeshellarg($dolibarr_main_db_host)." -u ".escapeshellarg($dolibarr_main_db_user)." -p".escapeshellarg($dolibarr_main_db_pass); //$param=escapeshellarg($dolibarr_main_db_name)." -h ".escapeshellarg($dolibarr_main_db_host)." -u ".escapeshellarg($dolibarr_main_db_user)." -p".escapeshellarg($dolibarr_main_db_pass);
//$param="-F c"; //$param="-F c";
$param="-F p"; $param="-F p";
@ -419,8 +429,8 @@ class Utils
dol_delete_file($val['fullname']); dol_delete_file($val['fullname']);
} }
} }
return 0; return 0;
} }
} }

View File

@ -140,7 +140,7 @@ SystemToolsArea=System tools area
SystemToolsAreaDesc=This area provides administration features. Use the menu to choose the feature you're looking for. SystemToolsAreaDesc=This area provides administration features. Use the menu to choose the feature you're looking for.
Purge=Purge Purge=Purge
PurgeAreaDesc=This page allows you to delete all files generated or stored by Dolibarr (temporary files or all files in <b>%s</b> directory). Using this feature is not necessary. It is provided as a workaround for users whose Dolibarr is hosted by a provider that does not offer permissions to delete files generated by the web server. PurgeAreaDesc=This page allows you to delete all files generated or stored by Dolibarr (temporary files or all files in <b>%s</b> directory). Using this feature is not necessary. It is provided as a workaround for users whose Dolibarr is hosted by a provider that does not offer permissions to delete files generated by the web server.
PurgeDeleteLogFile=Delete log file <b>%s</b> defined for Syslog module (no risk of losing data) PurgeDeleteLogFile=Delete log files, including <b>%s</b> defined for Syslog module (no risk of losing data)
PurgeDeleteTemporaryFiles=Delete all temporary files (no risk of losing data) PurgeDeleteTemporaryFiles=Delete all temporary files (no risk of losing data)
PurgeDeleteTemporaryFilesShort=Delete temporary files PurgeDeleteTemporaryFilesShort=Delete temporary files
PurgeDeleteAllFilesInDocumentsDir=Delete all files in directory <b>%s</b>. Temporary files but also database backup dumps, files attached to elements (third parties, invoices, ...) and uploaded into the ECM module will be deleted. PurgeDeleteAllFilesInDocumentsDir=Delete all files in directory <b>%s</b>. Temporary files but also database backup dumps, files attached to elements (third parties, invoices, ...) and uploaded into the ECM module will be deleted.