Синхронизация файлов на удалённый ftp-сервер

Попалась как-то задачка создать shell-скрипт для автономной синхронизации файлов с локальной Linux-системы на удалённый ftp-сервер.

Первый способ, используя ftp

#!/bin/sh
ftp -n -v -p -i "ftp.remote.domain" << cmd
user "login" "password"
cd /remotedir
lcd /localdir
mput *
quit

Минусы его заключаются в том, что все файлы из локальной директории закачиваются на удалённый ftp-сервер при каждом вызове скрипта. Именно все, а не те, что изменились.


Второй способ, используя lftp

 #!/bin/sh
lftp -p 21 -u login,password ftp.remote.domain/remotedir  << cmd
mirror -e -R -c -v --log=/var/log/lftp.log /localdir/path .
quit

листинг всех параметров lftp mirror:

Usage: mirror [OPTS] [remote [local]]

Mirror specified remote directory to local directory

-c, --continue		continue a mirror job if possible
-e, --delete		delete files not present at remote site
--delete-first		delete old files before transferring new ones
-s, --allow-suid	set suid/sgid bits according to remote site
--allow-chown		try to set owner and group on files
--ignore-time		ignore time when deciding whether to download
-n, --only-newer	download only newer files (-c won't work)
-r, --no-recursion	don't go to subdirectories
-p, --no-perms		don't set file permissions
--no-umask		don't apply umask to file modes
-R, --reverse		reverse mirror (put files)
-L, --dereference	download symbolic links as files
-N, --newer-than=SPEC	download only files newer than specified time
-P, --parallel[=N]	download N files in parallel
-i RX, --include RX	include matching files
-x RX, --exclude RX	exclude matching files
RX is extended regular	expression
-v, --verbose[=N]	verbose operation
--log=FILE		write lftp commands being executed to FILE
--script=FILE		write lftp commands to FILE, but don't execute them
--just-print, --dry-run	same as --script=-

When using -R, the first directory is local and the second is remote.
If the second directory is omitted, basename of first directory is used.
If both directories are omitted, current local and remote directories are used.

Таким образом, на удалённый ftp из локальной директории загружаются только новые файлы. Отсутствующие в локальной директории файлы также удаляются на удалённом ftp сервере. За это отвечает параметр -e

Источник http://cname.org.ua/2010/05/ftp/

Запись опубликована в рубрике *Unix,*Linux, *Web. Добавьте в закладки постоянную ссылку.

Один ответ на “Синхронизация файлов на удалённый ftp-сервер

  1. Eugen говорит:

    Создание локальной копии хостинга.

    #!/bin/sh
    date=`date`;
    echo "ftp_rsync $date";
    /usr/local/bin/lftp -p 21 -u login,pass host < > /usr/home/backup/log/rsync_ftp.log
    quit;

Добавить комментарий для Eugen Отменить ответ

Ваш адрес email не будет опубликован. Обязательные поля помечены *

Я не спамер This plugin created by Alexei91