Mirroring FTP directories with lftp

Since a while, I’ve been looking for a simple way to sync/mirror a local directory with an FTP directory. In this case, to update dickson.tf without going through FileZilla every time.

Turns out, there is a program for linux called lftp which will do just that, and quite easily.

The way I’ve done it is to create a script that will manage this, and launch it when I want to send an update:

#! /bin/bash
echo "Updating example.com"
HOST='ftp.example.com'
USER='YourUser'
PASS='YourPassword'
TARGETFOLDER='/your/remote_dir'
SOURCEFOLDER='/home/user/local_dir'

lftp -f "
open $HOST
user $USER $PASS
mirror --reverse \ # reverse mirror = put files
 --newer-only # only files that have been changed
 --delete \ # delete files that no longer exist
 --use-cache \ # can be faster
 --verbose \
 --exclude-glob .* \ # exclude files that start with .
 $SOURCEFOLDER $TARGETFOLDER \
"
echo "done"

This will sync all files from /home/user/local/dir towards /your/remote_dir of the remote server, creating a mirror copy.

You can then put this in a cron job to automatically update any changes every day:

@daily /home/user/mirror_site.sh

or anything else you fancy.

 

This entry was posted in Software, WebDev and tagged , , , , , . Bookmark the permalink.

Leave a Reply

Your email address will not be published. Required fields are marked *