循环拷贝测试USB的脚本

来源:互联网 发布:淘宝饰品店名字 编辑:程序博客网 时间:2024/05/22 19:31

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

主要实现功能:

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

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

说明见方法3。


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

#!/bin/bash DEST_PATH=$1         #The dst path; Usually a /media/U-disk or /homeSOUR_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.gzloop=0while true; doecho begin copy #curl -# file:/$SOUR_PATH$FILE -o $DEST_PATH$FILE     #One method use curlrsync -av --progress $SOUR_PATH$FILE $DEST_PATH      #Second method use rsyncdmesg | grep "Buffer I/O error"if [ $? -eq 0 ];then    echo "error is happen"    exitelse    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 3fi#echo 3 >/proc/sys/vm/drop_cachesdone

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

#!/bin/bash DEST_PATH=$1         #The dst path; Usually a /media/U-disk or /homeSOUR_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.gzloop=0while true; doecho begin copy curl -# file:$SOUR_PATH$FILE -o $DEST_PATH$FILE     #One method use curldmesg | grep "Buffer I/O error"if [ $? -eq 0 ];then    echo "error is happen"    exitelse    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 3fi#echo 3 >/proc/sys/vm/drop_cachesdone


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


#!/bin/bash DEST_PATH=$1         #The dst path; Usually a /media/U-disk or /homeSOUR_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.gzloop=0         #初始化循环次数while true; do        #无限循环echo begin copy cp -rv $SOUR_PATH$FILE  $DEST_PATH     #third method use cpdmesg | 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 3fi#echo 3 >/proc/sys/vm/drop_caches      #清空缓存done








原创粉丝点击