All Linux HowTo's Scripting HowTo's

Backup Your DVD Collection with Linux

This post will show you how create a self contained machine that will backup your DVD collection without any effort from you (apart from placing the disc in the drawer). This is just a quick and simple script that was designed to help convert a 500+ strong collection of DVDs into media files I can enjoy around the house on any device.

Credit for the software goes to the teams at Makemkv and Handbrake without them this machine would do nothing, I recommend if you set this machine up that you should contribute to both of those projects you can find them using the links below.

Handbrake Website
MakeMKV Website

You will be able to backup blu-ray discs for 30 days using Makemkv after that point you will need to purchase the software, normal DVDs do not have this restriction, I would recommend purchasing the software anyway however it can be expensive so if you can’t afford to purchase it you will just need to work around the restrictions.

I have documented building this system on Gentoo as this was the quickest way to get it done, you can however use Redhat or Debian and get the same result.

The hardware for this does not have to be anything fantastic, I recently built one of these on a simple Intel Celeron machine with 4GB of RAM and a 300gb hard disk drive. The only hardware that I would consider a must is a disc drawer that is not designed to close if it is left open for a long period of time, the reason that I make this suggestion is that if you place a disc in the machine over night you could end up backing it up multiple times if the disc drawer closes on it’s own.

Use the documentation here to get a base installation of Gentoo on your hardware and then come back here.

Now that you have a working Gentoo installation lets get started,

First thing lets sync so that you get the latest version of the applications

emerge --sync

Now we can begin installing the software that we need, first we need to accept the license etc. Keep in mind that these version numbers may change in time so if in doubt just run the next command and follow the prompts.

echo "=media-video/makemkv-1.9.8-r1 ~amd64" >> /etc/portage/package.accept_keywords ; echo "=media-video/handbrake-0.10.2-r1 ~amd64" >> /etc/portage/package.accept_keywords ; echo "=media-libs/fdk-aac-0.1.3 FraunhoferFDK" >> /etc/portage/package.license ; echo ">=media-video/makemkv-1.9.8-r1 MakeMKV-EULA" >> /etc/portage/package.license

Now we can run emerge,

emerge --ask -jv handbrake makemkv

Once that is installed we can start creating the other items that we need,

mkdir -vp /workbench/{"todo","done"} ; mkdir -vp /var/log/dvdbackups ; mkdir -vp /root/bin ; touch /var/log/dvdbackups/mount.log ; touch /root/bin/ ; touch /root/bin/

Now remove any crazy rules that might be in the way of us mounting the dvd when it is inserted. This step is optional and should only be done if there are not other udev services on this machine

rm -rf /etc/udev/rules.d/*

Now we can create our udev rule, this assumes that your DVD drive is detected as sr0 change that if needed.

echo 'SUBSYSTEM=="block", KERNEL=="sr0", SYMLINK+="dvd", GROUP="cdrom", ACTION=="change", RUN+="/root/bin/"' > /etc/udev/rules.d/50-dvdmount.rules

Copy and paste the following into /root/bin/

 if [ $ID_CDROM_MEDIA -eq 1 ]; then
    #Disc has been inserted, do something
    DISCNAME="$(dd if=/dev/sr0 bs=1 skip=32808 count=32 2> /dev/null | cut -c1-30)"
    echo "A disc named $DISCNAME has been inserted"
    mkdir -vp /media/$DISCNAME
    mount -vt iso9660 -o ro /dev/sr0 /media/$DISCNAME
    /bin/sleep 15
    umount /media/$DISCNAME
    rm -rf /media/*
} &>> "/var/log/dvdbackups/mount.log" &

Copy and paste the following into /root/bin/

#Set the name for the output files etc
cd /media/*/
DISCNAME="$(pwd | cut -c8-62)"
DATE="$(date +%d-%m-%Y_%H-%M)"

if [ -d /workbench/done/$DISCNAME ]; then
#Create the log file
date > /var/log/dvdbackups/dvdbackup-"$DISCNAME".log
#Make the working directories
sudo mkdir /workbench/todo/"$DISCNAME" >> /var/log/dvdbackups/dvdbackup-"$DISCNAME".log
sudo mkdir /workbench/done/"$DISCNAME" >> /var/log/dvdbackups/dvdbackup-"$DISCNAME".log
#Rip the files
sudo makemkvcon --minlength=700 -r --decrypt --directio=true mkv disc:0 all /workbench/todo/"$DISCNAME" >> /var/log/dvdbackups/dvdbackup-"$DISCNAME".log
#Loop through and encode the files then place them in the done folder
for f in /workbench/todo/"$DISCNAME"/*; do
    sudo HandBrakeCLI -i "$f" -o /workbench/done/"$DISCNAME"/title"$TITLE".mkv -e x264 -q 20 -B 160 >> /var/log/dvdbackups/dvdbackup-"$DISCNAME".log
    let TITLE++
#Move out of the media directory
cd /root/bin/
rm -rf /workbench/todo/*
#Eject the disc

Now lets share that media directory and log files so that we can get to our backed up data.

emerge --ask -jv samba

Now add the user that will be able to access the media over the network,

groupadd -g 1500 media ; useradd -u 1500 -g 1500 media ; passwd media ; smbpasswd -a media ; chown -R media:media /workbench

Copy and paste the following into /etc/samba/smb.conf

        comment = Media Output Location
        path = /workbench/done
        read only = no
        public = yes

        comment = Media Files
        path = /var/log/dvdbackups
        read only = yes
        public = yes

Allow samba to start with the machine and start samba

rc-update add samba default ; /etc/init.d/samba start

One optional program to install is htop, I like it but feel free to use regular top for the next step also

emerge --ask -jv htop

Ok now it would be time to test your setup, place a disc in the drive and what htop for a process titled “makemkv”. Alternatively you can tail the log files in /var/log/dvdbackup/

That should be it, once the dvd is backed up and converted the disc will be ejected automatically and you can just keep placing new ones in the drive. To get the media that has been backed up just browse over the network to the Linux machine and check the media share.

Should you have any issues getting this setup or just have some questions please leave a comment below and I will get back to you ASAP.

If you need a program to stream the files to your devices around your house I would suggest minidlna as I have had no issues with it so far and it is easy to install on Gentoo.

Share This:

Leave a Reply

Your email address will not be published. Required fields are marked *