Find the answer to your Linux question:
Results 1 to 4 of 4
I have custom software that writes to a sensitive large file when the user does something. I would like to make backup copies of The file that gets written to, ...
  1. #1
    Just Joined!
    Join Date
    Sep 2007
    Posts
    7

    Detect if a program is writing to a file.

    I have custom software that writes to a sensitive large file when the user does something. I would like to make backup copies of The file that gets written to, but if I make a gzip of the file at the same time someone is changing something, it will corrupt the backup because some of the data will be missing, as its backed up during being written to.

    a) Is there a way to detect if a file is currently being accessed/written to?
    That way if its currently being accessed, I can just make the script wait until its done and then finally back it up.

    b) Instead of backing up the large file while it has potential to get written to, would it be better to make a copy of the file first, then gzip the copy? This idea comes from the fact that gzipping the original takes 5-10 seconds, whereas making a copy only takes 1-2 seconds. The less time, the less chance of corruption.

    c) Is there anyway to freeze a program or a file to stop it from being written to for an amount of time?

    With a, b, and c together. The best solution I have to my problem would be a script that first detects rather the file is being accessed. If not, it would then freeze the file/program and then make a quick copy of it. Once the copy is created, it will unfreeze the original file/program and then go about gzipping the copy.

    Do you think this is the best solution? If not, what else would you recommend?

  2. #2
    Linux Guru Lakshmipathi's Avatar
    Join Date
    Sep 2006
    Location
    3rd rock from sun - Often seen near moon
    Posts
    1,568

    Post

    a) Is there a way to detect if a file is currently being accessed/written to?
    AS far as i'm concerned ,we have inotify.
    Monitor file system activity with inotify
    Kernel Korner - Intro to inotify


    Code:
    Instead of backing up the large file while it has potential to get written to, would it be better to make a copy of the file first, then gzip the copy?
    I'm not sure any specific tool but there are tools/commads available for incremental backup.try google.

    Is there anyway to freeze a program or a file to stop it from being written to for an amount of time?

    I didn't understand your question here ...if want your program to wait before accessing it use sleep.
    In case you don't want other programs to access the file ,there is file lock options available with linux programming.
    - Lakshmipathi.G
    -------------------
    FOSS India Award winning ext3fs Undelete tool and tutorials www.giis.co.in
    First they criticize you,Then they laugh at you,Then they fight with you,Then you win. - M.K.Gandhi
    -------------------

  3. #3
    Just Joined!
    Join Date
    Sep 2007
    Posts
    7
    Quote Originally Posted by Lakshmipathi View Post
    I didn't understand your question here ...if want your program to wait before accessing it use sleep.
    In case you don't want other programs to access the file ,there is file lock options available with linux programming.
    I am referring to if I use kill STOP to the program that writes to the file, will it stop it from writing to the file temporarily so I can back it up, then use kill CONT to continue.

  4. #4
    Linux Guru
    Join Date
    Nov 2007
    Location
    Córdoba (Spain)
    Posts
    1,513
    Quote Originally Posted by paroxsitic View Post
    I am referring to if I use kill STOP to the program that writes to the file, will it stop it from writing to the file temporarily so I can back it up, then use kill CONT to continue.
    That will not guarantee that the file integrity is sane on that concrete moment, if you STOP in the middle of something important the state of the file might be inconsistent.

    You can use inotify to know when a file contents changed, then set a write lock on it until you've done reading it.

    From the other side, if the program that is writing to the file is well behaved, it should also set a lock on its side so other programs can access concurrently on a smart manner without conflicting with each other.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
...