I need to take a backup of a file while it is being written to. A running program periodically appends data to a file, and another application copies contents to a backup file.
My approach: The backup program wakes up every x seconds and issues a pread request for y bytes from the previous offset in the original file. If the pread call returns a positive integer indicating the number of bytes retrieved, I write them to the backup file.
Can this approach lead to an inconsistent backup file? Is it possible that the pread call reads a chunk of data that was not fully written in the original file? Note that data is only appended to the original file. Initial tests show this approach works fine, but it could be incidental.
Writer code:
fd = open_file(); while(!done) { do_some_work(); write(fd, buf, bufsize); } Reader code:
fd_in = open_original_file(); fd_out = open_backup_file(); while(!done) { // Issue a read call bytes_in = pread(fd_in, buf, chunksize, current_offset); // Data retrieved if(bytes_in > 0) { pwrite(fd_out, buf, bytes_in, current_offset); current_offset += bytes_in; } sleep(5); }
fd = open_file(); while(!done) { do_some_work(); write(fd, buf, bufsize); }this never sets thedonevariable.write()places the data into an output stream buffer. Only when that buffer overflows will the data actually be written to the file. Suggest usingfflush()after each call towrite()so the data is immediately passed to the file.