Bash Script for Mirroring FTP Accounts

I wrote a quick ‘n dirty FTP mirroring shell script. It assumes that the accounts on the original server and the new server are the same. I wrote this script for a buddy who needed to move over 300+ accounts, but the servers didn’t support FXP. I’m sure this could have been done better, or more effectively, but it works.

The script writes two temp files to the user’s home directory, and one file to ~/tmp/. These directories can be changed easily if need be.

I hope you find it useful!


#!/bin/sh
# did the user supply username and password?
if [ $# -ne 2 ]; then
echo “Usage ./ftpxfer.sh <username> <password>”
exit 1
fi

# login info
USERNAME=$1
PASSWORD=$2
# server to get ftp user data from
FROMSERVER=’from.example.com’
# server to put ftp user data on
TOSERVER=’to.example.com’

echo “Starting Transfer”

# set commands needed to move files from first ftp
cat >> ~/fromftp.cmd <<-FROMEOF
open $FROMSERVER
user $USERNAME $PASSWORD
mirror ~ ~/tmp/web
quit
FROMEOF
# initiate the ftp session
echo "Grabbing files from $FROMSERVER (u: $USERNAME / p: $PASSWORD)"
lftp -f ~/fromftp.cmd
echo "Files received. Now sending files to $TOSERVER"

# getting the files is done, now to put then on the new server

cat >> ~/toftp.cmd <<-TOEOF
open $TOSERVER
user $USERNAME $PASSWORD
mirror -R ~/tmp/web ~
quit
TOEOF
# initiate the ftp session
lftp -f ~/toftp.cmd

# done
echo "Transfer completed."

# cleanup after the script
rm ~/fromftp.cmd
rm ~/toftp.cmd
rm -rf ~/tmp/web

exit 0

No Comments

Got something to say?

Blah blah blah...




You can use these tags in your comments:
<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

Browse at your leisure...

Check out the archives for your reading pleasure.

There's also a section if you're interested about me. You may also contact me, if you're so inclined.

You may also want to take a gander at the blogs I read if you tire of my ramblings.

Asides

View More Asides...

Colophon

Powered by WordPress
fullduplex.org (c) 2001-2006.
All rights reserved.
Proper XHTML & CSS.
RSS Feed. 30 queries. 0.051 sec.