launchd Script to Backup iTunes Library Files

I recently experienced the horrible fate of having my entire iTunes Library file wiped into corruption. About half the file was ok, but the second half was a mishmash of the Russian alphabet. This really hurt. All my ratings, all my play counts, all my date addeds, all my playlists were gone, and I couldn’t do anything about it. I read all over the web trying to find some secret iTunes recovery file, but no luck. My only hope is that I can plug in my iPod and the playlists will return and maybe the rating and play count values will be put back, but I haven’t tried it yet. Either way, this still reminds us to BACKUP, BACKUP, BACKUP. So, in honor of my “tragedy,” I have compiled a very simple and easy to use shell script that will do this backing up for you. This way, if you ever have such an event occur to you, you can always revert to an earlier version of your library. First up here’s the script. Just put these 3 lines into nano and save it as BackupItunes (no file extension):
x=`date "+%m%d%y"`; cp ~/Music/iTunes/iTunes\ Library ~/Music/iTunes/LibraryBackups/iTunes\ Library_$x cp ~/Music/iTunes/iTunes\ Music\ Library.xml ~/Music/iTunes/LibraryBackups/iTunes\ Music\ Library_$x.xml
This script is super simple. It just sets the variable x to today’s date. Then it copies the iTunes Library file and the iTunes Music Library.xml file into the folder LibraryBackups. Note, that you must make this file yourself. Just execute these two commands in Terminal or use Finder. You will also want to chmod the executable.
chmod +x BackupItunes cd ~/Music/iTunes/ mkdir LibraryBackups
Now that we have the folder for backups setup, and the script to do the backups, we need to hop into launchd to make the file execute every week. launchd is the new Cron for Mac OS X 10.4.
Since we want this to be a user script, navigate into your user library folder and create a folder called LaunchAgents (if it does not already exist).
cd ~/Library mkdir LaunchAgents
Inside this create a plist file named BackupItunes.plist and put the following in it’s contents:
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE plist PUBLIC "-//Apple Computer//DTD PLIST 1.0//EN" "http://www.apple. com/DTDs/PropertyList-1.0.dtd"> <plist version="1.0"> <dict> <key>Label</key> <string>com.dbachrach.BackupItunes</string> <key>ProgramArguments</key> <array> <string>/Users/deb/BackupItunes</string> </array> <key>LowPriorityIO</key> <true/> <key>Nice</key> <integer>1</integer> <key>StartCalendarInterval</key> <dict> <key>Hour</key> <integer>12</integer> <key>Minute</key> <integer>0</integer> <key>Weekday</key> <integer>1</integer> </dict> </dict> </plist>
In essence, this will call our script at noon every Monday. That’s it for the scripting. Now we need to setup the launchd service to call our script. So now you just type:
launchctl load /Users/deb/Library/LaunchAgents/BackupItunes.plist
Note that you need to switch in your username in place of deb. At this point, you are all set up. Every monday at noon, you’re computer will backup your iTunes library. Leaving you never in the dark the next time iTunes trashes your file. I hope this helps anyone who went through the fatal iTunes Library death.



