DISK=/dev/sdd PART=${DISK}2 LOG=todo IMG=backup.img while true; do # Run ddrescue, and kill it after 35 seconds (sleep 35; killall ddrescue)& ddrescue -d "$PART" "$IMG" "$LOG" -v -v # exit code 143 indicates killed by SIGTERM, it seems # Stop the backup if ddrescue exited with another error code (or # succesfullly), or if the logfile has no more sectors to do. if [ "$?" -ne 143 ] || ddrescuelog -D "$LOG"; then break; fi # Spindown disk, sometimes needed twice... hdparm -y "$DISK" hdparm -y "$DISK" sleep 5 # Cycle power (this hardcodes the port!) ./uhubctl/uhubctl -l 1-1.3 -p 3 -a cycle -d 5 # $DISK stays around during the powercycle and is only removed at # insertion, so wait a bit for that to happen. sleep 5 while ! [ -e "$DISK" ]; do sleep 1; done; sleep 2 done echo "DONE"