Skip to content

Instantly share code, notes, and snippets.

@DrDougPhD
Last active January 19, 2017 18:44
Show Gist options
  • Save DrDougPhD/b8d0cca3fb73826de07cf1bcc74074e9 to your computer and use it in GitHub Desktop.
Save DrDougPhD/b8d0cca3fb73826de07cf1bcc74074e9 to your computer and use it in GitHub Desktop.
Quick 'n' dirty script to recover a disk image and write to a drive.
#!/usr/bin/env bash
#
# Automatically recover a backup to a destination drive.
# NOTE: Must be run as root.
#
# DEPENDENCIES
#
# Lziprecover - for decompressing possibly corrupted lzip archives.
# pv - for status of writing image to drive
# smartmontools - for printing out drive info, used as safety measure to
# allow user to confirm selection
# TODO
#
# Auto-suggest valid files and drives when the user hits tab after an arg.
# Auto-detect archive file type to determine which decompressor to use.
# Verify a file is actually a file.
# Might not be necessary if the user is output simply to a file to be
# created.
# Print out drive smartctl and
function uncompressed_size {
# Calculate the uncompressed byte size of the archive.
#
# Usage: uncompressed_size <archive.file.lz>
if [ "$#" -ne 1 ]
then
echo "Illegal number of parameters: uncompressed_size <archive.file.lz>"
exit 1
fi
# The uncompressed size of the file is extracted from the output of
# lziprecover --list.
#
# e.g. $ lziprecover --list archive.tar.lz
# >> /absolute/path/to/archive.tar.lz: 15.986:1, 0.500 bits/byte, 93.74% saved. decompressed size 256060514304, compressed size 16018250608.
# field to extract: |------------|
# This output is split by spaces.
# Because the file might have spaces, the size is extracted by started from
# the end and counting backwards.
ARCHIVE_FILE="$1"
UNCOMPRESSED_SIZE=$(\
lziprecover --list "$ARCHIVE_FILE" \
| rev \
| cut --delimiter=' ' --field=4 \
| rev \
| tr --delete ,)
echo $UNCOMPRESSED_SIZE
}
# Recover a disk image and write to a drive.
#
# Usage: recover_sisk_img.sh <archive.file.lz> </dev/hdd>
INCORRECT_ARG_COUNT=$( test "$#" -ne 2 )
if [ $INCORRECT_ARG_COUNT ]
then
echo "Illegal number of parameters. Aborting."
echo "Usage: # bash recover_sisk_img.sh <archive.file.lz> </dev/hdd>"
exit 1
fi
# notify user of all errors
#if blah or blah or blah or blah:
#then
# echo "The following errors were encountered. Execution will be aborted."
# if $INCORRECT_ARG_COUNT
# then
# echo "Illegal number of parameters. Aborting."
# fi
# exit 1
#fi
ARCHIVE_FILE="$1"
DRIVE="$2"
echo "Archive: $ARCHIVE_FILE"
echo "Drive: $DRIVE"
# calculate byte size of uncompressed archive
UNCOMPRESSED_SIZE=$(uncompressed_size "$ARCHIVE_FILE")
echo "Bytes to write: $UNCOMPRESSED_SIZE"
# Decompress the archived image and write it to the drive.
echo '-------------------------------------------------------------------------------'
lziprecover --decompress --stdout "$ARCHIVE_FILE" | pv -s $UNCOMPRESSED_SIZE >"$DRIVE"
#echo 'pv -s ' $UNCOMPRESSED_SIZE 'lziprecover --decompress --stdout ' "$COMPRESSED_FILE" "$DRIVE"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment