Location: PHPKode > scripts > Wordpress Backup > backuper/backuper.php
<?php



/*



	Plugin Name: Backuper



	Description: Backup your Wordpress site with this small, easy and simple plugin.



	Author: WPbackup.org



	Version: 1.0



	Author URI: http://www.wpbackup.org/



	Plugin URI: http://www.wpbackup.org/



*/







define("WPBACKUP_OPTIONS","wpbackup_options");



define("WPBACKUP_TIMER","wpbackup_timer");







//main function



add_action('init','wpbackup_cron');







//admin menu



add_action('admin_menu', "wpbackup_populate_menu");



function wpbackup_populate_menu()



{



	add_submenu_page("options-general.php", "Backuper", 'Backuper', 'export', __FILE__, "wpbackup_form");



}







function wpbackup_timer($value = null)



{



	if ($value !== null)



	{



		if (!update_option(WPBACKUP_TIMER,$value))



			add_option(WPBACKUP_TIMER,$value);



	}



	



	return get_option(WPBACKUP_TIMER,0);



}







function wpbackup_options($value=null)



{



	if ($value !== null)



	{



		$current = get_option(WPBACKUP_OPTIONS,array());



		



		if (!update_option(WPBACKUP_OPTIONS,$value))



			add_option(WPBACKUP_OPTIONS,$value);







		if ( ($current['when'] != $value['when']) || ($current['when']==-1) )



		{



			//user changed period of backing up



			$timer = time()+$value['when'];



			wpbackup_timer($timer);



			wpbackup_cron();



		}



		



	}		



	



	$result = get_option(WPBACKUP_OPTIONS,array());



	



	if (empty($result))



	{//setting defaults



		$result['what'] = 'everything';



		$result['when'] = '86400';



		$result['where'] = 'email';



		$result['notify'] = 'yes';



		$result['email'] = get_option('admin_email');



	}



	



	return $result;



}







function wpbackup_form()



{



	echo "<div class=\"wrap\">";



	screen_icon();



	echo "<h2>Settings Page for Backuper <font style='font-size: 14px;'>by wpbackup.org</font></h2>\n";



	$nonce = 'wordpress-backup';







	if (!empty($_POST))



	{



		if (!wp_verify_nonce($_POST['_wpnonce'],$nonce))



			wpbackup_message(__("Failed to verify form submission. Please retry."),"error");



		else



		{



			if (!wpbackup_validemail($_POST['wpbackup']['email']))



				wpbackup_message(__("Invalid email address."),"error");



			else



			{



				wpbackup_options($_POST['wpbackup']);



				wpbackup_message(__("Changes have been saved"));



			}



		}



	}







	$options = wpbackup_options();



	



	$timer = wpbackup_timer();



	$next = ($timer) ? "Next backup scheduled at ".date("Y-m-d H:i:s",$timer+get_option('gmt_offset')*3600) : "No backup schedule defined";



	



	echo "<form action='' method='post' enctype='multipart/form-data'>";



	wp_nonce_field($nonce);







	echo '<div id="poststuff" style="margin-top:10px;">';



	



	wpbackup_start_block("About <strong>Backuper</strong>");



	echo "This plugin is used to backup your wordpress blog, easy and simple.<br /><br />";



	echo 'Need support? <a target="_blank" href="http://www.wpbackup.org/contact">Please use our support page</a><br /><br />';



	echo "<strong>$next</strong>";



	wpbackup_end_block();







	wpbackup_start_block("What do you want to backup?");



	echo "<label><input type=radio name='wpbackup[what]' value='everything'".wpbackup_checked($options['what'],'everything')."> Everything / Full-backup (recommended)</label><br /><br />";



	echo "<label><input type=radio name='wpbackup[what]' value='database'".wpbackup_checked($options['what'],'database')."> Only the database</label>";



	wpbackup_end_block();







	wpbackup_start_block("How often do you want to backup?");



	echo "<label><input type=radio name='wpbackup[when]' value='3600'".wpbackup_checked($options['when'],3600)."> Every hour</label><br /><br />";



	echo "<label><input type=radio name='wpbackup[when]' value='43200'".wpbackup_checked($options['when'],43200)."> Twice per day</label><br /><br />";



	echo "<label><input type=radio name='wpbackup[when]' value='86400'".wpbackup_checked($options['when'],86400)."> Daily (recommended)</label><br /><br />";



	echo "<label><input type=radio name='wpbackup[when]' value='604800'".wpbackup_checked($options['when'],604800)."> Weekly</label><br /><br />";



	echo "<label><input type=radio name='wpbackup[when]' value='-1'".wpbackup_checked($options['when'],-1)."> Just this one time</label>";



	wpbackup_end_block();







	wpbackup_start_block("Where do you want to save the backups?");



	echo "<label><input type=radio name='wpbackup[where]' value='email'".wpbackup_checked($options['where'],'email')."> Send to email</label><br />";



	echo "<p>Email address to send backups to:<br />";



	echo "<label><input type=text style='width:200px;text-align:left' name='wpbackup[email]' value=\"".htmlspecialchars($options['email'])."\"></label></p>";



	wpbackup_end_block();



	



	echo "<p class=\"submit\" style='text-align:left'><input class=\"button-primary\" type=\"submit\" name=\"Submit\" value=\"".__('Save Options')."\" /></p>";



	



	echo '</div>';







	//echo "<p><strong>Do you want to receive an email when a backup is completed?</strong></p>";



	//echo "<label><input type=radio name='wpbackup[notify]' value='yes'".wpbackup_checked($options['notify'],'yes')."> Yes (recommended)</label><br />";



	//echo "<label><input disabled type=radio name='wpbackup[notify]' value='no'".wpbackup_checked($options['notify'],'no')."> No</label><br />";











	echo "</form>";



	echo "</div>";



}







function wpbackup_cron()



{



	$now = time();



	$timer = wpbackup_timer();



	if (!$timer)



		return; //no backup scheduled



	if ($timer > $now)



		return; //not time to do the job yet



	$options = wpbackup_options();



	wpbackup_backup($options);



	if ($options['when']==-1)



		wpbackup_timer(0);



	else



		wpbackup_timer($now+$options['when']);



}







function wpbackup_backup($options)



{



	$now = date("Ymd_His",time()+get_option('gmt_offset')*3600);



	$host = strtolower(getenv("HTTP_HOST"));



	



	$generated = array();



	



	$temp_dir = get_temp_dir();



	$backup_sql = $temp_dir."/".DB_NAME.".$now.sql";



	



	require_once(ABSPATH . 'wp-admin/includes/class-pclzip.php');



	



	if ($options['what']=="everything")



	{



		$dirs_to_backup = array();



		$d = wp_upload_dir();



		$dirs_to_backup[] = $d['basedir'];



		$dirs_to_backup[] = WP_PLUGIN_DIR;



		$dirs_to_backup[] = WP_CONTENT_DIR."/themes";



		



		$target = $temp_dir."/$host-$now-backup.zip";







		$archive = new PclZip($target);



		if ($result = $archive->create($dirs_to_backup,PCLZIP_OPT_REMOVE_PATH,WP_CONTENT_DIR))



			$generated[] = $target;



	}



	



	if (true) //maybe will make database backup optional



	{



		global $wpdb;



		wpbackup_dump_database($backup_sql,$wpdb->prefix);



		



		$target = $temp_dir."/$host-$now-database_backup.zip";



		



		$archive = new PclZip($target);



		if ($result = $archive->create($backup_sql,PCLZIP_OPT_REMOVE_PATH,$temp_dir))



		{



			$generated[] = $target;



			@unlink($backup_sql);



		}



	}







	wp_mail($options['email'], "[Backuper] $host", "Your wordpress blog has been backed up, file(s) attached :)", array(), $generated);



	foreach ($generated as $garbage)



		unlink($garbage);



}







function wpbackup_dump_database($filename,$prefix="")



{



	$mh = mysql_connect(DB_HOST,DB_USER,DB_PASSWORD,true);



	



	if (!$mh)



		return;







	if (!mysql_select_db (DB_NAME,$mh))



		return;



	



	$tables = mysql_query("SET NAMES ".DB_CHARSET,$mh);







	$tables = mysql_query("show tables",$mh);







	$f = fopen($filename,"w");



	fputs($f,"-- Backup generated by Backuper (wpbackup.org)\n\n");



	while (list($table,) = mysql_fetch_row($tables))



	{



		if ($prefix && (substr($table,0,strlen($prefix))!=$prefix))



			continue;



				



		fputs($f,  "-- Backup of `$table`\n\n");







		fputs($f,  "-- Structure of `$table`\n");







		$res = mysql_query("show create table `$table`",$mh);



		list (,$structure) = mysql_fetch_row($res);



		fputs($f,  "$structure;\n\n");







		fputs($f,  "-- Data of `$table`\n");







		//generating field names



		$res = mysql_query("SHOW COLUMNS from `$table`",$mh);



		$columns = array();



		while (list($column,) = mysql_fetch_row($res))



			$columns[] = "`$column`";



		$keys = implode (",",$columns);







		$res = mysql_query("select * from `$table`",$mh);



		while ($row = mysql_fetch_row($res))



		{



			array_walk($row,"wpbackup_mslash");



			fputs($f,  "INSERT INTO `$table` ($keys) VALUES (".implode(",",$row).");\n");



		}







		fputs($f, "--Backup of `$table` ends\n\n");



	}



	mysql_close($mh);



	fclose($f);



}







function wpbackup_checked($value1, $value2)



{



	return ($value1==$value2) ? " checked='checked'" : "";



}







function wpbackup_mslash(&$s) 



{



	if ($s===null)



		$s = "null";



	else



		$s = "'".addcslashes ($s,"\"\'\\\r\n\t")."'";



}











function wpbackup_message($text, $type='updated')



{



	echo "<div class='$type fade'><p>$text</p></div>";



}







function wpbackup_validemail($email)



{



	//checks if $email address is correct



	return (eregi("^[_\.0-9a-z-]+@([0-9a-z][-0-9a-z\.]+)\.([a-z]{2,6}$)", $email, $check));



}







function wpbackup_start_block($title)



{



?>



<div class="postbox">



<h3 class="hndle"><span><?php echo $title?></span></h3>



<div class="inside">



<?php



}







function wpbackup_end_block()



{



?>



</div>



</div>



<?php



}







?>
Return current item: Wordpress Backup