Bash and mass backup of floppy disks
While dealing with cleaning out the family house, I have been tasked to backup all the pictures from floppy disks (3 1/2") and Compact Discs(CDs). Thankfully I still had both a CD reader and a 3 1/2" Floppy disk drive to read them.
As any sort of media goes bad over time, I first had to copy the contents onto my local drive before extracting and inspecting the contents of each. This brings me to a problem with the 3 1/2" Floppy disks, as most of them had read errors on them. This caused headaches when attempting to using a the standard COPY command.
This is where I turned to my trusty friend ddrescue*. This would allow me to copy the data into an IMG format, while retrying to all errors that come up while reading from the disk.
This brings me to attempting to automate the process. A quick bash script allowed inputting of the title of the disk, along with looping indefinitely.
This is a pretty simple script that requires 'sudo' access to run. Just copies the data off into IMG files, which then can be referenced later.
#!/bin/bash
FLOPPY_NUM=0;
echo $FLOPPY_NUM;
function wait_for_floppy() {
blkid /dev/sda 2&>1;
last_run=$?;
while [[ $last_run -ne 0 ]]; do
echo "No disk detected in drive";
sleep 1;
blkid /dev/sda;
last_run=$?;
done
}
while /bin/true; do
echo "Waiting for drive to become ready";
wait_for_floppy;
echo "Waiting for input:";
read -p "Enter Title: " TITLE
FLOPPY_NUM=$(($FLOPPY_NUM + 1));
echo "Copying Floppy $FLOPPY_NUM: $TITLE";
sudo ddrescue -A --verify-on-error /dev/sda "/unsorted/floppies/floppy${FLOPPY_NUM}-${TITLE}.img"
done;
But how can I automate this even more? That is where udev rules come in. First we need to find all the udev information about our drive, so we limit any automation just to our floppy drive.
udevadm info -q all -a /dev/sda
You will see a lot of information about the drive, and its parents, but we are looking only at how the drive is recognized:
looking at device '/devices/pci0000:00/0000:00:08.1/0000:09:00.3/usb3/3-3/3-3.3/3-3.3:1.0/host6/target6:0:0/6:0:0:0/block/sda':
KERNEL=="sda"
SUBSYSTEM=="block"
DRIVER==""
Now let us dive into the udev* rules. I would suggest looking into some basics with https://linuxconfig.org/tutorial-on-how-to-write-basic-udev-rules-in-linux. This is a nice quick article that will help you get up to speed.
Now we need to define what we are looking for:
- Detect when a new 3 1/2" disk is inserted into the Floppy Drive
- Execute the 'ddrescue'
- Eject (if possible) or Alert when once ddrescue is complete, so we can switch disks.
First we need to define our device we want to reference.
Due to the nature of how the kernel loads different devices, we need to try and stay away from identifying the device specifically by the current device name "sda".
What subsystem is it using?
SUBSYSTEM=="block"
Pretty simple.
References:
ddrescue*: https://www.gnu.org/software/ddrescue/
udevrules*: https://wiki.debian.org/udev
udevbasics*: https://linuxconfig.org/tutorial-on-how-to-write-basic-udev-rules-in-linux
Comments
Post a Comment