Backing up my iPod mp3’s

Note: prior to applying the script below, read all the way through to the bottom. There you will find an important update!

I recently bought a 60GB iPod photo, and encountered the problem that the iTunes “synchronisation” does not really work for me: I can store many more files on my iPod than I have space available on the hard disk of my PowerBook. Therefore, I decided not to use the “synch” option in iTunes and to do it manually … so far, so easy…

Recently I accidentally pressed the “automatically update” button in the iPod preferences in iTunes. To my horror suddenly it started deleting 20GB of mp3’s that I copied on my iPod and that I did not have on my hard disk any more 🙁

To prevent this accidental deletion of the files on my iPod, I made the following backup script. It uses low-level unix command line tools, and creates a “hard link” for each file on the iPod. The hard link does not prevent the file from being deleted using iTunes, but at least it ensures that the mp3 data itself is not erased. I.e., it is simple to add the mp3 back to your iTunes music collection on your iPod.

The hard link is similar as a copy of the file, except that only the pointer to the file is copied, the data is left alone. Therefore a hard link does not occupy additional space on your iPod. If you edit the ID3 tags of a file, the iD3 content of both the original file and of the hard-linked copy will be updated.

The following script creates the hard links in a subdirectory with today’s date as the name. Copy and paste it into a text file with the name “ipodbackup”, set the permissions to “execute” (e.g. chmod +x ipodbackup) and you can execute it from the terminal command line to make a backup of all files on your iPod prior to adding new mp3’s.

#!/bin/sh

IPOD="/Volumes/Robert Oostenveldâ~@~Ys iPod"
SOURCE="$IPOD"/iPod_Control/Music
TARGET="$IPOD"/Backup/`date +'%Y%m%d'`

# create the directory that will contain the backup
mkdir -p "$TARGET"
cd "$TARGET"

# create the subdirectories that conatin the mp3's
find "$SOURCE" -type d -mindepth 1 -exec basename {} ; | xargs mkdir 

# create the hard links in each subdirectory
for subdir in F* ; do
  echo linking `ls "$SOURCE"/$subdir | wc -l` music files in directory $subdir
  ln "$SOURCE"/$subdir/* "$TARGET"/$subdir
done

Important update (12 Aug 2005): After applying the script and making all the hard links, I discovered that they confuse the software on the iPod. It will not play the songs any more, although they still appear correctly in both iTunes and on the iPod when disconnected. Also, iTunes is still able to play the songs. To solve this, I had to copy all the songs off and reimport them in iTunes and on the iPod. So the backup script does not work as I hoped 🙁