fixing iTunes corruption

iTunes corrupted my MP3 files. Not sure what happened, but I mounted my carefully-groomed 300GB of music via SMB on my Mac, imported the music, and suddenly a bunch of my MP3 files no longer play on the Sonos system. Particularly the music I bought from Amazon. Not sure what the corruption is. The file lengths are the same but the ID3 tags are different, maybe it wrote album art in? That’s worked in the past, but iTunes 11 is such a clusterfuck maybe they finally screwed it up. Or maybe it’s Apple’s broken SMB implementation causing filesystem level corruption. Either way I have to restore from backup. iTunes will no longer have write access to my music library. Screw you, iTunes.

I didn’t want to just rsync everything from a backup; was not positive that wouldn’t do some harm, and at first I had a concern that the 300GB directory might take too long. (Which is dumb; rsync is very smart.) In the end I just did an rsync, trusting the way it doesn’t delete / overwrite stuff it doesn’t need to. But first I did a bunch of verification of the files. The good news is the mtimes on the files at least got set, so it’s relatively easy to find the damage. I have to be a bit careful because the directory names are full of all sorts of messy Unicode, spaces, etc that shell programs don’t like. Also it’s 300 gigs of files, so I don’t want to just rsync the whole thing.

# find all files modified in last 20 days
$ find . -mtime -20 > /tmp/mp3.txt
$ wc -l /tmp/mp3.txt
369 /tmp/mp3.txt

# get just the directory names (ended up not using this)
$ cat /tmp/mp3.txt | while read l; do echo `dirname "$l"`; done | sort | uniq > /tmp/dirnames.txt

# make a list of what's restorable from backup
$ cat /tmp/mp3.txt | while read f; do o=/mnt/sdb1/BACKUPS/rsnapshot/monthly.1/localhost/home/nelson/Music/"$f"; [ -f "$o" ] && echo "$f"; done >| /tmp/restore.txt
$ wc -l /tmp/restore.txt
339 /tmp/restore.txt

# check what rsync would do
$ rsync -avn /mnt/sdb1/BACKUPS/rsnapshot/monthly.1/localhost/home/nelson/Music/ /home/nelson/Music/
# visual inspection
$ rsync -avn /mnt/sdb1/BACKUPS/rsnapshot/monthly.1/localhost/home/nelson/Music/ /home/nelson/Music/  | grep / | wc -l
# meh, close enough

# restore from backup
$ rsync -av /mnt/sdb1/BACKUPS/rsnapshot/monthly.1/localhost/home/nelson/Music/ /home/nelson/Music/