现在的位置: 首页 > 综合 > 正文

循环拷贝测试USB的脚本

2013年08月13日 ⁄ 综合 ⁄ 共 2339字 ⁄ 字号 评论关闭

前段时间,进行USB拷贝方面的测试,积累了几个测试脚本,也可以进行磁盘间的拷贝,放在这里备份。

主要实现功能:

从一个存储设备拷贝数据到另一个位置,并比较两次拷贝的数据是否一致,并判断系统是否有错误产生;

采用3种方法,主要是为了直观反映拷贝情况。

说明见方法3。

1、使用rsync命令进行拷贝,以百分比提示,并动态显示文件大小

#!/bin/bash 

DEST_PATH=$1         #The dst path; Usually a /media/U-disk or /home
SOUR_PATH=$2         #The src file path; i.e the dir of testfile.tar.gz 
FILE=$3          #The tar file which will be copy
#eg:run like this: sh -x cp_usb_md5.sh /media/teclast/ /home/loongson/ testdir.tar.gz

loop=0

while true; do
echo begin copy 

#curl -# file:/$SOUR_PATH$FILE -o $DEST_PATH$FILE     #One method use curl
rsync -av --progress $SOUR_PATH$FILE $DEST_PATH      #Second method use rsync

dmesg | grep "Buffer I/O error"


if [ $? -eq 0 ];
then
    echo "error is happen"
    exit
else
    echo compare md5sum
    A=`md5sum $SOUR_PATH$FILE|awk -F ' ' '{print $1}'`
    B=`md5sum $DEST_PATH$FILE|awk -F ' ' '{print $1}'`
    echo $A >> md5A.txt
    echo $B >> md5B.txt
    test  $A == $B  && echo "md5sum1 = md5sum2"||echo "error"

    echo begin rm
    rm -rf $DEST_PATH$FILE
    echo "loop $((loop++))"
    sleep 3
fi

#echo 3 >/proc/sys/vm/drop_caches
done

2、使用curl命令进行拷贝,有进度条提示:

#!/bin/bash 

DEST_PATH=$1         #The dst path; Usually a /media/U-disk or /home
SOUR_PATH=$2         #The src file path; i.e the dir of testfile.tar.gz 
FILE=$3          #The tar file which will be copy
#eg:run like this: sh -x curl_usb_md5.sh /media/teclast/testdir.tar.gz /home/loongson/testdir.tar.gz

loop=0

while true; do
echo begin copy 

curl -# file:$SOUR_PATH$FILE -o $DEST_PATH$FILE     #One method use curl


dmesg | grep "Buffer I/O error"


if [ $? -eq 0 ];
then
    echo "error is happen"
    exit
else
    echo compare md5sum
    A=`md5sum $SOUR_PATH$FILE|awk -F ' ' '{print $1}'`
    B=`md5sum $DEST_PATH$FILE|awk -F ' ' '{print $1}'`
    echo $A >> md5A.txt
    echo $B >> md5B.txt
    test  $A == $B  && echo "md5sum1 = md5sum2"||echo "error"

    echo begin rm
    rm -rf $DEST_PATH$FILE
    echo "loop $((loop++))"
    sleep 3
fi

#echo 3 >/proc/sys/vm/drop_caches
done

3、使用命令进行拷贝,提示文件进度:

#!/bin/bash 

DEST_PATH=$1         #The dst path; Usually a /media/U-disk or /home
SOUR_PATH=$2         #The src file path; i.e the dir of testfile.tar.gz 
FILE=$3          #The tar file which will be copy          #设置输入变量
#eg:run like this: sh -x cp_usb_md5.sh /media/teclast/ /home/loongson/ testdir.tar.gz

loop=0         #初始化循环次数

while true; do        #无限循环
echo begin copy 

cp -rv $SOUR_PATH$FILE  $DEST_PATH     #third method use cp

dmesg | grep "error"          #查看是否有报错情况


if [ $? -eq 0 ];          #判断执行返回值
then
    echo "error is happen"
    exit                        #停止测试
else
    echo compare md5sum             #提取两边数据的md5值,判断是否一致
    A=`md5sum $SOUR_PATH$FILE|awk -F ' ' '{print $1}'`           #其实可以只做一次的
    B=`md5sum $DEST_PATH$FILE|awk -F ' ' '{print $1}'`
    echo $A >> md5A.txt
    echo $B >> md5B.txt
    test  $A == $B  && echo "md5sum1 = md5sum2"||echo "error" 

    echo begin rm
    rm -rf $DEST_PATH$FILE                #删除文件,并加一
    echo "loop $((loop++))"
    sleep 3
fi

#echo 3 >/proc/sys/vm/drop_caches      #清空缓存
done

抱歉!评论已关闭.