Draco
Draco

Reputation: 9

ftp upload script - from many directories, each to his own ftp path?

I have many directories, each of have some incoming files flow. need script, who upload all files from each defined directory, to defined ftp url. best, if that can be done via bash cycle with sleep in end - cycle going throught all defined, sleep for, say, 60 seconds, and start again - loop.

now i have something like that, but that was ugly, very long script:

    ####ana 

echo "Starting script:ana  $(date +%Y.%m.%d\ %H:%M:%S)..."

getfmts() { if [ -f "$1" ] || [ -d "$1" ]; then echo $(stat -c %Y $1); else echo 0; fi; }

DIRIN_ana=/DWD_sorted/ana  # Full path to input directory
DIROUT_ana=/DWD_sorted/ana_sent # Full path to output directory

cd $DIRIN_ana

if [ "$(ls -A .|grep bufr)" ]; then echo "Processing files..."; else echo "No files"; exit; fi

for f in *; do
ts=$(date +%s); tsf=$(getfmts $f)
if [ $((ts-tsf)) -gt 10 ]; then
echo "Sending file $f to ftp..."
curl -T $f  ftp://smart:smart@SM/../../smart/edit/dwd/ana
if [ $? -eq 0 ]; then
mv -f $f $DIROUT_ana/
else
echo "There was an error when trying to upload file!"
fi
fi
done

echo "Script finished: $(date +%Y.%m.%d\ %H:%M:%S)"




####hsy 

echo "Starting script:hsy  $(date +%Y.%m.%d\ %H:%M:%S)..."

getfmts() { if [ -f "$1" ] || [ -d "$1" ]; then echo $(stat -c %Y $1); else echo 0; fi; }

DIRIN_hsy=/DWD_sorted/hsy  # Full path to input directory
DIROUT_hsy=/DWD_sorted/hsy_sent # Full path to output directory

cd $DIRIN_hsy

if [ "$(ls -A .|grep bufr)" ]; then echo "Processing files..."; else echo "No files"; exit; fi

for f in *; do
ts=$(date +%s); tsf=$(getfmts $f)
if [ $((ts-tsf)) -gt 10 ]; then
echo "Sending file $f to ftp..."
curl -T $f  ftp://smart:smart@SM/../../smart/editor/dwd/gme/hsy
if [ $? -eq 0 ]; then
mv -f $f $DIROUT_hsy/
else
echo "There was an error when trying to upload file!"
fi
fi
done

echo "Script finished: $(date +%Y.%m.%d\ %H:%M:%S)"

Upvotes: 0

Views: 71

Answers (1)

perreal
perreal

Reputation: 97918

some rough cleanup and simplification:

echo "Starting script:ana  $(date +%Y.%m.%d\ %H:%M:%S)..."

getfmts() {
  if [ -f "$1" ] || [ -d "$1" ]; then
    echo $(stat -c %Y $1)
  else
    echo 0
  fi
}

for dir in ana hsy; do

    echo "Processing $dir"
    dir_in="/DWD_sorted/$dir"
    dir_out="${dir_in}_sent"

    cd $dir_in

    if [ "$(ls -A .|grep bufr)" ]; then
      echo "Processing files..."
    else 
      echo "No files"
      continue
    fi

    for f in *; do
        ts=$(date +%s); tsf=$(getfmts $f)
        if [ $((ts-tsf)) -gt 10 ]; then
            echo "Sending file $f to ftp..."
            curl -T $f  ftp://smart:smart@SM/../../smart/edit/dwd/$dir
            if [ $? -eq 0 ]; then
                mv -f $f $dir_out/
            else
                echo "There was an error when trying to upload file!"
            fi
        fi
    done

    echo "Script finished: $(date +%Y.%m.%d\ %H:%M:%S)"

done

Upvotes: 1

Related Questions