Linux / Unix: lftp Command Mirror Files and Directories

by on August 5, 2012 · 8 comments· LAST UPDATED August 5, 2012

in ,

How do I mirror files from remote ftp server using lftp command? How do put the entier directory from a local disk to a remote ftp server (reverse mirror) using lftp command under Linux and Unix like operating systems?

Tutorial details
DifficultyEasy
Root privilegesNo
Requirementslftp ftp client

lftp command is a file transfer program that allows sophisticated ftp, http and other connections to other hosts. lftp command has builtin mirror which can download or update a whole directory tree. There is also reverse mirror (mirror -R) which uploads or updates a directory tree on server. Mirror can also synchronize directories between two remote servers, using FXP if available. The syntax is:

 
mirror
mirror options
mirror -c
mirror -R
 

Example: How to mirror files from a remote ftp server (get files)

First, you need to connect to the remote ftp server using the following syntax:
lftp ftp.cyberciti.biz
Type username and password:

lftp ftp.cyberciti.biz:~> user vivek@cyberciti.biz

Sample outputs:

Password:
lftp vivek@cyberciti.biz@rawlogs.bitgravity.com:~>

Type ls command to see a list of files:

lftp ftp.cyberciti.biz:~> ls

Sample outputs:

-rw-r--r--    1 80       www      36809419 Jun 24 23:59 2012-06-24.log.gz
-rw-r--r--    1 80       www      100912271 Jun 25 23:59 2012-06-25.log.gz
-rw-r--r--    1 80       www      102926055 Jun 26 23:59 2012-06-26.log.gz

To mirror all of the above files in the current directory, enter:

lftp ftp.cyberciti.biz:~> mirror

With mirror lftp command you can specify source directory to local target directory. If target directory ends with a slash, the source base name is appended to target directory name. Source and/or target can be URLs pointing to directories. The syntax is:

lftp ftp.cyberciti.biz:~> mirror source target

OR

lftp ftp.cyberciti.biz:~> mirror logs/ /data/wwwlogs

I highly recommend that you start mirror job with resume option. This make sure if ftp mirror was failed due to network connection it will only download partial file

lftp ftp.cyberciti.biz:~> mirror -c source target

OR

lftp ftp.cyberciti.biz:~> mirror --continue

To download only newer files (-c won't work):

lftp ftp.cyberciti.biz:~> mirror --only-newer

OR

lftp ftp.cyberciti.biz:~> mirror -n

You can speed up ftp mirror operation by downloading files in parallel using the following syntax:

lftp ftp.cyberciti.biz:~> mirror -p

To download 10 files in parallel, enter:

lftp ftp.cyberciti.biz:~> mirror --parallel=10

You can use pget to transfer every single file:

lftp ftp.cyberciti.biz:~> mirror -c  --use-pget-n=10

Example: How to reverse mirror files to a remote ftp server (put files)

You need to pass -R or --reverse option to do reverse mirror i.e. put all files from a local disk to a remote ftp server including sub-dirs and all files in it. In this example reverse mirror /home/project/website/version5/ to a remote ftp server called ftp.cyberciti.biz:
First, you need to connect to the remote ftp server using the following syntax:
lftp ftp.cyberciti.biz
Type username and password:

lftp ftp.cyberciti.biz:~> user vivek@cyberciti.biz

Sample outputs:

Password:
lftp vivek@cyberciti.biz@rawlogs.bitgravity.com:~>

Change local directory to /home/project/website/version5/:

lftp ftp.cyberciti.biz:~> lcd /home/project/website/version5/

Sample outputs:

lcd ok, local cwd=/home/project/website/version5

To upload, enter:

lftp ftp.cyberciti.biz:~> mirror -R

OR set local directory and remote upload directory as follows:

lftp ftp.cyberciti.biz:~> mirror -R /home/vivek/projects/website/version10 /var/www/html

When using -R, the first directory is local and the second is remote. If the second directory is omitted, base name of first directory is used. If both directories are omitted, current local and remote directories are used. If target directory ends with a slash (except root directory) then base name of source directory is appended. To quit from ftp session type:

lftp ftp.cyberciti.biz:~> quit

A complete list of lftp mirror command options

Taken from the man page:

 
            -c, --continue      continue a mirror job if possible
            -e, --delete        delete files not present at remote site
                --delete-first       delete old files before transferring new ones
                --depth-first        descend into subdirectories before transferring files
            -s, --allow-suid         set suid/sgid bits according to remote site
                --allow-chown   try to set owner and group on files
                --ascii         use ascii mode transfers (implies --ignore-size)
                --ignore-time        ignore time when deciding whether to download
                --ignore-size        ignore size when deciding whether to download
                --only-missing  download only missing files
                --only-existing download only files already existing at target
            -n, --only-newer    download only newer files (-c won't work)
                --no-empty-dirs don't create empty directories (implies --depth-first)
            -r, --no-recursion  don't go to subdirectories
                --no-symlinks   don't create symbolic links
            -p, --no-perms      don't set file permissions
                --no-umask      don't apply umask to file modes
            -R, --reverse       reverse mirror (put files)
            -L, --dereference   download symbolic links as files
            -N, --newer-than=SPEC    download only files newer than specified time
                --on-change=CMD      execute the command if anything has been changed
                --older-than=SPEC    download only files older than specified time
                --size-range=RANGE   download only files with size in specified range
            -P, --parallel[=N]  download N files in parallel
                --use-pget[-n=N]     use pget to transfer every single file
                --loop          loop until no changes found
            -i RX, --include RX include matching files
            -x RX, --exclude RX exclude matching files
            -I GP, --include-glob GP include matching files
            -X GP, --exclude-glob GP exclude matching files
            -v, --verbose[=level]    verbose operation
                --log=FILE      write lftp commands being executed to FILE
                --script=FILE        write lftp commands to FILE, but don't execute them
                --just-print, --dry-run   same as --script=-
                --use-cache          use cached directory listings
            --Remove-source-files    remove files after transfer (use with caution)
            -a             same as --allow-chown --allow-suid --no-umask
 
 
TwitterFacebookGoogle+PDF versionFound an error/typo on this page? Help us!

{ 8 comments… read them below or add one }

1 kiranjith August 6, 2012 at 6:12 pm

1000 likes…
great stuff

Reply

2 Pothi Kalimuthu August 13, 2012 at 9:41 am

Very interesting tool. Can’t wait to test it out.

Reply

3 charles December 27, 2012 at 3:51 am

Great tutorial!
Thanks

Reply

4 tim May 5, 2013 at 9:40 pm

thx,
Very usefull to make backups from my minecraft server when i am not @ home

:D

Reply

5 Usama Aziz October 2, 2013 at 10:22 pm

Great tutorial! Very simple :)

Reply

6 Alexey Kostin January 13, 2014 at 10:54 pm

The -p option is --no-perms (not --parallel[=N]). I think you mean -P. Check this, please.

Reply

7 Nabyl January 28, 2014 at 12:20 pm

Great tutorial,
Quick question, I’m trying to reverse mirror specific subfolder via cronjobs
I’m using :
lftp sftp://username:password@host -e “cd path1; lcd /path2; mirror -Rn ; quit”
but it get stuck on files that keeps being changed. For instance /var/log/message will keep lftp stuck on transferring at 99%
it there a work around to skip files that are still being open ?
Thanks in advance
Regards

Reply

8 Indian Kumara April 1, 2014 at 8:57 pm

THanks! works very well.

Reply

Leave a Comment

Tagged as: , , , , , , , , , , , , , ,

Previous Faq:

Next Faq: