{"id":917,"date":"2008-10-22T13:16:22","date_gmt":"2008-10-22T17:16:22","guid":{"rendered":"http:\/\/onlineappsdba.com\/index.php\/2008\/10\/22\/recover-an-accidentally-deleted-file-when-the-database-is-still-open\/"},"modified":"2008-10-22T13:20:05","modified_gmt":"2008-10-22T17:20:05","slug":"recover-an-accidentally-deleted-file-when-the-database-is-still-open","status":"publish","type":"post","link":"https:\/\/onlineappsdba.com\/index.php\/2008\/10\/22\/recover-an-accidentally-deleted-file-when-the-database-is-still-open\/","title":{"rendered":"Recover an accidentally deleted file when the database is still open."},"content":{"rendered":"<p><font size=\"3\" color=\"#000080\">On Unix\/Linux, when a file is deleted, but a process still has the file open, the file is still there in the filesystem, and only the inode is removed.<br \/>\nBut the process can continue to use its file handle, and the file can also be accessible under \/proc\/&lt;pid&gt;\/fd .<\/font><\/p>\n<p><font size=\"3\" color=\"#000080\">In the following example, we use that behavior to recover a lost datafile after is has been dropped from the os (with rm) but the datafile is still open by the background processes.<\/font><\/p>\n<p><font size=\"3\" color=\"#000080\">First, we create a tablespace, and populate a table in it.<\/font><\/p>\n<p><font color=\"#000080\">SQL&gt; REM <strong> we create a tablespace: <\/strong><br \/>\nSQL&gt; create tablespace TEST_RM datafile &#8216;\/var\/tmp\/test_rm.dbf&#8217; size 10M;<br \/>\nTablespace created.<\/font><\/p>\n<p><font color=\"#000080\">SQL&gt; REM <strong> we create a table in it: <\/strong><br \/>\nSQL&gt; create table FRANCK tablespace test_rm as select * from dba_objects;<br \/>\nTable created.<\/font><\/p>\n<p><font color=\"#000080\">SQL&gt; REM <strong> we check that table data is accessible: <\/strong><br \/>\nSQL&gt; select count(*) from FRANCK;<br \/>\nCOUNT(*)<br \/>\n&#8212;&#8212;&#8212;-<br \/>\n12708<\/font><\/p>\n<p><font color=\"#000080\">SQL&gt; exit<br \/>\nDisconnected from Oracle Database 10g Express Edition Release 10.2.0.1.0 &#8211; Production<br \/>\n<\/font><\/p>\n<p><font size=\"3\" color=\"#000080\">Then, we drop the datafile from unix prompt.<\/font><\/p>\n<p><font color=\"#000080\"> <strong>here is the datafile <\/strong><br \/>\nls -l \/var\/tmp\/test_rm.dbf<br \/>\n-rw-r&#8212;&#8211; 1 oracle dba 10493952 Mar 26 14:25 \/var\/tmp\/test_rm.dbf<\/font><\/p>\n<p><font color=\"#000080\"><strong>we &#8216;accidently&#8217; drop the datafile <\/strong><br \/>\nrm \/var\/tmp\/test_rm.dbf<br \/>\nls -l \/var\/tmp\/test_rm.dbf<br \/>\nls: \/var\/tmp\/test_rm.dbf: no such file or directory<br \/>\n<\/font><\/p>\n<p><font size=\"3\" color=\"#000080\"><br \/>\nHere the datafile is lost.<br \/>\nNow we connect again.<\/font><font color=\"#000080\"><strong><br \/>\n<\/strong><\/font><\/p>\n<p><font color=\"#000080\">sqlplus \/ as sysdba<\/font><\/p>\n<p><font color=\"#000080\">Connected to:<br \/>\nOracle Database 10g Express Edition Release 10.2.0.1.0 &#8211; Production<\/font><\/p>\n<p><font color=\"#000080\">SQL&gt; REM <strong> and we check if table data is accessible: <\/strong><br \/>\nSQL&gt; select count(*) from FRANCK;<\/font><\/p>\n<p><font color=\"#000080\">select * from franck<br \/>\n*<br \/>\nERROR at line 1:<br \/>\nORA-01116: error in opening database file 5<br \/>\nORA-01110: data file 5: &#8216;\/var\/tmp\/test_rm.dbf&#8217;<br \/>\nORA-27041: unable to open file<br \/>\nLinux Error: 2: No such file or directory<br \/>\nAdditional information: 3<br \/>\n<\/font><\/p>\n<p><font size=\"3\" color=\"#000080\">The datafile is lost and data is not accessible.<\/font><\/p>\n<p><font size=\"3\" color=\"#000080\">However, the datafile should still have an open file descriptor by an oracle background process<br \/>\n<\/font><\/p>\n<p><font color=\"#000080\"><strong> we check the dbwriter pid: <\/strong><br \/>\nps -edf | grep dbw<br \/>\noracle <strong>2661<\/strong> 1 0 Mar25 ? 00:00:06 xe_<strong>dbw0<\/strong>_XE<br \/>\noracle 7044 7037 0 14:40 pts\/1 00:00:00 \/bin\/bash -c ps -edf | grep dbw<br \/>\noracle 7046 7044 0 14:40 pts\/1 00:00:00 grep dbw<\/font><\/p>\n<p><font color=\"#000080\"><strong> and we check its opened file descriptors for our file: <\/strong><br \/>\nls -l \/proc\/<strong>2661<\/strong>\/fd | grep test_rm<br \/>\nlrwx&#8212;&#8212; 1 oracle dba 64 Mar 26 14:02 <strong>66<\/strong> -&gt; \/var\/tmp\/test_rm.dbf (deleted)<\/font><\/p>\n<p><font color=\"#000080\"><strong>here it is: <\/strong><br \/>\nls -l \/proc\/2661\/fd\/<strong>66<\/strong><br \/>\nlrwx&#8212;&#8212; 1 oracle dba 64 Mar 26 14:02 \/proc\/<strong>2661<\/strong>\/fd\/<strong>66<\/strong> -&gt; \/var\/tmp\/test_rm.dbf (deleted)<br \/>\n<\/font><\/p>\n<p><font size=\"3\" color=\"#000080\">In some other unix, lsof may be needed to map the file descriptor with the deleted file name<\/font><\/p>\n<p><font color=\"#000080\"><strong> first we set a symbolic link so that oracle can see it as it was before the delete:<\/strong><br \/>\nln -s \/proc\/2661\/fd\/66 \/var\/tmp\/test_rm.dbf<br \/>\n<\/font><\/p>\n<p><font size=\"3\" color=\"#000080\"> here data is accessible, but that will be lost if dbwriter closes it file handle (i.e if the database is closed)<\/font><\/p>\n<p><font size=\"3\" color=\"#000080\">However we can now set the tablespace read only so that it is checkpointed, and no writes occurs on it.<\/font><\/p>\n<p><font color=\"#000080\">SQL&gt; alter tablespace TEST_RM read only;<br \/>\nTablespace altered.<br \/>\n<\/font><\/p>\n<p><font size=\"3\" color=\"#000080\">We can now copy the file safely.<\/font><\/p>\n<p><font color=\"#000080\"><strong> then we drop the symbolic link: <\/strong><br \/>\nrm \/var\/tmp\/test_rm.dbf<br \/>\nls -l \/var\/tmp\/test_rm.dbf<br \/>\n<\/font><\/p>\n<p><font color=\"#000080\">ls: \/var\/tmp\/test_rm.dbf: No such file or directory<br \/>\n<\/font><\/p>\n<p><font color=\"#000080\"><strong>and we can now copy the file <\/strong><br \/>\ncp -p \/proc\/2661\/fd\/66 \/var\/tmp\/test_rm.dbf<br \/>\nls -l \/var\/tmp\/test_rm.dbf<br \/>\n<\/font><\/p>\n<p><font color=\"#000080\">-rw-r&#8212;&#8211; 1 oracle dba 10493952 Mar 26 14:54 \/var\/tmp\/test_rm.dbf<br \/>\n<\/font><\/p>\n<p><font size=\"3\" color=\"#000080\">And datafile is now available again.<\/font><\/p>\n<p><font color=\"#000080\">SQL&gt; REM <strong> we have it back, lets put the tablespace back in read\/write <\/strong><br \/>\nSQL&gt; alter tablespace test_rm read write;<br \/>\nTablespace altered.<\/font><\/p>\n<p><font color=\"#000080\">SQL&gt; REM <strong> and we check data is still there: <\/strong><br \/>\nSQL&gt; select count(*) from FRANCK;<\/font><\/p>\n<p><font color=\"#000080\"> COUNT(*)<br \/>\n&#8212;&#8212;&#8212;-<br \/>\n12708<br \/>\n<\/font><\/p>\n<p><font size=\"3\" color=\"#000080\">This is not to be used like that in production. This is unsupported and may behave differently on different unix\/linux or oracle versions.<\/font><\/p>\n","protected":false},"excerpt":{"rendered":"<p>On Unix\/Linux, when a file is deleted, but a process still has the file open, the file is still there [&hellip;]<\/p>\n","protected":false},"author":115,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"site-sidebar-layout":"default","site-content-layout":"","ast-site-content-layout":"default","site-content-style":"default","site-sidebar-style":"default","ast-global-header-display":"","ast-banner-title-visibility":"","ast-main-header-display":"","ast-hfb-above-header-display":"","ast-hfb-below-header-display":"","ast-hfb-mobile-header-display":"","site-post-title":"","ast-breadcrumbs-content":"","ast-featured-img":"","footer-sml-layout":"","theme-transparent-header-meta":"","adv-header-id-meta":"","stick-header-meta":"","header-above-stick-meta":"","header-main-stick-meta":"","header-below-stick-meta":"","astra-migrate-meta-layouts":"default","ast-page-background-enabled":"default","ast-page-background-meta":{"desktop":{"background-color":"var(--ast-global-color-5)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"tablet":{"background-color":"","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"mobile":{"background-color":"","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""}},"ast-content-background-meta":{"desktop":{"background-color":"var(--ast-global-color-4)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"tablet":{"background-color":"var(--ast-global-color-4)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"mobile":{"background-color":"var(--ast-global-color-4)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""}},"footnotes":""},"categories":[424,134],"tags":[],"class_list":["post-917","post","type-post","status-publish","format-standard","hentry","category-database","category-unix"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v25.8 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>Recover an accidentally deleted file when the database is still open. -<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/onlineappsdba.com\/index.php\/2008\/10\/22\/recover-an-accidentally-deleted-file-when-the-database-is-still-open\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Recover an accidentally deleted file when the database is still open. -\" \/>\n<meta property=\"og:description\" content=\"On Unix\/Linux, when a file is deleted, but a process still has the file open, the file is still there [&hellip;]\" \/>\n<meta property=\"og:url\" content=\"https:\/\/onlineappsdba.com\/index.php\/2008\/10\/22\/recover-an-accidentally-deleted-file-when-the-database-is-still-open\/\" \/>\n<meta property=\"article:published_time\" content=\"2008-10-22T17:16:22+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2008-10-22T17:20:05+00:00\" \/>\n<meta name=\"author\" content=\"Masroof Ahmad\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Masroof Ahmad\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"3 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"WebPage\",\"@id\":\"https:\/\/onlineappsdba.com\/index.php\/2008\/10\/22\/recover-an-accidentally-deleted-file-when-the-database-is-still-open\/\",\"url\":\"https:\/\/onlineappsdba.com\/index.php\/2008\/10\/22\/recover-an-accidentally-deleted-file-when-the-database-is-still-open\/\",\"name\":\"Recover an accidentally deleted file when the database is still open. -\",\"isPartOf\":{\"@id\":\"https:\/\/onlineappsdba.com\/#website\"},\"datePublished\":\"2008-10-22T17:16:22+00:00\",\"dateModified\":\"2008-10-22T17:20:05+00:00\",\"author\":{\"@id\":\"https:\/\/onlineappsdba.com\/#\/schema\/person\/909a876ed58d400faf82caf81d61bfdb\"},\"breadcrumb\":{\"@id\":\"https:\/\/onlineappsdba.com\/index.php\/2008\/10\/22\/recover-an-accidentally-deleted-file-when-the-database-is-still-open\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/onlineappsdba.com\/index.php\/2008\/10\/22\/recover-an-accidentally-deleted-file-when-the-database-is-still-open\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/onlineappsdba.com\/index.php\/2008\/10\/22\/recover-an-accidentally-deleted-file-when-the-database-is-still-open\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/onlineappsdba.com\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Recover an accidentally deleted file when the database is still open.\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/onlineappsdba.com\/#website\",\"url\":\"https:\/\/onlineappsdba.com\/\",\"name\":\"\",\"description\":\"Oracle Implementation &amp; Training Experts\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/onlineappsdba.com\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Person\",\"@id\":\"https:\/\/onlineappsdba.com\/#\/schema\/person\/909a876ed58d400faf82caf81d61bfdb\",\"name\":\"Masroof Ahmad\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/onlineappsdba.com\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/secure.gravatar.com\/avatar\/10f9db7bdbbd7f9ccfbe9b2d208e5978fc28315e9c704383e639a926ea0fce5f?s=96&d=mm&r=g\",\"contentUrl\":\"https:\/\/secure.gravatar.com\/avatar\/10f9db7bdbbd7f9ccfbe9b2d208e5978fc28315e9c704383e639a926ea0fce5f?s=96&d=mm&r=g\",\"caption\":\"Masroof Ahmad\"},\"url\":\"https:\/\/onlineappsdba.com\/index.php\/author\/masroof\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Recover an accidentally deleted file when the database is still open. -","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/onlineappsdba.com\/index.php\/2008\/10\/22\/recover-an-accidentally-deleted-file-when-the-database-is-still-open\/","og_locale":"en_US","og_type":"article","og_title":"Recover an accidentally deleted file when the database is still open. -","og_description":"On Unix\/Linux, when a file is deleted, but a process still has the file open, the file is still there [&hellip;]","og_url":"https:\/\/onlineappsdba.com\/index.php\/2008\/10\/22\/recover-an-accidentally-deleted-file-when-the-database-is-still-open\/","article_published_time":"2008-10-22T17:16:22+00:00","article_modified_time":"2008-10-22T17:20:05+00:00","author":"Masroof Ahmad","twitter_card":"summary_large_image","twitter_misc":{"Written by":"Masroof Ahmad","Est. reading time":"3 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"WebPage","@id":"https:\/\/onlineappsdba.com\/index.php\/2008\/10\/22\/recover-an-accidentally-deleted-file-when-the-database-is-still-open\/","url":"https:\/\/onlineappsdba.com\/index.php\/2008\/10\/22\/recover-an-accidentally-deleted-file-when-the-database-is-still-open\/","name":"Recover an accidentally deleted file when the database is still open. -","isPartOf":{"@id":"https:\/\/onlineappsdba.com\/#website"},"datePublished":"2008-10-22T17:16:22+00:00","dateModified":"2008-10-22T17:20:05+00:00","author":{"@id":"https:\/\/onlineappsdba.com\/#\/schema\/person\/909a876ed58d400faf82caf81d61bfdb"},"breadcrumb":{"@id":"https:\/\/onlineappsdba.com\/index.php\/2008\/10\/22\/recover-an-accidentally-deleted-file-when-the-database-is-still-open\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/onlineappsdba.com\/index.php\/2008\/10\/22\/recover-an-accidentally-deleted-file-when-the-database-is-still-open\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/onlineappsdba.com\/index.php\/2008\/10\/22\/recover-an-accidentally-deleted-file-when-the-database-is-still-open\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/onlineappsdba.com\/"},{"@type":"ListItem","position":2,"name":"Recover an accidentally deleted file when the database is still open."}]},{"@type":"WebSite","@id":"https:\/\/onlineappsdba.com\/#website","url":"https:\/\/onlineappsdba.com\/","name":"","description":"Oracle Implementation &amp; Training Experts","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/onlineappsdba.com\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Person","@id":"https:\/\/onlineappsdba.com\/#\/schema\/person\/909a876ed58d400faf82caf81d61bfdb","name":"Masroof Ahmad","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/onlineappsdba.com\/#\/schema\/person\/image\/","url":"https:\/\/secure.gravatar.com\/avatar\/10f9db7bdbbd7f9ccfbe9b2d208e5978fc28315e9c704383e639a926ea0fce5f?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/10f9db7bdbbd7f9ccfbe9b2d208e5978fc28315e9c704383e639a926ea0fce5f?s=96&d=mm&r=g","caption":"Masroof Ahmad"},"url":"https:\/\/onlineappsdba.com\/index.php\/author\/masroof\/"}]}},"_links":{"self":[{"href":"https:\/\/onlineappsdba.com\/index.php\/wp-json\/wp\/v2\/posts\/917","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/onlineappsdba.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/onlineappsdba.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/onlineappsdba.com\/index.php\/wp-json\/wp\/v2\/users\/115"}],"replies":[{"embeddable":true,"href":"https:\/\/onlineappsdba.com\/index.php\/wp-json\/wp\/v2\/comments?post=917"}],"version-history":[{"count":0,"href":"https:\/\/onlineappsdba.com\/index.php\/wp-json\/wp\/v2\/posts\/917\/revisions"}],"wp:attachment":[{"href":"https:\/\/onlineappsdba.com\/index.php\/wp-json\/wp\/v2\/media?parent=917"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/onlineappsdba.com\/index.php\/wp-json\/wp\/v2\/categories?post=917"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/onlineappsdba.com\/index.php\/wp-json\/wp\/v2\/tags?post=917"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}