I have a folder on my server which get files from a ftp process running in another server.
What I need is to, whenever new file appears in the directory, move those files to another two network locations interchangeably. (In load-balancing manner - first file to Location1, Next file to location 2, Next to Location 1 likewise)
I use incrontab to run a bash script to do this.
What is the most suitable way to do this?
Here is the way I'm going to do it. Is it the best method to achieve this?
incrontab:
/myserver/monitored_folder IN_CREATE /root/scripts/move_files.sh $#
movefiles.sh bash script (pseudo):
start
read var from file1
if var is odd
move $# to NetworkLocation1
else
move $# to NetworkLocation2
end if
increment var
write var to file1
end
I've implemented it. It's working fine.
incrontab:
/data/source_folder/ IN_CREATE /path_to_script/file_delivery.sh $#
Bash Script:
#!/bin/bash
#log file location
logfile=/var/log/file_delivery.log
exec > $logfile 2>&1
read var < var.dat
rem=$(( $var % 2 ))
#action here
sleep 30
if [ $rem -eq 0 ]; then
scp /data/source_folder/$1 root@SERVER1:/path_on_server1/dest_folder
rm -f /data/source_folder/$1
echo "$(date +"%y-%m-%d-%H:%M:%S") - file $1 has been moved to server1"
else
scp /data/source_folder/$1 root@SERVER2:/path_on_server2/dest_folder
rm -f /data/source_folder/$1
echo "$(date +"%y-%m-%d-%H:%M:%S") - file $1 has been moved to server2"
fi
var=$((var+1))
echo $var > var.dat