Reputation: 11
I need a script to get the latest updated file and copy over to remote servers. Also, the script should terminate once run itself.
I have tried with While loop, it does copy but I am unable to stop the scripts once it completes its job. or I missed something not known to me.
#!/bin/bash
FILE="/opt/testdir/file.txt"
LATEST=$(ls -Art | tail -n 1)
while [ $LATEST != $FILE ]
do
rsync -avz $LATEST 192.168.20.20:/opt/testdir/.
done
i) file should copy to remote server and whenever script runs it copies over to remote server same file overwrite.
Upvotes: 0
Views: 517
Reputation: 19615
Maybe with using inotify
tools
# monitor current directory ./
# and get dir/file paths on these file-system events:
# - close_write (file written and closed)
# - create (file created)
# - moved_to (file moved to here)
inotifywait \
--quiet \
--monitor \
--event close_write \
--event create \
--event moved_to \
--format '%w%f' \
./ \
| {
# loop through all files
# as inotifywait may return multiple files (one per line)
while read -r LATEST; do
rsync \
--archive \
--compress \
--verbose \
"${LATEST}" \
192.168.20.20:/opt/testdir/.
done
}
See:
Upvotes: 1