Location: PHPKode > scripts > Yet Another Link Directory > yald2/admin/backup.php
<?php
/******************************************************************************
* This file is part of Yet Another Link Directory.                            *
*                                                                             *
* Yet Another Link Directory 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.                                         *
*                                                                             *
* Yet Another Link Directory 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 Yet Another Link Directory; if not, write to the Free Software   *
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA  *
******************************************************************************/

require('../inc/config.php');
require('../inc/functions.php');
require('../inc/version.php');
require('../inc/backup.class.php');
mysql_connect($mysql['host'],$mysql['username'],$mysql['password']);
mysql_select_db($mysql['db']);

$settings = getSettings();

session_start();

if(!isset($_SESSION['yald_admin_logged_in'])){
	header('Location: login.php');
	exit;
}

$template = file_get_contents('template.html');

$yald_body = '';

function parse_dump($filename,$compress){
	// Temporary variable, used to store current query
	$templine = null;
	$success = 0;
	// Read in entire file
	if($compress){
		$lines = gzfile($filename);
	} else {
		$lines = file($filename);
	}
	// Loop through each line
	foreach ($lines as $line_num => $line) {
		// Only continue if it's not a comment
		if (substr($line, 0, 2) != '--' && $line != '') {
			// Add this line to the current segment
			$templine .= $line;
			// If it has a semicolon at the end, it's the end of the query
			if (substr(trim($line), -1, 1) == ';') {
				// Perform the query
				if(!mysql_query($templine)){
					// there was an error, stop parsing the file!
					return $success; //print('Error performing query \'<b>' . $templine . '</b>\': ' . mysql_error() . '<br />');
				} else {
					$success++;
				}
				$templine = null;
			}
		}
	}

	// everything went ok
	return true;
}


if(isset($_GET['download'])){
	$compress = false;
	$backup_date = date('r');
	$filehead = "--\n-- MySQL database backup generated by Yet Another Link Directory version {$yald_version_number} (build {$yald_build_number})\n-- Generated on {$backup_date}\n--\n\n";

	$filename = '../tmp/'.rand(100000,999999).'.sql';
	$dispname = "yald_b{$yald_build_number}_backup.sql";

	if($_GET['type']=='gz'){
		$filename .= '.gz';
		$dispname .= '.gz';
		$compress = true;
	}

	header("Content-type: application/octet-stream");
	header("Content-Disposition: attachment; filename=$dispname");

	$dumper = new MySQLDump(null,$filename,$compress,false);
	$dumper->setFileHead($filehead);
	$dumper->doDump();

	readfile($filename);

	unlink($filename);

	exit;
}

if(isset($_POST['restore'])){

	$filepath = $_FILES['restore']['tmp_name'];
	$filename = $_FILES['restore']['name'];
	$ext = substr(strrchr(basename($filename), '.'), 1);
	switch($ext){
		case 'gz':
			$parse = parse_dump($filepath,true);
			break;
		case 'sql':
			$parse = parse_dump($filepath,false);
			break;
	}

	if(isset($parse)){
		if($parse == true){
			$error = 'The database was successfully restored.';
		} else {
			$error = 'There was an error restoring the database.';
			if($parse > 0) {
				$error .= ' Some changes were made. This could have corrupted the database.';
			} else {
				$error .= ' No changes were made.';
			}
		}
	} else {
		$error = 'The file uploaded has an unsupported extension.';
	}
	
	unlink($filepath);

}

if(isset($error)){
	$yald_body .= '<div align="center"><div class="errorbox"><b>'.$error.'</b></div></div>';
}

$yald_body .= 'This page allows you to backup or restore your entire YALD database. To get a current backup file, click the button below. To restore an older database, upload the old backup file. It is not suggested that you restore a database from a previous version of YALD.<br />
<br /><form action="'.$_SERVER['PHP_SELF'].'" method="get"><fieldset><legend><b>Make a backup:</b></legend>
<b>File type:</b> compressed (.gz) <input type="radio" name="type" value="gz" checked="checked" /> text (.sql) <input type="radio" name="type" value="sql" />
<input type="hidden" name="download" value="1" /><br /><input type="submit" name="" value="Download Backup File" /></fieldset></form><br /><br />
<form action="'.$_SERVER['PHP_SELF'].'" method="post" enctype="multipart/form-data" onsubmit="return confirm(\'Restoring this database will completely overwrite the current database. Do you wish to proceed?\')"><fieldset><legend><b>Restore previous backup:</b></legend>The backup file type will be automatically detected.<br /><br />
  <input type="file" name="restore" /><input type="hidden" name="restore" value="1" /><input type="submit" name="" value="Restore" /></fieldset>
</form>';

$yald_head = '';

$template = admin_output('stats');

print $template;

?>
Return current item: Yet Another Link Directory