Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/bin/bash
- # script to help recover/backup optical media
- # -a for audio cd
- # requires ddrescue (sometimes packaged as gddrescue) and cdparanoia
- # usage: cdrec -f mydisc
- # creates directory named mydisc and starts recovery into it
- Z_APP=$(echo "${0}" | rev | cut -d'/' -f1 | rev | cut -d'.' -f1)
- Z_APPPATH=$(dirname "$(realpath "${0}")")
- source "${Z_APPPATH}/ansihelper.sh" # https://pastebin.com/M44TmzAE
- function appmsg() {
- echo "$(str_color green '[')$(str_color cyan "${Z_APP}")$(str_color green ']') ${@}"
- }
- function cleanup() {
- if [ ! -z "${Z_TEMPDIR}" ]; then
- appmsg "Removing previously created temporary project directory \"${Z_PROJECT}\""
- rmdir "${Z_PROJECT_DIR}" 2>/dev/null
- fi
- }
- function apperr() {
- local exitcode=${1};
- shift;
- appmsg "$(str_color lred '[')$(str_color red ERROR )$(str_color lred ']') ${@}"
- cleanup
- exit ${exitcode};
- }
- while getopts "abf:" opts; do
- case "${opts}" in
- a)
- Z_AUDIO=1;
- shift;
- ;;
- b)
- Z_BROWSE=1;
- shift;
- ;;
- f)
- Z_PROJECT=${OPTARG};
- shift 2;
- ;;
- esac
- done
- if [ -z "${Z_PROJECT}" ] && [ -z "${Z_BROWSE}" ]; then
- apperr 1 "Usage: ${Z_APP} [-a] [-b] -f projectname"
- fi
- if [ -z "${Z_PROJECT}" ] && [ ! -z "${Z_BROWSE}" ]; then
- Z_TEMPDIR=1
- Z_PROJECT=zdisc
- appmsg "Using temporary project directory \"${Z_PROJECT}\""
- fi
- Z_PROJECT_DIR="$(realpath "$(dirname "${0}")")/${Z_PROJECT}"
- if [ ! -d "${Z_PROJECT_DIR}" ]; then
- appmsg "Creating ${Z_PROJECT_DIR}"
- mkdir "${Z_PROJECT_DIR}"
- fi
- if [ "${Z_AUDIO}" ]; then
- CMD=(cdparanoia -z -B --)
- elif [ "${Z_BROWSE}" ]; then
- Z_NOCD=1
- CMDS=3
- CMD1=(sudo mount /dev/sr0 "${Z_PROJECT_DIR}")
- CMD2=(mc "${Z_PROJECT_DIR}")
- CMD3=(sudo umount "${Z_PROJECT_DIR}")
- else
- CMD=(ddrescue -d --sector-size=2048 --retry-passes=-1 "${@}" /dev/sr0 "${Z_PROJECT}.iso" "${Z_PROJECT}.map")
- fi
- if [ -z "${Z_NOCD}" ]; then
- appmsg "Entering ${Z_PROJECT_DIR}"
- cd "${Z_PROJECT_DIR}"
- fi
- if [ ! -z "${CMDS}" ]; then
- for n in $(seq 1 "${CMDS}"); do
- Z_CMD=($(eval 'echo ${CMD'$n'[@]}'))
- appmsg "Executing command ${n} of ${CMDS}: ${Z_CMD[@]}"
- "${Z_CMD[@]}"
- Z_EC=$?
- if [ ${Z_EC} -ne 0 ]; then
- apperr ${Z_EC} "Command failed with error code ${Z_EC}"
- fi
- done
- else
- appmsg "Executing command: ${CMD[@]}"
- "${CMD[@]}"
- Z_EC=$?
- fi
- cleanup
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement