Location: PHPKode > projects > MembersGear > membersgear/admin/db_backup.php
<?php
  require_once "../config/config.php";
  require_once "../includes/dbbackup_class.php";
  
  $users = new users_class(); 
  if($users->IsAdminhavePermission($_SESSION[SESSION_PREFIX.'Admin_UserID'],'Can take backup')==false)
  {
    Redirect('admin_nopermission.php',true);  
  }
  //backup_tables('192.168.99.3','root','B!g$tore','test');
  $host = MEMBERSGEAR_DB_HOST;
  $user = MEMBERSGEAR_DB_USER;
  $pass = MEMBERSGEAR_DB_PASSWORD;
  $dbname = MEMBERSGEAR_DB_NAME;
  $filename = backup_tables($host,$user,$pass,$dbname);
  //echo $filename;die;
  
   if($filename != '')
   {         
           $loDbbackUp = new dbbackup_class();
  
           $lTypes=null;   
           $query = 'INSERT INTO '.TABLE_PREFIX.'backup
                    (filename, note, lastrestoredon, createdon, createdby, ipaddress)
                    VALUES(?,?,?,?,?,?)';
                    //VALUES('user4','user4','hide@address.com','address1','1950-12-12 00:00:00',1,1)'
           
           $lsValues = array();
           
           $lsValues[0] = $filename;
           $lsValues[1] = $_POST['txtNotes'];
           $lsValues[2] = 0;
           $lsValues[3] = mktime();
           $lsValues[4] = $_SESSION[SESSION_PREFIX.'Admin_UserID'];
           $lsValues[5] = $_SERVER['REMOTE_ADDR'];
           
           $lsInsertedrows = $loDbbackUp->Insert($query,$lsValues,null);

         $message->SetMessage('Backup created successfully.');
   }
   else
   {
        $message->SetMessage('Please change <b>/admin/db_backups/</b> folders permission to read and write');
        $_SESSION[SESSION_PREFIX.'is_err']=true;
   }              
         Redirect('admin_db_manage.php');
  
  
/* backup the db OR just a table */
function backup_tables($host,$user,$pass,$name,$tables = '*')
{
    $backupfolder = PHYSICAL_DIR.'/admin/db_backups/';
    if(!is__writable($backupfolder))
    {
         return;
    }
    
    $folderpath = 'db_backups/';
    $filename   = GetUniqueString();
    //$folder = $folderpath.'bak-'.date("Ymd");
    $folder = $folderpath;
    if(!file_exists($folder))
    {
        $slastFolder=""; 
        $explodedFolder=explode("/", $folder);
        foreach($explodedFolder as $key=>$value)
        {
            if(!file_exists($slastFolder.$value))
            {
              mkdir($slastFolder.$value) ;
            }  
            $slastFolder.=$value."/";
        }  
          $filefolder = $slastFolder;
    }
    else
    {
            //$filefolder = $folderpath.'bak-'.date("Ymd").'/';   
            $filefolder = $folderpath;   
    }
    
    $link = mysql_connect($host,$user,$pass);
    mysql_select_db($name,$link);
    
    //get all of the tables
    if($tables == '*')
    {
        $tables = array();
        $result = mysql_query('SHOW TABLES');
        while($row = mysql_fetch_row($result))
        {
            $tables[] = $row[0];
        }
    }
    else
    {
        $tables = is_array($tables) ? $tables : explode(',',$tables);
    }
   
     foreach($tables as $table)
    {
        if($table != TABLE_PREFIX."backup")
        {
                $result = mysql_query('SELECT * FROM '.$table);
                $num_fields = mysql_num_fields($result);
                
                $return.= 'DROP TABLE IF EXISTS '.$table.';';
                $row2 = mysql_fetch_row(mysql_query('SHOW CREATE TABLE '.$table));
                $return.= "\n\n".$row2[1].";\n\n";
              
                    while($row = mysql_fetch_row($result))
                    {
                        $return.= 'INSERT INTO '.$table.' VALUES(';
                        for($j=0; $j<$num_fields; $j++) 
                        {
                            $field = mysql_field_type($result,$j);  
                            $row[$j] = addslashes($row[$j]);
                            $row[$j] = ereg_replace("\n","\\n",$row[$j]);
                            
                            //if ($field == 'int' or $field == 'double' or $field == 'decimal' or $field == 'number' or $field == 'tinyint' or $field == 'smallint' or $field == 'mediumint' or $field == 'bigint' or $field == 'float' or $field == 'timestamp' or $field == 'numeric') {
                            if($field == 'int' or $field == 'real')
                            {
                                if (isset($row[$j]) and $row[$j] != '') 
                                { 
                                    $return.= $row[$j]; 
                                }
                                else 
                                { 
                                    if($field == 'int')
                                        $return.= 'NULL';
                                    else if($field == 'real')
                                        $return.= 'NULL'; 
                                }    
                            }
                            else
                            {
                                $quote = '\'';
                                if (isset($row[$j])) 
                                { 
                                    $return.= $quote.$row[$j].$quote ; 
                                }
                                else 
                                { 
                                    $return.= ''; 
                                }
                                
                            }
                            
                            
                            if ($j<($num_fields-1)) 
                            { 
                                $return.= ','; 
                            }
                            unset($quote);
                        }
                        $return.= ");\n";
                
                }
                $return.="\n\n\n";            
        }

    }
    
     //save file
    
    $handle = fopen($filefolder.'db_dbmembership_'.$filename.".sql","w+"); 
    fwrite($handle,$return);
    fclose($handle);
    return ('db_dbmembership_'.$filename.'.sql');
}
?>
Return current item: MembersGear