mirror2ftp

(6.1.2013, keywords: bash, lftp, script, ftp, transfer, mirror)

lftp is a file transfer program which has a rsync type options for transferring files to remote sites when rsync is not available. mirror2ftp is a command line shell (a bash script) which is meant to simplify (somewhat) the use of lftp program especially for ftp servers.

If you are familiar with graphical tools, you might be better off using tools like FileZilla, gftp or similar.

requirements

  1. linux (ubuntu, debian, centos, redhat...)
  2. bash
  3. GNU coreutils 7.4
  4. lftp (4.3.3) transfer program

setup

  1. Ensure that lftp has been installed (with yum, aptitude, synaptic...)
  2. Load script (check the contents).
  3. In terminal copy the script to /usr/local/bin directory (input bolded, backslash means that next line belongs to this line):



    user@teac:~/Downloads$ sudo cp mirror2ftp.sh \
    /usr/local/bin/mirror2ftp

    [sudo] password for user: [your-passwd-here]




  4. Set script execution rights:



    user@teac:~/Downloads$ sudo chmod ugo+x /usr/local/bin/mirror2ftp
    user@teac:~/Downloads$ ls -l /usr/local/bin
    total 28
    :
    -rwxr-xr-x. 1 root root 5426 May 19  2013 mirror2ftp
    :




  5. Make test run:



    user@teac:~/Downloads$ mirror2ftp

    Entering mirror2ftp interactive mode.
    Press CTRL+C at any time to cancel.

    Give username for ftp site login and press [ENTER]:
    [CTRL]+[C]



    If something else appears (usually an error message), you probably have to fix the environment. This script checks if lftp is available and will complain if it isn't. Google/Bing/etc. are your friends here.

    Help you get with argument -h:



    user@teac:~/Downloads$ mirror2ftp -h

    Usage:

    mirror2ftp [-u|--username] username] [[-p|--password] password]
                 [-d|--delete] [-v|--verbose]
                 [[-s|--source] local-directory]
                 [[-f|--ftp] ftp-server-name]
                 [[-t|--target] site-directory]

    Arguments are:

        [-u|--username [username]
            account name for ftp site
        [-p|--password password]
            password for account
        [[-s|--source] local-directory]
            local directory from which to mirror (a source)
        [[-f|--ftp ftp-server-name]
            target ftp site (DNS) name or IP address
        [[-t|--target site-directory]
            target ftp site directory
        [-d|--delete]
            delete target files which do not exist in source (not default)
        [-h|--help]
            show these instructions
        [-v|--verbose]
            show more detailed output



usage

You can for example update web pages in server which only allow ftp (such still exists...). You can do this with graphical clients quite well, but with this you transfer only changed files which makes transfer faster.

Example. (backslash means that next line belongs to previous one)


user@teac:~/www$ mirror2ftp -v -d -u [your-username-here] \
    -p [your-passwd-here] \
    -s /home/www/domainname/html/ \
    -f ftp.domainname.com \
    -t public_html/


If you forget to give some essential argument, script starts in interactive mode.

If you leave out delete option (-d), files accumulate in targed end.

Note that target directory is at the same level as the source directory. So example above is copying directory ...domainname/html/ contents to a (relative) directory public_html/. Directory html is not copied under directory public_html.

Also note that while you can use relative URLs (like the target directory in example) you might have to write whole URL. Make first a trial run with small amount of files; especially delete option -d might cause some unpleasant suprises. You might also consider full backup before trying delete option.

script

Version 0.1 (18.5.2013)

As file: mirror2ftp.sh

comments

Last edited: August 27, 2014, 21:36