[ Index ]

PHP Cross Reference of MyBB

title

Body

[close]

/inc/tasks/ -> backupdb.php (source)

   1  <?php
   2  /**
   3   * MyBB 1.6
   4   * Copyright 2010 MyBB Group, All Rights Reserved
   5   *
   6   * Website: http://mybb.com
   7   * License: http://mybb.com/about/license
   8   *
   9   * $Id: backupdb.php 5297 2010-12-28 22:01:14Z Tomm $
  10   */
  11  
  12  function task_backupdb($task)
  13  {
  14      global $db, $config, $lang;
  15      static $contents;
  16  
  17      @set_time_limit(0);
  18      
  19      if(!defined('MYBB_ADMIN_DIR'))
  20      {
  21          if(!isset($config['admin_dir']))
  22          {
  23              $config['admin_dir'] = "admin";
  24          }
  25      
  26          define('MYBB_ADMIN_DIR', MYBB_ROOT.$config['admin_dir'].'/');
  27      }
  28      
  29      // Check if folder is writable, before allowing submission
  30      if(!is_writable(MYBB_ADMIN_DIR."/backups"))
  31      {
  32          add_task_log($task, $lang->task_backup_cannot_write_backup);
  33      }
  34      else
  35      {
  36          $db->set_table_prefix('');
  37          
  38          $file = MYBB_ADMIN_DIR.'backups/backup_'.substr(md5($mybb->user['uid'].TIME_NOW), 0, 10).random_str(54);
  39          
  40          if(function_exists('gzopen'))
  41          {
  42              $fp = gzopen($file.'.sql.gz', 'w9');
  43          }
  44          else
  45          {
  46              $fp = fopen($file.'.sql', 'w');
  47          }
  48          
  49          $tables = $db->list_tables($config['database']['database'], $config['database']['table_prefix']);
  50      
  51          $time = date('dS F Y \a\t H:i', TIME_NOW);
  52          $header = "-- MyBB Database Backup\n-- Generated: {$time}\n-- -------------------------------------\n\n";
  53          $contents = $header;
  54          foreach($tables as $table)
  55          {
  56              $field_list = array();
  57              $fields_array = $db->show_fields_from($table);
  58              foreach($fields_array as $field)
  59              {
  60                  $field_list[] = $field['Field'];
  61              }
  62              
  63              $fields = "`".implode("`,`", $field_list)."`";
  64      
  65              $structure = $db->show_create_table($table).";\n";
  66              $contents .= $structure;
  67              clear_overflow($fp, $contents);
  68              
  69              $query = $db->simple_select($table);
  70              while($row = $db->fetch_array($query))
  71              {
  72                  $insert = "INSERT INTO {$table} ($fields) VALUES (";
  73                  $comma = '';
  74                  foreach($field_list as $field)
  75                  {
  76                      if(!isset($row[$field]) || is_null($row[$field]))
  77                      {
  78                          $insert .= $comma."NULL";
  79                      }
  80                      else
  81                      {
  82                          $insert .= $comma."'".$db->escape_string($row[$field])."'";
  83                      }
  84                      $comma = ',';
  85                  }
  86                  $insert .= ");\n";
  87                  $contents .= $insert;
  88                  clear_overflow($fp, $contents);
  89              }
  90          }
  91          
  92          $db->set_table_prefix(TABLE_PREFIX);
  93          
  94          if(function_exists('gzopen'))
  95          {
  96              gzwrite($fp, $contents);
  97              gzclose($fp);
  98          }
  99          else
 100          {
 101              fwrite($fp, $contents);
 102              fclose($fp);
 103          }
 104          
 105          add_task_log($task, $lang->task_backup_ran);
 106      }
 107  }
 108  
 109  // Allows us to refresh cache to prevent over flowing
 110  function clear_overflow($fp, &$contents) 
 111  {
 112      global $mybb;
 113      
 114      if(function_exists('gzopen')) 
 115      {
 116          gzwrite($fp, $contents);
 117      } 
 118      else 
 119      {
 120          fwrite($fp, $contents);
 121      }
 122          
 123      $contents = '';    
 124  }
 125  ?>


Generated: Tue Oct 8 19:19:50 2013 Cross-referenced by PHPXref 0.7.1