Fsniper is a useful tool for directory monitor, and execute predefined actions on files created or modified in that directory. For example, if we want to make a backup of each files created in your web upload folder or ftp directory, Using fsniper we can copy all files created in that folder to backup disk.

Fsniper uses inotify to watch for when a file is closed after being written to.

Step 1: Install Required Packages

First install the required packages for fsniper installation on your system.

# yum install file-libs file-devel pcre pcre-devel

Step 2: Download Fsniper Source

Download the latest fsniper source code or use below command to download it. Also extract the archive file in /usr/src directory.

# cd /usr/src/
# wget http://projects.l3ib.org/fsniper/files/fsniper-1.3.1.tar.gz
# tar xzf fsniper-1.3.1.tar.gz
# cd fsniper-1.3.1

Step 3: Install Fsniper

After extracting archive complile source code using following commands.

# ./configure
# make
# make install

Step 4: Configure Fsniper

Create fsniper configuration file as instruction’s below.

# mkdir ~/.config/fsniper/
# vim ~/.config/fsniper/config

Add below content in configuration file, You may need to change it as per your requirements.

watch {
    # watch the /var/www/upload directory for new files
    /var/www/upload {
	# matches any mimetype beginning with image/ in /var/www/upload directory.
        image/* {
            handler = cp %% /backup/web/upload/image/
        }
	# matches all file with .pdf extension in /var/www/upload directory.
        *.pdf {
            handler = cp /var/www/upload/%F /backup/web/upload/pdf/
        }
    }
}

Details about configuration file:

handlers: The handler is used to specify a command to be executed on match found in specified folder. .

handler = cp %% /backup/web/upload/image/

%% : is replaced with matched file name with full path.
%F : is replaced with file name only.

Step 5: Start Fsniper as Daemon

Fsniper can be started in daemon mode using following command. So the process will not terminate either you logged our from system.

# fsniper --daemon

Step 6: Fsniper init Script

Init scripts are useful for starting application on system boot, so we don’t need to start them after restarting system. Create an file named /etc/init.d/fsniper and add below content.

# vim /etc/init.d/fsniper
export HOME=/root

case "$1" in
start)
echo -n "Starting Fsniper: "
/usr/local/bin/fsniper --daemon
echo -e "... [ e[00;32mOKe[00m ]"
;;
stop)
echo -n "Shutdown Fsniper: "
kill -9 `ps aux | grep "fsniper --daemon" | grep -v grep | awk {'print $2'}`
echo -e "... [ e[00;32mOKe[00m ]"
;;
restart)
$0 stop
sleep 1
$0 start
;;
*)
echo "Usage: `basename $0` start|stop|restart"
exit 1
esac

exit 0

And set the execute permission on script to run.

# chmod +x /etc/init.d/fsniper
# service fsniper start

Thank You for reading this article, I hope this article will help you to setup fsniper on Linux server.