Skip to content
This repository has been archived by the owner on Apr 20, 2023. It is now read-only.

Implement backup management #26

Open
thanhansoft opened this issue May 29, 2017 · 4 comments
Open

Implement backup management #26

thanhansoft opened this issue May 29, 2017 · 4 comments

Comments

@thanhansoft
Copy link

Hi,
Omines really help me a lot, Please help me backup website with code omines DA?
Thanks

@curry684
Copy link
Member

Backup management is not implemented. PRs welcome.

@curry684 curry684 changed the title How to Backup website with omines? Implement backup management Oct 31, 2017
@akinuri
Copy link

akinuri commented Mar 14, 2018

I also need this feature.

I'm trying to fully automate website creation. I have a default backup that I modify its content (with a C# app I've written) so that it works with the new user. I've been using it for a while but there are still time consuming steps I need to take. For example, user creation, deleting default files in public_html, transfering the modified backup to the server and restoring it. I've figured out how to create/delete user and delete default files in public_html. Now I'm working on transferring the backup to the server. It would be nice to also do the restoring with the API.

@curry684
Copy link
Member

We're currently not actively extending this library as we're not actually using it ourselves anymore. I still do active maintenance and perform updates though, and will review/merge PRs as offered.

@akinuri
Copy link

akinuri commented Apr 9, 2018

I've recently had time to experiment with httpsocket.php at http://files.directadmin.com/services/all/httpsocket/ and I came up with the following code to restore a backup. I'll modify it further to use in my C# app.

I've just wrapped my head around authorization and running the API commands. So I can't provide a PR for this project, yet.

/* ==================== HELPER FUNCTIONS ==================== */

function param_serialize($array) {
    $pairs = [];
    foreach ($array as $key => $value) {
        $pairs[] = "$key=" . urlencode($value);
    }
    return join("&", $pairs);
}

function request_str($method = "GET", $path, $parameters, $headers) {
    $path .= "?" . param_serialize($parameters);
    $request = $method . " ". $path . " HTTP/1.1\r\n";
    foreach ($headers as $key => $value) {
        $request .= $key . ": " . $value . "\r\n";
    }
    return $request . "\r\n";
}

function parse_response($str) {
    $response = [
        "raw"      => $str,
        "protocol" => null,
        "status"   => null,
        "headers"  => [],
        "content"  => null,
    ];
    
    preg_match("/(HTTP\/.*?)\s/", $str, $protocol_match);
    $response["protocol"] = $protocol_match[1];
    
    preg_match("/HTTP\/.*?\s(.*?)$/m", $str, $status_match);
    $response["status"] = $status_match[1];
    
    $parts = preg_split("/\r?\n\r?\n/", $str);
    
    $headers = preg_split("/\r?\n/", $parts[0]);
    array_shift($headers);
    
    $headers_temp = [];
    
    foreach ($headers as $value) {
        $pair = explode(": ", $value);
        $headers_temp[$pair[0]] = $pair[1];
    }
    
    foreach ($headers_temp as $key => $value) {
        $response["headers"][$key] = $value;
    }
    
    $response["content"] = $parts[1];
    
    return $response;
}

function parse_da_content($str) {
    $content = [
        "error"    => null,
        "text"     => null,
        "details"  => null,
    ];
    preg_match("/error=(\d+)/", $str, $error_matches);
    $content["error"] = $error_matches[1];
    preg_match("/text=(.*?)&/", $str, $text_matches);
    $content["text"] = $text_matches[1];
    preg_match("/details=(.*)/s", $str, $details_matches);
    $content["details"] = $details_matches[1];
    $content["details"] = str_replace("<br>", "", $content["details"]);
    $content["details"] = str_replace("'", "'", $content["details"]);
    $content["details"] = preg_split("/\r?\n/", $content["details"]);
    $content["details"] = array_values(array_filter($content["details"], function ($line) { return $line != ""; }));
    return $content;
}




/* ==================== SETUP ==================== */

$server = [
    "ip"        => "_SERVER_IP_",
    "port"      => 2222,
];

$direct_admin = [
    "username"  => "_DA_USERNAME_",
    "password"  => "_DA_PASSWORD_",
];

$headers = [
    "Host"          => "{$server["ip"]}:{$server["port"]}",
    "User-Agent"    => "Socket",
    "Accept"        => "*/*",
    "Connection"    => "Close",
    "Authorization" => "Basic " . base64_encode("{$direct_admin["username"]}:{$direct_admin["password"]}"),
];

$parameters = [
    "action"         => "restore",
    "restore_submit" => "Submit",
    
    // these values are entered by default
    // so they should be passed too?
    "where"          => "local",
    "local_path"     => "/home/admin/user_backups",
    "ip_choice"      => "file",
    
    // this requires further examination
    // probably, selectX and archive order should match
    "select3"        => "_ARCHIVE_NAME_",
];

$response = null;

$request = request_str("GET", "/CMD_API_ADMIN_BACKUP", $parameters, $headers);





/* ==================== SOCKET ==================== */

$socket = @fsockopen($server["ip"], $server["port"], $socket_errno, $socket_errstr, 10);

fwrite($socket, $request);

$status = socket_get_status($socket);

while (!feof($socket) && !$status['timed_out']) {
    $response .= fgets($socket, 1024);
}

fclose($socket);

$response = parse_response($response);
$response["content"] = parse_da_content($response["content"]);

print_r($response);

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

3 participants