How to automatically sync the contents of a local folder with the contents of a ftp folder?

35,721

First solution

Install lftp

sudo apt-get install lftp

after that create script

#!/bin/bash
HOST='mysite.com'
USER='myuser'
PASS='myuser'
TARGETFOLDER='/new'
SOURCEFOLDER='/home/myuser/backups'
 
lftp -f "
open $HOST
user $USER $PASS
lcd $SOURCEFOLDER
mirror --reverse --delete --verbose $SOURCEFOLDER $TARGETFOLDER
bye
"

Save it on some place with name upload.sh. Give it +x permission.

sudo chmod +x /path_to_script/upload.sh

Setup crontab to run this command on every x period of time

For editing crontab run

crontab -e

For running command on every 5 min code is

 */5 * * * * /path_to_script/upload.sh

on every hour

 0 */1 * * * /path_to_script/upload.sh

to run on 4 am

 0 4 * * * /path_to_script/upload.sh

Solution two

Create a small and easy script called lftp-script that LFTP can read:

open ftp://username:[email protected]
mirror --reverse -v --only-newer /home/local/path/ /website.com/public_html/

Finally you can run LFTP and start the synchronisation. Set crontab like I write but put command

lftp -f /path/to/lftp-script

something like this

 */5 * * * * lftp -f /path/to/lftp-script

Solution three

Install curlftpfs

sudo apt-get install curlftpfs

you need to do in order to mount ftp locally is to to run these commands create dir witch will be sync-ed

mkdir hostr

mount remote ftp dir to local

sudo curlftpfs -o allow_other ftp://user:[email protected] host

user:pass is the username and password to log into ftp account.

You can add curlftpfs to fstab for automatic mounting by using this line :

curlftpfs#user:[email protected] /mnt/host fuse rw,uid=500,user,noauto 0 0
Share:
35,721

Related videos on Youtube

Edward Nunn
Author by

Edward Nunn

Updated on September 18, 2022

Comments

  • Edward Nunn
    Edward Nunn almost 2 years

    After searching everywhere I have not managed to find a specific answer for my question.

    On Windows there is an application called FTPbox that will automatically sync a local folder with an ftp one every set period of time.

    My question is, how do I achieve the same thing using Ubuntu? The main thing is here that I dont want to have to do it manually. It needs to be a totally automatic process with no human intervention required (obviously I will have to set this up if that classes as human intervention LOL).

    Many thanks in anticipation for helpful answers.

    -Edward

  • Edward Nunn
    Edward Nunn about 8 years
    TARGETFOLDER='/new' = folder on ftp? SOURCEFOLDER='/home/myuser/backups' = local folder with files i want to upload?
  • 2707974
    2707974 about 8 years
    yes and yes. :)
  • Edward Nunn
    Edward Nunn about 8 years
    saying this Transferring file Untitled 1.odt' mirror: /home/edward/bl3/bl2/bl1/Untitled 1.odt: No such file or directory Unknown command ;'
  • 2707974
    2707974 about 8 years
    Based on ftp server path meybe not only /new folder. Check both path.
  • 2707974
    2707974 about 8 years
    If you create folder in source folder on local, that folder must be sync-ed with ftp folder.
  • mr.bjerre
    mr.bjerre over 4 years
    Very neat! Is it possible to move the files? I.e. remove files that has already been uploaded? Need some way of knowing whether a file has been uploaded or not. Also; what happens if connection are lost in the different solutions? Will it retry to connect and how does one get notified?