Ftp目录整个下载

最近有个需求是要下载ftp接近十个T的数据,在调研过多个工具后发现还是lftp的mirror最省事

mirror参数

复制代码
Mirror specified source directory to local target directory. If target directory ends with a slash, the source base name is appended to target directory name.  Source
       and/or target can be URLs pointing to directories.

            -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
                   --depth-first             descend into subdirectories before transferring files
            -s,    --allow-suid              set suid/sgid bits according to remote site
                   --allow-chown             try to set owner and group on files
                   --ascii                   use ascii mode transfers (implies --ignore-size)
                   --ignore-time             ignore time when deciding whether to download
                   --ignore-size             ignore size when deciding whether to download
                   --only-missing            download only missing files
                   --only-existing           download only files already existing at target
            -n,    --only-newer              download only newer files (-c won't work)
                   --no-empty-dirs           don't create empty directories (implies --depth-first)
            -r,    --no-recursion            don't go to subdirectories
                   --no-symlinks             don't create symbolic links
            -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
                   --on-change=CMD           execute the command if anything has been changed
                   --older-than=SPEC         download only files older than specified time
                   --size-range=RANGE        download only files with size in specified range
            -P,    --parallel[=N]            download N files in parallel
                   --use-pget[-n=N]          use pget to transfer every single file
                   --loop                    loop until no changes found
            -i RX, --include RX              include matching files
            -x RX, --exclude RX              exclude matching files
            -I GP, --include-glob GP         include matching files
            -X GP, --exclude-glob GP         exclude matching files
            -v,    --verbose[=level]         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=-
                   --use-cache               use cached directory listings
                   --Remove-source-files     remove files after transfer (use with caution)
            -a                               same as --allow-chown --allow-suid --no-umask

问题记录

1.虽然mirror支持多线程,我们也是针对三个大目录(其中很多子目录)下载,但是整个过程中list列表比较费时间,建议是直接mirror子目录 这样线程会多一些。

2.注意使用--only-missing参数,其他的参数比如only-newer 不太清楚原因但是会先删掉本地再下载一遍

复制代码
#!/bin/bash

# FTP服务器信息
FTP_HOST="xxxxx"
FTP_USER="xxxx"
FTP_PASS="xxxxxxx"

# 定义要同步的远程和本地目录对

declare -A DIR_MAP=(
["/fumulu/zimulu1"]="/data/0/bendi/fumulu/"
["/fumulu/zimulu2"]="/data/0/bendi/fumulu/"
["/fumulu/zimulu3"]="/data/0/bendi/fumulu/"
["/fumulu/zimulu4"]="/data/0/bendi/fumulu/"
["/fumulu/zimulu5"]="/data/0/bendi/fumulu/"
["/fumulu/zimulu6"]="/data/0/bendi/fumulu/"
["/fumulu/zimulu7"]="/data/0/bendi/fumulu/"
)
# 创建日志目录
LOG_DIR="sync_logs"
mkdir -p "$LOG_DIR"

sync_directory() {
    local remote_dir=$1
    local local_dir=$2
    
    # 生成日志文件名(将目录分隔符替换为下划线)
    local log_name=$(echo "${remote_dir}" | tr '/' '_')
    local log_file="$LOG_DIR/${log_name}sync.log"
    
    # 确保本地目录存在
    mkdir -p "$local_dir"
    
    echo "开始同步 $remote_dir 到 $local_dir..." | tee -a "$log_file"
    echo "同步开始时间: $(date)" >> "$log_file"

    # 使用lftp进行同步操作,添加 --size-only 参数
    temp_log=$(mktemp)
    lftp -c "open -u $FTP_USER,$FTP_PASS $FTP_HOST; \
             mirror --parallel=1000 --verbose --only-missing  $remote_dir $local_dir" 2>&1 | tee -a "$temp_log" "$log_file"
    
    # 检查文件下载失败的情况
    if grep -i "File not available" "$temp_log" > /dev/null; then
        echo "发现文件下载失败,记录到 shibai.txt..."
        # 提取并记录失败的文件信息
        grep -i "File not available" "$temp_log" | while read -r line; do
            # 提取完整的文件路径和文件名
            full_path=$(echo "$line" | grep -o "@.*" | cut -d' ' -f1)
            echo "$full_path" >> shibai.txt
        done
    fi
    
    echo "同步结束时间: $(date)" >> "$log_file"
    echo "----------------------------------------" >> "$log_file"
    
    # 清理临时日志文件
    rm -f "$temp_log"
}

# 同时启动所有同步任务
for remote_dir in "${!DIR_MAP[@]}"; do
    local_dir=${DIR_MAP[$remote_dir]}
    sync_directory "$remote_dir" "$local_dir" &
done

# 等待所有后台任务完成
wait

echo "所有同步任务已完成。"
相关推荐
古希腊数通小白(ip在学)7 分钟前
HCIA实现不同vlan间的通信
linux·服务器·网络
星辰离彬20 分钟前
Java 与 MySQL 性能优化:MySQL连接池参数优化与性能提升
java·服务器·数据库·后端·mysql·性能优化
半桔21 分钟前
【Linux手册】从接口到管理:Linux文件系统的核心操作指南
android·java·linux·开发语言·面试·系统架构
禁默29 分钟前
Linux Vim 编辑器详解:从入门到进阶(含图示+插件推荐)
linux·vim·excel
小苹果13572 小时前
阿里云mysql数据丢失,如何通过服务器备份在其他服务器上恢复数据,并获取mysql丢失数据,完成mysql数据恢复
服务器·mysql·阿里云
许白掰2 小时前
Linux入门篇学习——Linux 工具之 make 工具和 makefile 文件
linux·运维·服务器·前端·学习·编辑器
hrrrrb3 小时前
【TCP/IP】12. 文件传输协议
服务器·网络·tcp/ip
张璐月3 小时前
mysql join语句、全表扫描 执行优化与访问冷数据对内存命中率的影响
数据库·mysql
全干engineer5 小时前
ClickHouse 入门详解:它到底是什么、优缺点、和主流数据库对比、适合哪些场景?
数据库·clickhouse