<? /** * @package as_admintool * @desc as_admintool_backup.php - "backup SQL data" plugin for as_admintool.php * @version 1.001.003 * @author Alexander Selifonov <as-works@narod.ru> * @copyright Alexander Selifonov 2007 * @link http://as-works.narod.ru/en/php/ * modified 28.02.2008 (dd.mm.yyyy) */
require_once('as_dbutils.php'); // DB access wrapper class
define('ASADM_BACKUP','backup'); // ID type for this module define('ASADM_BACKUP_TPR',6); // Tables Per Row in backup form define('ASADM_BACKUP_GZIP',1); // Pack backup file to gzip
# $as_admt_bckpfolder - default folder for backup files, with ending '/' if(!isset($as_admt_bckpfolder)) $as_admt_bckpfolder = 'backup/'; if(!isset($as_iface['prompt_bckpfilename'])) $as_iface['prompt_bckpfilename'] = 'Backup filename (leave empty for default)'; # $as_admt_bcktemplate - prefix for backup filenames, date parts allowed: YYYY,MM,DD,HH,MI $as_admt_bcktemplate = 'backup-YYYY-MM-DD-HHMI';
# mandatory string - registering plugin CAsAdminTool::RegisterPlugin(ASADM_BACKUP,'AsAdm_Backup_Form','AsAdm_Backup_Exec');
// interface localization strings if(empty($as_iface['startbackup'])) $as_iface['startbackup'] ='Start backup'; if(empty($as_iface['backupresult'])) $as_iface['backupresult'] ='Backup result'; if(empty($as_iface['alltables'])) $as_iface['alltables'] ='All tables';
# AsAdm_Backup_Form - function for drawing "backing up" client interface page # first par $pginfo is array: [0] - pageid, [1] - parent layer width, [2] - parent layer height function AsAdm_Backup_Form($pginfo='',$usrparam='', $usrparam2='', $usrparam3=false) { global $as_dbengine, $as_iface, $as_cssclass; $pageid=isset($pginfo[0])? $pginfo[0]: 0; $lwidth = isset($pginfo[1])? $pginfo[1]: 800; $lheight = isset($pginfo[2])? $pginfo[2]: 600;
$self = $_SERVER['PHP_SELF']; static $bckp_js_drawn = false; if(!$bckp_js_drawn) { #<3> draw only once !!! $bckp_js_drawn = true; ?> <script language='javascript'>
var ajax_backupbusy = false; function AsAdm_Runbackup(pageid) { if (ajax_backupbusy) return; fm = asGetObj('asadt_backup_'+pageid); var xmlreq = NewXMLHttpRequest(); if(!xmlreq) return false; ajax_backupbusy = true; xmlreq.onreadystatechange= function() { //<3> if (xmlreq.readyState == 4) { //<3A> // alert(xmlreq.responseText); //debug var spl = xmlreq.responseText.split("{|}"); delete xmlreq; ajax_backupbusy = false; if(spl.length < 2) { asGetObj('backupresult_'+pageid).innerHTML ='wrong response from server: '+spl[0]; } else { asGetObj('backupresult_'+pageid).innerHTML = spl[1]; } //<4> } //<3A> } //<3>
xmlreq.open('POST','<?=$self?>',true); xmlreq.setRequestHeader("Content-Type", postcont); params = 'adm_action_type=backup&pageid=' + pageid + '&'+ComputeParamString('asadt_backup_'+pageid); xmlreq.send(params); asGetObj('backupresult_'+pageid).innerHTML = '<?=$as_iface['msg_waiting']?>'; return false; }
</script> <? } #<3>
$bckpfolder = false; $tblistfile = ''; $tables = array(); $dbname = is_string($usrparam3) ? $usrparam3:''; if(is_string($usrparam2)) $bckpfolder = $usrparam2; if(is_array($usrparam)) { $tables = isset($usrparam['tablelist']) ? $usrparam['tablelist'] : $usrparam; } else $tblistfile=$usrparam;
if($tblistfile!=='' && is_file($tblistfile)) { # read tables list from file $tlst = file($tblistfile); for($kkk=0; $kkk<count($tlst); $kkk++) { $tstk = trim($tlst[$kkk]); if(strlen($tstk)) $tables[] = $tstk; } unset($tlst); } elseif($usrparam==='') { # '' - all tables in DB if(!empty($dbname)) $as_dbengine->select_db($dbname); $tables = $as_dbengine->GetTableList(); }
?> <table id='asadt_tbl_<?=$pageid?>'> <tr><form name='asadt_backup_<?=$pageid?>'> <? if( $bckpfolder !==false ) echo "<input type='hidden' name='backupfolder' value='$bckpfolder'>"; if(!empty($dbname)) echo "<input type=hidden name='_dbname_' value='$dbname'>"; $r_width = $lwidth - 20; $r_height = $lheight-255; ?> <tr><td colspan=4> <div id='bckp_checkbox_<?=$pageid?>' class='<?=$as_cssclass['resultarea']?>' style='overflow:auto; height:170px; width:<?=$r_width?>px;'> <table border=0 cellspacing=0 cellpadding=0><tr><td colspan=4> <input type='checkbox' name='_all_tables_' value='1'> <?=$as_iface['alltables']?></td></tr> <? for($kkp=0; $kkp<count($tables); $kkp++) { if($kkp>=ASADM_BACKUP_TPR && ($kkp % (ASADM_BACKUP_TPR) ==0)) echo "</tr><tr>"; // 4 parameter per line echo "<td nowrap><input type='checkbox' name='tb_{$tables[$kkp]}' value='1'> {$tables[$kkp]}</td>\n"; } ?> <tr><td colspan="4"> <input type="text" name="_bckp_filename_" class='<?=$as_cssclass['textfield']?>' style='width:200' /> <?=$as_iface['prompt_bckpfilename']?></td></tr> </table></div> </tr></table> <br><div align=center> <table width='98%'> <tr><td><button class='button' name='runsql' onClick='AsAdm_Runbackup(<?=$pageid?>)'><?=$as_iface['startbackup']?></button> </td></tr></form> <tr><td><?=$as_iface['backupresult']?></td></tr> <tr><td><div id='backupresult_<?=$pageid?>' class='<?=$as_cssclass['resultarea']?>' style='overflow:auto; height:<?=$r_height?>px; width:<?=$r_width?>px;'> </div></td></tr> </tr> </table> </div> <? }
// # AsAdm_Backup_Exec - function that executes backup and returns result string function AsAdm_Backup_Exec($parms) { global $as_dbengine, $as_iface, $as_admt_bckpfolder,$as_admt_bcktemplate; $pageid = isset($parms['pageid'])? $parms['pageid'] : '1'; $ret = "$pageid{|}"; $b_alltables = empty($parms['_all_tables_'])? false:true; $bklist = array(); $savefolder = $as_admt_bckpfolder; if(isset($parms['backupfolder'])) $savefolder = $parms['backupfolder'];
$dbname = isset($parms['_dbname_']) ? $parms['_dbname_']: ''; if(strlen($dbname)) $as_dbengine->select_db($dbname);
$s_from = array('YYYY','YY','MM','DD','HH','MI','SS'); $s_to = array(date('Y'), date('y'),date('m'),date('d'),date('H'),date('i'),date('s')); $savefile = ''; if(!empty($parms['_bckp_filename_'])) { $savefile = trim($parms['_bckp_filename_']); $savefile= preg_replace ( "/[^\w\.-]+/" , "_" ,$savefile); # avoid non-letter/digit chars } if(empty($savefile)) $savefile = str_replace($s_from, $s_to,$as_admt_bcktemplate); $savename = $savefolder . $savefile.'.xml'; foreach($parms as $key=>$val) { $tbname = (substr($key,0,3)==='tb_')? substr($key,3) : ''; if($tbname !='' && (!empty($val) || $b_alltables)) $bklist[] = $tbname; }
// $bklist has all tablenames to backup, let's start... $as_dbengine->SaveDDLMode(true); // include Create table operators $as_dbengine->SetVerbose(true); $as_dbengine->CreateContents(true); // create table list in the beginning of backup file ob_start(); // intercept all html output! $result = $as_dbengine->BckpBackupTables($bklist,$savename,ASADM_BACKUP_GZIP); $ret = "$pageid{|}".ob_get_contents()."<br>Backup to <b>{$as_dbengine->outputfile}</b>, saved tables: <b>$result</b><br>"; ob_end_clean(); return $ret; }
?>
|