Skip to content

Instantly share code, notes, and snippets.

@vectorsigma
Created March 9, 2023 23:25
Show Gist options
  • Save vectorsigma/97c4339e193aea26b147a33903fdb411 to your computer and use it in GitHub Desktop.
Save vectorsigma/97c4339e193aea26b147a33903fdb411 to your computer and use it in GitHub Desktop.
quick script to assist batch optical media copying
#!/bin/bash
#
# small script to help automate backup and recovery of my box o dvd backups.
# Config parameters
BACKUP_DIR="/mnt/nas/optical/backup"
DISC_MP="/mnt/cdrom"
BACKUP_VOL="$(lsblk --output LABEL /dev/sr0 | tail -1)"
ERROR_LOG="${BACKUP_DIR}/backup-${BACKUP_VOL}-log-$(date +%Y%m%d).txt"
BACKUP_DEST="${BACKUP_DIR}/${BACKUP_VOL}"
# Useful aliases
LOGGER="tee -a ${ERROR_LOG}"
# make sure the NFS target is mounted.
if [[ $(grep -qF '/mnt/nas' /proc/mounts) ]]; then
echo "/mnt/nas not mounted, mounting..."
mount /mnt/nas 2>&1 | $LOGGER
if [[ $(grep -qF '/mnt/nas' /proc/mounts) ]]; then
echo "Unable to mount /mnt/nas, check the log file: $ERROR_LOG"
exit 1
fi
fi
echo "Starting processing of volume: $BACKUP_VOL"
# try to mount the disc
mount ${DISC_MP} 2>&1 | $LOGGER
if [[ $(grep -qF '/mnt/cdrom' /proc/mounts) ]]; then
echo "$DISC_MP not mounted, errors likely encountered, check the log file: $ERROR_LOG."
exit 2
fi
# Create the backup folder location
if [[ ! -d "${BACKUP_DEST}" ]]; then
mkdir -p "${BACKUP_DEST}" 2>&1 | $LOGGER
if [[ $? != 0 ]]; then
echo "Error creating directory: $BACKUP_DEST, check the log file: $ERROR_LOG."
exit 3
fi
else
echo "Backup directory: ${BACKUP_DEST} already exists, check your neck!"
exit 3
fi
# Check for discs mounted in such a way as to need root access
ELEVATE=""
ls -l ${DISC_MP} | grep -qF "??"
if [[ "$?" == "0" ]]; then
echo "Disc mounted funny, probably need root access to copy discs."
ELEVATE="sudo"
fi
# Now start actually copying stuff.
time $ELEVATE cp -a ${DISC_MP}/* "${BACKUP_DEST}"/ 2>&1 | $LOGGER
sudo eject
echo "Finished processing volume: $BACKUP_VOL"
exit $?
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment