【php开源代码栏目提醒】:网学会员鉴于大家对php开源代码十分关注,论文会员在此为大家搜集整理了“backup.php”一文,供大家参考学习
<?php
/*
* Nucleus:
PHP/MySQL Weblog CMS (http://nucleuscms.org/)
* Copyright (C) 2002-2005 The Nucleus Group
*
* 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 2
* of the License, or (at your option) any later version.
* (see nucleus/documentation/index.html#license for more info)
*/
/**
* Scripts to create/restore a backup of the Nucleus database
*
* Based on code in phpBB (http://phpBB.sourceforge.net)
*
* @license http://nucleuscms.org/license.txt GNU General Public License
* @copyright Copyright (C) 2002-2005 The Nucleus Group
* @version $Id: backup.php 760 2005-08-15 10:51:09Z dekarma $
*/
/**
* This function creates an sql dump of the database and sends it to
* the user as a file (can be gzipped if they want)
*
* @requires
* no output may have preceded (new headers are sent)
* @param gzip
* 1 = compress backup file, 0 = no compression (default)
*/
function do_backup($gzip = 0) {
global $manager;
// tables of which backup is needed
$tables = array(
sql_table('actionlog'),
sql_table('ban'),
sql_table('blog'),
sql_table('comment'),
sql_table('config'),
sql_table('item'),
sql_table('karma'),
sql_table('member'),
sql_table('skin'),
sql_table('skin_desc'),
sql_table('team'),
sql_table('template'),
sql_table('template_desc'),
sql_table('plugin'),
sql_table('plugin_event'),
sql_table('plugin_option'),
sql_table('plugin_option_desc'),
sql_table('category'),
sql_table('activation'),
sql_table('tickets'),
);
// add tables that plugins want to backup to the list
// catch all output generated by plugins
ob_start();
$res = sql_query('SELECT pfile FROM '.sql_table('plugin'));
while ($plugName = mysql_fetch_object($res)) {
$plug =& $manager->getPlugin($plugName->pfile);
if ($plug) $tables = array_merge($tables, $plug->getTableList());
}
ob_end_clean();
// remove duplicates
$tables = array_unique($tables);
// make sure browsers don't cache the backup
header("Pragma: no-cache");
// don't allow gzip compression when extension is not loaded
if (($gzip != 0) && !extension_loaded("zlib"))
$gzip = 0;
if ($gzip) {
// use an output buffer
@ob_start();
@ob_implicit_flush(0);
// set filename
$filename = 'nucleus_db_backup_'.strftime("%Y%m%d", time()).".sql.gz";
} else {
$filename = 'nucleus_db_backup_'.strftime("%Y%m%d", time()).".sql";
}
// send headers that tell the browser a file is coming
header("Content-Type: text/x-delimtext; name=\"$filename\"");
header("Content-disposition: attachment; filename=$filename");
// dump header
echo "#\n";
echo "# This is a backup file generated by Nucleus \n";
echo "# http://www.nucleuscms.org/\n";
echo "#\n";
echo "# backup-date: " . gmdate("d-m-Y H:i:s", time()) . " GMT\n";
global $nucleus;
echo "# Nucleus CMS version: " . $nucleus['version'] . "\n";
echo "#\n";
echo "# WARNING: Only try to restore on servers running the exact same version of Nucleus\n";
echo "#\n";
// dump all tables
reset($tables);
array_walk($tables, '_backup_dump_table');
if($gzip)
{
$Size = ob_get_length();
$Crc = crc32(ob_get_contents());
$contents = gzcompress(ob_get_contents());
ob_end_clean();
echo "\x1f\x8b\x08\x00\x00\x00\x00\x00".substr($contents, 0, strlen($contents) - 4).gzip_PrintFourChars($Crc).gzip_PrintFourChars($Size);
}
exit;
}
/**
* Creates a dump for a single table
* ($tablename and $key are filled in by array_walk)
*/
function _backup_dump_table($tablename, $key) {
echo "#\n";
echo "# TABLE: " . $tablename . "\n";
echo "#\n";
// dump table structure
_backup_dump_structure($tablename);
// dump table contents
_backup_dump_contents($tablename);
}
function _backup_dump_structure($tablename) {
// add command to drop table on restore
echo "DROP TABLE IF EXISTS $tablename;\n";
echo "CREATE TABLE $tablename(\n";
//
// Ok lets grab the fields...
//
$result = mysql_query("SHOW FIELDS FROM $tablename");
$row = mysql_fetch_array($result);
while ($row) {
echo ' ' . $row['Field'] . ' ' . $row['Type'];
if(!empty($row['Default']))
echo ' DEFAULT \'' . $row['Default'] . '\'';
if($row['Null'] != "YES")
echo ' NOT NULL';
if($row['Extra'] != "")
echo ' ' . $row['Extra'];