Backupgame.sh

From Ju's wiki
Jump to navigation Jump to search

Script to rip and compress game console media. Needs cdrdao and p7zip installed. File format will be CUE/BIN (with backup of TOC) in a 7z archive. Assists with multi-disc games also. You probably need to change ROMPATH and TMPPATH to suit your needs. As always, use at your own risk. Click here to see the license text for this script.

  1 #!/bin/bash
  2 
  3 # backupgame.sh 0.2 by Ju
  4 # Rips and compresses video game discs
  5 #
  6 # 0.2 changes:
  7 # - swap position of Disc no. and Region in file names (+space separated) to match RetroArch database
  8 # - suggest entering the full region name (ie. USA instead of U), also to match rdb
  9 
 10 # User configuration
 11 
 12 ROMPATH="$HOME/yourROMfolder"
 13 TMPPATH="/tmp"
 14 CONFFILE="$HOME/.backupgamerc"
 15 
 16 # Config file sourced below only contains the game information saved last time
 17 
 18 . $CONFFILE
 19 
 20 trap ctrl_c INT
 21 
 22 function ctrl_c() {
 23         echo "User abort, cleaning up..."
 24 	cleanup
 25 	exit
 26 }
 27 
 28 function cleanup {
 29 	if [ ! -s "$GAMEPATH/$GAMEFILE.7z" ]; then rm -f "$GAMEPATH/$GAMEFILE.7z"; fi
 30 	rm -f "$GAMEFILE.bin" "$GAMEFILE.toc.bak" "$GAMEFILE.cue"  "$GAMEFILE.log"
 31         cd
 32 	rmdir --ignore-fail-on-non-empty "$TMPPATH"
 33 }
 34 
 35 function saveconf {
 36 	echo "LASTGAME=\"$GAMENAME\"
 37 LASTDISC=\"$DISCNO\"
 38 LASTSYSTEM=\"$SYSTEM\"
 39 LASTREGION=\"$REGION\"
 40 LASTDRIVE=\"$DRIVEDEV\"
 41 " > $CONFFILE
 42 }
 43 
 44 # Asking user for game name
 45 
 46 read -p "Enter game name [$LASTGAME]: " userinput
 47 GAMENAME="$LASTGAME"
 48 if [ "$userinput" != "" ]; then
 49 	GAMENAME="$userinput"
 50 fi
 51 
 52 # Asking user for disc no.
 53 
 54 if [ $LASTDISC == "0" ]; then
 55 	DISCNO=0
 56 else
 57 	DISCNO=$((LASTDISC+1))
 58 fi
 59 while true; do
 60 	read -p "Enter disc number, 0 for single-disc game [$DISCNO]: " userinput
 61 	case $userinput in
 62 		0) DISCNO="0"; break;;
 63 		[123456789]) DISCNO="$userinput"; break;;
 64 		"") break;;
 65 		*) echo "Only numerical input allowed.";;
 66 	esac
 67 done
 68 
 69 if [ "$DISCNO" == "0" ]; then
 70 	FDISCNO=""
 71 else
 72 	FDISCNO=" (Disc $DISCNO)"
 73 fi
 74 
 75 # Asking user for System
 76 
 77 echo "System list:
 78 0 - Sega Saturn
 79 1 - Sega CD/Mega CD
 80 2 - PlayStation
 81 3 - PlayStation (with subchannels)
 82 4 - PlayStation 2
 83 5 - PC Engine Super CD/Turbo CD"
 84 
 85 while true; do
 86         read -p "Enter system ID, see list above [$LASTSYSTEM]: " userinput
 87         case $userinput in
 88                 0) SYSTEM="Sega Saturn"; break;;
 89                 1) SYSTEM="Mega CD"; break;;
 90                 2) SYSTEM="PlayStation"; break;;
 91 		3) SYSTEM="PlayStation (with subchannels) (DONT USE - SBI INSTEAD)"; break;;
 92                 4) SYSTEM="PlayStation 2"; break;;
 93                 5) SYSTEM="PC Engine CD"; break;;
 94 		"") SYSTEM="$LASTSYSTEM"; break;;
 95                 *) echo "Invalid system ID";;
 96         esac
 97 done
 98 
 99 SYSTEMPATH="$SYSTEM"
100 
101 # Console specific settings
102 
103 if [ "$SYSTEM" == "PlayStation" ]; then MOREOPTS="--read-raw --no-mode2-mixed"; fi
104 if [ "$SYSTEM" == "PlayStation (with subchannels) (DONT USE - SBI INSTEAD)" ]; then MOREOPTS="--read-raw --no-mode2-mixed --read-subchan rw_raw"; SYSTEMPATH="PlayStation"; fi
105 
106 # Asking user for game region
107 
108 REGION="$LASTREGION"
109 read -p "Enter game region (i.e. Europe, USA, Japan, etc.) [$LASTREGION]: " userinput
110 if [ "$userinput" != "" ]; then
111 	REGION="$userinput"
112 fi
113 FREGION=" ($REGION)"
114 
115 # Asking user for disc drive device
116 
117 DRIVEDEV=$LASTDRIVE
118 read -p "Enter drive device [$DRIVEDEV]: " userinput
119 if [ "$userinput" != "" ]; then 
120         DRIVEDEV="$userinput"
121 fi
122 FDRIVEDEV="/dev/$DRIVEDEV"
123 
124 GAMEFILE="$GAMENAME$FREGION$FDISCNO"
125 GAMEPATH="$ROMPATH/$SYSTEMPATH"
126 TMPPATH="$TMPPATH/backupgame-$$"
127 
128 # User confirmation
129 while true; do
130 	read -p "Image will be saved as $GAMEPATH/$GAMEFILE.7z, type y if that's ok [y]: " userinput
131 	case $userinput in
132 		n) echo Too bad, bye then.; exit;;
133 		"") break;;
134 		*) echo Answer yes or no;;
135 	esac
136 done
137 
138 saveconf
139 mkdir -p "$GAMEPATH"
140 if [ -e "$GAMEPATH/$GAMEFILE.7z" ]; then
141 	echo File already exists!
142 	exit
143 fi
144 
145 echo "Ripping disc..."
146 
147 touch "$GAMEPATH/$GAMEFILE.7z"
148 mkdir -p "$TMPPATH"
149 cd $TMPPATH
150 umount $FDRIVEDEV # can't rip if it's mounted
151 LC_ALL=C cdrdao read-cd $MOREOPTS -v 2 --device "$FDRIVEDEV" --driver generic-mmc-raw:0x20000 --datafile "$GAMEFILE.bin" "$GAMEFILE.toc" &> "$GAMEFILE.log"
152 
153 if [ "$?" != "0" ]; then
154 	echo "Ripping process was not completed, ripped files will not be deleted."
155 	tail -n10 "$GAMEFILE.log"
156 	exit
157 fi
158 
159 eject $DRIVEDEV
160 toc2cue "$GAMEFILE.toc" "$GAMEFILE.cue" &> /dev/null
161 mv "$GAMEFILE.toc" "$GAMEFILE.toc.bak"
162 
163 echo "Ripping done, compressing file..."
164 rm -f "$GAMEPATH/$GAMEFILE.7z"
165 nice -n20 ionice -c3 7za a -mx=9 "$GAMEPATH/$GAMEFILE.7z" "$GAMEFILE.bin" "$GAMEFILE.toc.bak" "$GAMEFILE.cue" "$GAMEFILE.log" &> /dev/null
166 
167 if [ "$?" != "0" ]; then
168         echo "Archiving failed, ripped files will not be deleted."
169         exit
170 fi
171 
172 cleanup
173 
174 echo Done.