How to Backup and Restore a USB pendrive (stick) in Ubuntu Linux

USB key

USB key

The following is a bash script I quickly put together to do three things:

  1. Make a full backup (low level disk image) of a USB pendrive.
  2. Perform a full restoration to a USB pendrive from a previously created backup image file.
  3. Wipe a USB pendrive with random data.

The script has two prerequisites:

  • Download and install dcfldd (free open source available from sourceforge.net).
    dcfldd is an enhanced version of GNU dd with extra features useful for forensics and security.
    The program dcfldd has one supreme advantage over the standard dd utility – Status output – dcfldd can update the user of its progress in terms of the amount of data transferred and how much longer operation will take.
  • The program must be run as root in order to access the USB device.

The script has two default values that may be overridden using the command line parameters.

  • The default backup image filename is:  USB_backup_$RIGHT_NOW.gz
    where $RIGHT_NOW has the format :  YYYY-MM-DD_HHhMMmins
  • The default path to USB device is: /dev/sdg1

#!/bin/bash
# The purpose of this script is to provide backup,
# restoration and cleaning services for a USB drive.
# There are three services provided:
# - perform a backup of a USB drive to a file
# (partition) image.
# - restore a USB drive from an file (partition) image
# - wipe a USB drive with random data.
 
function usage
{
   echo "usage: manageUSB [[-f backupfile ] [-d usbdrive] [-b] [-r] [-w]] | [-h]]"
   echo ""
   echo "   -f, --file (optional) name of backup file"
   echo "   -d, --drive (optional) name of USB drive"
   echo "   -b, --backup  perform a backup of the USB"
   echo " -r, --restore    restore a backup to the USB"
   echo "  -w, --wipe      wipe the USB with random data"
   echo ""
}
 
function backup
{
   echo "Beginning backup of USB drive..."
   dcfldd conv=notrunc,noerror bs=4096 if=$usbDrive | gzip > $backupFilename
   echo "Backup of USB drive complete."
}
 
function restore
{
   echo "Beginning restoration of USB drive..."
   gzip -dc $backupFilename | dcfldd of=$usbDrive
   echo "Restoration of USB drive complete."
}
 
function wipe
{
   echo "Wiping USB drive..."
   dcfldd bs=65536 if=/dev/urandom of=$usbDrive
}
 
## Main
 
interactive=
RIGHT_NOW=$(date +"%F_%Hh%Mmins")
backupFilename="USB_backup_$RIGHT_NOW.gz"
usbDrive=/dev/sdg1
 
while [ "$1" != "" ]; do
 case $1 in
   -f | --file )         shift
   backupFilename=$1
   ;;
   -d | --drive )     shift
   usbDrive=$1
   ;;
   -b | --backup )  backup
   exit
   ;;
   -r | --restore )  restore
   exit
   ;;
   -w | --wipe )   wipe
   exit
   ;;
   -h | --help )     usage
   exit
   ;;
   * )             usage
   exit 1
 esac
shift
done
 
if [ $# -eq 0 ]; then
usage
fi

References:

5 comments on “How to Backup and Restore a USB pendrive (stick) in Ubuntu Linux

  1. Its what digests the sandwich you had for lunch, and
    turns that sandwich into living, functioning cells within the body.
    However sometimes the problem is far more serious and becomes chronic and debilitating.

    As a part of the welfare program of their employees, the companies provide special sessions wherein the
    professional instructors teach them some simple exercises which help people in getting rid of these aches and pains.

  2. Great advice with a qualified and caring expert, Samantha
    South. Here I have just listed some primary factors that go ahead
    and take form of stressors and trigger depression.
    The immune system responds in a few ways for the feeling
    of sadness.

  3. Hi, I do believe this is a great web site. I stumbledupon
    it😉 I am going to come back yet again since i have book marked it.
    Money and freedom is the greatest way to change,
    may you be rich and continue to help other people.

  4. peterpants says:

    Computer hoax are false warnings about messages or programs that may do harm to your system. People who distribute the warnings often do so in good faith, and recipients of the warnings often do not know if the warnings are valid. The subsequent armed forces maintain good databases on the warnings that are known to be hoaxes. Some of the services also list “urban legends” which regularly socialize on computer networks, such as calls for get-well cards to be sent to unreal patients. Check with one or more of them before you send all of your friends a copy of the latest caution. It may already be known to be a hoax.

  5. tom says:

    nice script. probably some improvements like:
    function usage
    {
    echo “usage: $0 [[-f backupfile ] [-d usbdrive] [-b] [-r] [-w]] | [-h]]”
    echo “”
    echo ” -f, –file (optional) name of backup file”
    echo ” -d, –drive (optional) name of USB drive”
    echo ” -b, –backup perform a backup of the USB”
    echo ” -r, –restore restore a backup to the USB”
    echo ” -w, –wipe wipe the USB with random data”
    echo “”
    }

    function backup
    {
    echo “Beginning backup of USB drive…”
    dcfldd conv=notrunc,noerror bs=4096 if=$usbDrive | gzip > $backupFilename
    if [ $? -ne 0 ]
    then
    echo “ERROR at backup of USB drive.”
    exit
    fi
    echo “Backup of USB drive complete.”
    }

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s