Skip to content

Instantly share code, notes, and snippets.

@Calinou
Last active January 2, 2023 21:19
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Calinou/4a7c96731aade5a06637192116280857 to your computer and use it in GitHub Desktop.
Save Calinou/4a7c96731aade5a06637192116280857 to your computer and use it in GitHub Desktop.
Compress ISO/CUE files to CHD on a remote filesystem accessible via SFTP/SSH
#!/bin/bash
set -euo pipefail
IFS=$'\n\t'
# Compresses ISO/CUE files to CHD on a remote filesystem accessible via SFTP/SSH.
# Source ISO files on the remote host are not removed after conversion
# (you have to do this manually if desired).
#
# chdman must be installed (it's typically packaged as `mame-tools`).
# If it's not in your distribution repositories, build
# <https://github.com/CharlesThobe/chdman> from source.
# The SFTP host (user@host[:port]).
HOST="user@example.com"
# Path to the folder containing ISO/CUE files. Must not end with a trailing slash.
ISO_CUE_DIR="path/to/folder"
# Paths between SSH and SFTP may differ on some hosts.
# Set this to e.g. "/volume1/" if using a Synology NAS.
SSH_PATH_PREFIX=""
iso_cues=$(ssh $HOST "ls $SSH_PATH_PREFIX$ISO_CUE_DIR/*.{iso,cue}")
# Optimize each ISO/CUE individually to avoid running out of disk space or RAM.
for iso_cue in $iso_cues; do
input_iso_cue=$(basename "$iso_cue")
output_chd="${input_iso_cue%.*}.chd"
scp "$HOST:$ISO_CUE_DIR/$input_iso_cue" /tmp
if [[ "${input_iso_cue: -4}" == ".cue" ]]; then
# Transfer BIN file alongside the CUE sheet (as it contains the actual data).
scp "$HOST:$ISO_CUE_DIR/${input_iso_cue%.*}.bin" /tmp
fi
chdman createcd -i "/tmp/$input_iso_cue" -o "/tmp/$output_chd"
# ISO/CUE/BIN file is no longer needed locally after being converted to CHD.
rm -f "/tmp/$input_iso_cue" "/tmp/${input_iso_cue%.*}.bin"
scp "/tmp/$output_chd" "$HOST:$ISO_CUE_DIR"
# CHD file is no longer needed locally after being transferred.
rm -f "/tmp/$output_chd"
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment