Skip to content

Instantly share code, notes, and snippets.

@cr1901
Created June 16, 2014 06:20
Show Gist options
  • Save cr1901/4cfa4120fd67cd69de9a to your computer and use it in GitHub Desktop.
Save cr1901/4cfa4120fd67cd69de9a to your computer and use it in GitHub Desktop.
ddrescue command generator
define(`LQ',`changequote(<,>)`dnl'
changequote`'')dnl
define(`RQ',`changequote(<,>)dnl`
'changequote`'')dnl
# m4 -DSRCDRV=$1 -DDESTDIR=$2 -DIMGNAME=$3 -DLOGNAME=$4 ddtemplate > $2/rescue.sh;
if [ -e curr_phase_`'DESTDIR ]; then
CURR_PHASE=`'LQ()`'cat curr_phase_`'DESTDIR`'`'LQ()`'
else
CURR_PHASE=1
fi
DD_PHASE1="ddrescue --no-split SRCDRV IMGNAME LOGNAME"
DD_PHASE2="ddrescue --direct --max-retries=3 SRCDRV IMGNAME LOGNAME"
DD_PHASE3="ddrescue --direct --retrim --max-retries=3 SRCDRV IMGNAME LOGNAME"
#DD_PHASE1=/bin/true
#DD_PHASE2=/bin/true
#DD_PHASE3=/bin/true
define(DD_CMD, `if [ $CURR_PHASE = $2 ]; then
echo "Phase $2... $1"
$1
PHASE_RET=$?
if [ $PHASE_RET = 0 ]; then
echo eval($2+1) > curr_phase_`'DESTDIR`'
CURR_PHASE=eval($2+1)
else
echo "ddrescue phase $2 failed with error code $PHASE_RET..."
exit 1
fi
fi')dnl
DD_CMD($DD_PHASE1, 1)
DD_CMD($DD_PHASE2, 2)
DD_CMD($DD_PHASE3, 3)
echo "ddrescue script completed successfully!"
#!/bin/sh
#Syntax: ./mkrescue [source_drive] [destdir] [image_name] [log_name]
if [ ! $( id -u ) = 0 ]; then
echo "Please run this script as root."
exit 1
fi
if [ ! -e $1 ]; then
echo "Source drive does not exist."
fi
if [ ! -e $2 ]; then
echo "Creating directory $2..."
if mkdir -v $2; then
echo "Ok"
else
echo "Directory creation failed."
exit 2
fi
fi
if [ -e $2/rescue.sh ]; then
echo "Rescue script exists already... not overwriting."
exit 4
fi
if m4 -DSRCDRV=$1 -DDESTDIR=$2 -DIMGNAME=$3 -DLOGNAME=$4 ddtemplate.m4 > $2/rescue.sh; then
chmod 755 $2/rescue.sh
echo "Rescue script ready in $2/rescue.sh"
else
echo "Script creation failed..."
exit 3
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment