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.

Fileman::restoredb does not work as expected

Discussion in 'cPanel Developers' started by nesanjoseph, Sep 16, 2011.

  1. nesanjoseph

    nesanjoseph Active Member

    Joined:
    Dec 31, 2010
    Messages:
    31
    Likes Received:
    0
    Trophy Points:
    6
    Hi,

    I was trying to test backup and restore of MySQL databases of a user using the APIs provided by cPanel. I could do the database backup perfectly using the API. [I got the file "user1_first_database.sql.gz" as backup file]

    But when I tried to restore that file using the Fileman::restoredb api []ApiFileman < ApiDocs/Api1 < TWiki, I could see the output print mentioning that the restore has been done. But the database contents are not restored.

    The point to note is, the database has been created and I could see the DB in the user1's cPanel console. But no tables were restored.

    More Info: The same file when used from the cPanel console itself, MySQL DB is restored successfully.

    URL that I used with PHP-curl stuff: http://<my_cpanel_server>:2082/xml-...api_func=restoredb&cpanel_xmlapi_apiversion=1

    Output Print:
    Code:
    -----------------------------------------8<---------------------------------------
    <?xml version="1.0" ?> <cpanelresult><module>Fileman</module><func>restoredb</func><type>event</type><source>internal</source><apiversion>1</apiversion><data><result>-- MySQL dump 10.11 -- -- Host: localhost Database: user1_first_database -- ------------------------------------------------------ -- Server version 5.0.92-community /*!40101 SET @OLD_CHARACTER_SET_CLIENT=@@CHARACTER_SET_CLIENT */; /*!40101 SET @OLD_CHARACTER_SET_RESULTS=@@CHARACTER_SET_RESULTS */; /*!40101 SET @OLD_COLLATION_CONNECTION=@@COLLATION_CONNECTION */; /*!40101 SET NAMES utf8 */; /*!40103 SET @OLD_TIME_ZONE=@@TIME_ZONE */; /*!40103 SET TIME_ZONE=&amp;#39;+00:00&amp;#39; */; /*!40014 SET @OLD_UNIQUE_CHECKS=@@UNIQUE_CHECKS, UNIQUE_CHECKS=0 */; /*!40014 SET @OLD_FOREIGN_KEY_CHECKS=@@FOREIGN_KEY_CHECKS, FOREIGN_KEY_CHECKS=0 */; /*!40101 SET @OLD_SQL_MODE=@@SQL_MODE, SQL_MODE=&amp;#39;NO_AUTO_VALUE_ON_ZERO&amp;#39; */; /*!40111 SET @OLD_SQL_NOTES=@@SQL_NOTES, SQL_NOTES=0 */; -- -- Table structure for table `TABLE_ONE` -- DROP TABLE IF EXISTS `TABLE_ONE`; /*!40101 SET @saved_cs_client = @@character_set_client */; /*!40101 SET character_set_client = utf8 */; CREATE TABLE `TABLE_ONE` ( `NAME` varchar(255) NOT NULL, `NUMBER` int(10) NOT NULL, `MARK` int(10) NOT NULL ) ENGINE=MyISAM DEFAULT CHARSET=latin1; /*!40101 SET character_set_client = @saved_cs_client */; -- -- Dumping data for table `TABLE_ONE` -- LOCK TABLES `TABLE_ONE` WRITE; /*!40000 ALTER TABLE `TABLE_ONE` DISABLE KEYS */; INSERT INTO `TABLE_ONE` (`NAME`, `NUMBER`, `MARK`) VALUES (&amp;#39;dsssds&amp;#39;,22222,22222),(&amp;#39;4344eee&amp;#39;,32323,2323232); /*!40000 ALTER TABLE `TABLE_ONE` ENABLE KEYS */; UNLOCK TABLES; /*!40103 SET TIME_ZONE=@OLD_TIME_ZONE */; /*!40101 SET SQL_MODE=@OLD_SQL_MODE */; /*!40014 SET FOREIGN_KEY_CHECKS=@OLD_FOREIGN_KEY_CHECKS */; /*!40014 SET UNIQUE_CHECKS=@OLD_UNIQUE_CHECKS */; /*!40101 SET CHARACTER_SET_CLIENT=@OLD_CHARACTER_SET_CLIENT */; /*!40101 SET CHARACTER_SET_RESULTS=@OLD_CHARACTER_SET_RESULTS */; /*!40101 SET COLLATION_CONNECTION=@OLD_COLLATION_CONNECTION */; /*!40111 SET SQL_NOTES=@OLD_SQL_NOTES */; -- Dump completed on 2011-09-16 14:01:08 </result></data> <event> <result>1</result> </event> </cpanelresult>
    ----------------------------------------->8---------------------------------------
    Could any of you kindly help me fix this problem?


    Many thanks,
    Nesan Joseph R.
     
  2. Brian

    Brian Well-Known Member

    Joined:
    Dec 1, 2010
    Messages:
    117
    Likes Received:
    2
    Trophy Points:
    18
    Location:
    Texas
    cPanel Access Level:
    Root Administrator
    What you're encountering is a limitation of API 1. Specifically, with the type of call you're attempting to utilize. This is discussed in further detail by David N, an integration developer, in the following post:

    http://forums.cpanel.net/f42/fileman-uploadfiles-167786.html

    Essentially, you will not be able to use the XML API for API 1 calls that require/utilize "Form Data". You can see "Form Data" mentioned for the "dir" argument under the description for the API 1 call you're using here:

    ApiFileman < ApiDocs/Api1 < TWiki

    David N goes into the technical explanation as to why this is a problem. I don't see an API 2 equivalent for this functionality, so this would appear to be something that you will presently be unable to utilize via the XML API.

    You would need to explore alternative methods in the context of David N's advice from the linked thread.
     
  3. nesanjoseph

    nesanjoseph Active Member

    Joined:
    Dec 31, 2010
    Messages:
    31
    Likes Received:
    0
    Trophy Points:
    6
    Dear cPanelBrianO,

    Many thanks for the detailed response. However, I still don't understand why the API could not extract the file and do a restore. :-(.

    I do the same form post data [purely through PHP-curl] for Home Directory restore which works well [Fileman::restorefiles].

    Following is the full PHP code that I used to restore the Home Directory that worked perfectly:
    Code:
    <?php
    $whmusername = "user1";
    $whmpassword = "password";
    
    $postFields["dir"] = "/home/user1";
    $postFields["file1"] = "@D:\\backup-user1.somedomain.com-9-16-2011.tar.gz";
    $url = "http://<cplinuxserver>:2082/xml-api/cpanel?user=sg&cpanel_xmlapi_module=Fileman&cpanel_xmlapi_func=restorefiles&cpanel_xmlapi_apiversion=1";
    
    $curl_handle=curl_init();
    $curl_log = fopen("D:\\curl_log.txt", 'w'); 
    curl_setopt($curl_handle, CURLOPT_STDERR, $curl_log); 
    curl_setopt($curl_handle, CURLOPT_SSL_VERIFYPEER,0);	
    curl_setopt($curl_handle, CURLOPT_SSL_VERIFYHOST,0); 	
    curl_setopt($curl_handle, CURLOPT_RETURNTRANSFER,true);	
    curl_setopt($curl_handle, CURLOPT_FRESH_CONNECT, true);
    curl_setopt($curl_handle, CURLOPT_FOLLOWLOCATION, true); 
    curl_setopt($curl_handle, CURLOPT_MAXREDIRS, 5);
    curl_setopt($curl_handle, CURLOPT_URL,$url);
    curl_setopt($curl_handle, CURLOPT_POSTFIELDS,$postFields);
    curl_setopt($curl_handle, CURLOPT_RETURNTRANSFER,1);
    curl_setopt($curl_handle, CURLOPT_USERPWD, $whmusername.":".$whmpassword);
    $response = curl_exec($curl_handle);
    print curl_error($curl_handle);
    if(curl_errno($curl_handle)){
    	print curl_error($curl_handle);
    }
    curl_close($curl_handle);
    echo htmlentities($response);
    ?>
    
    Following is the very similar code that I use for MySQL DB restore that did not work:
    Code:
    <?php
    $whmusername = "user1";
    $whmpassword = "password";
    
    $postFields["dir"] = "/home/user1";
    $postFields["file1"] = "@D:\\user1_first_database.sql.gz";
    $url = "http://<cplinuxserver>:2082/xml-api/cpanel?user=user1&cpanel_xmlapi_module=Fileman&cpanel_xmlapi_func=restoredb&cpanel_xmlapi_apiversion=1";
    
    $curl_handle=curl_init();
    $curl_log = fopen("D:\\curl_log.txt", 'w'); 
    curl_setopt($curl_handle, CURLOPT_STDERR, $curl_log); 
    curl_setopt($curl_handle, CURLOPT_SSL_VERIFYPEER,0);	
    curl_setopt($curl_handle, CURLOPT_SSL_VERIFYHOST,0); 	
    curl_setopt($curl_handle, CURLOPT_RETURNTRANSFER,true);	
    curl_setopt($curl_handle, CURLOPT_FRESH_CONNECT, true);
    curl_setopt($curl_handle, CURLOPT_FOLLOWLOCATION, true); 
    curl_setopt($curl_handle, CURLOPT_MAXREDIRS, 5);
    curl_setopt($curl_handle, CURLOPT_URL,$url);
    curl_setopt($curl_handle, CURLOPT_POSTFIELDS,$postFields);
    curl_setopt($curl_handle, CURLOPT_RETURNTRANSFER,1);
    curl_setopt($curl_handle, CURLOPT_USERPWD, $whmusername.":".$whmpassword);
    $response = curl_exec($curl_handle);
    print curl_error($curl_handle);
    if(curl_errno($curl_handle)){
    	print curl_error($curl_handle);
    }
    curl_close($curl_handle);
    echo htmlentities($response);
    ?>
    
    Since Home Directory restores [and Email Fwders/Filters restore too] work well, I am unable to understand why MySQL db restore [Fileman::restoredb] alone became impossible.

    Kindly correct me if I am trying out something impossible. I welcome your suggestions on overcoming this limitation too.

    Awaiting your response,

    Many thanks,
    Nesan Joseph R.
     
  4. Brian

    Brian Well-Known Member

    Joined:
    Dec 1, 2010
    Messages:
    117
    Likes Received:
    2
    Trophy Points:
    18
    Location:
    Texas
    cPanel Access Level:
    Root Administrator
    It makes sense why Fileman::restorefiles works and Fileman::restoredb doesn't work via the XML-API when you look at it from the standpoint that I've mentioned. At least, as to why restorefiles works to the capacity you were looking for it to work.

    [ Fileman::restoredb ]
    Parameters:
    file-* (required)
    Form Data:
    dir (required)

    [ Fileman::restorefiles ]
    Parameters:
    files-* (required) [This is listed as "Form Data", but I believe this is erroneous and should be a parameter]
    Form Data:
    dir (OPTIONAL)

    The key item would be that dir is optional with restorefiles. I gather that "dir" is being ignored in your XML-API example (just as it would be ignored in restoredb) but it doesn't matter with restoresfiles because 'dir' is optional for that API call.

    When Fileman::restorefiles lacks the dir parameter, it just defaults to the user's homedir (which is coincidentally where you were trying to restore the files anyway)

    When Fileman::restoredb lacks the dir parameter, it's resulting in a null path to unpack the database backup to. This explains why the DB creates but does not restore content. The API call is unable to unpack the backup (due to null 'dir' form data) and it leaves you with an empty DB.

    I would have no further workaround recommendations than what David N provided in the thread I linked in my prior response. Such API1 calls that require "Form Data" are simply not able to be utilized via the XML-API. If the "Form Data" arguments are optional (like with Fileman::restorefiles) then you could certainly use those API 1 calls if you haven't the need to specify those optional Form Data arguments.

    Does that help clarify?
     
  5. buenon

    buenon Registered

    Joined:
    Feb 22, 2012
    Messages:
    3
    Likes Received:
    0
    Trophy Points:
    1
    cPanel Access Level:
    Website Owner
    Hi,

    I'm new to cPanel API and I've encountered the same issue mentioned above.
    I understand the reason for it but I would like to know what other options I have in order to call restoredb from an external website.
    Is there absolutely no way of doing it?

    Thanks,
    buenon
     
Loading...
Similar Threads - Fileman restoredb does
  1. Unfixed
    Replies:
    11
    Views:
    571
  2. antonbiz
    Replies:
    2
    Views:
    308

Share This Page