Plugin Directory

Changeset 192495


Ignore:
Timestamp:
01/11/2010 08:05:11 AM (16 years ago)
Author:
DanCoulter
Message:

Added a functional (and highly experimental) restore feature

File:
1 edited

Legend:

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

    r190810 r192495  
    44Plugin URI: http://www.wordpressbackup.org/
    55Description: Automatically upload backups of important parts of your blog to Amazon S3
    6 Version: 1.1.0-dev3
     6Version: 1.1.0-dev5
    77Author: Dan Coulter
    88Author URI: http://dancoulter.com/
     
    4040
    4141class cmAWB {
    42     static $version = '1.1.0-dev3';
     42    static $version = '1.1.0-dev4';
    4343
    4444    /**
     
    8686   
    8787    function setup() {
    88         $awb_dir = WP_CONTENT_DIR . '/uploads/awb';
    89         if ( !file_exists($awb_dir) ) {
    90             mkdir($awb_dir);
    91         }
    92         if ( !file_exists($awb_dir . '/.htaccess') ) {
    93             file_put_contents($awb_dir . '/.htaccess', 'deny from all');
    94         }
    95 
     88        require_once ABSPATH . '/wp-admin/includes/class-wp-filesystem-base.php';
     89        require_once ABSPATH . '/wp-admin/includes/class-wp-filesystem-direct.php';
     90       
     91        $fsd = new WP_Filesystem_Direct(1);
     92        $awb_dir = trailingslashit($fsd->wp_content_dir()) . '/uploads/awb';
     93       
     94        if ( !$fsd->exists($fsd->wp_content_dir() . '/uploads') ) {
     95            if ( !$fsd->mkdir($fsd->wp_content_dir() . '/uploads') ) {
     96                return false;
     97            }
     98        }
     99        if ( !$fsd->exists($awb_dir) ) {
     100            if ( !$fsd->mkdir($awb_dir) ) {
     101                return false;
     102            }
     103        }
     104        if ( !$fsd->exists($awb_dir . '/.htaccess') ) {
     105            if ( !$fsd->put_contents($awb_dir . '/.htaccess', 'deny from all') ) {
     106                return false;
     107            }
     108        }
     109       
     110        if ( !$fsd->touch(trailingslashit($awb_dir) . 'awb_test.txt') ) {
     111            return false;
     112        } else {
     113            $fsd->delete(trailingslashit($awb_dir) . 'awb_test.txt');
     114        }
     115
     116        return true;
    96117    }
    97118   
     
    151172        }
    152173        $settings = self::get_settings();
     174       
     175        if ( !self::setup() ) {
     176            $fsd = new WP_Filesystem_Direct(1);
     177            if ( !$fsd->exists($fsd->wp_content_dir() . '/uploads') ) {
     178                echo "<div id='awb-warning' class='updated fade'><p>".__("It appears that you don't have a writeable uploads folder. Be sure that you create one and make it writeable.", 'automatic-wordpress-backup')."</p></div>";
     179            } else {
     180                echo "<div id='awb-warning' class='updated fade'><p>".__("It appears that your uploads folder isn't writeable by the web server. Be sure that you set your permissions so that it can write to it. ", 'automatic-wordpress-backup')."</p></div>";
     181            }
     182           
     183        }
     184       
     185        $fsd = new WP_Filesystem_Direct(1);
     186        //print_r($fsd->dirlist($fsd->wp_content_dir(), true, true));
     187       
     188
    153189       
    154190        ?>
     
    367403                        $("#restore-trigger").click(function(){
    368404                            if ( confirm('<?php _e('Are you SURE that you want to restore from this backup?') ?>') ) {
    369                                 $.post("<?php echo wp_nonce_url('admin-ajax.php', 'awb') ?>", {
     405                                var data = {
    370406                                    action: 'awb_restore',
    371407                                    backup: $('input[name=restore-backup]:checked').val()
    372                                 }, function(rsp){
    373                                     console.log(rsp);
    374                                     //window.location.reload();
    375                                 }, 'text');
     408                                };
     409                                if ( $("#ftp-hostname").length ) {
     410                                    data.hostname = $("#ftp-hostname").val();
     411                                    data.username = $("#ftp-username").val();
     412                                    data.password = $("#ftp-password").val();
     413                                    data.method = $("#ftp-method").val();
     414                                }
     415                                $.post(
     416                                    "<?php echo wp_nonce_url('admin-ajax.php', 'awb') ?>",
     417                                    data,
     418                                    function(rsp){
     419                                        if ( rsp.success == false && rsp.error == "Insufficient Permissions" ) {
     420                                            $(".restore.panel p.submit").before("<div class='error fade'><p><?php _e('To restore from this backup, you will need to enter your FTP login credentials.', 'automatic-wordpress-backup') ?></p></div>");
     421                                            $(".restore.panel p.submit").before("<div><p><?php _e('FTP host name:', 'automatic-wordpress-backup') ?> <input type='text' id='ftp-hostname' name='hostname' value='localhost' /></p></div>");
     422                                            $(".restore.panel p.submit").before("<div><p><?php _e('FTP user name:', 'automatic-wordpress-backup') ?> <input type='text' id='ftp-username' name='username' /></p></div>");
     423                                            $(".restore.panel p.submit").before("<div><p><?php _e('FTP password:', 'automatic-wordpress-backup') ?> <input type='password' id='ftp-password' name='password' /></p></div>");
     424                                            $(".restore.panel p.submit").before("<input type='hidden' id='ftp-method' name='method' value='" + rsp.method + "' />");
     425                                        }
     426                                       
     427                                        console.log(rsp);
     428                                        //window.location.reload();
     429                                    },
     430                                    'json'
     431                                );
    376432                            }
    377433                        });
     
    534590        if ( !wp_verify_nonce($_GET['_wpnonce'], 'awb') ) die("{success: false, error: 'Invalid nonce'}");
    535591       
    536         global $wpdb, $wp_version;
     592        global $wpdb, $wp_version, $wp_filesystem;
    537593       
    538594        require_once('S3.php');
    539595       
    540596        self::setup();
     597
     598        if ( isset($_POST['method']) && !defined('FS_METHOD') ) {
     599            define('FS_METHOD', $_POST['method']);
     600        }
     601       
     602        require_once ABSPATH . '/wp-admin/includes/file.php';
     603
     604        if ( isset($_POST['hostname']) ) {
     605            WP_Filesystem(array(
     606                'hostname' => $_POST['hostname'],
     607                'username' => $_POST['username'],
     608                'password' => $_POST['password']
     609            ), ABSPATH);
     610        }
     611       
     612        $fsd = new WP_Filesystem_Direct(1);
    541613        $settings = self::get_settings();
    542614        $s3 = new S3($settings['access-key'], $settings['secret-key']);     
     
    574646       
    575647        // 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                         }
     648        self::backup(true);
     649       
     650        if ( is_null($wp_filesystem) ) {
     651            $ready = true;
     652            foreach ( $manifest['sections'] as $section ) {
     653                if ( $section == 'config' ) {
     654                    $method = get_filesystem_method(array(), $fsd->abspath());
     655                    if ( $method != 'direct' ) {
     656                        $ready = false;
     657                        break;
    589658                    }
    590                     break;
    591             }
    592         }
    593        
     659                } elseif ( $section == 'themes' ) {
     660                    $method = get_filesystem_method(array(), $fsd->wp_themes_dir());
     661                    if ( $method != 'direct' ) {
     662                        $ready = false;
     663                        break;
     664                    }
     665                } elseif ( $section == 'plugins' ) {
     666                    $method = get_filesystem_method(array(), $fsd->wp_plugins_dir());
     667                    if ( $method != 'direct' ) {
     668                        $ready = false;
     669                        break;
     670                    }
     671                } elseif ( $section == 'uploads' ) {
     672                    $method = get_filesystem_method(array(), $fsd->wp_content_dir() . '/uploads');
     673                    if ( $method != 'direct' ) {
     674                        $ready = false;
     675                        break;
     676                    }
     677                }
     678            }
     679       
     680            if ( !$ready ) {
     681                chdir($cwd);
     682                self::rrmdir($awb_dir . '/tmp');
     683                die(json_encode(array('success' => false, 'error' => 'Insufficient Permissions', 'method' => $method, 'manifest' => $manifest)));
     684            }
     685        }
     686       
     687        if ( !defined('FS_METHOD') ) define('FS_METHOD', 'direct');
     688        if ( is_null($wp_filesystem) ) WP_Filesystem(array(), ABSPATH);
     689       
     690        $rdir = $wp_filesystem->find_folder($awb_dir . '/tmp');
     691       
     692        if ( in_array('database2', $manifest['sections']) ) {
     693            $sql = file_get_contents('awb-database-backup.sql');
     694            $sql = preg_replace('|^\#.*$|m', '', $sql);
     695            $sql = explode(";\n", $sql);
     696            foreach ( $sql as $statement ) {
     697                if ( trim($statement) != '' ) {
     698                    $wpdb->query($statement);
     699                }
     700            }
     701            $wp_filesystem->delete($rdir . '/awb-database-backup.sql');
     702        }
     703       
     704        $wp_filesystem->delete($rdir . '/awb-database-backup.sql');
     705        $wp_filesystem->delete($rdir . '/backup.zip');
     706
     707        $files = self::rscandir($restore_dir);
     708        foreach ( $files as $file ) {
     709            $file = substr($file, strlen($restore_dir));
     710            if ( !$wp_filesystem->exists($wp_filesystem->abspath() . dirname($file)) ) {
     711                $wp_filesystem->mkdir($wp_filesystem->abspath() . dirname($file));
     712            }
     713            @$wp_filesystem->move($rdir . $file, $wp_filesystem->abspath() . $file);
     714        }
     715
    594716        chdir($cwd);
    595         self::rrmdir($awb_dir . '/tmp');
     717        $wp_filesystem->delete($rdir, true);
     718       
    596719        die(json_encode(array('success' => true, 'manifest' => $manifest)));
    597720
    598         //self::backup(true);
    599        
    600         /*
    601 
    602        
    603         if ( file_exists($awb_dir . '/tmp/wp-content/uploads/awb/awb-database-backup.sql') ) {
    604             echo 'restore database';
    605         } elseif ( file_exists($awb_dir . '/tmp/wp-content/uploads/awb/wp-s3-database-backup.sql') ) {
    606             echo 'restore database';
    607         }
    608        
    609         self::rrmdir($awb_dir . '/tmp/wp-content/uploads/awb');
    610        
    611         $uploads = self::rscandir($awb_dir . '/tmp/wp-content/uploads');
    612         if ( $uploads && count($uploads) ) {
    613             echo 'restore uploads';
    614         }
    615        
    616         $uploads = self::rscandir($awb_dir . '/tmp/wp-content/plugins');
    617         if ( $uploads && count($uploads) ) {
    618             echo 'restore plugins';
    619         }
    620        
    621         $uploads = self::rscandir($awb_dir . '/tmp/wp-content/themes');
    622         if ( $uploads && count($uploads) ) {
    623             echo 'restore themes';
    624         }
    625        
    626         if ( is_file('.htaccess') ) {
    627             echo 'restore htaccess';
    628         }
    629        
    630         if ( is_file('wp-config.php') ) {
    631             echo 'restore config';
    632         }
    633        
    634        
    635         //restore database.
    636         //remove backup awb folder
    637         //do cleanup
    638         //restore files
    639        
    640         //wp-s3-database-backup.sql
    641         //awb-database-backup.sql
    642        
    643        
    644         chdir($cwd);
    645         self::rrmdir($awb_dir . '/tmp');
    646        
    647         exit;
    648         */
     721    }
     722   
     723    function restore_dir($source, $source_file, $dest_file) {
     724   
    649725    }
    650726   
Note: See TracChangeset for help on using the changeset viewer.