Restoring files scattered on a backup drive using rsync

I realized yesterday morning that some of the best tracks of my library had disappeared. It concerned artists of the ’S-U' section : the classic Shakira’s Laundry Service was amputated from its masterpiece Whenever, Wherever, Master Blaster Jammin was missing from Stevie’s Greatest Hits, and so on.

The deletion must have occurred months ago due to an unfortunate keystrokes sequence in iTunes. Fortunately, I had a 1 year old rsync backup of my music files on a dusty external hard drive.
In such a situation, what is the shortest path to get the precious gems back to their original location ?

Have a glimpse at the damages

In, execute rsync to list the files that have been wiped out of your drive since last backup. Something like :

rsync -vnru --ignore-existing ~/Backup/\[STU\]/ ~/Music/\[STU\]/

Options description:

-v, —verbose increase verbosity
-n, —dry-run show what would have been transferred
-r, —recursive recurse into directories
-u, —update skip files that are newer on the receiver
—ignore-existing skip updating files that exist on receiver

Note that it’s the -n option which is responsible for only showing files and not initiating transfer (dry run aka simulation mode).

Separate the good from the bad

A problem with this recursive recovery method is that you will see re-emerge lame 1-star tunes from the darkness, files that you deleted intentionally from your drive since the last backup. Use the —exclude option to filter these black sheeps.

Refine your rsync command until it spits a file listing that suits you. Then drop the -n option to leave simulation mode and actually do the transfer, for real :

rsync -vru --ignore-existing ~/Backup/\[STU\]/ ~/Music/\[STU\]/

Icing on the cake : iTunes import

For the iTunes users in the crowd, here is a trick to ease the import process of recovered tracks.

  • log output of rsync command in a text file :

    rsync -vru --ignore-existing ~/Backup/\[STU\]/ ~/Music/\[STU\]/ > allPaths.txt
  • filter the text content to keep only audio files paths, save the result in a playlist :

    grep -i "\.mp3" allPaths.txt > audioRelPaths.m3u
  • filepaths must be made absolute so that iTunes can interpret them. Use lam utility to prepend the parent directory path :

    lam -s "/Users/flap/Music/\[STU\]/" audioRelPaths.m3u > audioAbsPaths.m3u
  • drag & drop the m3u file on the iTunes dock icon to reimport all restored tracks in your library.