Plugin Directory

Changeset 190810


Ignore:
Timestamp:
01/07/2010 07:59:48 AM (16 years ago)
Author:
DanCoulter
Message:

Enabled the database restore.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • automatic-wordpress-backup/trunk/automatic-wordpress-backup.php

    r190431 r190810  
    44Plugin URI: http://www.wordpressbackup.org/
    55Description: Automatically upload backups of important parts of your blog to Amazon S3
    6 Version: 1.1.0-dev2
     6Version: 1.1.0-dev3
    77Author: Dan Coulter
    88Author URI: http://dancoulter.com/
     
    4040
    4141class cmAWB {
    42     static $version = '1.1.0-dev2';
     42    static $version = '1.1.0-dev3';
    4343
    4444    /**
     
    151151        }
    152152        $settings = self::get_settings();
     153       
    153154        ?>
    154155            <script type="text/javascript">
     
    369370                                    action: 'awb_restore',
    370371                                    backup: $('input[name=restore-backup]:checked').val()
    371                                 }, function(){
     372                                }, function(rsp){
     373                                    console.log(rsp);
    372374                                    //window.location.reload();
    373                                 });
     375                                }, 'text');
    374376                            }
    375377                        });
     
    399401   
    400402    function backup($manual = false) {
    401         global $wpdb;
     403        global $wpdb, $wp_version;
    402404        require_once('S3.php');
    403405       
     
    455457                $backups[$key] = str_replace(ABSPATH, '', $value);
    456458            }
    457             $result = shell_exec('zip -r ' . $file . ' ' . implode(' ', $backups) . ' -x *uploads/awb*');
     459            $result = shell_exec('zip -r ' . $file . ' ' . implode(' ', apply_filters('awb_backup_folders', $backups)) . ' -x *uploads/awb*');
    458460
    459461            chdir(WP_CONTENT_DIR . '/uploads/awb');
     
    492494            }
    493495           
    494             file_put_contents('manifest.txt', "Backup generated by Automatic WordPress Backup v" . self::$version . " on " . date('Y-m-d H:i:s T') ."\n\nThe following sections were backed up:\n * " . implode("\n * ", $manifest) . "\n\nMachine Readable: " . serialize(array('time'=>time(),'version'=>self::$version, 'sections'=>$sections)));
     496            file_put_contents('manifest.txt', "Backup generated by Automatic WordPress Backup v" . self::$version . " on " . date('Y-m-d H:i:s T') ." with WordPress " . $wp_version . "\n\nThe following sections were backed up:\n * " . implode("\n * ", $manifest) . "\n\nMachine Readable: " . serialize(array('wordpress'=>$wp_version,'time'=>time(),'version'=>self::$version, 'sections'=>$sections)));
    495497            $result = shell_exec('zip -u ' . $file . ' manifest.txt');
    496498            @unlink('manifest.txt');
     
    508510       
    509511        chdir($cwd);
    510 
    511512    }
    512513   
     
    531532   
    532533    function restore() {
    533         if ( !wp_verify_nonce($_GET['_wpnonce'], 'awb') ) die("invalid nonce");
    534         //self::backup(true);
     534        if ( !wp_verify_nonce($_GET['_wpnonce'], 'awb') ) die("{success: false, error: 'Invalid nonce'}");
     535       
     536        global $wpdb, $wp_version;
    535537       
    536538        require_once('S3.php');
     
    541543       
    542544        $awb_dir = WP_CONTENT_DIR . '/uploads/awb';
    543         mkdir($awb_dir . '/tmp');
    544 
     545        $restore_dir = $awb_dir . '/tmp';
     546       
     547        mkdir($restore_dir);
    545548        $cwd = getcwd();
    546         chdir($awb_dir . '/tmp');
     549        chdir($restore_dir);
    547550        exec("wget --no-check-certificate -O backup.zip '" . $s3->getObjectURL(get_option('s3b-bucket'), $_POST['backup']) . "'");
    548551        exec('unzip backup.zip');
    549         $contents = self::rscandir($awb_dir . '/tmp', true);
     552        $contents = self::rscandir($restore_dir, true);
     553       
     554        if ( !is_file('manifest.txt') ) {
     555            chdir($cwd);
     556            self::rrmdir($restore_dir);
     557            die(json_encode(array('success' => false, 'error' => __("No manifest was found. It is possible that an old version of this plugin created a backup that is not able to be automatically restored."))));
     558            die("{success: false, error: ''}");
     559        }
     560       
     561        $manifest = file_get_contents('manifest.txt');
     562        if ( !(preg_match('|Machine Readable: (.*)|', $manifest, $matches) && ($manifest = unserialize($matches[1]))) ) {
     563            chdir($cwd);
     564            self::rrmdir($restore_dir);
     565            die(json_encode(array('success' => false, 'error' => __('The file manifest seems to be corrupted.  There may be a problem with your backup.'))));
     566        }
     567       
     568        $manifest['test'] = (float) $wp_version;
     569        if ( (float) $manifest['wordpress'] > (float) $wp_version ) {
     570            chdir($cwd);
     571            self::rrmdir($restore_dir);
     572            die(json_encode(array('success' => false, 'error' => __('Cannot restore to an older version of WordPress'), 'manifest' => $manifest)));
     573        }
     574       
     575        // Run a manual backup...
     576        //self::backup(true);
     577       
     578        foreach ( $manifest['sections'] as $section ) {
     579            switch ( $section ) {
     580                case 'database':
     581                 $wpdb->show_errors();
     582                    $sql = file_get_contents('awb-database-backup.sql');
     583                    $sql = preg_replace('|^\#.*$|m', '', $sql);
     584                    $sql = explode(";\n", $sql);
     585                    foreach ( $sql as $statement ) {
     586                        if ( trim($statement) != '' ) {
     587                            $wpdb->query($statement);
     588                        }
     589                    }
     590                    break;
     591            }
     592        }
     593       
     594        chdir($cwd);
     595        self::rrmdir($awb_dir . '/tmp');
     596        die(json_encode(array('success' => true, 'manifest' => $manifest)));
     597
     598        //self::backup(true);
     599       
     600        /*
     601
    550602       
    551603        if ( file_exists($awb_dir . '/tmp/wp-content/uploads/awb/awb-database-backup.sql') ) {
     
    594646       
    595647        exit;
     648        */
    596649    }
    597650   
     
    744797                            }
    745798                        }
    746                         $output .= " \n" . $entries . implode(', ', $values) . ');';
     799                        $output .= "\n" . $entries . implode(', ', $values) . ');';
    747800                    }
    748801                    $row_start += $row_inc;
Note: See TracChangeset for help on using the changeset viewer.