Backupgame.sh

From Ju's wiki
Revision as of 17:51, 10 February 2019 by Ju (talk | contribs)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to: navigation, 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.

#!/bin/bash

# backupgame.sh 0.2 by Ju
# Rips and compresses video game discs
#
# 0.2 changes:
# - swap position of Disc no. and Region in file names (+space separated) to match RetroArch database
# - suggest entering the full region name (ie. USA instead of U), also to match rdb

# User configuration

ROMPATH="$HOME/yourROMfolder"
TMPPATH="/tmp"
CONFFILE="$HOME/.backupgamerc"

# Config file sourced below only contains the game information saved last time

. $CONFFILE

trap ctrl_c INT

function ctrl_c() {
        echo "User abort, cleaning up..."
	cleanup
	exit
}

function cleanup {
	if [ ! -s "$GAMEPATH/$GAMEFILE.7z" ]; then rm -f "$GAMEPATH/$GAMEFILE.7z"; fi
	rm -f "$GAMEFILE.bin" "$GAMEFILE.toc.bak" "$GAMEFILE.cue"  "$GAMEFILE.log"
        cd
	rmdir --ignore-fail-on-non-empty "$TMPPATH"
}

function saveconf {
	echo "LASTGAME=\"$GAMENAME\"
LASTDISC=\"$DISCNO\"
LASTSYSTEM=\"$SYSTEM\"
LASTREGION=\"$REGION\"
LASTDRIVE=\"$DRIVEDEV\"
" > $CONFFILE
}

# Asking user for game name

read -p "Enter game name [$LASTGAME]: " userinput
GAMENAME="$LASTGAME"
if [ "$userinput" != "" ]; then
	GAMENAME="$userinput"
fi

# Asking user for disc no.

if [ $LASTDISC == "0" ]; then
	DISCNO=0
else
	DISCNO=$((LASTDISC+1))
fi
while true; do
	read -p "Enter disc number, 0 for single-disc game [$DISCNO]: " userinput
	case $userinput in
		0) DISCNO="0"; break;;
		[123456789]) DISCNO="$userinput"; break;;
		"") break;;
		*) echo "Only numerical input allowed.";;
	esac
done

if [ "$DISCNO" == "0" ]; then
	FDISCNO=""
else
	FDISCNO=" (Disc $DISCNO)"
fi

# Asking user for System

echo "System list:
0 - Sega Saturn
1 - Sega CD/Mega CD
2 - PlayStation
3 - PlayStation (with subchannels)
4 - PlayStation 2
5 - PC Engine Super CD/Turbo CD"

while true; do
        read -p "Enter system ID, see list above [$LASTSYSTEM]: " userinput
        case $userinput in
                0) SYSTEM="Sega Saturn"; break;;
                1) SYSTEM="Mega CD"; break;;
                2) SYSTEM="PlayStation"; break;;
		3) SYSTEM="PlayStation (with subchannels) (DONT USE - SBI INSTEAD)"; break;;
                4) SYSTEM="PlayStation 2"; break;;
                5) SYSTEM="PC Engine CD"; break;;
		"") SYSTEM="$LASTSYSTEM"; break;;
                *) echo "Invalid system ID";;
        esac
done

SYSTEMPATH="$SYSTEM"

# Console specific settings

if [ "$SYSTEM" == "PlayStation" ]; then MOREOPTS="--read-raw --no-mode2-mixed"; fi
if [ "$SYSTEM" == "PlayStation (with subchannels) (DONT USE - SBI INSTEAD)" ]; then MOREOPTS="--read-raw --no-mode2-mixed --read-subchan rw_raw"; SYSTEMPATH="PlayStation"; fi

# Asking user for game region

REGION="$LASTREGION"
read -p "Enter game region (i.e. Europe, USA, Japan, etc.) [$LASTREGION]: " userinput
if [ "$userinput" != "" ]; then
	REGION="$userinput"
fi
FREGION=" ($REGION)"

# Asking user for disc drive device

DRIVEDEV=$LASTDRIVE
read -p "Enter drive device [$DRIVEDEV]: " userinput
if [ "$userinput" != "" ]; then 
        DRIVEDEV="$userinput"
fi
FDRIVEDEV="/dev/$DRIVEDEV"

GAMEFILE="$GAMENAME$FREGION$FDISCNO"
GAMEPATH="$ROMPATH/$SYSTEMPATH"
TMPPATH="$TMPPATH/backupgame-$$"

# User confirmation
while true; do
	read -p "Image will be saved as $GAMEPATH/$GAMEFILE.7z, type y if that's ok [y]: " userinput
	case $userinput in
		n) echo Too bad, bye then.; exit;;
		"") break;;
		*) echo Answer yes or no;;
	esac
done

saveconf
mkdir -p "$GAMEPATH"
if [ -e "$GAMEPATH/$GAMEFILE.7z" ]; then
	echo File already exists!
	exit
fi

echo "Ripping disc..."

touch "$GAMEPATH/$GAMEFILE.7z"
mkdir -p "$TMPPATH"
cd $TMPPATH
umount $FDRIVEDEV # can't rip if it's mounted
LC_ALL=C cdrdao read-cd $MOREOPTS -v 2 --device "$FDRIVEDEV" --driver generic-mmc-raw:0x20000 --datafile "$GAMEFILE.bin" "$GAMEFILE.toc" &> "$GAMEFILE.log"

if [ "$?" != "0" ]; then
	echo "Ripping process was not completed, ripped files will not be deleted."
	tail -n10 "$GAMEFILE.log"
	exit
fi

eject $DRIVEDEV
toc2cue "$GAMEFILE.toc" "$GAMEFILE.cue" &> /dev/null
mv "$GAMEFILE.toc" "$GAMEFILE.toc.bak"

echo "Ripping done, compressing file..."
rm -f "$GAMEPATH/$GAMEFILE.7z"
nice -n20 ionice -c3 7za a -mx=9 "$GAMEPATH/$GAMEFILE.7z" "$GAMEFILE.bin" "$GAMEFILE.toc.bak" "$GAMEFILE.cue" "$GAMEFILE.log" &> /dev/null

if [ "$?" != "0" ]; then
        echo "Archiving failed, ripped files will not be deleted."
        exit
fi

cleanup

echo Done.