Skip to content

Instantly share code, notes, and snippets.

@sophieforceno
Last active December 11, 2021 23:38
Show Gist options
  • Save sophieforceno/ec825154f246ef1f2e706e2716bd019d to your computer and use it in GitHub Desktop.
Save sophieforceno/ec825154f246ef1f2e706e2716bd019d to your computer and use it in GitHub Desktop.
Bash script to automate archiving of optical discs
#! /bin/bash
#
# adisc - Optical Disc Auto-Transfer Tool - v0.9
# by Sophie Forceno
#
# This script loops until user intervention
# Iteratively creates directories, mounts each disc
# Uses rsync to copy disc contents to dir, unmounts disc
# Then prompts user to insert next disc
#
#
# If your optical disk drive auto-mounts to $disk_label/
# it is recommended that you add the noauto parameter to the fstab entry
# Example:
# /dev/sr0 /media/optical udf,iso9660 users,noauto,rw 0 0
# Optical drive device location
OPTICAL_DEV="/dev/sr0"
# Optical disc mount point
OPTICAL_DIR="/media/andy/optical"
# Destination to which discs are copied
DEST_DIR="/media/andy/discs"
# Upper limit of for loop (set to # of discs)
LIMIT="250"
# Main loop.
for ((i=1;i<=$LIMIT;i++)); do
# Check that mountpoint folder exists
if [[ ! -d "$OPTICAL_DIR" ]]; then
sudo mkdir "$OPTICAL_DIR"
fi
# Check if folder is empty
if [ -n "$(ls -A "$OPTICAL_DIR")" ]; then
# Status = 1: Disc mounted
status="1"
else
# Status = 0: Disc not mounted
status="0"
echo "Mounting disc..."
sudo mount -t iso9660 "$OPTICAL_DEV" "$OPTICAL_DIR" && wait
sleep 8
status="1"
fi
# If disc is mounted, begin copying
if [[ "$status" = "1" ]]; then
echo -e "Copying disc $i...\n"
# Iteratively generate folders
sudo mkdir "$DEST_DIR/disc $i"
sudo rsync -av "$OPTICAL_DIR/" "$DEST_DIR/disc $i" && wait
echo "Ejecting..."
eject && wait && sleep 2
status="0"
fi
# If disc is (most likely) ejected, ask for next disc
if [[ "$status" = "0" ]]; then
echo "Please insert a disc into the drive and press any key to continue"
echo "Or press the x key to e(X)it..."
read -n1 char && wait && sleep 6
case $char in
x|X)
exit 0
;;
*)
sleep 4
continue
;;
esac
fi
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment