Add archive generation for documents directory

This commit is contained in:
atm-greg 2018-10-10 14:01:20 +02:00
parent eff0e959b3
commit 24988f075c
2 changed files with 268 additions and 4 deletions

View File

@ -493,6 +493,7 @@ print '</table>';
</div> <!-- end div fichehalfleft -->
<div id="backupdatabaseright" class="fichehalfright" style="height:480px; overflow: auto;">
<div class="ficheaddleft">
@ -505,21 +506,94 @@ print '<br>';
</div>
</div>
</form>
</fieldset>
<br>
<!-- Dump of a server -->
<form method="post" action="export_files.php" name="dump">
<input type="hidden" name="token" value="<?php echo $_SESSION['newtoken']; ?>" />
<input type="hidden" name="export_type" value="server" />
<fieldset><legend class="legendforfieldsetstep" style="font-size: 3em">2</legend>
<div class="fichehalfleft">
<?php
print $langs->trans("BackupDesc2",DOL_DATA_ROOT).'<br>';
print $langs->trans("BackupDescX").'<br><br>';
?>
<label for="zipfilename_template"> <?php echo $langs->trans("FileNameToGenerate"); ?></label><br>
<input type="text" name="zipfilename_template" style="width: 90%"
id="zipfilename_template"
value="<?php
$prefix='documents';
$ext='zip';
$file=$prefix.'_'.$dolibarr_main_db_name.'_'.dol_sanitizeFileName(DOL_VERSION).'_'.strftime("%Y%m%d%H%M").'.'.$ext;
echo $file;
?>" /> <br>
<br>
<?php
// Show compression choices
print '<div class="formelementrow">';
print "\n";
print $langs->trans("Compression").': &nbsp; ';
$filecompression = $compression;
array_shift($filecompression);
$filecompression['zip']= array('function' => 'dol_compress_dir', 'id' => 'radio_compression_zip', 'label' => $langs->trans("FormatZip"));
foreach($filecompression as $key => $val)
{
if (! $val['function'] || function_exists($val['function'])) // Enabled export format
{
print '<input type="radio" name="compression" value="'.$key.'" id="'.$val['id'].'" checked>';
print ' <label for="'.$val['id'].'">'.$val['label'].'</label>';
}
else // Disabled export format
{
print '<input type="radio" name="compression" value="'.$key.'" id="'.$val['id'].'" disabled>';
print ' <label for="'.$val['id'].'">'.$val['label'].'</label>';
print ' ('.$langs->trans("NotAvailable").')';
}
print ' &nbsp; &nbsp; ';
}
print '</div>';
print "\n";
?>
<br>
<div align="center"><input type="submit" class="button"
value="<?php echo $langs->trans("GenerateBackup") ?>" id="buttonGo" /><br>
<br>
</div>
</div>
<div id="backupdatabaseright" class="fichehalfright" style="height:480px; overflow: auto;">
<div class="ficheaddleft">
<?php
$filearray=dol_dir_list($conf->admin->dir_output.'/documents','files',0,'','',$sortfield,(strtolower($sortorder)=='asc'?SORT_ASC:SORT_DESC),1);
$result=$formfile->list_of_documents($filearray,null,'systemtools','',1,'documents/',1,0,$langs->trans("NoBackupFileAvailable"),0,$langs->trans("PreviousDumpFiles"));
print '<br>';
?>
</div>
</div>
</fieldset>
</form>
<?php
// End of page

View File

@ -0,0 +1,190 @@
<?php
/* Copyright (C) 2006-2014 Laurent Destailleur <eldy@users.sourceforge.net>
* Copyright (C) 2011 Juanjo Menent <jmenent@2byte.es>
* Copyright (C) 2015 Raphaël Doursenaud <rdoursenaud@gpcsolutions.fr>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
/**
* \file htdocs/admin/tools/export.php
* \brief Page to export a database into a dump file
*/
require '../../main.inc.php';
require_once DOL_DOCUMENT_ROOT.'/core/lib/admin.lib.php';
require_once DOL_DOCUMENT_ROOT.'/core/lib/files.lib.php';
require_once DOL_DOCUMENT_ROOT.'/core/class/utils.class.php';
require_once DOL_DOCUMENT_ROOT.'/core/class/html.formfile.class.php';
$langs->load("admin");
$action=GETPOST('action','alpha');
$what=GETPOST('what','alpha');
$export_type=GETPOST('export_type','alpha');
$file=GETPOST('zipfilename_template','alpha');
$compression = GETPOST('compression');
$sortfield = GETPOST('sortfield','alpha');
$sortorder = GETPOST('sortorder','alpha');
$page = GETPOST("page",'int');
if (! $sortorder) $sortorder="DESC";
if (! $sortfield) $sortfield="date";
if ($page < 0) { $page = 0; }
elseif (empty($page)) $page = 0;
$limit = GETPOST('limit','int')?GETPOST('limit','int'):$conf->liste_limit;
$offset = $limit * $page;
if (! $user->admin) accessforbidden();
$errormsg='';
/*
* Actions
*/
if ($action == 'delete')
{
$file=$conf->admin->dir_output.'/'.GETPOST('urlfile');
$ret=dol_delete_file($file, 1);
if ($ret) setEventMessages($langs->trans("FileWasRemoved", GETPOST('urlfile')), null, 'mesgs');
else setEventMessages($langs->trans("ErrorFailToDeleteFile", GETPOST('urlfile')), null, 'errors');
$action='';
}
/*
* View
*/
$_SESSION["commandbackuplastdone"]='';
$_SESSION["commandbackuptorun"]='';
$_SESSION["commandbackupresult"]='';
// Increase limit of time. Works only if we are not in safe mode
$ExecTimeLimit=600;
if (!empty($ExecTimeLimit))
{
$err=error_reporting();
error_reporting(0); // Disable all errors
//error_reporting(E_ALL);
@set_time_limit($ExecTimeLimit); // Need more than 240 on Windows 7/64
error_reporting($err);
}
$MemoryLimit=0;
if (!empty($MemoryLimit))
{
@ini_set('memory_limit', $MemoryLimit);
}
$form=new Form($db);
$formfile = new FormFile($db);
//$help_url='EN:Backups|FR:Sauvegardes|ES:Copias_de_seguridad';
//llxHeader('','',$help_url);
//print load_fiche_titre($langs->trans("Backup"),'','title_setup');
// Start with empty buffer
$dump_buffer = '';
$dump_buffer_len = 0;
// We will send fake headers to avoid browser timeout when buffering
$time_start = time();
$outputdir = $conf->admin->dir_output.'/documents';
$result=dol_mkdir($outputdir);
$utils = new Utils($db);
if ($compression == 'zip')
{
$ret = dol_compress_dir(DOL_DATA_ROOT, $outputdir."/".$file, $compression);
if ($ret < 0)
{
$errormsg = $langs->trans("ErrorFailedToWriteInDir",$outputfile);
}
}
elseif (in_array($compression, array('gz', 'bz')))
{
$file = substr($file, 0, strrpos($file, '.'));
$file .= '.tar';
$cmd = 'tar -cf '.$outputdir."/".$file." --exclude=documents/admin/documents -C ".DOL_DATA_ROOT." ".DOL_DATA_ROOT."/../documents/";
exec($cmd, $out, $retval);
//var_dump($cmd, DOL_DATA_ROOT);exit;
if ($retval != 0)
{
$langs->load("errors");
dol_syslog("Documents tar retval after exec=".$retval, LOG_ERR);
$errormsg = 'Error tar generation return '.$retval;
}
else
{
if ($compression == 'gz')
{
$cmd = "gzip " . $outputdir."/".$file;
}
if ($compression == 'bz')
{
$cmd = "bzip2 " . $outputdir."/".$file;
}
exec($cmd, $out, $retval);
if ($retval != 0)
{
$errormsg = 'Error '.$compression.' generation return '.$retval;
unlink($outputdir."/".$file);
}
}
}
if ($errormsg)
{
setEventMessages($langs->trans("Error")." : ".$errormsg, null, 'errors');
$resultstring='';
$resultstring.='<div class="error">'.$langs->trans("Error")." : ".$errormsg.'</div>';
$_SESSION["commandbackupresult"]=$resultstring;
}
else
{
if ($what)
{
setEventMessages($langs->trans("BackupFileSuccessfullyCreated").'.<br>'.$langs->trans("YouCanDownloadBackupFile"), null, 'mesgs');
$resultstring='<div class="ok">';
$resultstring.=$langs->trans("BackupFileSuccessfullyCreated").'.<br>';
$resultstring.=$langs->trans("YouCanDownloadBackupFile");
$resultstring.='<div>';
$_SESSION["commandbackupresult"]=$resultstring;
}
}
print '<br>';
// Redirect t backup page
header("Location: dolibarr_export.php");
$time_end = time();
$db->close();