File: 1.03.1a/server/web/admin.backup.php (View as HTML)

  1: <?php
  2: /* -------------------------------------------------------------
  3: This file is part of FreeNATS
  4: 
  5: FreeNATS is (C) Copyright 2008 PurplePixie Systems
  6: 
  7: FreeNATS is free software: you can redistribute it and/or modify
  8: it under the terms of the GNU General Public License as published by
  9: the Free Software Foundation, either version 3 of the License, or
 10: (at your option) any later version.
 11: 
 12: FreeNATS is distributed in the hope that it will be useful,
 13: but WITHOUT ANY WARRANTY; without even the implied warranty of
 14: MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 15: GNU General Public License for more details.
 16: 
 17: You should have received a copy of the GNU General Public License
 18: along with FreeNATS.  If not, see www.gnu.org/licenses
 19: 
 20: For more information see www.purplepixie.org/freenats
 21: -------------------------------------------------------------- */
 22: 
 23: ob_start();
 24: require("include.php");
 25: $NATS->Start();
 26: if (!$NATS_Session->Check($NATS->DB))
 27: 	{
 28: 	header("Location: ./?login_msg=Invalid+Or+Expired+Session");
 29: 	exit();
 30: 	}
 31: if ($NATS_Session->userlevel<9) UL_Error("Backup Manager");
 32: 
 33: if (isset($_REQUEST['action']))
 34: 	{
 35: 	switch ($_REQUEST['action'])
 36: 		{
 37: 		case "backup":
 38: 		
 39: 		header("Content-type: text/sql");
 40: 		if (isset($_REQUEST['filename'])) $filename=$_REQUEST['filename'];
 41: 		else $filename="freenats-".date("Ymd").".sql";
 42: 		header("Content-Disposition: attachment; filename=".$filename);
 43: 		
 44: 		echo "-- FreeNATS SQL Data Backup\n";
 45: 		echo "-- ".date("Y-m-d H:i:s")."\n\n";
 46: 		echo "-- Warning: Only use on FreeNATS ".$NATS->Version." or later schemas!\n\n";
 47: 		foreach($_REQUEST['table'] as $table)
 48: 			{
 49: 			echo "-- ".$table."\n";
 50: 			if (isset($_REQUEST['truncate_'.$table]))
 51: 				echo "TRUNCATE TABLE `".$table."`;\n";
 52: 			$q="SELECT * FROM ".ss($table);
 53: 			$r=$NATS->DB->Query($q);
 54: 			while ($row=$NATS->DB->Fetch_Array($r))
 55: 				{
 56: 				$keys=array();
 57: 				$vals=array();
 58: 				foreach($row as $key => $val)
 59: 					{
 60: 					if (!is_numeric($key))
 61: 						{
 62: 						$keys[]=$key;
 63: 						$vals[]=$val;
 64: 						}
 65: 					}
 66: 				$uq="INSERT INTO `".$table."`(";
 67: 				$first=true;
 68: 				foreach($keys as $key)
 69: 					{
 70: 					if ($first) $first=false;
 71: 					else $uq.=",";
 72: 					$uq.="`".$key."`";
 73: 					}
 74: 				$uq.=") VALUES(";
 75: 				$first=true;
 76: 				foreach($vals as $val)
 77: 					{
 78: 					if ($first) $first=false;
 79: 					else $uq.=",";
 80: 					$uq.="\"".$val."\"";
 81: 					}
 82: 				$uq.=");";
 83: 				echo $uq."\n";
 84: 				}
 85: 			echo "\n";
 86: 			}
 87: 		echo "\n-- End of Backup\n";
 88: 		ob_end_flush();
 89: 		exit();
 90: 		break;
 91: 		
 92: 		case "restore":
 93: 		if (isset($_REQUEST['live_run'])) $live=true;
 94: 		else $live=false;
 95: 		Screen_Header("Restoration",1,1,"","main","admin");
 96: 		if ($live) echo "<b>Live Data Restore...</b><br><br>";
 97: 		else echo "<b>Dummy Run - Show What Will be Done</b><br><br>";
 98: 		
 99: 		$data=file_get_contents($_FILES['uploadfile']['tmp_name']);
100: 		$lines=explode("\n",$data);
101: 		$errctr=0;
102: 		foreach($lines as $line)
103: 			{
104: 			$line=trim($line);
105: 			if ( ($line!="") && ($line[0]!="-") )
106: 				{
107: 				if (!$live) echo $line."<br>";
108: 				else
109: 					{
110: 					$NATS->DB->Query($line);
111: 					if ($NATS->DB->Error())
112: 						{
113: 						echo "<br><b style=\"color: red;\">".$line."</b><br>";
114: 						echo "Error: ".$NATS->DB->Error_String()."<br><br>";
115: 						$errctr++;
116: 						}
117: 					else
118: 						echo "<b style=\"color: green;\">".$line."</b><br>";
119: 					}
120: 				
121: 				}
122: 			}	
123: 		echo "<br><br>";
124: 		if ($live)
125: 			{
126: 			echo "<b>Import Complete: ".$errctr." Errors</b><br>";
127: 			echo "Please see the detail of any errors above<br><br>";
128: 			echo "<a href=./>Click here to continue</a><br><br>";
129: 			}
130: 		else
131: 			{
132: 			echo "<b>Dummy Import Complete</b><br><br>";
133: 			echo "<a href=admin.backup.php>Click here to continue</b><br><br>";
134: 			}
135: 		Screen_Footer();
136: 		exit();
137: 		break;
138: 		
139: 		default:
140: 		echo "Unknown Action<br><br>";
141: 		break;
142: 		}
143: 		
144: 	}
145: 
146: ob_end_flush();
147: Screen_Header("Backup and Restore",1,1,"","main","admin");
148: 
149: echo "<br><b class=\"subtitle\">Make a Backup</b><br><br>";
150: echo "<form action=admin.backup.php method=post>";
151: echo "<input type=hidden name=action value=backup>";
152: $q="SHOW TABLE STATUS";
153: if (!isset($_REQUEST['show_all_tables']))
154: 	{
155: 	echo "<a href=admin.backup.php?show_all_tables=1><i>Show All Tables in Database (not just fn*)</i></a><br><br>";
156: 	$q.=" LIKE \"fn%\"";
157: 	}
158: $defs=array();
159: function addt($name,$def,$desc)
160: {
161: global $defs;
162: $defs[$name]=array( "def" => $def, "desc" => $desc);
163: }
164: 
165: addt("fnalert",true,"Alert History for Nodes");
166: addt("fnalertaction",true,"Alert Actions e.g. Email Lists");
167: addt("fnalertlog",false,"Log Events for Alerts");
168: addt("fnconfig",true,"System-Wide Configuration");
169: addt("fneval",true,"Custom Test Evaluators");
170: addt("fngroup",true,"Node Groups");
171: addt("fngrouplink",true,"Links Nodes to Groups");
172: addt("fnlocaltest",true,"Configured Local/Server Tests");
173: addt("fnlog",false,"System Event Log");
174: addt("fnnalink",true,"Node to Alert Action Links");
175: addt("fnnode",true,"Nodes");
176: addt("fnnstest",true,"Nodeside Test Configurations");
177: addt("fnrecord",false,"Historic Test Information");
178: addt("fnreport",true,"Saved Availability Reports");
179: addt("fnscheditem",true,"Schedule Ranges");
180: addt("fnschedule",true,"Test Schedules");
181: addt("fnsession",false,"User Sessions");
182: addt("fntestrun",false,"Historic Test Runs");
183: addt("fnuser",true,"Users");
184: addt("fnview",true,"Views");
185: addt("fnviewitem",true,"View Items");
186: 
187: echo "<table class=\"nicetable\">";
188: echo "<tr>";
189: function tdc($t)
190: {
191: echo "<td>".$t."</td>";
192: }
193: tdc("<b>Backup</b>");
194: tdc("<b>Clear ".hlink("Backup:Truncate",12)."</b>");
195: tdc("<b>Table</b>");
196: tdc("<b>Size</b>");
197: tdc("<b>Description</b>");
198: echo "</tr>";
199: 
200: $count=0;
201: $r=$NATS->DB->Query($q);
202: while ($row=$NATS->DB->Fetch_Array($r))
203: 	{
204: 	echo "<tr>";
205: 	if (isset($defs[$row['Name']]) && ($defs[$row['Name']]['def']===true) )
206: 		$s=" checked";
207: 	else $s="";
208: 	tdc("<input type=checkbox name=\"table[".$count."]\" value=\"".$row['Name']."\"".$s.">");
209: 	tdc("<input type=checkbox name=\"truncate_".$row['Name']."\" value=1 checked>");
210: 	tdc($row['Name']);
211: 	tdc($row['Rows']." Rows, ".(round($row['Data_length']/1024,2))." Mb");
212: 	if (isset($defs[$row['Name']]))
213: 		tdc($defs[$row['Name']]['desc']);
214: 	else
215: 		tdc("<i>Unknown</i>");
216: 	echo "</tr>";
217: 	$count++;
218: 	}
219: 
220: echo "</table>";
221: echo "<input type=submit value=\"Generate Backup File\">";
222: echo "&nbsp;&nbsp;";
223: echo "<input type=text name=filename value=\"freenats-".date("Ymd").".sql\" size=30>";
224: echo "</form><br><br>";
225: 
226: 
227: echo "<b class=\"subtitle\">Restore from Backup</b><br><br>";
228: echo "<form enctype=\"multipart/form-data\" method=\"POST\" action=\"admin.backup.php\">";
229: echo "<input type=hidden name=action value=restore>";
230: echo "<b>Backup File: </b><input type=file name=uploadfile> <input type=submit value=Restore><br>";
231: echo "<input type=checkbox name=live_run value=1> Actually Perform Changes to Database (Live Import)";
232: 	
233: 
234: Screen_Footer();
235: ?>
236: