Changeset 13800

Show
Ignore:
Timestamp:
03/12/12 13:57:02 (1 year ago)
Author:
p_lindheimer
Message:

closes #5598 strip out comments, SET from legacy .sql dumps also, tested with a unit test but not on a real backup

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • modules/branches/2.10/backup/functions.inc/restore.php

    r13797 r13800  
    415415   
    416416  //migrate mysql files to a format that we can restore from 
    417   if (is_file($dir2 . '/asterisk.sql')) { 
    418     rename($dir2 . '/asterisk.sql', $dir2 . '/mysql-db.sql'); 
     417  $src = $dir2 . '/asterisk.sql'; 
     418  if (is_file($src)) { 
     419    $dst = $dir2 . '/mysql-db.sql'; 
     420     
    419421    unset($ret['file_list'][array_search('asterisk.sql', $ret['file_list'])]);//remove from manifest 
    420422    $ret['fpbx_db'] = 'mysql-db'; 
    421423    $ret['mysql']['db'] = array('file' => 'mysql-db.sql'); 
    422     foreach($file = file($dir2 . '/mysql-db.sql') as $num => $line) { 
    423       if (in_array(substr($line, 0, 2), array('--', '/*'))) { 
    424         unset($file[$num]); 
    425       } 
    426     } 
    427  
    428     file_put_contents($dir2 . '/mysql-db.sql', $file); 
    429   } 
    430    
    431   if (is_file($dir2 . '/asteriskcdr.sql')) { 
    432     rename($dir2 . '/asteriskcdr.sql', $dir2 . '/mysql-cdr.sql'); 
     424 
     425    // remove SET and comments that later break restores when using pear 
     426    $cmd[] = fpbx_which('grep'); 
     427    $cmd[] = "-v '^\/\*\|^SET\|^--'"; 
     428    $cmd[] = $src; 
     429    $cmd[] = ' > ' .  $dst; 
     430    exec(implode(' ', $cmd), $file, $status); 
     431    if ($status) { 
     432      // The grep failed, if there is a $dst file remove it and either way rename the $src 
     433      freepbx_log(FPBX_LOG_ERROR, _("Failed converting asterisk.sql to proper format, renaming to mysql-db.sql in current state")); 
     434      if (is_file($dst)) { 
     435        unlink($dst); 
     436      } 
     437      rename($src,$dst); 
     438    } else { 
     439      unlink($src); 
     440    } 
     441    unset($cmd, $file); 
     442  } 
     443   
     444  $src = $dir2 . '/asteriskcdr.sql'; 
     445  if (is_file($src)) { 
     446    $dst = $dir2 . '/mysql-cdr.sql'; 
    433447    unset($ret['file_list'][array_search('asteriskcdr.sql', $ret['file_list'])]);//remove from manifest 
    434448    $ret['fpbx_cdrdb'] = 'mysql-cdr'; 
    435449    $ret['mysql']['cdr'] = array('file' => 'mysql-cdr.sql'); 
    436     foreach($file = file($dir2 . '/mysql-cdr.sql') as $num => $line) { 
    437       if (in_array(substr($line, 0, 2), array('--', '/*'))) { 
    438         unset($file[$num]); 
    439       } 
    440     } 
    441     file_put_contents($dir2 . '/mysql-cdr.sql', $file); 
     450 
     451 
     452    // remove SET and comments that later break restores when using pear 
     453    $cmd[] = fpbx_which('grep'); 
     454    $cmd[] = "-v '^\/\*\|^SET\|^--'"; 
     455    $cmd[] = $src; 
     456    $cmd[] = ' > ' .  $dst; 
     457    exec(implode(' ', $cmd), $file, $status); 
     458    if ($status) { 
     459      // The grep failed, if there is a $dst file remove it and either way rename the $src 
     460      freepbx_log(FPBX_LOG_ERROR, _("Failed converting asteriskcdr.sql to proper format, renaming to mysql-cdr.sql in current state")); 
     461      if (is_file($dst)) { 
     462        unlink($dst); 
     463      } 
     464      rename($src,$dst); 
     465    } else { 
     466      unlink($src); 
     467    } 
     468    unset($cmd, $file); 
    442469  } 
    443470