Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Syntax for using lftp to synchronize local folder with an ftp folder?

I would like to synchronize two folders with each other. It should go two ways, always keeping the folders up to date (I use a regular cronjob). However, first I do not get the two way file transfer to work (it just downloads from the ftp and not the opposite).

Secondly, it downloads the whole content from the ftp, even though the login information has been set up on the ftp so that access is only restricted to a specific folder. Why??

Here is the code (thanks in advance!):

#!/bin/bash

#get username and password
USER=username
PASS=password

HOST="myftpserver.com/users/user1/" #here I have tried with only specifying server name as well as including whole path
LCD="~/Desktop/localfolder/"
RCD="users/user1/"

lftp -c "set ftp:list-options -a;
open ftp://$USER:$PASS@$HOST; 
lcd $LCD;
mirror -c --reverse --verbose $LCD $RCD" #I have tried a few different options w/o result
like image 855
Toby Avatar asked Mar 09 '11 12:03

Toby


People also ask

What is lftp command?

lftp is a file transfer program that allows sophisticated ftp, http and other connections to other hosts. If site is specified then lftp will connect to that site otherwise a connection has to be established with the open command.

How does the lftp work?

lftp command (lftp Manuel Page) is a file transfer program that allows sophisticated , HTTP, and other connections to other hosts. lftp command has a built-in mirror that can download or update a whole directory tree. There is also a reverse mirror (mirror -R) that uploads or updates a directory tree on the server.

What is lftp mirror?

The Linux lftp command is a sophisticated FTP/HTTP client that is also capable of being able to mirror the entire contents of a remote website to your local server, or vice-versa. Using the lftp command on your server can help save you time when copying website files from a remote server.


1 Answers

You probably don't need this anymore (4 years late) but I'll just update this, and if someone get's here with the same issue here's a help.

Local directory to FTP server directory

If you want to sync the FTP server folder with the content in your folder you should use something like this

#!/bin/bash

#get username and password
USER=username                   #Your username
PASS=password                   #Your password
HOST="myftpserver.com"          #Keep just the address
LCD="~/Desktop/localfolder"     #Your local directory
RCD="/users/user"               #FTP server directory

lftp -f "
open $HOST
user $USER $PASS
lcd $LCD
mirror --continue --reverse --delete --verbose $LCD $RCD
bye
" 

FTP server directory to your local directory

Simply remove the --reverse and swap the folders in the mirror command.

#!/bin/bash

#get username and password
USER=username                   #Your username
PASS=password                   #Your password
HOST="myftpserver.com"          #Keep just the address
LCD="~/Desktop/localfolder"     #Your local directory
RCD="/users/user"               #FTP server directory

lftp -f "
open $HOST
user $USER $PASS
lcd $LCD
mirror --continue --delete --verbose $RCD $LCD
bye
" 

To do something like you commented in the question, sync both ways and keep the most updated value from each, i don't believe it's possible using lftp alone you'll need something to detect the change and decide which script use.

like image 198
Lucas Farias Avatar answered Sep 26 '22 15:09

Lucas Farias