Watch /tmp for file creation and prevent deletion of files?
chattr +a /tmp/*some folder*
will set the folder to be append-only. Files can be created and written to but not deleted. Use chattr -a /tmp/*some folder*
when you're done.
You can use the inotifywait
command from inotify-tools in a script to create hard links of files created in /tmp/some_folder
. For example, hard link all created files from /tmp/some_folder
to /tmp/some_folder_bak
:
#!/bin/sh
ORIG_DIR=/tmp/some_folder
CLONE_DIR=/tmp/some_folder_bak
mkdir -p $CLONE_DIR
inotifywait -mr --format='%w%f' -e create $ORIG_DIR | while read file; do
echo $file
DIR=`dirname "$file"`
mkdir -p "${CLONE_DIR}/${DIR#$ORIG_DIR/}"
cp -rl "$file" "${CLONE_DIR}/${file#$ORIG_DIR/}"
done
Since they are hard links, they should be updated when the program modifies them but not deleted when the program removes them. You can delete the hard linked clones normally.
Note that this approach is nowhere near atomic so you rely on this script to create the hard links before the program can delete the newly created file.
If you want to clone all changes to /tmp
, you can use a more distributed version of the script:
#!/bin/sh
TMP_DIR=/tmp
CLONE_DIR=/tmp/clone
mkdir -p $CLONE_DIR
wait_dir() {
inotifywait -mr --format='%w%f' -e create "$1" 2>/dev/null | while read file; do
echo $file
DIR=`dirname "$file"`
mkdir -p "${CLONE_DIR}/${DIR#$TMP_DIR/}"
cp -rl "$file" "${CLONE_DIR}/${file#$TMP_DIR/}"
done
}
trap "trap - TERM && kill -- -$$" INT TERM EXIT
inotifywait -m --format='%w%f' -e create "$TMP_DIR" | while read file; do
if ! [ -d "$file" ]; then
continue
fi
echo "setting up wait for $file"
wait_dir "$file" &
done