Mirror ftp sites using lftp








The shell script below can mirror two ftp sites:
#!/bin/bash
     
    usage() {
      echo ""
      echo "USAGE: `basename $0` <src_ftp_address> <dst_ftp_address>"
      echo "DESCRIPTION:"
      echo "             ftp_address  is in the format of ftp://username:password@host:port/path, e.g. ftp://wilson:pass@168.site90.net:21/public_html"
      echo ""
    }
     
    # lftp is required
    if [[ -z `which lftp` ]]; then
      echo 1>&2 "Error: could not find lftp."
      exit 1
    fi
     
    # save current path
    CWD=`pwd`
     
    # check arguments
    if [[ $# -ne 2 ]]; then
      echo 1>&2 "Error: invalid arguments."
      usage
      exit 1
    fi
     
    # parse src address
    s=$1
    [[ $s == ftp://* ]] && s=${s:6}
    s1=`echo $s | awk -F @ '{print \$1}'`
    s2=`echo $s | awk -F @ '{print \$2}'`
    SRC_FTP_USER=`echo $s1 | awk -F : '{print \$1}'`
    SRC_FTP_PASS=`echo $s1 | awk -F : '{print \$2}'`
    SRC_FTP_HOST=`echo $s2 | awk -F : '{print \$1}'`
    s3=`echo $s2 | awk -F : '{print \$2}'`
    i=`expr index $s3 /`
    SRC_FTP_PORT=${s3:0:$((i-1))}
    [[ -z $SRC_FTP_PORT ]] && SRC_FTP_PORT=21
    SRC_FTP_PATH=${s3:$((i-1))}
    SRC_FTP_ADDR="ftp://${FTP_HOST}:${FTP_PORT}${FTP_PATH}"
     
    # parse dst address
    s=$2
    [[ $s == ftp://* ]] && s=${s:6}
    s1=`echo $s | awk -F @ '{print \$1}'`
    s2=`echo $s | awk -F @ '{print \$2}'`
    DST_FTP_USER=`echo $s1 | awk -F : '{print \$1}'`
    DST_FTP_PASS=`echo $s1 | awk -F : '{print \$2}'`
    DST_FTP_HOST=`echo $s2 | awk -F : '{print \$1}'`
    s3=`echo $s2 | awk -F : '{print \$2}'`
    i=`expr index $s3 /`
    DST_FTP_PORT=${s3:0:$((i-1))}
    [[ -z $DST_FTP_PORT ]] && DST_FTP_PORT=21
    DST_FTP_PATH=${s3:$((i-1))}
    DST_FTP_ADDR="ftp://${FTP_HOST}:${FTP_PORT}${FTP_PATH}"
     
    TMP_DIR=`export TMPDIR=$CWD; mktemp -d`
     
    # download from src ftp server to local temp dir.
    echo "Downloading from $SRC_FTP_HOST..."
    lftp -c "set ftp:list-options -a; 
             open -u $SRC_FTP_USER,$SRC_FTP_PASS -p $SRC_FTP_PORT $SRC_FTP_HOST;
             lcd $TMP_DIR;
             cd $SRC_FTP_PATH;
             mirror --delete  --verbose"
     
    # upload from local temp dir to dst ftp server
    echo "Uploading to $DST_FTP_HOST..."
    lftp -c "set ftp:list-options -a;
             open -u $DST_FTP_USER,$DST_FTP_PASS -p $DST_FTP_PORT $DST_FTP_HOST;
             lcd $TMP_DIR;
             cd $DST_FTP_PATH;
             mirror --delete --reverse --verbose"
     
    # remove temp directory
    rm -fr $TMP_DIR


see also

Stackoverflow.com





No comments:

Post a Comment