自动化 SCP 将文件从多个目录(括号内)复制到适当的目录

Automate SCP copy files from multiple directories (in brackets) to appropraite directories

我有一个 bash 脚本用于从远程主机的不同目录复制一些文件。他们都有相同的parent。所以我把它们放到列表中:

LIST=\{ADIR, BDIR, CDIR\}

我使用 scp 命令

sshpass -p  scp -o LogLevel=debug -r @192.168.121.1$/PATH/$LIST/*.txt /home/test/test

该命令使我能够将所有 .txt 文件从 ADIR、BDIR、CDIR 复制到我的测试目录。是否有任何选项可以将所有 .txt 文件放在适当的目录中,如 /home/test/test/ADIR 或 /home/test/test/BDIR ...?

您是否考虑过使用 rsync

您可以尝试以下方法:

# Rsync Options
# -a, --archive               archive mode; equals -rlptgoD (no -H,-A,-X)
# -D                          same as --devices --specials
# -g, --group                 preserve group
# -l, --links                 copy symlinks as symlinks
# -o, --owner                 preserve owner (super-user only)
# -O, --omit-dir-times        omit directories from --times
# -p, --perms                 preserve permissions
# -r, --recursive             recurse into directories
# -t, --times                 preserve modification times
# -u, --update                skip files that are newer on the receiver
# -v, --verbose               increase verbosity
# -z, --compress              compress file data during the transfer


for DIR in 'ADIR' 'BDIR' 'CDIR'
do
        rsync -zavu --rsh="ssh -l {username}" 192.168.121.1:/$PATH/$DIR /home/test/test/
done

最后是我的工作代码:

SOURCE='/usr/.../'
DEST='/home/test/test'
DIRS_EXCLUDED='test/ADIR test/BDIR'
EXTENSIONS_EXCLUDED='*.NTX *.EXE'

EXCLUDED_STRING=''

for DIR in $DIRS_EXCLUDED
do
    EXCLUDED_STRING=$EXCLUDED_STRING'--exclude '"$DIR"' '
done

for EXTENSION in $EXTENSIONS_EXCLUDED
do
    EXCLUDED_STRING=$EXCLUDED_STRING'--exclude '"$EXTENSION"' '
done

rsync -zavu $EXCLUDED_STRING --rsh="sshpass -p  ssh -l " 192.168.xxx.xxx:$SOURCE $DEST