This is an old revision of the document!


Synchronization with lftp

This page talk about folder synchronization with lftp.

Installation

Install lftp package.

Configuration

There is no specific configuration to do but bookmarks can simplify the use of lftp.

Utilisation : bookmark [SOUS-COMMANDE]
La commande bookmark contrôle les signets

Les sous-commandes suivantes sont valides :
  add <nom> [<url>]  - ajoute l'url en cours ou l'url indiquée aux signets
                       et l'associe au nom donné
  del <nom>          - supprime le signet
  edit               - démarre l'éditeur de signets
  import <type>      - importe un fichier de signets externe
  list               - affiche les signets (par défaut)

Example :

lftp :/> bookmark add site.com ftp://user:pass@ftp.site.com
lftp :/> bookmark list
site.com                        ftp://user:XXXX@ftp.site.com
lftp :/>

It's possible to use bookmarks with the “connect” command or directly on the command line :

lftp site.com

Manual synchronization

Before automatic synchronization, it's advised to do some tests manually.
:!: Be carreful, there is a risk to delete source file by mistakes.

Synchronization is done with “mirror” :

lftp matthieu@gandalf.bouthors.fr:/> help mirror
Utilisation : mirror [OPTS] [distant [local]]

Duplique le répertoire distant spécifié vers le répertoire local

 -c, --continue         continue, si c'est possible, un travail de duplication
 -e, --delete           efface les fichiers absents sur le site distant
     --delete-first     efface les anciens fichiers avant d'en transférer                         de nouveaux
 -s, --allow-suid       duplique aussi les bits suid/sgid
     --allow-chown      essaye de dupliquer aussi les propriétaires
     --ignore-time      ignore l'heure pour décider ou non du téléchargement
 -n, --only-newer       télécharge uniquement les nouveaux fichiers
 -r, --no-recursion     n'entre pas dans les sous-répertoires
 -p, --no-perms         ne définit pas les permissions de fichier
     --no-umask         n'applique pas umask aux modes de fichier
 -R, --reverse          duplication inversée (émet les fichiers)
 -L, --dereference      télécharge les liens symboliques comme des fichiers
 -N, --newer-than=SPEC  télécharge uniquement les fichiers plus jeunes que
                        la date et l'heure spécifiées par SPEC
 -P, --parallel[=N]     télécharge N fichiers en parallèle
 -i RX, --include RX    inclut les fichiers correspondant à RX
 -x RX, --exclude RX    exclut les fichiers correspondant à RX
                        RX est une expression régulière étendue
 -v, --verbose[=N]      mode volubile
     --log=FILE         écrit dans FILE les commandes exécutées par lftp
     --script=FILE      écrit dans FILE les commandes à exécuter par lftp
                        mais ne les exécute pas.
     --just-print, --dry-run    identique à --script=-

Lorsque vous utilisez -R, le premier répertoire est local et le second est
distant. Si le second répertoire est omis, le nom de base du premier est
utilisé. Si les deux répertoires sont omis, les répertoires courants local
et distant sont utilisés.

The option –dry-run is usefull to make some tests without risks.

Some example of synchronisation (download) :

mirror /Data/Photos /data/transfert/photos -v --log /var/log/lftp/download_nas --parallel=10 --no-perms -e
  • /Data/Photos is the source folder located on the remote server
  • /data/transfert/photos is the local destination folder
  • -v activate the verbose mode
  • –log option set the file to use for logs, this file is overrided each time and is not used with –dry-run
  • –parallel activate parralel transferts, usefull with small files
  • –no-perms disable permission synchronization
  • -e delete files on the destination folder that are no more present on the source

Another example (upload) :

mirror -R /space/Videos/ /data/videos/ --ignore-time --no-perms -e -v
  • -R activate the reverse mode (upload)
  • /space/Videos/ is the local source
  • /data/videos/ is the destination folder on the remote server
  • –ignore-time disable date check used to determined if the file need to be synchronized (only the file size is used)
  • –no-perms disable permission synchronization
  • -e delete files on the destination folder that are no more present on the source
  • -v activate the verbose mode

When uploading, it is not possible to set the date/time on the files uploaded, that's why –ignore-time is needed.

When the complete command is defined, it's possible to use it inside an lftp script.

Example :

connect nas
mirror /Data/Photos /data/transfert/photos -v --log /var/log/lftp/download_nas --parallel=10 --no-perms -e

To use the script, launch lftp with -f option.

Automatic synchronization

To run automaticaly the synchronization, please find below a small batch that will launch lftp.
lftp is started in background like a daemon.

To use the script, you need to :

  1. copy it inside a new file
  2. change NAME variable as you like
  3. specify the link to your lftp script inside DAEMON_OPTS
  4. make the script executable

The script has the following options :

  • start : start the synchronization
  • stop : stop the daemon
  • restart : restart the daemin
  • check : indicate if the synchronization is running or not

Script :

#!/bin/sh

# Quick start-stop-daemon example, derived from Debian /etc/init.d/ssh
set -e

# Must be a valid filename
NAME=lftp_synchro
PIDFILE=/var/run/$NAME.pid
#This is the command to be run, give the full pathname
DAEMON=/usr/bin/lftp
DAEMON_OPTS="-f /chemin/vers/cmd.lftp"

export PATH="${PATH:+$PATH:}/usr/sbin:/sbin"

# check if lftp is running

RUNNING=false
if [ -e $PIDFILE ]
then
        currentpid=`cat $PIDFILE`
        if `ps -p $currentpid > /dev/null`
        then
                RUNNING=true
        else
                rm -f $PIDFILE
        fi
fi

case "$1" in
  start)
        date
        if $RUNNING
        then
                started=`date -r $PIDFILE`
                echo "daemon $NAME already started since $started, you need to stop it first"
        else
                echo -n "Starting daemon: "$NAME
                start-stop-daemon --start --quiet --pidfile $PIDFILE --make-pidfile --background --exec $DAEMON -- $DAEMON_OPTS
                echo "."
        fi
        ;;
  stop)
        date
        echo -n "Stopping daemon: "$NAME
        start-stop-daemon --stop --quiet --oknodo --pidfile $PIDFILE
        rm -f $PIDFILE
        echo "."
        ;;
  restart)
        date
                echo -n "Restarting daemon: "$NAME
        if $RUNNING
        then
                start-stop-daemon --stop --quiet --oknodo --retry 30 --pidfile $PIDFILE
                rm -f $PIDFILE
        fi
        start-stop-daemon --start --quiet --pidfile $PIDFILE --make-pidfile --background --exec $DAEMON -- $DAEMON_OPTS
        echo "."
        ;;
  check)
        if $RUNNING
        then
                echo "$NAME is running"
        else
                echo "$NAME is not running"
        fi
        ;;

  *)
        echo "Usage: "$1" {start|stop|restart|check}"
        exit 1
esac

exit 0
en/linux/synchro_lftp.1267909620.txt.gz · Last modified: 2010/03/06 22:07 by matthieu
Recent changes RSS feed Debian Powered by PHP Valid XHTML 1.0 Valid CSS Driven by DokuWiki