ftp by shell script

I like to have a local copy of my website. With Wordpress, I write directly into my hosted site. So I have to occassionally ftp my host site to my local drive to keep them in sync. I have asked my site host to backup my sql database weekly. Then I can download it to my local drive and have a local copy.

Sometimes my sql is the only file that has changed on my site. So I have written a shell script that goes and grabs the sql file.

The script is as follows:

#!/bin/sh
HOST=’www.bradrice.com’
USER=’insertlogin’
PASSWD=’insertpassword’
FILE=’mydatabase.sql’
LOCATION=’/Library/Webserver/Websites/¬
  localsite/mydatabase.sql’

ftp -n $HOST < quote USER $USER
quote PASS $PASSWD
get $FILE $LOCATION
quit
END_SCRIPT
exit 0

The trick to this script is using the -n switch. The -n switch disables the auto login feature of ftp that asks you for a user and password. This allows you to feed it to the remote host yourself.

Save the script into a file named ftp_script.sh and change the host,user, password, file and location variables. chmod 755 ftp_script.sh Then run it from your terminal like this ./ftp_script.sh. You should find that it grabs the file you want. You could set this up as a cron script if you want to have it sync a remote file. If you want to use the script to put a file instead of get, just adjust the get command to a put command.

6 Responses to “ftp by shell script”

  1. DS Says:

    I’ve found a better way to upload and download files on a ftp server. Try using curl. It’s fast, easy and gives better results in shell script. For example :
    curl -s -T /Users/me/Sites/index.htm ftp://me:passwd@ftp.server.com/
    lets you send directly the file index.htm you’ve prepared locally in your folder “Sites” to your distant Web server. While :
    curl -s ftp://ftp.server.com/“IMG*.jpg”
    downloads every picture whose name begins with IMG and ends with the extension jpg.
    In your case, you could try :
    curl -s http://www.bradrice.com/mydatabase.sql -o /Library/Webserver/Websites/localsite/mydatabase.sql
    See “man curl” for more…

  2. Ted Says:

    curl is probably the best idea for this sort of thing on OS X.

    However, you should also consider things like ncftpget. ncftp is a great command-line FTP client, and it comes with ncftpget which is an easy way to send command line requests for specific files.

    You can also use rcp, scp, and rsync (as well as a number of other similar apps) for easy ways to pull down files.

    All of these solutions are very portable to other OS’s too.

  3. Aaron Dodd Says:

    I would suggest you chmod 700 since you store your username and password in the file. 755 allows anyone with access to your machine to read and execute your script.

  4. Shawn Holwegner Says:

    Most 4.4-derived ftp clients can use the .netrc file for doing exactly what you’ve stated above.

    A better (in my opinion) solution is to install NcFTP’s clients; ncftpput/ncftpget are designed to be scriptable in a situation where you can not use scp. As the post above me notes curl usage, I just happen to like NcFTP for it’s globbing and other abilities.

  5. Chandrakant Says:

    I want to echo the reply sent by FTP server…
    i used ftp in shell script…
    #! /usr/bin/bash
    server=$1
    user=$2
    password=$3
    ftp -n $server

  6. links for 2007-05-10 » Graham English Social Networking Says:

    […] Mac OS X and Unix tips and tricks » Blog Archive » ftp by shell script Sometimes my sql is the only file that has changed on my site. So I have written a shell script that goes and grabs the sql file. (tags: FTP Mac Apple OSX MySQL Unix tips HowTo tutorials code programming scripts scripting) […]