sens dump

This commit is contained in:
BB2A Anthony Berton 2022-02-21 15:56:09 +01:00
parent f0f81cd2cd
commit 2595f1f9ee
3 changed files with 97 additions and 0 deletions

View File

@ -1,6 +1,7 @@
<?php
/* Copyright (C) 2016 Laurent Destailleur <eldy@users.sourceforge.net>
* Copyright (C) 2021 Regis Houssin <regis.houssin@inodbox.com>
* Copyright (C) 2022 Anthony Berton <anthony.berton@bb2a.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
@ -1176,4 +1177,96 @@ class Utils
return 1;
}
/**
* Make a send last backup of database or fil in param
* CAN BE A CRON TASK
*
* @param string $sendto Recipients emails
* @param string $from Sender email
* @param string $subject Topic/Subject of mail
* @param string $message Message
* @param string $filename List of files to attach (full path of filename on file system)
* @return int 0 if OK, < 0 if KO (this function is used also by cron so only 0 is OK)
*/
public function sendDumpDatabase($sendto = '', $from = '', $subject = '', $message = '', $filename = '')
{
global $conf, $langs;
$output = '';
if (!empty($from)) {
$from = dol_escape_htmltag($from);
} elseif (!empty($conf->global->MAIN_INFO_SOCIETE_MAIL)) {
$from = dol_escape_htmltag($conf->global->MAIN_INFO_SOCIETE_MAIL);
} else {
$error++;
}
if (!empty($sendto)) {
$sendto = dol_escape_htmltag($sendto);
} else {
$error++;
}
if (!empty($subject)) {
$subject = dol_escape_htmltag($subject);
} else {
$subject = dol_escape_htmltag($langs->trans('MakeSendLocalDatabaseDumpShort'));
}
if (empty($message)) {
$message = dol_escape_htmltag($langs->trans('MakeSendLocalDatabaseDumpShort'));
}
require_once DOL_DOCUMENT_ROOT.'/core/lib/files.lib.php';
if ($filename) {
if (dol_is_file($conf->admin->dir_output.'/backup/'.$filename)) {
$tmpfiles = dol_most_recent_file($conf->admin->dir_output.'/backup', $filename);
}
} else {
$tmpfiles = dol_most_recent_file($conf->admin->dir_output.'/backup');
}
if ($tmpfiles) {
foreach ($tmpfiles as $key => $val) {
if ($key == 'fullname') {
$filepath = array($val);
$filesize = dol_filesize($val);
}
if ($key == 'type') {
$mimetype = array($val);
}
if ($key == 'relativename') {
$filename = array($val);
}
}
}
if ($filepath) {
if ($filesize > 100000000) {
$error++;
}
} else {
$error++;
}
if (!$error) {
include_once DOL_DOCUMENT_ROOT . '/core/class/CMailFile.class.php';
$mailfile = new CMailFile($subject, $sendto, $from, $message, $filepath, $mimetype, $filename, '', '', 0, -1);
if ($mailfile->error) {
$error++;
} else {
$result = $mailfile->sendfile();
}
}
dol_syslog(__METHOD__, LOG_DEBUG);
$this->error = $error;
$this->output = $output;
if ($result == true) {
return 0;
} else {
return $result;
}
}
}

View File

@ -1,6 +1,7 @@
<?php
/* Copyright (C) 2012 Nicolas Villa aka Boyquotes http://informetic.fr
* Copyright (C) 2013 Florian Henry <florian.henry@open-concept.pro>
* Copyright (C) 2022 Anthony Berton <anthony.berton@bb2a.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
@ -99,6 +100,7 @@ class modCron extends DolibarrModules
$this->cronjobs = array(
0=>array('entity'=>0, 'label'=>'PurgeDeleteTemporaryFilesShort', 'jobtype'=>'method', 'class'=>'core/class/utils.class.php', 'objectname'=>'Utils', 'method'=>'purgeFiles', 'parameters'=>'tempfilesold+logfiles', 'comment'=>'PurgeDeleteTemporaryFiles', 'frequency'=>2, 'unitfrequency'=>3600 * 24 * 7, 'priority'=>50, 'status'=>1, 'test'=>true),
1=>array('entity'=>0, 'label'=>'MakeLocalDatabaseDumpShort', 'jobtype'=>'method', 'class'=>'core/class/utils.class.php', 'objectname'=>'Utils', 'method'=>'dumpDatabase', 'parameters'=>'none,auto,1,auto,10', 'comment'=>'MakeLocalDatabaseDump', 'frequency'=>1, 'unitfrequency'=>3600 * 24 * 7, 'priority'=>90, 'status'=>0, 'test'=>in_array($this->db->type, array('mysql', 'mysqli'))),
2=>array('entity'=>0, 'label'=>'MakeSendLocalDatabaseDumpShort', 'jobtype'=>'method', 'class'=>'core/class/utils.class.php', 'objectname'=>'Utils', 'method'=>'sendDumpDatabase', 'parameters'=>'', 'comment'=>'MakeSendLocalDatabaseDump', 'frequency'=>1, 'unitfrequency'=>604800, 'priority'=>91, 'status'=>0, 'test'=>in_array($this->db->type, array('mysql', 'mysqli'))),
// 1=>array('entity'=>0, 'label'=>'My label', 'jobtype'=>'command', 'command'=>'', 'parameters'=>'', 'comment'=>'Comment', 'frequency'=>1, 'unitfrequency'=>3600*24)
);

View File

@ -82,6 +82,8 @@ UseMenuModuleToolsToAddCronJobs=Go into menu "<a href="%s">Home - Admin tools -
JobDisabled=Job disabled
MakeLocalDatabaseDumpShort=Local database backup
MakeLocalDatabaseDump=Create a local database dump. Parameters are: compression ('gz' or 'bz' or 'none'), backup type ('mysql', 'pgsql', 'auto'), 1, 'auto' or filename to build, number of backup files to keep
MakeSendLocalDatabaseDumpShort=Send local database backup
MakeSendLocalDatabaseDump=Send local datebase backup to mail. Parameters are: to, from, subject, message , filename
WarningCronDelayed=Attention, for performance purpose, whatever is next date of execution of enabled jobs, your jobs may be delayed to a maximum of %s hours, before being run.
DATAPOLICYJob=Data cleaner and anonymizer
JobXMustBeEnabled=Job %s must be enabled