Backing up your iTunes / iPhone / iPad universe on Windows 7

Although I've never owned a Mac, I'm about as deep into the iUniverse as one can get. My entire life is crammed into this iPhone and has been for nearing 4 years now. I'm not the only one in this situation. Losing the device isn't that big of a deal since you can go buy another one, plug it into iTunes and drop the last backup of your life on it without skipping a beat. But what happens when your computer goes up in flames? Or what if you switch computers? You want to be able to get iTunes back up and running exactly as you left it, complete with all the apps, iPhone & iPad backups, playlists, and music libraries. Unfortunately, Apple has chosen to make the back-end happenings quite complicated. The following is what I've figured out from snooping around my machine and from the intarwebs and how to automate the process of backing all of this up.

Before we get started, note that this post applies to Windows 7 and iTunes 10.1.1.4. Future versions of iTunes may not be organized the same way, and could ruin Christmas. Previous versions of iTunes and Windows may have the same difficulties. I'll try to keep up with changes to iTunes in future posts.

Couldn't care less about what these files are and just want the backup script? Fine, here it is. Your browser may interpret it as just a text file (Chrome did). Save it as such, then change the extension to .bat to make it executable. Replace 'Kevin' with your Windows 7 user name and 'Z:\iTunes_Backup' with a backup directory of your choice. I do recommended you read on though...

Backing up the iTunes folder in your user directory

There are two important files located here: C:\Users\UserName\Music\iTunes\
Apple explains what they do here, but I'll regurgitate it here as well. (Interesting note: That knowledge base page I linked to shows the files as 'iTunes Library' but mine are named 'iTunes Music Library'.)

iTunes Music Library.xml. - This file is pretty long (mine was 104,373 lines long!) and contains all sorts of information about the tracks, like where they live, play counts, etc. Other applications use this file, but it looks like only iLife-related ones care. This is more important on Mac OS.

iTunes Music Library.itl - This is the important one! It's a database that contains song-specific data like ratings and comments. Most importantly, it contains your playlists. Sadly, it's not something that's human readable. We want to hang on to this one.

If you've never changed your iTunes media folder, it lives here and is aptly named 'iTunes Media'. We'll discuss that folder later.

What else is in this folder? In my installation, it looks like the rest of the stuff here is a graveyard of old files and apps from iTunes-es of yesteryear. It's better to be safe than sorry, so I back up the entire business.

Backing up iTunes local application data

The next folder we want to hang on to is: C:\Users\UserName\AppData\Local\Apple Computer\iTunes
iPodDevices.xml looks like it keeps track of what devices iTunes has found in the past.
Not going to lie to you, I have no idea what the rest of these files are for. The only reason I recommend hanging on to them is because the timestamp is always recent on them, so iTunes is actively using them. 

Backing up iTunes roaming application data

Hang on to this guy: C:\Users\UserName\AppData\Roaming\Apple Computer\iTunes

There are several more files and folders in this directory, but again I'm not too sure what they're for. We'll keep them though. 

Also, this guy: C:\Users\UserName\AppData\Roaming\Apple Computer\MobileSync

This is where your iPhone and iPad backups live. They're named incoherently, but these are the backups you choose from in iTunes when restoring from an old image of your device. There should be a few gigs of data here.

Let's also hang on to these other folders since they seem to also be related:
C:\Users\UserName\AppData\Roaming\Apple Computer\SyncServices
C:\Users\UserName\AppData\Roaming\Apple Computer\Preferences
C:\Users\UserName\AppData\Roaming\Apple Computer\Logs

Backing up Music, Video, Books, Apps, and everything else

Find your iTunes media folder location by opening up iTunes, then selecting 'Edit' - 'Preferences' - 'Advanced' (tab). iTunes defaults to C:\Users\UserName\Music\iTunes\iTunes Media

This is where iTunes keeps your apps, downloaded music, videos, podcasts, movies, TV shows, books, etc. That said, this is a pretty important directory. I don't recommend keeping it in the default location; instead I'd rather see it on a separate hard drive so it's not tied into your user account and with the rest of the OS. As a rule I always keep data separate from the operating system drive in case it goes down. Since we're backing this stuff up, it may be a moot point, but a good practice nonetheless. 

Backing up pictures and videos taken on your iPhone

Unfortunately, I haven't figured out how to automate this part yet. When Windows finds your phone after you've plugged it in, it shows up in Windows Explorer and you can manually copy those files out of it.

Automating this process with a script

I use a script to back up my data, and you can download it here. Unless you happen to be me, you'll need to change it slightly.
  1. Change all instances of 'Kevin' to whatever your Windows 7 user name is. If you're name is Kevin, you just saved yourself nearly 8 seconds of work.
  2. Change all instances of 'Y:\iTunes_Backup' to the path where you want to back all this stuff up to. 
  3. Change 'J:\iTunes' to your iTunes media directory. If you keep it in the default location, C:\Users\UserName\Music\iTunes\, you can comment this line out since it will be included in the first robocopy command. Add two colons '::' to the beginning of the line to comment it out.
  4. Don't be alarmed when launching this script opens a command prompt; people get scared of DOS-looking things. Nothing shady is going on, with the exception of sending your bank account and credit card information to Wikileaks. 
Every time I sync my phone, download music, or do anything else iTunes related, I always launch the script after I close the application. It just takes a second to kick off, then you can walk away and let it do its thing. Just make sure you close iTunes before you run it. Putting an icon on your desktop helps too. 

Recovering from a disaster with your backup

Simply install the latest version of iTunes, copy your backup files to their original locations, and pray that this version of iTunes hasn't changed where it wants to see these things. Since we kept the folder structure the same in the backup as it is in your user directory, you can simply copy all of the backup data from its safe haven into your user directory once you get your computer fixed/installed/revived/etc. If you are able to maintain the same paths that you had before, when you open up iTunes after restoring all of these files, everything will be exactly as you left it. If the path to your iTunes media folder has changed, you'll have to create a new library and the associated playlists, comments, ratings, etc will be wiped out. Bummer aye? That's iTunes for ya. Hey, at least you've got your iPhone backups, right?


Hit up the comments below this post if you have any questions or corrections.



Wacky Folder Problem in Windows 7

One of the folders on my desktop that I keep a mirrored copy of on my network is an iTunes media folder. This guy contains about 80GB of music and video that I'd rather not see vanish when my RAID array breaks, a HDD explodes, or Somali pirates make off with my desktop. After running a command line utility called 'robocopy' regularly to perform this backup across the network, I noticed something strange in the drive where I keep this folder.

[Click to enlarge]

Yep, there are two folders named iTunes there. Try making two folders with the same name in Windows Explorer. Good luck with that.

Reviewing the properties of both of these folders showed that one was about 80GB and the other was just a handful of bytes. It's pretty clear which one was bogus, but attempts to delete it through the file browser were met with "The system cannot find the specified path". Awesome.

Interestingly, drilling down into the bogus folder looked like I was drilling down into the non-bogus folder. It was filled with the same files and folders as the 80GB one. I created a few new files, dragged, and dropped them in. These files simply vanished; they were not in this folder when opened.

I tried all sorts of things, ridiculous things, things that didn't make sense all in an attempt to trick Windows into figuring out where this folder actually was (or was not!). Nothing worked. Renaming the good iTunes folder allowed me to try removing it via the command line, but none of the commands could figure out where this magical unicorn of a folder was. However, once I renamed the good iTunes folder, the contents of the bogus folder changed.

[Click to enlarge]

Inside the folder were two files that I had dropped into this folder earlier, but had disappeared. A folder, MIR, lived in here as well and was also un-deletable. This newly discovered folder, however, links this issue to robocopy. I use the switch /MIR to perform the mirroring of this folder, making this mess likely a leftover turd from robocopy. Presumably it was caused by prematurely terminated one of the backup sessions, but I have yet to repeat it.

After scouring the internets for a solution to this problem over the course of a few days, I finally ran into this thread. All the way at the bottom, a forum member mentioned using the short DOS folder names to wipe out un-deletable folders by using the /x switch for the dir command.

[Click to enlarge]

The bogus folder had a short DOS folder name of ITUNES~1. Armed with this alternate path to the same folder, I ran 'rmdir /s ITUNES~1' and miraculously, it was gone forever. I had the same issue on the remote drive and was able to erase it the same way.

It was suggested that this could be a symbolic link; something I was familiar with in Linux but had no idea even existed in Windows. I looked into this quite a bit, and found that they were treated very similarly to regular files and folders when it comes to removing them. However, what I had going on here wasn't a symbolic link. I created one to see how it would show up.

[Click to enlarge]

The command prompt, even in its seemingly archaic fashion, still knows best. Symbolic links are shown as what they are; what I had before was most definitely not a symbolic link. It was something else entirely. 

So what was it? Maybe it was just a slightly corrupt folder, but I suspect this won't be the last time it happens. And when it does, it's short DOS folder names all the way.