App-WordPressTools

 view release on metacpan or  search on metacpan

script/wp-tools  view on Meta::CPAN

            }
        };

        my @paths = map { -e "$nice_path/$_" ? shell_quote("$nice_path/$_") : () } @backup_whitelist;
        my $paths = join(' ', @paths);
        my $size = eval { `du -msc $paths` } || '';
        $size =~ s/.*?(\d+)\s+total.*/$1/s;
        if (!$size || $size !~ /^\d+$/) {
            die 'Cannot determine size of WordPress installation';
        }
        if ($args->{max_size} < $size) {
            # try to reduce content that is included in the backup
            my $backupsize = $size;
            for my $type (sort { $skippable->{$b} <=> $skippable->{$a} } keys %$skippable) {
                my $skip_path   = "$args->{'path'}/wp-content/$type";
                my $skip_pathq  = shell_quote($skip_path);
                next if !-d $skip_path;
                $skips->{$type} = $skippable->{$type} || eval { `du -ms $skip_pathq` } || 0;
                $skips->{$type} =~ s/^(\d+).*/$1/s;
                $backupsize -= $skips->{$type};
                last if $backupsize <= $args->{max_size};
            }
            if ($args->{max_size} < $backupsize) {
                die "Cannot backup this WordPress installation because it is too large ($size/$backupsize)";
            }
        }
    }

    ### backup database
    my $result = '';
    my $dump_command = '';

    # we cannot check the database without wp-config.php (credential storage location)
    if (!-f "$nice_path/wp-config.php") {
        $args->{'__skip_database'} = 1;
    }
    else {
        #acceptable failure states for databaseless accounts
        my $no_db_regex = qr/(?:Access denied for user|Unknown MySQL server host|Unknown database.*when selecting the database|is marked as crashed and last \(automatic\?\) repair failed when using LOCK TABLES|Got error: 130: Incorrect file format|Go...
        if ($plus3713) {
            $dump_command = "db export $db_file --add-drop-table 2>&1";
            $result = _run_wpcli($nice_path, $dump_command);
        }
        if ($result =~ $no_db_regex) {
            $args->{'__skip_database'} = 1;
        }
        elsif ($result !~ /^Success/) {
            # wp-cli backup can fail for wp <3.7.13, so try mysqldump instead
            my $wp_configq = shell_quote("$args->{'path'}/wp-config.php");
            my $username = `grep DB_USER     <$wp_configq | grep -v '$definition_check_string' | cut -d \\' -f 4`;
            my $password = `grep DB_PASSWORD <$wp_configq | grep -v '$definition_check_string' | cut -d \\' -f 4`;
            my $database = `grep DB_NAME     <$wp_configq | grep -v '$definition_check_string' | cut -d \\' -f 4`;
            #disallow starting whitespace
            $username =~ s/^[\r\n]+//;
            $password =~ s/^[\r\n]+//;
            $database =~ s/^[\r\n]+//;
            chomp $username;
            chomp $password;
            chomp $database;
            if ($username =~ /[\r\n]/ || $password =~ /[\r\n]/ || $database =~ /[\r\n]/) {
                die "Multiple credentials found in $args->{'path'}/wp-config.php.  Cannot determine which to use.  Backup operation halted.";
            }
            open (my $fh, '>', $defaults_file) or die "Cannot write to $defaults_file: $!";
            close $fh;
            chmod(0600, $defaults_file) or die "Cannot chmod $defaults_file: $!";
            write_text($defaults_file,"[client]\nuser=$username\npassword=$password");
            my $databaseq = shell_quote($database);
            $dump_command = "mysqldump --defaults-file=$defaults_file $databaseq 2>&1 >$db_file";
            my $result = `$dump_command`;
            #acceptable failure states for databaseless accounts
            if ($result =~ $no_db_regex) {
                $args->{'__skip_database'} = 1;
                $db_file = '';
            }
            elsif ($?) {
                die "mysqldump command ($dump_command) failed (exit: $?): $result";
            }
        }
        if (!$args->{'__skip_database'}) {
            if (!-f $db_file || -s $db_file < 1024) {
                die "Aborting because there doesn't seem to be any data to back up (command: $dump_command)";
            }
            open my $db_test, '<', $db_file;
            my $first_line = <$db_test>;
            close $db_test;
            if ($first_line =~ /Usage: mysqldump/) {
                die "Database dump looks like it failed because it contains usage (command: $dump_command)";
            }
        }
    }

    ### backup filesystem
    my $backup_filename = "$args->{backup_dir}/$backup_file";
    mkpath($args->{backup_dir}, {mode => 0750}) if !-d $args->{backup_dir};
    my $exclusions = '';
    for my $type (keys %$skips) {
        if (!$skips->{$type}) {
            delete $skips->{$type};
            next;
        }
        my $spath = "$wpdir/wp-content/$type";
        $spath = "./$spath" if $spath =~ /^-/;
        my $skip_path = shell_quote($spath);
        $exclusions .= " --exclude $skip_path";
    }
    my $inclusions = '';
    for my $file (@backup_whitelist) {
        next if !-e "$args->{'path'}/$file";
        my $apath = "$wpdir/$file";
        $apath = "./$apath" if $apath =~ /^-/;
        my $add_path = shell_quote($apath);
        $inclusions .= " $add_path";
    }
    open(my $fh, '>', $manifest_file) or die "Cannot write to $manifest_file: $!";
    print $fh "version:1\n";
    print $fh "path:$nice_path\n";
    print $fh "time:$time\n";
    print $fh "nodb:1\n" if $args->{'__skip_database'};
    if (scalar keys %$skips) {
        print $fh "skipped:".join(',', keys %$skips)."\n";
    }

 view all matches for this distribution
 view release on metacpan -  search on metacpan

( run in 0.862 second using v1.00-cache-2.02-grep-82fe00e-cpan-2c419f77a38b )