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



