Because an actual prepared solution is asked for:
#!/usr/bin/zsh # Copyright 2022 Marcus Müller # SPDX-License-Identifier: BSD-3-Clause # Find the license text under https://spdx.org/licenses/BSD-3-Clause.html # THIS SCRIPT IS UNTESTED AND COMES WITH NO WARRANTIES, FOLKS. IN_DEVICE=/dev/yoursource_ssd BACKUP_IMG=myimage LOGFILE=broken_mbs.txt # get size, round up to full MB size_in_bytes=$(blockdev "${IN_DEVICE}") size_in_MB=$(( ( ${size_in_bytes} + 2**20 - 1) / 2**20 )) #check whether size > 0 if [[ ! ${size_in_MB} -gt 0 ]]; then logger -p user.crit "Nope, can't determine size of ${IN_DEVICE}. I'm outta here." echo "Failure on input" >&2 exit -1 else logger -p user.info "Trying to back up ${IN_DEVICE}, size ${size_in_MB} MB" fi if fallocate -l "${size_in_MB}MiB" "${BACKUP_IMG}" ; then logger -p user.info "preallocated ${BACKUP_IMG}" else logger -p user.crit "failed to preallocate ${BACKUP_IMG}" echo "failure on output" >&2 exit -2 fi failcounter=0 MB=$((2**20)) for i in {0..$((${size_in_MB}-1))}; do if \ dd \ "if=${IN_DEVICE}" \ "of=${BACKUP_IMG}" \ "ibs=${MB}" "obs=${MB}" \ "skip=${i}" "seek=${i}" ; \ then echo "backed up MB nr. ${i}" else failcounter=$(( ${failcounter} + 1 )) echo "${failcounter}. error: couldn't backup MB nr. $i" > &2 echo "${i}" >> ${LOGFILE} logger -p user.err "couldn't backup MB nr. $i" fi sleep 0.5 done echo "Got ${failcounter} failures" exit ${failcounter}