The following is a bash script I quickly put together to do three things:
- Make a full backup (low level disk image) of a USB pendrive.
- Perform a full restoration to a USB pendrive from a previously created backup image file.
- 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
# 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.
echo "usage: manageUSB [[-f backupfile ] [-d usbdrive] [-b] [-r] [-w]] | [-h]]"
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 "Beginning backup of USB drive..."
dcfldd conv=notrunc,noerror bs=4096 if=$usbDrive | gzip > $backupFilename
echo "Backup of USB drive complete."
echo "Beginning restoration of USB drive..."
gzip -dc $backupFilename | dcfldd of=$usbDrive
echo "Restoration of USB drive complete."
echo "Wiping USB drive..."
dcfldd bs=65536 if=/dev/urandom of=$usbDrive
while [ "$1" != "" ]; do
case $1 in
-f | --file ) shift
-d | --drive ) shift
-b | --backup ) backup
-r | --restore ) restore
-w | --wipe ) wipe
-h | --help ) usage
* ) usage
if [ $# -eq 0 ]; then