linux 递归上传ftp,使用ftp递归地将文件放入远程服务器

因为我总是会遇到这个问题,所以我会把我的笔记贴在这里:

有一件事我总是搞混了,那就是语法;所以下面有一个

bash

测试人员脚本,它创建一些临时目录,然后启动一个临时ftp服务器,并比较

rsync

(在纯本地文件模式下,因为它不支持ftp)

lftp

ftpsync

.

事情是-你可以用

rsync /path/to/local /path/to/remote/

,rsync会自动发现

local

在下创建的子目录

remote

然而,为了

LFTP

FTPSYNC

手动指定目标目录,如

... /path/to/local /path/to/remote/local

(如果它不存在,将创建它)。

以下是

puttest.sh

脚本,显示不同情况下的递归放置行为:

$ bash puttest.sh

Recreate directories; populate loctest, keep srvtest empty:

show dirs:

+ tree --noreport -a /tmp/srvtest /tmp/loctest

/tmp/srvtest

/tmp/loctest

âââ .git

â   âââ tempa2.txt

âââ tempa1.txt

*NOTE, rsync can automatically figure out parent dir:

+ rsync -a --exclude '*.git*' /tmp/loctest /tmp/srvtest/

show dirs:

+ tree --noreport -a /tmp/srvtest /tmp/loctest

/tmp/srvtest

âââ loctest

âââ tempa1.txt

/tmp/loctest

âââ .git

â   âââ tempa2.txt

âââ tempa1.txt

cleanup:

+ rm -rf /tmp/srvtest/loctest

Start a temporary ftp server:

+ sudo bash -c 'python /path/to/pyftpdlib/ftpserver-cli.py --username=user --password=12345 --directory=/tmp/srvtest &'

+ sleep 1

Using: user: user pass: 12345 port: 21 dir: /tmp/srvtest

[I 14-03-02 23:24:01] >>> starting FTP server on 127.0.0.1:21, pid=21549 <<<

[I 14-03-02 23:24:01] poller:

[I 14-03-02 23:24:01] masquerade (NAT) address: None

[I 14-03-02 23:24:01] passive ports: None

[I 14-03-02 23:24:01] use sendfile(2): False

test with lftp:

*NOTE, lftp syncs *contents* of local dir (rsync-like syntax doesn't create target dir):

+ lftp -e 'mirror -R -x ".*\.git.*" /tmp/loctest / ; exit' -u user,12345 127.0.0.1

show dirs:

+ tree --noreport -a /tmp/srvtest /tmp/loctest

/tmp/srvtest

âââ tempa1.txt

/tmp/loctest

âââ .git

â   âââ tempa2.txt

âââ tempa1.txt

cleanup:

+ rm -rf /tmp/srvtest/tempa1.txt

*NOTE, specify lftp target dir explicitly (will be autocreated):

+ lftp -e 'mirror -R -x ".*\.git.*" /tmp/loctest /loctest ; exit' -u user,12345 127.0.0.1

show dirs:

+ tree --noreport -a /tmp/srvtest /tmp/loctest

/tmp/srvtest

âââ loctest

âââ tempa1.txt

/tmp/loctest

âââ .git

â   âââ tempa2.txt

âââ tempa1.txt

cleanup:

+ sudo rm -rf /tmp/srvtest/loctest

*NOTE, ftpsync syncs *contents* of local dir (rsync-like syntax doesn't create target dir); also info mode -i is buggy (it puts, although it shouldn't):

*NOTE, ftpsync --ignoremask is for older unused code; use --exclude instead (but it is buggy; need to change in source)

+ /path/to/ftpsync/ftpsync -i -d '--exclude=.*\.git.*' /tmp/loctest ftp://user:12345@127.0.0.1/

show dirs:

+ tree --noreport -a /tmp/srvtest /tmp/loctest

/tmp/srvtest

âââ tempa1.txt

/tmp/loctest

âââ .git

â   âââ tempa2.txt

âââ tempa1.txt

cleanup:

+ sudo rm -rf /tmp/srvtest/tempa1.txt

*NOTE, specify ftpsync target dir explicitly (will be autocreated):

+ /path/to/ftpsync/ftpsync -i -d '--exclude=.*\.git.*' /tmp/loctest ftp://user:12345@127.0.0.1/loctest

show dirs:

+ tree --noreport -a /tmp/srvtest /tmp/loctest

/tmp/srvtest

âââ loctest

âââ tempa1.txt

/tmp/loctest

âââ .git

â   âââ tempa2.txt

âââ tempa1.txt

cleanup:

+ sudo rm -rf /tmp/srvtest/loctest

+ sudo pkill -f ftpserver-cli.py

这里是

普什图

脚本:

#!/usr/bin/env bash

set -x

# change these to match your installations:

FTPSRVCLIPATH="/path/to/pyftpdlib"

FTPSYNCPATH="/path/to/ftpsync"

{ echo "Recreate directories; populate loctest, keep srvtest empty:"; } 2>/dev/null

sudo rm -rf /tmp/srvtest /tmp/loctest

mkdir /tmp/srvtest

mkdir -p /tmp/loctest/.git

echo aaa > /tmp/loctest/tempa1.txt

echo aaa > /tmp/loctest/.git/tempa2.txt

{ echo "show dirs:"; } 2>/dev/null

tree --noreport -a /tmp/srvtest /tmp/loctest

{ echo -e "\n*NOTE, rsync can automatically figure out parent dir:"; } 2>/dev/null

rsync -a --exclude '*.git*' /tmp/loctest /tmp/srvtest/

{ echo "show dirs:"; } 2>/dev/null

tree --noreport -a /tmp/srvtest /tmp/loctest

{ echo "cleanup:"; } 2>/dev/null

rm -rf /tmp/srvtest/*

{ echo -e "\nStart a temporary ftp server:"; } 2>/dev/null

# https://askubuntu.com/questions/17084/how-do-i-temporarily-run-an-ftp-server

sudo bash -c "python $FTPSRVCLIPATH/ftpserver-cli.py --username=user --password=12345 --directory=/tmp/srvtest &"

sleep 1

{ echo "test with lftp:"; } 2>/dev/null

# see http://russbrooks.com/2010/11/19/lftp-cheetsheet

# The -R switch means "reverse mirror" which means "put" [upload].

{ echo -e "\n*NOTE, lftp syncs *contents* of local dir (rsync-like syntax doesn't create target dir):"; } 2>/dev/null

lftp -e 'mirror -R -x ".*\.git.*" /tmp/loctest / ; exit' -u user,12345 127.0.0.1

{ echo "show dirs:"; } 2>/dev/null

tree --noreport -a /tmp/srvtest /tmp/loctest

{ echo "cleanup:"; } 2>/dev/null

rm -rf /tmp/srvtest/*

{ echo -e "\n*NOTE, specify lftp target dir explicitly (will be autocreated):"; } 2>/dev/null

lftp -e 'mirror -R -x ".*\.git.*" /tmp/loctest /loctest ; exit' -u user,12345 127.0.0.1

{ echo "show dirs:"; } 2>/dev/null

tree --noreport -a /tmp/srvtest /tmp/loctest

{ echo "cleanup:"; } 2>/dev/null

sudo rm -rf /tmp/srvtest/*

{ echo -e "\n*NOTE, ftpsync syncs *contents* of local dir (rsync-like syntax doesn't create target dir); also info mode -i is buggy (it puts, although it shouldn't):"; } 2>/dev/null

{ echo -e "\n*NOTE, ftpsync --ignoremask is for older unused code; use --exclude instead (but it is buggy; need to change ` 'exclude=s' => \$opt::exclude,` in source)"; } 2>/dev/null

$FTPSYNCPATH/ftpsync -i -d --exclude='.*\.git.*' /tmp/loctest ftp://user:12345@127.0.0.1/

{ echo "show dirs:"; } 2>/dev/null

tree --noreport -a /tmp/srvtest /tmp/loctest

{ echo "cleanup:"; } 2>/dev/null

sudo rm -rf /tmp/srvtest/*

{ echo -e "\n*NOTE, specify ftpsync target dir explicitly (will be autocreated):"; } 2>/dev/null

$FTPSYNCPATH/ftpsync -i -d --exclude='.*\.git.*' /tmp/loctest ftp://user:12345@127.0.0.1/loctest

{ echo "show dirs:"; } 2>/dev/null

tree --noreport -a /tmp/srvtest /tmp/loctest

{ echo "cleanup:"; } 2>/dev/null

sudo rm -rf /tmp/srvtest/*

sudo pkill -f ftpserver-cli.py

{ set +x; } 2>/dev/null

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值