Commit a586b2c5 authored by Jean-Paul Saman's avatar Jean-Paul Saman

Move do_backup.php and do_restore.php into backup.class.php

parent 57e496ef
......@@ -29,6 +29,9 @@ if (!defined('W2P_BASE_DIR')) {
die('You should not access this file directly.');
}
require_once "$baseDir/lib/adodb/adodb-xmlschema.inc.php";
include_once $baseDir . '/modules/backup/zip.lib.php';
/**
* The Backup class
*/
......@@ -61,4 +64,195 @@ class CBackup extends w2p_Core_BaseObject {
}
*/
public function backup($output_format, $drop_table, $export_what)
{
if ($output_format == 'xml') {
$schema = new adoSchema($GLOBALS['db']);
$output = $schema->ExtractSchema(($export_what == 'table') ? false : true);
} else {
// Build the SQL manually.
$db->setFetchMode(ADODB_FETCH_NUM);
$alltables = $db->MetaTables('TABLES');
$output = '';
$output .= '# Backup of database \'' . $w2PgetParam['dbname'] . '\'' . "\r\n";
$output .= '# Generated on ' . date('j F Y, H:i:s') . "\r\n";
$output .= '# OS: ' . PHP_OS . "\r\n";
$output .= '# PHP version: ' . PHP_VERSION . "\r\n";
if ($w2PgetParam['dbtype'] == 'mysql')
$output .= '# MySQL version: ' . mysql_get_server_info() . "\r\n";
$output .= "\r\n";
$output .= "\r\n";
// fetch all tables on by one
foreach ($alltables as $table)
{
// introtext for this table
$output .= '# TABLE: ' . $table . "\r\n";
$output .= '# --------------------------' . "\r\n";
$output .= '#' . "\r\n";
$output .= "\r\n";
if ($drop_table)
{
// drop table
$output .= 'DROP TABLE IF EXISTS `' . $table . '`;' . "\r\n";
$output .= "\r\n";
}
if ($export_what != 'data')
{
// structure of the table
$rs = $db->Execute('SELECT * FROM ' . $table . ' WHERE -1');
$fields = $db->MetaColumns($table);
$indexes = $db->MetaIndexes($table);
$output .= 'CREATE TABLE `' . $table . '` (' . "\r\n";
$primary = array();
$first = true;
if (is_array($fields)) {
foreach ($fields as $details) {
if ($first)
$first = false;
else
$output .= ",\r\n";
if ($details->primary_key)
$primary[] = $details->name;
$output .= ' `' . $details->name . '` ' . $details->type;
if ($details->max_length > -1) {
$output .= '(' . $details->max_length;
if (isset($details->scale))
$output .= ',' . $details->scale;
$output .= ')';
}
if ($details->not_null)
$output .= ' NOT NULL';
if ($details->has_default)
$output .= ' DEFAULT ' . "'$details->default_value'";
if ($details->auto_increment)
$output .= ' auto_increment';
}
}
if (is_array($indexes)) {
foreach ($indexes as $index => $details) {
if ($first)
$first = false;
else
$output .= ",\r\n";
$output .= ' ';
if ($details['unique'])
$output .= 'UNIQUE ';
$output .= 'KEY `' . $index . '` ( `' . implode('`, `', $details['columns'] ) . '` )';
}
}
if (count($primary)) {
$output .= "\r\n" . ' PRIMARY KEY ( `'. implode('`, `', $primary) . '` )';
}
$output .= "\r\n" . ');' . "\r\n\r\n";
}
if ($export_what != 'table')
{
// all data from table
$db->setFetchMode(ADODB_FETCH_ASSOC) ;
$result = $db->Execute('SELECT * FROM '.$table);
while($tablerow = $result->fetchRow())
{
$output .= 'INSERT INTO `'.$table.'` ( `' . implode('`, `', array_keys($tablerow)) . '` )' . "\r\n";
$output .= ' VALUES (';
$first = true;
foreach ($tablerow as $value) {
if ($first)
$first = false;
else
$output .= ',';
// remove all enters from the field-string. MySql stamement must be on one line
$value = str_replace("\r\n",'\n',$value);
$value = str_replace("\n", '\n', $value); // Just in case there are unadorned newlines.
// replace ' by \'
$value = str_replace('\'',"\'",$value);
$output .= '\''.$value.'\'';
}
$output .= ');' . "\r\n";
} // while
$output .= "\r\n";
$output .= "\r\n";
}
}
//}
switch ($output_format) {
case 'xml':
header('Content-Disposition: attachment; filename="backup.xml"');
header('Content-Type: text/xml');
echo $output;
break;
case 'zip':
header('Content-Disposition: inline; filename="backup.zip"');
header('Content-Type: application/x-zip');
$zip = new zipfile;
$zip->addFile($output,'backup.sql');
echo $zip->file();
break;
case 'sql':
header('Content-Disposition: inline; filename="backup.sql"');
header('Content-Type: text/sql');
echo $output;
break;
}
return true;
}
public function restore()
{
$upload_tmp_file = $_FILES['xmlfile']['tmp_name'];
$continue = w2PgetParam($_POST, 'continue', false);
$schema = new adoSchema($GLOBALS['db']);
$schema->setUpgradeMethod('REPLACE');
$schema->ContinueOnError(true);
if (($sql = $schema->ParseSchemaFile($upload_tmp_file)) == false) {
$AppUI->setMsg('Error in parsing XML file', UI_MSG_ERR);
$AppUI->redirect();
}
$result = 0;
$AppUI->setMsg('');
$errs = array();
echo '<pre>' . "\n";
foreach ($sql as $query) {
if ( $db->Execute($query)) {
if (! $result)
$result = 2;
} else {
echo 'Error in Query: ' . $query . "\n";
echo 'Error: ' . $db->ErrorMsg() . "\n";
if (! $continue) {
$result = 0;
break;
} else {
$result = 1;
}
}
}
echo "</pre>\n";
switch ($result) {
case 0:
echo "<B>" . $AppUI->_('Failed to restore backup') . "</b>";
break;
case 1:
echo "<B>" . $AppUI->_('Backup restored, but with errors') . "</b>";
break;
case 2:
echo "<B>" . $AppUI->_('Backup Restored OK') . "</b>";
break;
}
echo '<br/><b>' . $AppUI->_('xmlLoginMsg') . '</b>';
if ($result == 0)
return false;
return true;
}
?>
<?php
// backup database module for dotProject
// (c)2003 Daniel Vijge
// Licensed under GNU/GPL v2 or later
// Based on the work of the phpMyAdmin
// (c)2001-2002 phpMyAdmin group [http://www.phpmyadmin.net]
// Completely rewritten for 2.0 by Adam Donnison <ajdonnison@dotproject.net>
$perms =& $AppUI->acl();
if (! $perms->checkModule('backup', 'view'))
$AppUI->redirect('m=public&a=access_denied');
$export_what = w2PgetParam($_POST, 'export_what');
$output_format = w2PgetParam($_POST, 'output_format');
$droptable = w2PgetParam($_POST, 'droptable', false);
$valid_export_options = array('all', 'table', 'data');
$valid_output_formats = array('xml', 'zip', 'sql');
if (! in_array($export_what, $valid_export_options)
|| ! in_array($output_format, $valid_output_formats)) {
$AppUI->setMsg('Invalid Options', UI_MSG_ERR);
$AppUI->redirect('m=public&a=access_denied');
}
require_once "$baseDir/lib/adodb/adodb-xmlschema.inc.php";
if ($output_format == 'xml') {
$schema = new adoSchema($GLOBALS['db']);
$output = $schema->ExtractSchema(($export_what == 'table') ? false : true);
} else {
// Build the SQL manually.
$db->setFetchMode(ADODB_FETCH_NUM);
$alltables = $db->MetaTables('TABLES');
$output = '';
$output .= '# Backup of database \'' . $dPconfig['dbname'] . '\'' . "\r\n";
$output .= '# Generated on ' . date('j F Y, H:i:s') . "\r\n";
$output .= '# OS: ' . PHP_OS . "\r\n";
$output .= '# PHP version: ' . PHP_VERSION . "\r\n";
if ($dPconfig['dbtype'] == 'mysql')
$output .= '# MySQL version: ' . mysql_get_server_info() . "\r\n";
$output .= "\r\n";
$output .= "\r\n";
// fetch all tables on by one
foreach ($alltables as $table)
{
// introtext for this table
$output .= '# TABLE: ' . $table . "\r\n";
$output .= '# --------------------------' . "\r\n";
$output .= '#' . "\r\n";
$output .= "\r\n";
if ($drop_table)
{
// drop table
$output .= 'DROP TABLE IF EXISTS `' . $table . '`;' . "\r\n";
$output .= "\r\n";
}
if ($export_what != 'data')
{
// structure of the table
$rs = $db->Execute('SELECT * FROM ' . $table . ' WHERE -1');
$fields = $db->MetaColumns($table);
$indexes = $db->MetaIndexes($table);
$output .= 'CREATE TABLE `' . $table . '` (' . "\r\n";
$primary = array();
$first = true;
if (is_array($fields)) {
foreach ($fields as $details) {
if ($first)
$first = false;
else
$output .= ",\r\n";
if ($details->primary_key)
$primary[] = $details->name;
$output .= ' `' . $details->name . '` ' . $details->type;
if ($details->max_length > -1) {
$output .= '(' . $details->max_length;
if (isset($details->scale))
$output .= ',' . $details->scale;
$output .= ')';
}
if ($details->not_null)
$output .= ' NOT NULL';
if ($details->has_default)
$output .= ' DEFAULT ' . "'$details->default_value'";
if ($details->auto_increment)
$output .= ' auto_increment';
}
}
if (is_array($indexes)) {
foreach ($indexes as $index => $details) {
if ($first)
$first = false;
else
$output .= ",\r\n";
$output .= ' ';
if ($details['unique'])
$output .= 'UNIQUE ';
$output .= 'KEY `' . $index . '` ( `' . implode('`, `', $details['columns'] ) . '` )';
}
}
if (count($primary)) {
$output .= "\r\n" . ' PRIMARY KEY ( `'. implode('`, `', $primary) . '` )';
}
$output .= "\r\n" . ');' . "\r\n\r\n";
}
if ($export_what != 'table')
{
// all data from table
$db->setFetchMode(ADODB_FETCH_ASSOC) ;
$result = $db->Execute('SELECT * FROM '.$table);
while($tablerow = $result->fetchRow())
{
$output .= 'INSERT INTO `'.$table.'` ( `' . implode('`, `', array_keys($tablerow)) . '` )' . "\r\n";
$output .= ' VALUES (';
$first = true;
foreach ($tablerow as $value) {
if ($first)
$first = false;
else
$output .= ',';
// remove all enters from the field-string. MySql stamement must be on one line
$value = str_replace("\r\n",'\n',$value);
$value = str_replace("\n", '\n', $value); // Just in case there are unadorned newlines.
// replace ' by \'
$value = str_replace('\'',"\'",$value);
$output .= '\''.$value.'\'';
}
$output .= ');' . "\r\n";
} // while
$output .= "\r\n";
$output .= "\r\n";
}
}
}
switch ($output_format) {
case 'xml':
header('Content-Disposition: attachment; filename="backup.xml"');
header('Content-Type: text/xml');
echo $output;
break;
case 'zip':
header('Content-Disposition: inline; filename="backup.zip"');
header('Content-Type: application/x-zip');
include_once $baseDir . '/modules/backup/zip.lib.php';
$zip = new zipfile;
$zip->addFile($output,'backup.sql');
echo $zip->file();
break;
case 'sql':
header('Content-Disposition: inline; filename="backup.sql"');
header('Content-Type: text/sql');
echo $output;
break;
}
?>
<?php
// Copyright 2005, Adam Donnison <adam@saki.com.au>
// Released under GPL version 2 or later
// Restores an XML file.
$perms =& $AppUI->acl();
if (! $perms->checkModule('backup', 'edit'))
$AppUI->redirect('m=public&a=access_denied');
// Try restoring the XML file.
if (! isset($_FILES['xmlfile'])) {
$AppUI->setMsg('No upload file', UI_MSG_ERR);
$AppUI->redirect();
}
$upload_tmp_file = $_FILES['xmlfile']['tmp_name'];
$continue = w2PgetParam($_POST, 'continue', false);
require_once $baseDir . '/lib/adodb/adodb-xmlschema.inc.php';
$schema = new adoSchema($GLOBALS['db']);
$schema->setUpgradeMethod('REPLACE');
$schema->ContinueOnError(true);
if (($sql = $schema->ParseSchemaFile($upload_tmp_file)) == false) {
$AppUI->setMsg('Error in parsing XML file', UI_MSG_ERR);
$AppUI->redirect();
}
$result = 0;
$AppUI->setMsg('');
$errs = array();
echo '<pre>' . "\n";
foreach ($sql as $query) {
if ( $db->Execute($query)) {
if (! $result)
$result = 2;
} else {
echo 'Error in Query: ' . $query . "\n";
echo 'Error: ' . $db->ErrorMsg() . "\n";
if (! $continue) {
$result = 0;
break;
} else {
$result = 1;
}
}
}
echo "</pre>\n";
switch ($result) {
case 0:
echo "<B>" . $AppUI->_('Failed to restore backup') . "</b>";
break;
case 1:
echo "<B>" . $AppUI->_('Backup restored, but with errors') . "</b>";
break;
case 2:
echo "<B>" . $AppUI->_('Backup Restored OK') . "</b>";
break;
}
echo '<br/><b>' . $AppUI->_('xmlLoginMsg') . '</b>';
exit;
?>
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment