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);
}