Location: PHPKode > projects > Fretsweb > fretsweb-1.2/fretsweb/admin/backup.php
<?php

/*
Fretsweb - A Frets on Fire chart server
Copyright (C) 2009, Daan Sprenkels

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 3 of the License, or
(at your option) any later version.

This program 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 this program.  If not, see <http://www.gnu.org/licenses/>.
*/

require_once "class.backup.php";
include_once "log.php";

//Make sure directory "backups" exists
if(!is_dir("backups"))
{
	if(@mkdir("backups"))
		clog("Made directory \"backups\"");
		$file_handle = fopen("backups/.htaccess");
		fwrite($file_handle);
		fclose($file_handle);
	else
	{
		echo "<b>Sorry but you have a bad host. (You don't have writing permissions.) You must store every backup yourself! You can ignore the strange error messages.</b>";
		clog("Tried to make directory \"backups\", but failed");
	}
}

function use_sql($sql)
{
	$sqls = explode(';' , $sql);
	foreach ($sqls as $eachsql)
		mysql_query(stripslashes($eachsql));
	clog("Used SQL");
	return "Used SQL";
}

function list_backups()
{
	if ($handle = opendir('backups'))
	{
	$i = 0;
		while (false !== ($file = readdir($handle))) {
			if ($file != "." && $file != "..")
			{
				list($file_name , $file_ext) = explode('.', $file);
				if($file_ext == 'sql' && $file_date = date('Y-m-d H:i:s', $file_name))
					$out[$i] = "<input type=\"radio\" name=\"useradio\" value=\"$file_name\">Backup: <a href=\"backups/$file\">$file_date</a><br/>";
			}
			$i++;
		}
		if(isset($out))
		{
			sort($out);
			foreach($out as $o)
				echo $o;
		}
		else
			echo "No backups available.";
	}
	else
		echo '<p class="info">Error while opening directory "backups"</p>';
	@closedir($handle);
}

function create_backup($anddown = false)
{
	$classback = new MySQL_Backup;
	$classback->connected = true;
	$classback->tables = array('contest_config', 'contest_songs', 'contest_players', 'contest_scores', 'contest_forum', 'contest_news');
	$classback->backup_dir = 'backups/';
	$classback->fname_format = 'U';
	$task = MSB_SAVE;
	$output = '';
	if (!$classback->Execute($task, $filename, $use_gzip))
		$output .= $classback->error.'<br/>';
	else
		$output .= 'Backup created<br/>';
	if($anddown)
	{
		$task = MSB_DOWNLOAD;
		if (!$classback->Execute($task, $filename, $use_gzip))
			$output .= $classback->error;
	}
	return $output;
	clog('Wrote backup.');
}

function use_backup($timestamp)
{
	$file_name = dirname(__FILE__) . "/backups/".$timestamp.".sql";
	$file_handle = fopen($file_name, 'r') or die( 'Unable to open backup file.' );
	$file_content = fread($file_handle, filesize($file_name));
	$sqls = explode(';' , $file_content);
	foreach ($sqls as $eachsql)
		mysql_query(stripslashes($eachsql));
	fclose($file_handle);
	clog('Used backup');
	return 'Successfully used a backup.';
}

?>
Return current item: Fretsweb