Macro 32 Ramblings

Mind Archive

Linux / Unix: lftp Command Mirror Files and DirectoriesLinux / Unix: lftp Command Mirror Files and Directories

From: https://www.cyberciti.biz/faq/lftp-mirror-example/

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
Difficulty Easy
Root privileges No
Requirements lftp 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