How to restore mysql database from cpanel api1 in console app
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:
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!
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://: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!
-
Hello :) Have you tried using the formulated URL in your browser to see if you get a similar result? Thank you. 0 -
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 : http://:2086/cpsess7337161488/json-api/restoredb?api.version=1&dir=public_html/mydumbsqlfile.sql&file-1=file-chi4d960_testing
And the result like this:{"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:http://: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:{"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 ):{"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,0 -
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. 0 -
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. [COLOR="silver">- - - Updated - - - Sorry because i don't mention about result when i run this code. The changed code still response with the same result. 0
Please sign in to leave a comment.
Comments
4 comments