Solved

Restore deleted file which is still open.

Posted on 2009-03-31
4
707 Views
Last Modified: 2013-12-06
We deleted (rm) a file that was (is) open by a 24/7 process.
We have a backup of this file and want to copy it over to the original location.

Scenario:
1) Process P has file F open.
2) cp F /tmp/F
3) rm F

We cannot "stop" process "P", but would like to restore the "F" file to it's original location.
What would be the consequences to the "P" process? would it fail? would it take the file as nothing had happened?

0
Comment
Question by:MikeOM_DBA
  • 2
  • 2
4 Comments
 
LVL 40

Expert Comment

by:omarfarid
ID: 24032871
if process P has the file open then it has a file descriptor that points to an inode which is deleted, so it should see an error when it writes to it. Then it depends on the code of the process if it attempts to reopen the file for write again.

You may avoid accidental delete of file by having another hard link to it, use ln command
0
 
LVL 29

Author Comment

by:MikeOM_DBA
ID: 24039203

Thanks for your reply.
I have to admit I have limited knowlege of Unix.

As I understand it, when a process is executing an has a file open, if the file is removed (rm) unix will not actually release the space, but allow the process to continue using the (now deleted) file.

That is the situation here: the file was removed and the process still believes it's available. Now we need to actually place the backup in the right location so the process may actually find it if it attempts to close/reopen this file.

Would this work?


0
 
LVL 40

Accepted Solution

by:
omarfarid earned 500 total points
ID: 24042746
From my understanding, the file space will not be released till that process is killed / stopped.

When a process opens a file it get a file descriptor which is pointing to a vnode (virtual node) in memory which points to inode on disk.

A dir is nothing but a special file that contains a table (in my opinion) which has rows of entries, each entry is a file name and an inode number. inode is a structure that contains info about the file on the disk. a file is deleted from a dir by removing that entry in the table, and is removed from a hard disk or file system when the number of dirs references to it becomes 0.

 When you delete a file from a dir you are simply decrementing the number of references to it. The file could have other references in the same dir or some other dir (this is in the context of the same file system and is called a hard link).

Now, when you copy / restore / recreate a file with the same name in the same dir, it will get a different inode number and hence has nothing to do with the previous one of the same name.

Placing the file in the same location with the same name, in your case, will help if the process closes and reopen the file. The data written between the removal, the file restore from backup, and file reopen, is going somewhere else and not to this restored file.

I hope that I clarified to you more confused you :)
0
 
LVL 29

Author Comment

by:MikeOM_DBA
ID: 24049864
It's clear, that is what I suspected.
Seems that the only way to fix it is to have the process close/open the file.
Thanks.
 
0

Featured Post

Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Unistall fileset without dependecies on AIX 3 70
Can I delete authorized_keys in FreeBSD 1 99
check unix curl command return value 7 152
SQUD PROXY SERVER, UNIX, SLL/HTTPS 5 104
Using libpcap/Jpcap to capture and send packets on Solaris version (10/11) Library used: 1.      Libpcap (http://www.tcpdump.org) Version 1.2 2.      Jpcap(http://netresearch.ics.uci.edu/kfujii/Jpcap/doc/index.html) Version 0.6 Prerequisite: 1.      GCC …
FreeBSD on EC2 FreeBSD (https://www.freebsd.org) is a robust Unix-like operating system that has been around for many years. FreeBSD is available on Amazon EC2 through Amazon Machine Images (AMIs) provided by FreeBSD developer and security office…
Learn how to find files with the shell using the find and locate commands. Use locate to find a needle in a haystack.: With locate, check if the file still exists.: Use find to get the actual location of the file.:
This video shows how to set up a shell script to accept a positional parameter when called, pass that to a SQL script, accept the output from the statement back and then manipulate it in the Shell.

828 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question