使用 shell 脚本复制文件

使用 shell 脚本复制文件

大家好,我需要一个可以从中复制文件的脚本 shell使用 SCP 将服务器迁移至另一台服务器.并将文件放入編輯夾然后从 tmp 复制到目标/product/serv/nep/

如果 tmp 为空,请从备份文件夹复制压缩文件/product/backup_dumps在目的地内提取 /product/serv/nep/. 注意:我需要比较压缩文件并复制较新的文件。然后提取目标文件内的文件. 而这一切

提前谢谢你 :) ^_^

答案1

这是一个带有测试用例的解决方案,关于较新文件的比较,我还没有真正理解其含义:

测试案例1:

## TEST Case if /tmpdir is empty, that means that /mediation from the remote server is empty
## Cleanup
rm -rf /product/serv/nep/*
rm -rf /tmpdir/*
## Creation
mkdir -p /product/backup_dumps/
mkdir /backup 
mkdir /backup/dir
touch /backup/dir/file2
touch /backup/dir/file2
tar -czvf backup.tar.gz /backup
cp backup.tar.gz /product/backup_dumps/
mkdir -p /product/serv/nep/

测试案例2:

## TEST Case if /tmpdir is not empty, that means that /mediation from the remote server is not empty
## Cleanup
rm -rf /tmpdir/*
## Creation
touch /tmpdir/filex
mkdir /tmpdir/dirX 
touch /tmpdir/dirX/filexx

脚本 :

#!/bin/sh
mkdir /tmpdir
scp -r <user>@<serverb>:/mediation/* /tmpdir
## Check whether /tmpdir is empty
if ls -1qA /tmpdir | grep -q .
then  
    ## If /tmpdir is not empty  then copy all the files without structure in /tmpdir to /product/serv/nep/
    find /tmpdir/ -type f | xargs -I {} cp {} /product/serv/nep/
else  
    ## If /tmp is empty, then copy the files in the compressed archive without structure to /product/serv/nep/
    list="$(tar -tvf /product/backup_dumps/backup.tar.gz)"
    echo "$list" 
    while IFS= read -r line
    do
        char="$(echo "$line" | head -c 1)"
        ## Copy only files whithin the archive
        if [ $char != "d" ]
        then 
            file="$(echo "$line" | awk '{print $6}')"
            star -s '|*/||' -x -f /product/backup_dumps/backup.tar.gz $file -C /product/serv/nep/
        fi             
    done <<< "$list" 
fi

相关内容