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

2

9

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
Middleoftheroad answered 9/3, 2011 at 12:43 Comment(3)
Seems more like a question for ServerFault than StackOverflow.Hagan
just read that rsync does not work over ftp. Seems I need to figure out how to do it with lftp.Middleoftheroad
ok, now I have it working. However, the 'syncing' is not really very useful. E.g. if I remove a file from one of the folders, it gets downloaded again next time the lftp-script is run. Since it is synchronization I am after, I would of course want it to keep the most recent change and not overwrite it. Is that possible with lftp?Middleoftheroad
L
20

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.

Lakshmi answered 26/8, 2015 at 20:1 Comment(5)
for clarification: first script makes changes server-side, second script – locallyHamza
I get prompted for a password, is there a way to avoid this?Doretheadoretta
there is an error in the second script: you need to change "lcd $LCD" to "cd $RCD"Fawnia
This answer is horribly unclear. I'd be too afraid to replace a folder by an empty one simply because I "synced" in the wrong direction. The description is basically the same ("from local to FTP") for both the scripts.Percentage
if sync the remote FTP server folder to the local folder: mirror --continue --delete --verbose $RCD $LCDPriggery
P
0

if sync the remote FTP server folder to the local folder and using lftp-4.9 above, please try this script:

#!/bin/bash

LFTP_HOME=/home/lftp-4.9.2
#get username and password
REMOTE_FTP_USER="user"
REMOTE_FTP_PASS="passwd"
REMOTE_HOST="ftp-server"
REMOTE_PORT="ftp-pport"
LOCAL_FOLDER="/home/ftpRoot/backup_mirror/"
REMOTE_FOLDER="/"

cd $LOCAL_FOLDER
$LFTP_HOME/bin/lftp -f "
open -p $REMOTE_PORT $REMOTE_HOST
user $REMOTE_FTP_USER $REMOTE_FTP_PASS
mirror -c -e --verbose --target-directory=$LOCAL_FOLDer $REMOTE_FILDER
bye
"
Priggery answered 13/10, 2021 at 2:54 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.