主机层文件迁移的校验脚本

主机层文件迁移的校验脚本

#!/bin/bash
#声明哈希表组
declare -A file_Source_hashes
declare -A file_Tgarger_hashes
#验证输入路径格式
Regex='^\/[a-zA-Z,_-/]+[^/]$'
#生成日志
# shellcheck disable=SC2034
Time=$(date  '+%m%d%H%M')
LogFile=/root/Comparison_$Time.log

#生成颜色
function color_title()
{
    title_color="echo -en  \\033[01;34m"
    title_end="echo -en \E[0m"
    [ "$2" = 0 ] && ${title_color} 
    echo "$1"
    ${title_end}
}


function color_sign()
{
    RES_COL=-1
    MOVE_TO_COL="echo -en \\033[${RES_COL}G"
    Clock_red="echo -en   \\033[01;31m"
    clock_green="echo -en  \\033[01;32m"
    Clock_yellow="echo -en  \\033[01;33m"
    Clock_end="echo -en \E[0m"
    echo -n "$1" && $MOVE_TO_COL
    echo -n "["
    if [ "$2" = "success" ] || [ "$2" = "0" ];then
        ${clock_green}
        echo -n $"OK "
    elif [ "$2" = "failure" ] || [ "$2" = "1"  ];then 
        ${Clock_red}
        echo -n $"FAILED"
    else
        ${Clock_yellow}
        echo -n $"WARNING"
    fi
    ${Clock_end}
    echo -n "]"
    echo 
}


#获取文件来源并检查
function Check_file_Source()
{
read -r -p "请输入源文件路径或者源文件名称:"  Source_file_name
[[  $Source_file_name =~ $Regex ]] || {  color_sign "Warning: Please enter the full file path (need/start), special characters can include:, -_  " ; exit 1; }
[ -e "$Source_file_name" ] || { color_sign "warning: The directory or file does not exist"; exit 1 ;}

read -r -p "请输入目标文件存在的路径或者源文件名称:" Tgarger_file_name
[[ $Tgarger_file_name =~ $Regex ]] || { color_sign "Warning: Please enter the full file path (need/start), special characters can include:, -_  "  ; exit 1; }
[ -e "$Tgarger_file_name" ]  || { color_sign "warning: The directory or file does not exist  " 1 ; exit 1 ;}

}


#获取源端文件的哈希值与路径
function Source_contact_file() 
{ 
    while read -r A B ;do
    file_Source_hashes["$A"]=$B
    done <<< "$(find "$Source_file_name" \( -type f  -o -type l -type s \) -exec sha256sum {} \;)"

}

#获取目标端文件的哈希值与路径
function Tgarger_contact_file() 
{
    
    while read -r C D ;do
    file_Tgarger_hashes["$C"]=$D
    done <<< "$(find  "$Tgarger_file_name" \( -type f  -o -type l -type s \) -exec sha256sum {} \;)"

}


#对比远端路径与目标端路径的一致性
function Contrast_file()
{
    [ -e "$LogFile" ] || touch "$LogFile"
    total_files="${#file_Source_hashes[@]}"
    current_file=0
    Error_num=0
    #progress=0

    for i in "${!file_Source_hashes[@]}";do
        if [  "${file_Source_hashes["$i"]##*/}" != "${file_Tgarger_hashes["$i"]##*/}" ];then
        # shellcheck disable=SC2129
        color_sign "文件:${file_Source_hashes["$i"]##*/}迁移失败" 1  &>> "$LogFile"
        color_sign "该文件哈希值为:$i"      >> "$LogFile"
        color_sign "路径为:${file_Source_hashes["$i"]}" >> "$LogFile"
        ((Error_num++))
        fi
        ((current_file++))
        #echo "$current_file/$total_files 进度: $((current_file * 100 / total_files))%"
        sleep 0.1
        printf "%d/%d 进度: %d%%\r" "$current_file" "$total_files" "$((current_file * 100 / total_files))"
    done
    color_sign "已完成$total_files个文件校验,不通过文件数为$Error_num,请查看$LogFile" 0

    
}

function main()
{
    Check_file_Source
    Source_contact_file
    Tgarger_contact_file
    Contrast_file
}

main
  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值