Schedule Auto File Move / Copy Bash Script
16 Jul 2014
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#Scheduled Auto File Move/Copy Script | |
#------------------------------------ | |
# #Filename : config | |
# #Config File for File Dropper | |
# AUTHOR=Aayush Tuladhar | |
# DESTINATION_DIR=/appserver/userDocs | |
# 5 -- 5 Sec | |
# 300 - 5 Minutes | |
# 900 -- 15 Minutes | |
# 1200 - 20 Minutes | |
# WAIT_TIME=500 | |
# BATCH_NUM=1 | |
# SOURCE_DIR=/home/asadmin/art/file_dropper/Source/*.txt | |
#!/bin/bash | |
CONFIG_FILENAME="config" | |
# Reading Configuration from CONFIG_FILENAME | |
echo "Reading Config" | |
AUTHOR=$(readProp AUTHOR $CONFIG_FILENAME) | |
DIR=$(readProp DESTINATION_DIR $CONFIG_FILENAME) | |
WAIT_TIME=$(readProp WAIT_TIME $CONFIG_FILENAME) | |
BATCH_NUM=$(readProp BATCH_NUM $CONFIG_FILENAME) | |
SOURCE_DIR=$(readProp SOURCE_DIR $CONFIG_FILENAME) | |
echo $AUTHOR | |
echo $DIR | |
echo $WAIT_TIME | |
echo $BATCH_NUM | |
#Running as Demon Process | |
while true; do | |
. config_read.sh | |
echo "Executing Process" | |
#Check if there is file in /userdocs | |
#-- Start Hack to Remove Files if gets clogged --# | |
userdoc_num=0 | |
# Sleep for 5 Minutes if file exists in DIR | |
while [ "$(ls -A $DIR)" ]; do | |
echo "Loading Files to "$DIR | |
num=0 | |
FILE=$SOURCE_DIR | |
for f in $FILE | |
do | |
echo $num | |
echo "Copying File" | |
if [[ -f $f ]]; then | |
echo $f | |
# cat $f | |
cd /appserver/userDocs | |
cp $f . | |
echo $f | |
echo "Copy Complete" | |
rm $f | |
echo "Remove Complete" | |
let num++ | |
fi | |
if [ $num -eq $BATCH_NUM ]; then | |
echo "Sleeping" | |
break | |
fi | |
done | |
if [ $num -eq 0 ]; then | |
echo "No Files left in " $DIR | |
echo "Deamon in Sleep Mode for $WAIT_TIME sec" | |
# Deamon in Sleep Mode | |
# exit 0 | |
fi | |
sleep $WAIT_TIME | |
done | |
# Reads Configuration file | |
# Format : KEY=VALUE | |
# Ignores Comments | |
function readProp { | |
# Arg 1 : Properties Term | |
# Arg 2 : FileName | |
while read line | |
do | |
# echo "$line" | |
pattern="$1=(.+)" | |
# echo $pattern | |
if [[ $line =~ $pattern ]]; then | |
# echo "Matched" | |
echo "${BASH_REMATCH[1]}" | |
fi | |
done < $2 | |
} |