---
layout: post
title: "Automatic SD Card Backer Upper"
---

I play on my DSi a lot, and i have all of my cartridges dumped to an SD card
so I don't need to carry them with me to switch games. All my saves are stored
on the SD card too, and sometimes I worry about the card getting corrupted or
lost. So I used my Raspberry Pi to easily back it up. All I have to do is plug
the card into the Pi, and it copies the files into a new folder and toggles a
light when its done.

# What I used:
 * SD Card adapter
 * USB mouse that I didn't care about
 (any usb device that can light up will work)
 * Raspberry Pi 3
 (any raspberry pi with a USB connection and 3.5mm jack will work)
  
I did this on Ubuntu, but the instructions should work for Raspbian as well.

# Files

I made a few files for this to work: a udev rule file, a systemd unit, and a
shell script.

## Udev Rule
```
# /etc/udev/rules.d/999-automount.rules 
ACTION=="add", SUBSYSTEM=="block", ENV{ID_SERIAL_SHORT}=="000000000828", RUN+="/usr/bin/systemctl start DSi.service"
```
You'll have to modify the `000000000828` to match the serial of your device,
which you can get by plugging in the device, finding its /dev file, and running
`udevadm info /dev/sdx | grep ID_SERIAL_SHORT`.

## Systemd Service
```
# /etc/systemd/system/DSi.service
[Unit]
Description=back up dsi sd
[Service]
ExecStart=/home/ubuntu/autocopy.sh
[Install]
```
There's nothing in the `[Install]` section because this isn't made to be enabled
with `systemctl enable`, just executed with `systemctl start`, which the udev
rule will do automatically. You'll want to modify the `ExecStart` line to point
to your shell script.

## Shell Script
```
#!/bin/sh
# /home/ubuntu/autocopy.sh
TIME=$(TZ=EST date +%F_%T) # used for folder name
LABEL=DSi # the label of the SD cards filesystem
MNT=/home/ubuntu/dsimnt # mount point of the sd card
BACKUPDIR=/home/ubuntu/DSi # main directory all backups go to
USBDEV=/sys/bus/usb/devices/1-1.5 # the sysfs entry for the usb device to use as a light

# turn on the "led indicator" thats really a usb mouse torn apart
echo 1 > ${USBDEV}/authorized
mount "/dev/disk/by-label/${LABEL}" ${MNT}
mkdir "${BACKUPDIR}/${TIME}"
cp -r ${MNT}/* ${BACKUPDIR}/${TIME}/
umount ${MNT}
echo "done! you can remove the card."
# turn that mouse back off
echo 0 > ${USBDEV}/authorized
```
You'll want to change the `LABEL` variable to whatever the label of your SD card
filesystem is.

You will probably want to change the `TZ=EST` inside the `TIME` variable unless 
you happen to live on the eastern coast of the United States.

The mount point in the `MNT` variable is where the SD card should mount, which
can be anywhere.

The `BACKUPDIR` variable should be on a separate drive unless you have a 
decently sized microSD inside your Pi that you don't mind filling up.

The `USBDEV` variable is the path to your USB "light" inside 
`/sys/bus/usb/devices`. You can run `lsusb` to get the ID of the usb device,
then `cat` the `idProduct` and `idVendor` files inside each usb device folder
until you find the one that matches your usb device.

# Final Product

What you'll have is something you can plug your SD card into and have a backup
taken automagically. The light turns on when it's copying files, and turns off
and unmounts the card when its done.
You can see [a video of it working](/assets/sdcardpithing.mp4) here. Excuse the
mess :) To access these backups, just go to the backup directory.

![image](/assets/sdcardfolder.png)
