Multiple site FTP downloads, multiple variables in Bash script - function, loop or other?

63 Views Asked by At

I have tried searching but can't find exactly what I'm after and maybe I don't even know exactly what to search for...

I need to FTP a variety of csv files from multiple sites each with different credentials. I am able to do this one by one with the following, however I need to do this for 30 sites and do not want to copy paste all this.

What would be the best way to write this and if you can show me how or point me to an answer that would be great.

And for bonus points (I might have to ask a separate question), mget is not working linux to linux, only from linux to windows. I have also tried curl but no luck either.

Thanks a lot.

p.s. not sure if it makes a difference, but I will be running this as a cron job every 15 minutes. I'm ok with that part ;)

#!/bin/bash
chmod +x ftp.sh

#Windows site global variables
ROOT='/data'
PASSWD='passwd'

# Site 1
SITE='site1'
HOST='10.10.10.10'
USER='sitename1'

ftp -in $HOST <<EOF
user $USER $PASSWD
binary
cd "${ROOT}/${SITE}/"
lcd "/home/Downloads"
mget "${SITE}}.csv1" "${SITE}}.csv2"       #needs second "}" as part of file name
quit
EOF
echo "Site 1 FTP complete"

# Site 2
SITE='site2'
HOST='20.20.20.20'
USER='sitename2'

ftp -in $HOST <<EOF
user $USER $PASSWD
binary
cd "${ROOT}/${SITE}/"
lcd "/home/instrum/Downloads"
mget "${SITE}}.csv1" "${SITE}}.csv2"       #needs second "}" as part of file name
quit
EOF
echo "Site 2 FTP complete"

#Linux site Global variables
ROOT='/home/path'
USER='user'
PASSWD='passwd2'

#Site 3
SITE='site_3'
HOST='30.30.30.30'

ftp -in $HOST << EOF
user $USER $PASSWD
binary

cd "${ROOT}/${SITE}/"
lcd "/home/Downloads"
get "${SITE}file1.csv"             #mget not working for linux to linux FTP, don't know why.
get "${SITE}file2.csv"  
quit
EOF
echo "Site 3 FTP complete"

#Site 4
SITE='site_4'
HOST='40.40.40.40'

ftp -in $HOST << EOF
user $USER $PASSWD
binary

cd "${ROOT}/${SITE}/"
lcd "/home/Downloads"
get "${SITE}file1.csv"             #mget not working for linux to linux FTP, don't know why.
get "${SITE}file2.csv"  
quit
EOF
echo "Site 4 FTP complete"
1

There are 1 best solutions below

0
On

For credentials, put this into a separate file, with variables for site 1 as, site1, host1, user1, and comments, so if a different user is running this script, the user would be able to understand this quickly, and also for less chance of amending the passwords on the file and creating an error. When your main script loads, you can load the file with the passwords before running the main script.

On your main script, if the functionality is similar on all sites, and you are always going to run the same code for all 30 sites as well, then you can use a while loop starting at 1 and ending at 30. In your code amend the variables, site, host and user, to insert the number at the end, to execute the code with the right variables.

There are tools for copying files for example, if these servers are on your network, for example rsync which is efficient as well. If you would like to take a look