[ Index ]
 

Code source de eGroupWare 1.2.106-2

Accédez au Source d'autres logiciels libresSoutenez Angelica Josefina !

title

Body

[fermer]

/setup/ -> db_backup.php (source)

   1  <?php
   2      /**************************************************************************\
   3      * eGroupWare - Setup - DB backup and restore                               *
   4      * http://www.egroupware.org                                                *
   5      * Written by RalfBecker@outdoor-training.de                                *
   6      * --------------------------------------------                             *
   7      *  This program is free software; you can redistribute it and/or modify it *
   8      *  under the terms of the GNU General Public License as published by the   *
   9      *  Free Software Foundation; either version 2 of the License, or (at your  *
  10      *  option) any later version.                                              *
  11      \**************************************************************************/
  12  
  13       /* $Id: db_backup.php 22796 2006-11-06 13:31:49Z ralfbecker $ */
  14  
  15       if (!is_object(@$GLOBALS['egw']))    // called from outside eGW ==> setup
  16       {
  17          $GLOBALS['egw_info'] = array(
  18              'flags' => array(
  19                  'noheader' => True,
  20                  'nonavbar' => True,
  21                  'currentapp' => 'home',
  22                  'noapi' => True
  23          ));
  24          include  ('./inc/functions.inc.php');
  25  
  26          @set_time_limit(0);
  27  
  28          // Check header and authentication
  29          if (!$GLOBALS['egw_setup']->auth('Config'))
  30          {
  31              Header('Location: index.php');
  32              exit;
  33          }
  34          // Does not return unless user is authorized
  35  
  36          $GLOBALS['egw_setup']->loaddb();
  37  
  38          $tpl_root = $GLOBALS['egw_setup']->html->setup_tpl_dir('setup');
  39          $self = 'db_backup.php';
  40      }
  41      $db_backup = CreateObject('phpgwapi.db_backup');
  42      $asyncservice = CreateObject('phpgwapi.asyncservice');
  43  
  44      // download a backup, has to be before any output !!!
  45      if ($_POST['download'])
  46      {
  47          list($file) = each($_POST['download']);
  48          $file = $db_backup->backup_dir.'/'.basename($file);    // basename to now allow to change the dir
  49  
  50          $browser = CreateObject('phpgwapi.browser');
  51          $browser->content_header(basename($file));
  52          fpassthru($f = fopen($file,'rb'));
  53          fclose($f);
  54          exit;
  55      }
  56       $setup_tpl = CreateObject('phpgwapi.Template',$tpl_root);
  57      $setup_tpl->set_file(array(
  58          'T_head' => 'head.tpl',
  59          'T_footer' => 'footer.tpl',
  60          'T_db_backup' => 'db_backup.tpl',
  61      ));
  62      $setup_tpl->set_block('T_db_backup','schedule_row','schedule_rows');
  63      $setup_tpl->set_block('T_db_backup','set_row','set_rows');
  64  
  65      $setup_tpl->set_var('stage_title',$stage_title = lang('DB backup and restore'));
  66      $setup_tpl->set_var('stage_desc',lang('This program lets you backup your database, schedule a backup or restore it.'));
  67      $setup_tpl->set_var('error_msg','');
  68  
  69      $bgcolor = array('#DDDDDD','#EEEEEE');
  70  
  71      if (is_object($GLOBALS['egw_setup']->html))
  72      {
  73          $GLOBALS['egw_setup']->html->show_header($stage_title,False,'config',$GLOBALS['egw_setup']->ConfigDomain . '(' . $GLOBALS['egw_domain'][$GLOBALS['egw_setup']->ConfigDomain]['db_type'] . ')');
  74      }
  75      else
  76      {
  77          $setup_tpl->set_block('T_db_backup','setup_header');
  78          $setup_tpl->set_var('setup_header','');
  79          $GLOBALS['egw_info']['flags']['app_header'] = $stage_title;
  80          $GLOBALS['egw']->common->phpgw_header();
  81          parse_navbar();
  82      }
  83      // create a backup now
  84      if($_POST['backup'])
  85      {
  86          if (is_resource($f = $db_backup->fopen_backup()))
  87          {
  88              echo '<p align="center">'.lang('backup started, this might take a few minutes ...')."</p>\n".str_repeat(' ',4096);
  89              $db_backup->backup($f);
  90              fclose($f);
  91              $setup_tpl->set_var('error_msg',lang('backup finished'));
  92          }
  93          else
  94          {
  95              $setup_tpl->set_var('error_msg',$f);
  96          }
  97      }
  98      $setup_tpl->set_var('backup_now_button','<input type="submit" name="backup" title="'.htmlspecialchars(lang("back's up your DB now, this might take a few minutes")).'" value="'.htmlspecialchars(lang('backup now')).'" />');
  99      $setup_tpl->set_var('upload','<input type="file" name="uploaded" /> &nbsp;'.
 100          '<input type="submit" name="upload" value="'.htmlspecialchars(lang('upload backup')).'" title="'.htmlspecialchars(lang("uploads a backup to the backup-dir, from where you can restore it")).'" />');
 101  
 102      if ($_POST['upload'] && is_array($_FILES['uploaded']) && !$_FILES['uploaded']['error'] &&
 103          is_uploaded_file($_FILES['uploaded']['tmp_name']))
 104      {
 105          move_uploaded_file($_FILES['uploaded']['tmp_name'],$db_backup->backup_dir.'/'.$_FILES['uploaded']['name']);
 106  
 107          if (function_exists('md5_file'))    // php4.2+
 108          {
 109              $md5 = ', md5='.md5_file($db_backup->backup_dir.'/'.$_FILES['uploaded']['name']);
 110          }
 111          $setup_tpl->set_var('error_msg',lang("succesfully uploaded file %1",$_FILES['uploaded']['name'].', '.
 112              sprintf('%3.1lf MB (%d)',$_FILES['uploaded']['size']/(1024*1024),$_FILES['uploaded']['size']).$md5));
 113      }
 114      // delete a backup
 115      if ($_POST['delete'])
 116      {
 117          list($file) = each($_POST['delete']);
 118          $file = $db_backup->backup_dir.'/'.basename($file);    // basename to not allow to change the dir
 119  
 120          if (unlink($file)) $setup_tpl->set_var('error_msg',lang("backup '%1' deleted",$file));
 121      }
 122      // rename a backup
 123      if ($_POST['rename'])
 124      {
 125          list($file) = each($_POST['rename']);
 126          $new_name = $_POST['new_name'][$file];
 127          if (!empty($new_name))
 128          {
 129              $file = $db_backup->backup_dir.'/'.basename($file);    // basename to not allow to change the dir
 130              $ext = preg_match('/(\.gz|\.bz2)+$/i',$file,$matches) ? $matches[1] : '';
 131              $new_file = $db_backup->backup_dir.'/'.preg_replace('/(\.gz|\.bz2)+$/i','',basename($new_name)).$ext;
 132              if (rename($file,$new_file)) $setup_tpl->set_var('error_msg',lang("backup '%1' renamed to '%2'",basename($file),basename($new_file)));
 133          }
 134      }
 135      // restore a backup
 136      if ($_POST['restore'])
 137      {
 138          list($file) = each($_POST['restore']);
 139          $file = $db_backup->backup_dir.'/'.basename($file);    // basename to not allow to change the dir
 140  
 141          if (is_resource($f = $db_backup->fopen_backup($file,true)))
 142          {
 143              echo '<p align="center">'.lang('restore started, this might take a few minutes ...')."</p>\n".str_repeat(' ',4096);
 144              $db_backup->restore($f);
 145              fclose($f);
 146              $setup_tpl->set_var('error_msg',lang("backup '%1' restored",$file));
 147          }
 148          else
 149          {
 150              $setup_tpl->set_var('error_msg',$f);
 151          }
 152      }
 153      // create a new scheduled backup
 154      if ($_POST['schedule'])
 155      {
 156          $asyncservice->set_timer($_POST['times'],'db_backup-'.implode(':',$_POST['times']),'admin.admin_db_backup.do_backup','');
 157      }
 158      // cancel a scheduled backup
 159      if (is_array($_POST['cancel']))
 160      {
 161          list($id) = each($_POST['cancel']);
 162          $asyncservice->cancel_timer($id);
 163      }
 164      // list scheduled backups
 165      if (($jobs = $asyncservice->read('db_backup-%')))
 166      {
 167          foreach($jobs as $job)
 168          {
 169              $setup_tpl->set_var($job['times']);
 170              $setup_tpl->set_var('next_run',date('Y-m-d H:i',$job['next']));
 171              $setup_tpl->set_var('actions','<input type="submit" name="cancel['.$job['id'].']" value="'.htmlspecialchars(lang('delete')).'" />');
 172              $setup_tpl->parse('schedule_rows','schedule_row',true);
 173          }
 174      }
 175      // input-fields to create a new scheduled backup
 176      foreach($times=array('year'=>'*','month'=>'*','day'=>'*','dow'=>'2-6','hour'=>3,'min'=>0) as $name => $default)
 177      {
 178          $setup_tpl->set_var($name,'<input name="times['.$name.']" size="5" value="'.$default.'" />');
 179      }
 180      $setup_tpl->set_var('next_run','&nbsp;');
 181      $setup_tpl->set_var('actions','<input type="submit" name="schedule" value="'.htmlspecialchars(lang('schedule')).'" />');
 182      $setup_tpl->parse('schedule_rows','schedule_row',true);
 183  
 184      // listing the availible backup sets
 185      $setup_tpl->set_var('backup_dir',$db_backup->backup_dir);
 186      $setup_tpl->set_var('set_rows','');
 187      $handle = @opendir($db_backup->backup_dir);
 188      $files = array();
 189      while($handle && ($file = readdir($handle)))
 190      {
 191          if ($file != '.' && $file != '..')
 192          {
 193              $files[filectime($db_backup->backup_dir.'/'.$file)] = $file;
 194          }
 195      }
 196      if ($handle) closedir($handle);
 197  
 198      krsort($files);
 199      foreach($files as $ctime => $file)
 200      {
 201          $size = filesize($db_backup->backup_dir.'/'.$file);
 202          $setup_tpl->set_var(array(
 203              'filename'    => $file,
 204              'date'        => date('Y-m-d H:i',$ctime),
 205              'size'        => sprintf('%3.1lf MB (%d)',$size/(1024*1024),$size),
 206              'actions'    => '<input type="submit" name="download['.$file.']" value="'.htmlspecialchars(lang('download')).'" />&nbsp;'."\n".
 207                  '<input type="submit" name="delete['.$file.']" value="'.htmlspecialchars(lang('delete')).'" onclick="return confirm(\''.
 208                      htmlspecialchars(lang('Confirm to delete this backup?')).'\');" />&nbsp;'."\n".
 209                  '<input name="new_name['.$file.']" value="" size="15" /><input type="submit" name="rename['.$file.']" value="'.htmlspecialchars(lang('rename')).'" />&nbsp;'."\n".
 210                  '<input type="submit" name="restore['.$file.']" value="'.htmlspecialchars(lang('restore')).'" onclick="return confirm(\''.
 211                      htmlspecialchars(lang('Restoring a backup will delete/replace all content in your database. Are you sure?')).'\');" />',
 212          ));
 213          $setup_tpl->parse('set_rows','set_row',true);
 214      }
 215  
 216      $setup_tpl->set_var(array(
 217          'lang_scheduled_backups'=> lang('scheduled backups'),
 218          'lang_year'                => lang('year'),
 219          'lang_month'            => lang('month'),
 220          'lang_day'                => lang('day'),
 221          'lang_dow'                => lang('day of week<br />(0-6, 0=sunday)'),
 222          'lang_hour'                => lang('hour (0-24)'),
 223          'lang_minute'            => lang('minute'),
 224          'lang_next_run'            => lang('next run'),
 225          'lang_actions'            => lang('actions'),
 226          'lang_backup_sets'        => lang('backup sets'),
 227          'lang_filename'            => lang('filename'),
 228          'lang_date'                => lang('created'),
 229          'lang_size'                => lang('size'),
 230      ));
 231  
 232      $setup_tpl->set_var('self',$self);
 233      $setup_tpl->pparse('out','T_db_backup');
 234  
 235      if (is_object($GLOBALS['egw_setup']->html))
 236      {
 237          $GLOBALS['egw_setup']->html->show_footer();
 238      }
 239  ?>


Généré le : Sun Feb 25 17:20:01 2007 par Balluche grâce à PHPXref 0.7