FTP download most recent file using Linux and ncftp

One of our projects recently called for a regular file download of a database backup from a server. The goal was to create a warm standby server, and so the database to be downloaded was to be the most recent one available.

There’s an FTP client available for Linux that provides a useful FTP tool set that goes beyond the normal FTP commands. NcFTP is available as download in source or a simple using a simple apt-get install on Ubuntu.

Here’s a bash script that I’ve used to download the most recent file from an FTP directory:

 

#!/bin/bash

echo “Contacting ¬†Server…”

echo “Retrieving most recent database backup file name..”

dlfile=`ncftpls -1 -u username -p password ftp://ipaddress/path/ | tail -n1 `

dest=”/tmp/destinationdir”

echo “Cleaning up previous download…”

rm $dest/*

echo “Most recent file found: $dlfile”

echo “Downloading file to $dest …”

ncftpget -u username -p password ipaddress $dest /FTPpath/$dlfile

 

Share

Leave a Reply