1
0
mirror of https://github.com/e107inc/e107.git synced 2025-07-30 19:30:25 +02:00

Basic implementation of a system DB backup cron added. Needs testing.

This commit is contained in:
CaMer0n
2012-06-03 09:47:00 +00:00
parent 862dcceebc
commit 3b387741fd
2 changed files with 67 additions and 2 deletions

View File

@@ -47,7 +47,7 @@ class cron_admin extends e_admin_dispatcher
protected $adminMenu = array(
'main/list' => array('caption'=> 'Manage', 'perm' => '0'),
// 'main/create' => array('caption'=> LAN_CREATE, 'perm' => '0'),
'main/refresh' => array('caption'=> "Refresh", 'perm' => '0','url'=>'cron.php'),
// 'main/prefs' => array('caption'=> 'Settings', 'perm' => '0'),
// 'main/custom' => array('caption'=> 'Custom Page', 'perm' => '0')
);
@@ -151,9 +151,16 @@ class cron_admin_ui extends e_admin_ui
'description' => 'Process bounce retriggers<br />Only needed if retriggering of bans enabled.',
'available' => e107::getPref('ban_retrigger')
),
4 => array(
'name' => 'Database Backup',
'category' => 'backup',
'function' => 'dbBackup',
'description' => 'Backup the system database to '.e_SYSTEM.'backups/',
'available' => e107::getPref('ban_retrigger')
),
);
if(!$_GET['action'])
if(!$_GET['action'] || $_GET['action'] == 'refresh')
{
$this->cronImport($cronDefaults); // import Core Crons (if missing)

View File

@@ -93,6 +93,64 @@ class _system_cron
}
}
// Very basic and needs improvement. (for large DBs)
function dbBackup()
{
require(e_BASE."e107_config.php");
$sql = e107::getDb();
$dbtable = $mySQLdefaultdb; // TODO - retrieve this in a better way. (without including e107_config)
$backupFile = e_SYSTEM."backups/".SITENAME."_".date("Y-m-d-H-i-s").".sql";
$result = mysql_list_tables($dbtable);
while ($tab = mysql_fetch_array($result, MYSQL_NUM))
{
$table = $tab[0];
$text = "";
$sql->db_Select_gen("SHOW CREATE TABLE `".$table."`");
$row2 = $sql->db_Fetch();
$text .= $row2['Create Table'];
$text .= ";\n\n";
$sql->db_Select_gen("SELECT * FROM `".$table."`");
$data_array = array();
while($row = $sql->db_Fetch())
{
if(!$fields)
{
$fields = array_keys($row);
}
$d = array();
foreach($fields as $val)
{
$d[] = "'".mysql_real_escape_string($row[$val])."'";
}
$data_array[] = "(".implode(", ",$d).")";
}
$text .= "\nINSERT INTO `".$table."` (`".implode("` ,`",$fields)."`) VALUES \n";
$text .= implode(",\n",$data_array);
$text .= ";\n\n\n";
$c++;
file_put_contents($backupFile,$text,FILE_APPEND);
unset($fields);
}
}
}