I am looking after an application that generates a large amount of data in a log file (about 5G a day) on a Red Hat server. This process runs for 24 hours during the week, so there is no point in the day when the file is not being modified, although the information being added to it over midnight is not particularly important, so it is OK if I lose let's say a few seconds of data during that period.
In order to make "safe" archives of the log file on a daily basis, I've created a script that does the following at some point in the early morning:
- makes a copy of the file to a local folder
- truncate the "active" file
- tar+compress the copy
- move the tar.gz of the copy to our archive space
Here is the script itself in case there are glaring problems with it:
DF=$(date +"%Y%m%d_%H%M%S")
TARGET="fixdata-logs-$DF"
cp -r ./fixdata/logs $TARGET
#Truncate the original log file
find ./fixdata/logs -name '*.log' -exec sh -c 'cat /dev/null >| {}' \;
#Zip the log files
tar -zcvf $TARGET.tar.gz $TARGET
#Delete the labelled copy
rm -rf $TARGET
#Archive files older tha 3 days
find . -type f -mtime +3 -name \*.gz -exec mv {} $ARCHIVE_DIR \;
(I understand that some data may be lost but this script is run over a time where a few seconds of data loss is not important.)
Problem is, during this period the application frequently reports errors that is related to system resources. For example, the heartbeat monitor of its queue often fail to produce regular heartbeats. It is clear that this copy->tar.gz->move process is causing enough of an impact on server IO that it impacts application behaviour.
How can I reduce the impact of this script? Time-to-finish is not important - if a solution takes longer but does not cause application errors, then that' preferable to something that is quick. Are there other approaches that I should consider?
For completeness, I have considered the following but have doubts:
- Skip the copy part and tar directly: But I'm worried that tar will have issues if the file is being modified while it is busy.
- Copy to the archive folder first and then tar - perhaps if the compression is done on a different disk then the IO impact is less? I'm worried that the archive space we use is not suitable for doing input/output disk operations like compression as I don't think it's a traditional random access disk. I'm also not sure if a copy to a different physical disk is not going to make things worse, as I would have thought the OS has some clever way to make a local copy without physically reading all the bytes in the file. Unfortunately my *nix skills is not helping here.
- Wait until the weekend: unfortunately the disk space on the server is not sufficient to contain a week's worth of data before archiving. Of course I can ask to increase it but first I want to see if there are more sane solutions.