Example #1
0
 function backup_table($table, $segment = 'none')
 {
     global $wpdb;
     /*
     Taken partially from phpMyAdmin and partially from
     Alain Wolf, Zurich - Switzerland
     Website: http://restkultur.ch/personal/wolf/scripts/db_backup/
     
     Modified by Scott Merril (http://www.skippy.net/) 
     to use the WordPress $wpdb object
     */
     $table_structure = $wpdb->get_results("DESCRIBE {$table}");
     if (!$table_structure) {
         backup_errors(__('Error getting table details') . ": {$table}");
         return FALSE;
     }
     if ($segment == 'none' || $segment == 0) {
         //
         // Add SQL statement to drop existing table
         $this->stow("\n\n");
         $this->stow("#\n");
         $this->stow("# Delete any existing table " . $this->backquote($table) . "\n");
         $this->stow("#\n");
         $this->stow("\n");
         $this->stow("DROP TABLE IF EXISTS " . $this->backquote($table) . ";\n");
         //
         //Table structure
         // Comment in SQL-file
         $this->stow("\n\n");
         $this->stow("#\n");
         $this->stow("# Table structure of table " . $this->backquote($table) . "\n");
         $this->stow("#\n");
         $this->stow("\n");
         $create_table = $wpdb->get_results("SHOW CREATE TABLE {$table}", ARRAY_N);
         if (FALSE === $create_table) {
             $this->backup_error(sprintf(__("Error with SHOW CREATE TABLE for %s."), $table));
             $this->stow("#\n# Error with SHOW CREATE TABLE for {$table}!\n#\n");
         }
         $this->stow($create_table[0][1] . ' ;');
         if (FALSE === $table_structure) {
             $this->backup_error(sprintf(__("Error getting table structure of %s"), $table));
             $this->stow("#\n# Error getting table structure of {$table}!\n#\n");
         }
         //
         // Comment in SQL-file
         $this->stow("\n\n");
         $this->stow("#\n");
         $this->stow('# Data contents of table ' . $this->backquote($table) . "\n");
         $this->stow("#\n");
     }
     if ($segment == 'none' || $segment >= 0) {
         $ints = array();
         foreach ($table_structure as $struct) {
             if (0 === strpos($struct->Type, 'tinyint') || 0 === strpos(strtolower($struct->Type), 'smallint') || 0 === strpos(strtolower($struct->Type), 'mediumint') || 0 === strpos(strtolower($struct->Type), 'int') || 0 === strpos(strtolower($struct->Type), 'bigint') || 0 === strpos(strtolower($struct->Type), 'timestamp')) {
                 $ints[strtolower($struct->Field)] = "1";
             }
         }
         // Batch by $row_inc
         if ($segment == 'none') {
             $row_start = 0;
             $row_inc = ROWS_PER_SEGMENT;
         } else {
             $row_start = $segment * ROWS_PER_SEGMENT;
             $row_inc = ROWS_PER_SEGMENT;
         }
         do {
             if (!ini_get('safe_mode')) {
                 @set_time_limit(15 * 60);
             }
             $table_data = $wpdb->get_results("SELECT * FROM {$table} LIMIT {$row_start}, {$row_inc}", ARRAY_A);
             /*
             if (FALSE === $table_data) {
             	$wp_backup_error .= "Error getting table contents from $table\r\n";
             	fwrite($fp, "#\n# Error getting table contents fom $table!\n#\n");
             }
             */
             $entries = 'INSERT INTO ' . $this->backquote($table) . ' VALUES (';
             //    \x08\\x09, not required
             $search = array("", "\n", "\r", "");
             $replace = array('\\0', '\\n', '\\r', '\\Z');
             if ($table_data) {
                 foreach ($table_data as $row) {
                     $values = array();
                     foreach ($row as $key => $value) {
                         if ($ints[strtolower($key)]) {
                             $values[] = $value;
                         } else {
                             $values[] = "'" . str_replace($search, $replace, $this->sql_addslashes($value)) . "'";
                         }
                     }
                     $this->stow(" \n" . $entries . implode(', ', $values) . ') ;');
                 }
                 $row_start += $row_inc;
             }
         } while (count($table_data) > 0 and $segment == 'none');
     }
     if ($segment == 'none' || $segment < 0) {
         // Create footer/closing comment in SQL-file
         $this->stow("\n");
         $this->stow("#\n");
         $this->stow("# End of data contents of table " . $this->backquote($table) . "\n");
         $this->stow("# --------------------------------------------------------\n");
         $this->stow("\n");
     }
 }
Example #2
0
function backup_restore_tar($dir = "", $file = "", $filetype = "", $display = "")
{
    global $asterisk_conf, $amp_conf;
    $tar = '/bin/tar';
    $error_cause = array();
    if ($amp_conf['AMPBACKUPSUDO'] == true) {
        $sudo = '/usr/bin/sudo';
    } else {
        $sudo = '';
    }
    //use sudo if requested - DANGEROUS!!!
    switch ($filetype) {
        case 'ALL':
            $fileholder = substr($file, 0, -7);
            exec("/bin/rm -rf /tmp/ampbackups.{$fileholder} 2>&1", $out_arr, $ret);
            backup_errors($error_cause, $ret, _('failed to remove previous backup set'));
            // First restore voicemial (for some reason if you do it all at once these don't get restored
            exec('/bin/rm -rf ' . $amp_conf['ASTSPOOLDIR'] . '/voicemail 2>&1', $out_arr, $ret);
            backup_errors($error_cause, $ret, _('failed to remove voicemail directory'));
            $tar_cmd = "{$tar} -PxvOz -f \"{$dir}\" /tmp/ampbackups.{$fileholder}/voicemail.tar.gz | {$tar} -Pxvz";
            exec($tar_cmd, $out_arr, $ret);
            backup_errors($error_cause, $ret, _('failed to untar voicemail.tar.gz'));
            // Next, recordings cause same issue as above
            $tar_cmd = "{$tar} -PxvOz -f \"{$dir}\" /tmp/ampbackups.{$fileholder}/recordings.tar.gz | {$tar} -Pxvz";
            exec($tar_cmd, $out_arr, $ret);
            backup_errors($error_cause, $ret, _('failed to untar recordings.tar.gz'));
            // Now the rest and then we'll get on with the databases
            $tar_cmd = "{$tar} -PxvOz -f \"{$dir}\" /tmp/ampbackups.{$fileholder}/configurations.tar.gz | {$tar} -Pxvz";
            exec($tar_cmd, $out_arr, $ret);
            backup_errors($error_cause, $ret, _('failed to untar configurations.tar.gz'));
            $tar_cmd = "{$tar} -PxvOz -f \"{$dir}\" /tmp/ampbackups.{$fileholder}/fop.tar.gz /tmp/ampbackups.{$fileholder}/cdr.tar.gz  | {$tar} -Pxvz";
            exec($tar_cmd, $out_arr, $ret);
            backup_errors($error_cause, $ret, _('failed to untar fop.tar.gz and cdr.tar.gz'));
            $tar_cmd = "{$tar} -Pxvz -f \"{$dir}\" /tmp/ampbackups.{$fileholder}/asterisk.sql /tmp/ampbackups.{$fileholder}/asteriskcdr.sql /tmp/ampbackups.{$fileholder}/astdb.dump";
            exec($tar_cmd, $out_arr, $ret);
            backup_errors($error_cause, $ret, _('failed to untar astdb.dump, asterisk.sql and asteriskcdr.sql'));
            $sql_cmd = "mysql -u " . $amp_conf['AMPDBUSER'] . " -p" . $amp_conf['AMPDBPASS'] . " < /tmp/ampbackups.{$fileholder}/asterisk.sql 2>&1";
            exec($sql_cmd, $out_arr, $ret);
            backup_errors($error_cause, $ret, _('failed to untar asterisk.sql'));
            $sql_cmd = "mysql -u " . $amp_conf['AMPDBUSER'] . " -p" . $amp_conf['AMPDBPASS'] . " < /tmp/ampbackups.{$fileholder}/asteriskcdr.sql 2>&1";
            exec($sql_cmd, $out_arr, $ret);
            backup_errors($error_cause, $ret, _('failed to restore asteriskcdr.sql'));
            exec($amp_conf['AMPBIN'] . "/restoreastdb.php {$fileholder} 2>&1", $out_arr, $ret);
            backup_errors($error_cause, $ret, _('failed to restore the astdb'));
            //restore additional file (aka AMPPROVROOT), using sudo if requested
            $tar_cmd = "{$sudo} {$tar} -PxvOz -f \"{$dir}\" /tmp/ampbackups.{$fileholder}/phoneconfig.tar.gz |{$sudo} {$tar} -Pxvz";
            exec($tar_cmd, $out_arr, $ret);
            backup_errors($error_cause, $ret, _('failed to untar phoneconfig.tar.gz'));
            exec("/bin/rm -rf /tmp/ampbackups.{$fileholder}", $out_arr, $ret);
            backup_errors($error_cause, $ret, _('failed to remove exploded backup sets from tmp'));
            if (!count($error_cause)) {
                $message = _("Restored All Files in Backup Set");
            }
            break;
        case 'VoiceMail':
            $fileholder = substr($file, 0, -7);
            exec("/bin/rm -rf /tmp/ampbackups.{$fileholder} 2>&1", $out_arr, $ret);
            backup_errors($error_cause, $ret, _('failed to remove previous backup set'));
            exec('/bin/rm -rf ' . $amp_conf['ASTSPOOLDIR'] . '/voicemail 2>&1', $out_arr, $ret);
            backup_errors($error_cause, $ret, _('failed to remove voicemail directory'));
            $tar_cmd = "{$tar} -PxvOz -f \"{$dir}\" /tmp/ampbackups.{$fileholder}/voicemail.tar.gz | {$tar} -Pxvz";
            exec($tar_cmd, $out_arr, $ret);
            backup_errors($error_cause, $ret, _('failed to untar voicemail.tar.gz'));
            exec("/bin/rm -rf /tmp/ampbackups.{$fileholder} 2>&1", $out_arr, $ret);
            backup_errors($error_cause, $ret, _('failed to remove exploded backup sets from tmp'));
            if (!count($error_cause)) {
                $message = _("Restored VoiceMail");
            }
            break;
        case 'Recordings':
            $fileholder = substr($file, 0, -7);
            exec("/bin/rm -rf /tmp/ampbackups.{$fileholder} 2>&1", $out_arr, $ret);
            backup_errors($error_cause, $ret, _('failed to remove previous backup set'));
            $tar_cmd = "{$tar} -PxvOz -f \"{$dir}\" /tmp/ampbackups.{$fileholder}/recordings.tar.gz | {$tar} -Pxvz";
            exec($tar_cmd, $out_arr, $ret);
            backup_errors($error_cause, $ret, _('failed to untar recordings.tar.gz'));
            exec("/bin/rm -rf /tmp/ampbackups.{$fileholder} 2>&1", $out_arr, $ret);
            backup_errors($error_cause, $ret, _('failed to remove exploded backup sets from tmp'));
            if (!count($error_cause)) {
                $message = _("Restored System Recordings");
            }
            break;
        case 'Configurations':
            $fileholder = substr($file, 0, -7);
            exec("/bin/rm -rf /tmp/ampbackups.{$fileholder} 2>&1", $out_arr, $ret);
            backup_errors($error_cause, $ret, _('failed to remove previous backup set'));
            $tar_cmd = "{$tar} -PxvOz -f \"{$dir}\" /tmp/ampbackups.{$fileholder}/configurations.tar.gz | {$tar} -Pxvz";
            exec($tar_cmd, $out_arr, $ret);
            backup_errors($error_cause, $ret, _('failed to untar configurations.tar.gz'));
            $tar_cmd = "\${$tar} -Pxvz -f \"{$dir}\" /tmp/ampbackups.{$fileholder}/asterisk.sql /tmp/ampbackups.{$fileholder}/astdb.dump";
            exec($tar_cmd, $out_arr, $ret);
            backup_errors($error_cause, $ret, _('failed to untar asterisk.sql and astdb.dump'));
            $sql_cmd = "mysql -u " . $amp_conf['AMPDBUSER'] . " -p" . $amp_conf['AMPDBPASS'] . " < /tmp/ampbackups.{$fileholder}/asterisk.sql";
            exec($sql_cmd, $out_arr, $ret);
            backup_errors($error_cause, $ret, _('failed to restore asterisk.sql'));
            exec($amp_conf['AMPBIN'] . "/restoreastdb.php {$fileholder} 2>&1", $out_arr, $ret);
            backup_errors($error_cause, $ret, _('failed to restore the astdb'));
            //restore additional file (aka AMPPROVROOT), using sudo if requested
            $tar_cmd = "{$sudo} {$tar} -PxvOz -f \"{$dir}\" /tmp/ampbackups.{$fileholder}/phoneconfig.tar.gz |{$sudo} {$tar} -Pxvz";
            exec($tar_cmd, $out_arr, $ret);
            backup_errors($error_cause, $ret, _('failed to untar phoneconfig.tar.gz'));
            exec("/bin/rm -rf /tmp/ampbackups.{$fileholder} 2>&1", $out_arr, $ret);
            backup_errors($error_cause, $ret, _('failed to remove exploded backup sets from tmp'));
            if (!count($error_cause)) {
                $message = _("Restored System Configuration");
            }
            break;
        case 'FOP':
            $fileholder = substr($file, 0, -7);
            exec("/bin/rm -rf /tmp/ampbackups.{$fileholder} 2>&1", $out_arr, $ret);
            backup_errors($error_cause, $ret, _('failed to remove previous backup set'));
            $tar_cmd = "{$tar} -PxvOz -f \"{$dir}\" /tmp/ampbackups.{$fileholder}/fop.tar.gz | {$tar} -Pxvz";
            exec($tar_cmd, $out_arr, $ret);
            backup_errors($error_cause, $ret, _('failed to untar fop.tar.gz'));
            exec("/bin/rm -rf /tmp/ampbackups.{$fileholder} 2>&1", $out_arr, $ret);
            backup_errors($error_cause, $ret, _('failed to remove exploded backup sets from tmp'));
            if (!count($error_cause)) {
                $message = _("Restored Operator Panel");
            }
            break;
        case 'CDR':
            $fileholder = substr($file, 0, -7);
            exec("/bin/rm -rf /tmp/ampbackups.{$fileholder} 2>&1", $out_arr, $ret);
            backup_errors($error_cause, $ret, _('failed to remove previous backup set'));
            $tar_cmd = "{$tar} -PxvOz -f \"{$dir}\" /tmp/ampbackups.{$fileholder}/cdr.tar.gz | {$tar} -Pxvz";
            exec($tar_cmd, $out_arr, $ret);
            backup_errors($error_cause, $ret, _('failed to untar cdr.tar.gz'));
            $tar_cmd = "{$tar} -Pxvz -f \"{$dir}\" /tmp/ampbackups.{$fileholder}/asteriskcdr.sql";
            exec($tar_cmd, $out_arr, $ret);
            backup_errors($error_cause, $ret, _('failed to untar asteriskcdr.sql'));
            $sql_cmd = "mysql -u " . $amp_conf['AMPDBUSER'] . " -p" . $amp_conf['AMPDBPASS'] . " < /tmp/ampbackups.{$fileholder}/asteriskcdr.sql 2>&1";
            exec($sql_cmd, $out_arr, $ret);
            backup_errors($error_cause, $ret, _('failed to restore asteriskcdr.sql'));
            exec("/bin/rm -rf /tmp/ampbackups.{$fileholder} 2>&1", $out_arr, $ret);
            backup_errors($error_cause, $ret, _('failed to remove exploded backup sets from tmp'));
            if (!count($error_cause)) {
                $message = _("Restored CDR logs");
            }
            break;
    }
    return count($error_cause) ? $error_cause : $message;
}