Shell script to copy all files recursively and upload them to remote FTP server

#!/bin/bash
# Shell script to copy all files recursively and upload them to
# remote FTP server (copy local all directories/tree to remote ftp server)
#
# If you want to use this script in cron then make sure you have
# file pointed by $AUTHFILE (see below) and add lines to it:
# host ftp.mycorp.com
# user myftpuser
# pass mypassword
#
# This is a free shell script under GNU GPL version 2.0 or above
# Copyright (C) 2005 nixCraft
# Feedback/comment/suggestions : http://cyberciti.biz/fb/
# -------------------------------------------------------------------------
# This script is part of nixCraft shell script collection (NSSC)
# Visit http://bash.cyberciti.biz/ for more information.
# -------------------------------------------------------------------------
 
FTP="/usr/bin/ncftpput"
CMD=""
AUTHFILE="/root/.myupload"
 
if [ -f $AUTHFILE ] ; then
  # use the file for auth
  CMD="$FTP -m -R -f $AUTHFILE $myf $remotedir $localdir"
else
  echo "*** To terminate at any point hit [ CTRL + C ] ***"
  read -p "Enter ftpserver name : " myf
  read -p "Enter ftp username : " myu
  read -s -p "Enter ftp password : " myp
  echo ""
  read -p "Enter ftp remote directory [/] : " remotedir
  read -p "Enter local directory to upload path [.] : " localdir
  [ "$remotedir" == "" ] && remotedir="/" || :
  [ "$localdir" == "" ] && localdir="." || :
  CMD="$FTP -m -R -u $myu -p $myp $myf $remotedir $localdir"
fi
 
$CMD

Featured Articles:

Want to read Linux tips and tricks, but don't have time to check our blog everyday? Subscribe to our email newsletter to make sure you don't miss a single tip/tricks.

{ 10 comments… read them below or add one }

1 O2Link April 20, 2008 at 11:56 am

Thank you!
it was so usefull for my work.

Reply

2 elejido April 22, 2009 at 10:16 pm

If the SAME file exists in the remote server, the script overwrite it? append? resume?

Reply

3 Vivek Gite April 23, 2009 at 11:22 am

It will overwrite the file.

Reply

4 me May 19, 2009 at 11:13 am

I love you!

Reply

5 me May 19, 2009 at 12:12 pm

If using the AUTHFILE option, don’t forget to set the following variables in this script

myf=”my-ftp-server.com”
remotedir=”/my/remote/dir”
localdir=”/my/local/dir”

And yes, you will need ncftp installed, if it is not already
For OS X with mac ports,
sudo port install ncftp

Reply

6 Dabidi July 8, 2009 at 9:16 am

How to skip upload existing files on remote server?

Reply

7 Rotx September 10, 2009 at 9:46 am

I am taking server name from a file and everything working fine buy my problem is if a server in the list is not connecting it is not going to next available server. How to get the ftp status in the script if ftp is not connecting how to skip to next available server .

Reply

8 BJones December 13, 2009 at 8:30 pm

Can we add this script to cron and have it auto backup? I am looking to back up entire dir every week, like at 3am, when there is the least amount of traffic, but I don’t want to have to be around for this to happen.

Reply

9 Fred THL December 21, 2009 at 12:22 pm

Why not considering rsync over ssh ?

Reply

10 jamuna December 24, 2009 at 7:20 am

HI please explain it

[ "$remotedir" == "" ] && remotedir=”/” || :
[ "$localdir" == "" ] && localdir=”.” || :

Reply

Previous post:

Next post: