Copy the USB script cyclically

Source: Internet
Author: User
Tags gz file

Some time ago, we performed a USB copy test and accumulated several test scripts. We can also copy data between disks and store them here for backup.

Main functions:

Copy data from one storage device to another, compare the two copies to see if they are consistent, and determine whether an error occurs in the system;

Three methods are used to intuitively reflect the copy situation.

For more information, see method 3.

1. Run the rsync command to copy the file, and display the file size dynamically as a percentage:

#!/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. Run the curl command to copy the file. a progress bar prompt is displayed:

#!/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. Run the following command to copy the file:

#! /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 # Set the input variable # eg: run like this: sh-x cp_usb_md5.sh/Media/teclast // home/loongson/testdir.tar.gz loop = 0 # Number of initialization cycles while true; do # infinite loop echo begin copy CP-RV $ sour_path $ File $ dest_path # Third method use cpdmesg | grep "error" # Check whether an error is reported Case if [$? -EQ 0]; # determine the execution return value then Echo "error is happen" exit # Stop the test else echo compare md5sum # extract the MD5 value of the data on both sides, determine if it is consistent A = 'md5sum $ sour_path $ file | awk-f''' {print $1} ''# In fact, only one B = 'md5sum $ dest_path $ file can be used. | 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 # delete an object, add echo "loop $ (loop ++)" Sleep 3fi # echo 3>/proc/sys/Vm/drop_caches # Clear cache done

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.