Recovering data written to file that moved

data-recoveryfilesgziplogsrm

I have a Python daemon running on CentOS that opens a file at the beginning of a session, and keeps writing to it.

A cronjob however, gzipped the file that was being written to, and so the file moved from log.txt to log.txt.gz. The daemon however kept writing to log.txt. The daemon then has been stopped and closed the file descriptor to log.txt.

Is there any way to recover the data that was written by the daemon to log.txt after the file was moved to log.txt.gz?

Best Answer

AFAICT, no. The problem is that the gzip process will create a new file and will free the previous (the unzipped) one, including a removal from the directory. If no other hard-link in the filesystem is pointing to the file it will get lost once the last file descriptor refering to it is closed.

For the future you'd be advised to synchronize the access to that file ressource instead of letting two processes simultaneously access the file (for writing and resp. deleting).

Another option is to let gzip create a zipped copy. But then you'd have a race condition where not all written contents in the file might get into the gz file.

Related Question