Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Retrieve just deleted document

Tags:

couchdb

I deleted a document but I can still see it in _changes, so I can see last valid _rev, which is deleted, so get doc with id and last revision just returns:

{   "_id":"25efa4ec8489d8b89b34c5cad6000059",   "_rev":"3-a982bd6dccce8f405433f8453ab86880",   "_deleted":true } 

and no other attributes.

How can I recover in this situation? Previous revision can not be seen in _changes. Will writing empty document (setting _deleted to false) help to see all revisions info?

like image 930
avalez Avatar asked Jun 01 '12 17:06

avalez


People also ask

How do you recover a document you just deleted?

To Restore That Important Missing File or Folder:Type Restore files in the search box on the taskbar, and then select Restore your files with File History. Look for the file you need, then use the arrows to see all its versions. When you find the version you want, select Restore to save it in its original location.

Can you recover documents that have been permanently deleted?

First, find and open the folder in which the deleted files were. Then right-click and click on “History,” then click Previous. Select the desired file. Left-click on "Restore." By now, the files must have been recovered.


2 Answers

Ok, figured it out, if anyone interested:

  1. get deleted history, e.g.:

    curl http://example.iriscouch.com/test/_changes 
  2. you'll see deleted documents with $id and $rev, put empty document as new version, e.g.:

    curl -X PUT http://example.iriscouch.com/test/$id?rev=$rev -H "Content-Type: application/json" -d {} 
  3. now you can get all revisions info, e.g:

    curl http://example.iriscouch.com/test/$id?revs_info=true 
  4. obtain version before deletion, e.g.:

    curl http://example.iriscouch.com/test/$id?rev=$prev_rev 
  5. put it back to couchdb, e.g.:

    curl -X PUT http://example.iriscouch.com/test/$id?rev=$rev -H \'Content-Type: application/json\' -d \'$data\' 

Let me know if you have any better way, or script.

like image 102
avalez Avatar answered Oct 07 '22 16:10

avalez


Just been restoring deleted data from a couchdb. This is how I solved it after a little help from the good people on couchdb irc.

1) A get request to $db/$id?revs=true&open_revs=all where $db is your database name and $id is the id of the doc you deleted.

2) Clean the response. The result of this request wasn't valid json, strangely, and required cleaning. The following worked for me:

var deletedDoc = JSON.parse(xhReq.responseText.substring(xhReq.responseText.indexOf("{"), xhReq.responseText.lastIndexOf("}") + 1));

The resulting object looks like this:

  {        "_id":"37b580b03b903da2b50f88587d89c15d",        "_rev":"2-bf3a2888dfe1ce0facef18720dcf97e2",        "_deleted":true,        "_revisions":{               "start":2,               "ids":["bf3a2888dfe1ce0facef18720dcf97e2","85f141069731f6bc77c910b0341e599f"]              }      } 

3) Now we can construct the last revision number, the one before it was deleted. Pull out the second guid in the _revisions.ids array and append it with _revisions.start - 1 and a "-" character. This gives you the rev id of the document just before it was deleted.

var preDeleteRevisionNumber = (deletedDoc._revisions.start - 1) + "-"+ deletedDoc._revisions.ids[1]; 

4) Now collect the original (predelete data) with a get to $db/$id?rev=$preDeleteRevisionNumber

5) To overwrite the old deleted entry you have to post or put back the document with the correct id and the latest revision number (not the pre delete revision number, but the revision number the document has now it has been deleted).

Hope this helps someone.

like image 29
TABlackmore Avatar answered Oct 07 '22 17:10

TABlackmore