Advertisement
zefie

cdrec

Dec 26th, 2019
488
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 2.30 KB | None | 0 0
  1. #!/bin/bash
  2. # script to help recover/backup optical media
  3. # -a for audio cd
  4.  
  5. # requires ddrescue (sometimes packaged as gddrescue) and cdparanoia
  6.  
  7. # usage: cdrec -f mydisc
  8. # creates directory named mydisc and starts recovery into it
  9.  
  10. Z_APP=$(echo "${0}" | rev | cut -d'/' -f1 | rev | cut -d'.' -f1)
  11. Z_APPPATH=$(dirname "$(realpath "${0}")")
  12. source "${Z_APPPATH}/ansihelper.sh" # https://pastebin.com/M44TmzAE
  13.  
  14. function appmsg() {
  15.     echo "$(str_color green '[')$(str_color cyan "${Z_APP}")$(str_color green ']') ${@}"
  16. }
  17.  
  18. function cleanup() {
  19.     if [ ! -z "${Z_TEMPDIR}" ]; then
  20.         appmsg "Removing previously created temporary project directory \"${Z_PROJECT}\""
  21.         rmdir "${Z_PROJECT_DIR}" 2>/dev/null
  22.     fi
  23. }
  24. function apperr() {
  25.     local exitcode=${1};
  26.     shift;
  27.     appmsg "$(str_color lred '[')$(str_color red ERROR )$(str_color lred ']') ${@}"
  28.     cleanup
  29.     exit ${exitcode};
  30. }
  31.  
  32.  
  33. while getopts "abf:" opts; do
  34.     case "${opts}" in
  35.         a)
  36.             Z_AUDIO=1;
  37.             shift;
  38.             ;;
  39.         b)
  40.             Z_BROWSE=1;
  41.             shift;
  42.             ;;
  43.         f)
  44.             Z_PROJECT=${OPTARG};
  45.             shift 2;
  46.             ;;
  47.     esac
  48. done
  49.  
  50. if [ -z "${Z_PROJECT}" ] && [ -z "${Z_BROWSE}" ]; then
  51.     apperr 1 "Usage: ${Z_APP} [-a] [-b] -f projectname"
  52. fi
  53.  
  54. if [ -z "${Z_PROJECT}" ] && [ ! -z "${Z_BROWSE}" ]; then
  55.     Z_TEMPDIR=1
  56.     Z_PROJECT=zdisc
  57.     appmsg "Using temporary project directory \"${Z_PROJECT}\""
  58. fi
  59.  
  60. Z_PROJECT_DIR="$(realpath "$(dirname "${0}")")/${Z_PROJECT}"
  61.  
  62. if [ ! -d "${Z_PROJECT_DIR}" ]; then
  63.     appmsg "Creating ${Z_PROJECT_DIR}"
  64.     mkdir "${Z_PROJECT_DIR}"
  65. fi
  66.  
  67. if [ "${Z_AUDIO}" ]; then
  68.     CMD=(cdparanoia -z -B --)
  69. elif [ "${Z_BROWSE}" ]; then
  70.     Z_NOCD=1
  71.     CMDS=3
  72.     CMD1=(sudo mount /dev/sr0 "${Z_PROJECT_DIR}")
  73.     CMD2=(mc "${Z_PROJECT_DIR}")
  74.     CMD3=(sudo umount "${Z_PROJECT_DIR}")
  75. else
  76.     CMD=(ddrescue -d --sector-size=2048 --retry-passes=-1 "${@}" /dev/sr0 "${Z_PROJECT}.iso" "${Z_PROJECT}.map")
  77. fi
  78. if [ -z "${Z_NOCD}" ]; then
  79.     appmsg "Entering ${Z_PROJECT_DIR}"
  80.     cd "${Z_PROJECT_DIR}"
  81. fi
  82.  
  83. if [ ! -z "${CMDS}" ]; then
  84.     for n in $(seq 1 "${CMDS}"); do
  85.         Z_CMD=($(eval 'echo ${CMD'$n'[@]}'))
  86.         appmsg "Executing command ${n} of ${CMDS}: ${Z_CMD[@]}"
  87.         "${Z_CMD[@]}"
  88.         Z_EC=$?
  89.         if [ ${Z_EC} -ne 0 ]; then
  90.             apperr ${Z_EC} "Command failed with error code ${Z_EC}"
  91.         fi
  92.     done
  93. else
  94.     appmsg "Executing command: ${CMD[@]}"
  95.     "${CMD[@]}"
  96.     Z_EC=$?
  97. fi
  98. cleanup
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement