Bash – Download multiple files from ftp server with bash script

backupbashftp

I have 2 or 3 FTP Servers where our hoster puts database backups each day. Since he only keeps one, I was orded to write a bash script for downloading alle database backups every day so we have more than the one day old backup.

I tried to get it working with ftp command, but since it seems I can't download an complete directory I thought I could do it with a for loop but that won't work with the ftp command and I always get an unxpected end of file error.

I should say, only can connect via ftp on the server. No SSH access or something else!

At the moment I have this:

echo "Download all the database backups from honds ftp-servers"

SERVERS="ftp.srv1.de ftp.srv2.de"

#for SERVER in $SERVERS; do
    ftp -in ftp://user:pw@ftp.srv1.de << EOF
    binary
    get Datenbankbackup
EOF

Best Answer

You could cd into the directory then use mget command to get all files in that directory:

ftp -in ftp://user:pw@ftp.srv1.de << EOF
    binary
    cd Datenbankbackup
    mget *
EOF
Related Question