Backup / Restore a user on OS X

I can’t say enough about how important it is to occasionally backup your data. I’ve had several instances where doing this really saved me, and I’ve had friends who didn’t do it, and it has really cost them. I’m a firm believer in the awesomeness of Time Machine on the Mac. I’ve used it a few times to restore my system to a previous state, after I thought my system had been compromised, and it’s just a thing of beauty…for the most part.

The one caveat that I have about this is that there seems to be a minor flaw in doing the restorations, at least when using the OS X DVD that came with my MacBook Pro, which I believe was 10.6.3. It seems that if you don’t use the latest snapshot, then you can’t really guarantee which snapshot you’ll actually get. In my case, I’ve twice tried to restore my drive using a snapshot from an hour before the last one. This resulted in my system reverting anywhere from 2-5 days before my latest snapshot. I don’t know for sure how far back the revision was, I’m just basing this on changes in my user’s home folder, and CDs I’d ripped to iTunes. In both cases, it wasn’t a huge deal, because my files hadn’t changed that much in that amount of time. But if I’d been developing a new website at the time, I would have been pretty annoyed!

Now I know, most people would simply choose the latest snapshot, and I’ve done that too, and the restoration process worked perfectly. So this is definitely a rare bug, but I’m not most people. I tend to do things differently.

Anyway, this article discusses another way to make sure that you protect your data. In fact, I’ve recently had to repeat this little task, due to some configuration error that I was having with OS X. The only fix that the Apple Genius Bar could come up with, was to completely nuke my HD and start over. And I didn’t want to use the Time Machine image, since those snapshots would have been corrupted as well. So I’m glad that I took a snapshot of my main user’s folder before taking the laptop over there.

So here’s the process to make sure that all your pictures, iTunes downloads, and what-not are still safe.

Note: It is important to point out that this process isn’t to be used to restore your primary administrative account. You’re going to have to create at least one administrator before restoring the users in Step 4. If you need to restore your primary administrator account, create another administrator when installing your system, and use it to restore your old account. Then stop using administrator accounts for daily use!

Step 1: Backup your user’s folder

The optimal way to do this, is to boot from the OS X DVD that came with your system, but instead of letting the install utility progress, find the Utilities menu item at the top of the screen, and select Disk Utility. If your system is stable and active, you can also do this from the local Disk Utility, which is located under Applications > Utilities, if necessary. You’ll need to have a external drive connected directly to your system, as Disk Utility won’t recognize network drives (unfortunately).

Either way, with the Disk Utility application open, create an image from the /Users/ home folder, by selecting File > New Image from Directory.

In the Select Folder to Image window, select your primary drive, MacIntosh HD by default. If you need to backup more than one user folder, navigate to the user’s folder /Users/. In the case of just backing up a single user, navigate to /Users/youruserfolder/.

Click the Image button.

In the New Image from Folder window, select your external drive as the storage location. Give the file another name if you like. I tend to add the date of the backup to the filename.

Click the Save button.

Step 2: Verify the image

Before you proceed, it’s a good idea to verify the image, to make sure it was copied correctly. The image you just created should appear in the list on the left in the Disk Utility window.

Click on the image, and click the Verify button at the top of the window. This process could take a bit of time depending on the size of your image.

When that process is complete, you’ll want to also scan the image to make sure you can use it to restore from. From the menu, select Images > Scan Image for Restore.

Note: You can also use this basic process to image the whole drive, and then use the image for restoration using the Disk Utility application.

Tip: Store the image file in more than one place if possible. I have had these files get corrupted on occasion. It can ruin your day if you wipe your system, only to find your image file is corrupt.

Once you’ve verified the image, you can close the Disk Utility application and proceed with reinstalling your system.

Step 3: Reinstall your system

Make sure to verify the restoration image before you wipe your drive!!! I can’t emphasize that enough.

Reinstall your system from the OS X DVD as normal. During this process you’ll need to create an administrative account other than one of the ones you’re attempting to restore, as noted above. Do not create the user accounts you’ll be restoring. We’ll cover that in Step 5.

Install any software you need, and then proceed to Step 4.

Step 4: Restoring the user folders

At this point, you’ll need to be logged in as the primary administrator. Once you have your system to the point where you’re ready to restore your user accounts, open the disk image by double clicking on it. It’s a good idea to re-verify the image using the Disk Utility, if it’s been a while since you created it (see Step 2).

Now we’ll copy the user folders back to /Users/ individually. Open up the Terminal application for this process. It’s located under Applications > Utilities.

If you imaged the entire /Users/ folder, run the following command for each user folder:

sudo cp -Rp /Volumes/imageName/userfolder /Users

Otherwise, for a single user folder image:

sudo cp -Rp /Volumes/imageName /Users/userfolder

Note: You need to be careful to leave off a trailing / on the folder names for these commands to work properly. If you add say, userfolder/, then you’ll copy the contents of the folder, instead of the entire folder. Also we’re assuming the the userfolder/ doesn’t already exist in /Users/.

You use the -p option in these commands to make sure that the file creation times are preserved. You probably don’t want today’s date to be the creation time on all your files after all.

Make sure all the folders in /Users/ are named the way you want them before proceeding. The way OS X sets these up by default is all lowercase and no spaces.

Please take a moment to read the (Optional) Step 6, and decide if you want to do this step. If so, now may be a good time to do that. Otherwise you can do it later.

Step 5: Create the user accounts

Open the System Preferences window from either the Dock, or from under the Apple symbol in the menu bar.

Under System, select Accounts. The list of users on the system appears on the left. There is a + and button under this list. You’re going to click the + button to create new users.

Create a new user with the same Account name as the folders you created in Step 4. The Account name (short name) must be exactly the same as the folder name. The Account name field should be all lower case and cannot have any spaces, so neither can the folder names. The Full name can be whatever you prefer. I believe you can use either name to log in with, but the Full name appears in Login window, unless you’ve changed your Login Options.

You’ll get a dialog asking if you want to use the existing folder in /Users. Select the OK button.

This will create the user, using the old folder, and it will automatically go through the folder and update the ownership/permissions of each file to make them appropriate for the user.

Repeat this step for each user you need to restore.

(Optional) Step 6: Delete the user caches

The most recent time that I used this method, my user account was acting a little bit quirky. The solution for this, after several calls to Apple was to delete the caches for the user. You can either delete them after you’ve logged in as the user, or while setting up the user accounts as administrator. When you delete these files, they will be created again when the user logs in the next time, so it’s apparently safe.

Disclaimer: It’s a good idea to keep these files until you make sure the user account works properly, so rename the Caches/ directory or move it to the trash, until you’re sure it works. If you rename it, just delete this folder later, once you’re happy with the functioning of the user account.

To remove the Caches/ folder for each user, locate the following folder in each user’s folder and move it to the trash, or rename them.

/Users/userfolder/Library/Caches

You can either do this in Finder, or in the Terminal window.

If you skipped Step 5, you can click here to go back.

If all went well you should be able to log into each user, and have the exact same setup as when you created the backup image. The same background, window locations, email settings, etc. I hope this has been helpful. If so, please leave me a comment. If you’ve run into problems, I won’t guarantee to provide assistance, but you never know.

Leave a Reply

Your email address will not be published. Required fields are marked *