The Community Forums

Interact with an entire community of cPanel & WHM users!
  1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

How to restore mysql database from cpanel api1 in console app

Discussion in 'cPanel Developers' started by vietvoquoc, May 22, 2014.

  1. vietvoquoc

    vietvoquoc Registered

    Joined:
    May 22, 2014
    Messages:
    3
    Likes Received:
    0
    Trophy Points:
    1
    cPanel Access Level:
    Website Owner
    Hello everyone!

    I'm doing a tool that will restore my database automatically. The way i using is cpanel api.

    My situation i am facing is:

    I already have a database on hosting and .sql file in public_html folder. Now, i wanna restore that database from my .sql file in public_html folder.


    I am using console application in .net to do this.


    My Code look like below:

    Code:
    
    string username = "myusername";
                string password = "mypassword";
    
                string dir = "dir=public_html";           
                string parameters = dir;
    
                string baseurl = "json-api/cpanel?cpanel_jsonapi_user=myusername&cpanel_jsonapi_module=Fileman&cpanel_jsonapi_func=restoredb&cpanel_jsonapi_apiversion=1&arg-0=file-mydatabasename";            
                string url = "http://<myserverip>:2082/";
                HttpWebRequest req = HttpWebRequest.Create(url + baseurl) as HttpWebRequest;
                string auth = "Basic " + Convert.ToBase64String(System.Text.ASCIIEncoding.ASCII.GetBytes(username + ":" + password));
                req.Method = "POST";
                req.Headers["Authorization"] = auth;
                byte[] data = Encoding.ASCII.GetBytes(parameters);
                Stream requestStream = req.GetRequestStream();
                requestStream.Write(data, 0, data.Length);
                requestStream.Close();
    
                WebResponse resp = req.GetResponse();
                Stream receiveStream = resp.GetResponseStream();
                StreamReader reader = new StreamReader(receiveStream, Encoding.UTF8);
                string content = reader.ReadToEnd();
    
                System.Console.WriteLine(content);
                System.Console.Read();
    
    


    My database name like : mydatabasename.

    My .sql file name like: mysqlname

    The folder that contain .sql file is : public_html



    When i run this code cpanel return a message with result is nothing!


    I don't know where i am wrong and i am very appreciate any help. Please help me out this problem.



    Thanks so much!
     
  2. cPanelMichael

    cPanelMichael Forums Analyst
    Staff Member

    Joined:
    Apr 11, 2011
    Messages:
    30,723
    Likes Received:
    660
    Trophy Points:
    113
    cPanel Access Level:
    Root Administrator
  3. vietvoquoc

    vietvoquoc Registered

    Joined:
    May 22, 2014
    Messages:
    3
    Likes Received:
    0
    Trophy Points:
    1
    cPanel Access Level:
    Website Owner
    I am very happy when you took the time to replied me!
    Thank for that.

    I did a request using on the browser like so:

    1. I requested using api1 :


    Code:
    
    http://<myserverip>:2086/cpsess7337161488/json-api/restoredb?api.version=1&dir=public_html/mydumbsqlfile.sql&file-1=file-chi4d960_testing
    
    

    And the result like this:


    Code:
    {"cpanelresult":{"apiversion":"2","error":"Access denied","data":{"reason":"Access denied","result":"0"},"type":"text"}}
    
    
    Note: cpsess7337161488 i am using is session i get from cpanel console when i login with username and password in hosting.

    And i also made the call using whm like this:


    Code:
    
    http://<myserverip>:2086/json-api/cpanel?cpanel_jsonapi_user=user&cpanel_jsonapi_module=Fileman&cpanel_jsonapi_func=restoredb&cpanel_jsonapi_apiversion=1&arg-0=file-chi4d960_testing&arg-1=public_html/mydumbsqlfile.sql
    
    
    And result is the same:


    Code:
    
    {"apiversion":"1","error":"Access denied","data":{"reason":"Access denied","result":"0"},"type":"text"}
    
    

    And this is result that server response when i request in console app .net with basic authentication ( Code in the first post ):

    Code:
    
    {"apiversion":"1","type":"event","module":"Fileman","func":"restoredb","source":"module","data":{"result":""},"event":{"result":1}}
    
    
    i don't know where i am wrong and hope receive some help from you!

    Best regards!

    vietvoquoc,
     
  4. storminternet

    storminternet Well-Known Member

    Joined:
    Nov 2, 2011
    Messages:
    462
    Likes Received:
    0
    Trophy Points:
    16
    cPanel Access Level:
    Root Administrator
    I am not seeing command to restore the sql database with appropriate path for sql file in your code. Not sure if it the problem but I will suggest you to check your code with the help of your developer.
     
  5. vietvoquoc

    vietvoquoc Registered

    Joined:
    May 22, 2014
    Messages:
    3
    Likes Received:
    0
    Trophy Points:
    1
    cPanel Access Level:
    Website Owner
    Thanks for help me. From your suggestion with the path to sql file that using to restore my database. I chang code line in my method like this :
    string dir = "dir=/home/username/public_html/testing.sql";

    I just post a parameter with value of path locate to dumb sql file that i use to restore database. i don't post any file content inside body of this request. Please take a look and give me some advice! Thanks.

    - - - Updated - - -

    Sorry because i don't mention about result when i run this code. The changed code still response with the same result.
     
Loading...

Share This Page