使用 Bash 与 7zip 备份文件到阿里云盘

echo "Start:"

folders=(
    "/www/wwwroot/test1"
    "/www/wwwroot/test2"
    "/www/wwwroot/test3"
    "/www/wwwroot/test4"
)

cloudDestPath="/加密备份"

zipPassword="your-zip-password"

# export PATH=$PATH:/root/aliyunpan-linux-amd64

# 在备份开始前删除所有这些路径下的相同命名规则的 zip 文件
for folderPath in ${folders[@]}
do
    folderName=$(basename "$folderPath")
    find $(dirname "$folderPath") -maxdepth 1 -name "$folderName*.zip" -type f -delete
done

for folderPath in ${folders[@]}
do
    # 获取文件夹名称
    folderName=$(basename "$folderPath")

    # 创建压缩包名称
    timestamp=$(date '+%Y%m%d%H%M%S')
    # zipFileName="$folderName-$timestamp.zip"
    zipFileName="$folderName-$timestamp.7z"

    # 获取父路径
    parentPath=$(dirname "$folderPath")

    # 完整的压缩包路径
    zipFilePath="$parentPath/$zipFileName"

    echo "Compress...: $zipFilePath"

    # 执行压缩操作
    # zip 不支持文件名加密
    # 7z a -tzip -p"$zipPassword" -mem=AES256 "$zipFilePath" "$folderPath"
    # -mhe=on 文件头(包括文件名)加密, 没有密码无法看到文件列表
    7z a -t7z -p"$zipPassword" -mhe=on "$zipFilePath" "$folderPath"
    
    # 判断压缩是否成功
    if [ $? -ne 0 ]; then
        echo "Compress failed, deleting incomplete zip file and skipping upload..."
        rm -f "$zipFilePath"
        continue  # 跳过当前循环的剩余部分
    fi

    echo "Compress success"

    echo "Uploading..."

	# https://120365.moeci.com/apps/AlipanCli
	./coo alipan upload --from "$zipFilePath" --to "$cloudDestPath$folderPath/$zipFileName" --remote-drive-type resource

    # 判断上传是否成功
    if [ $? -ne 0 ]; then
        echo "Upload failed, deleting zip file..."
        rm -f "$zipFilePath"
        continue  # 跳过当前循环的剩余部分
    fi

    echo "Upload success"

    # 备份上传完后删除压缩包
    rm "$zipFilePath"

    echo "Delete success"
done

echo "Finished"