Friday, December 17, 2010

BackupRestore User Data with DeployStudio

One of the minor annoyances I've run into during a re-imaging process is the time and trouble it takes to backup and restore user data. Our users store home folders locally on their Macs and use AD accounts to log in. I don't want to get into network homes or syncing homes. We do backup computers over the network via Tivoli storage manager, but thats another topic and we don't backup graduate student machines.

Usually, I have to spend time backing up the home folders and restoring them after imaging the Mac. This relatively easy task slows me down taking up my (precious?) time to do this. Training someone would be fairly simple, but then we'd have to hire someone who can support Macs; thats not an option right now.

The manpower I do have available to me have already been trained to use DeployStudio. The great thing about DeployStudio is; I can tell the staff person what workflow to run, I know the workflow will run the correct steps for the task, and I can give access to workflows for specific users as they grow to do more (or remove access if needed).

Since DeployStudio does not have a built in option to;
  • backup user data
  • image a machine
  • restore the data
...then we have to do it ourselves!

Introducing the DeployStudio BackupRestore* scripts

The concept with the scripts are easy...

Backup Script:
  • Creates a new folder called "Backups" DeployStudio Repository
  • Data is stored in a subfolder using the MAC address as the name (sans ":")
  • User home folder is backed up using tar
  • User account is backed up using plist, password hash is also backed up
  • Network and Mobile accounts (AD/OD) are skipped (if uid is > 1000, skip account)
Once the backup task is finished, you can run as many other tasks as you'd like. Image the hard drive, install packages, create users, etc... The last step is to restore the user data.

Restore Script:
  • Finds the backup folder based on the MAC address
  • For local accounts; Creates all the accounts & restores password hash
  • Restores the user home folders
  • Does NOT delete backups
It's important to note that the data is not removed from the backup folder. If you leave the data there, it will be restored on the machine if you run the workflow again. This could lead to unexpected results. For example, if you run the BackupRestore scripts in January you will backup "userA" and "userB". Lets say "userB" leaves in March, you delete their data on the machine, then "userC" starts using the Mac. Then, sometime in May, you run the BackupRestore scripts again The script will overwrite the backups of "userA" and "userB" and, for the first time, backup "userC". When the data is restored, you will restore all three users. This can be avoided by removing the specific users files in the Backups folder.


MAC Address: 01:02:03:04:05:06
Users: "userA" and "userB"
"userA" is a local user - GeneratedUID="ABC12345"
"userB" is a mobile account

When the Backup script creates a machine specific folder, it uses the MAC address of the machine without the ":".


Then the users home folder is backed up, with tar, to the Backups folder using their username, machine MAC address, and the keyword "HOME":


The account details are backed up to a plist file with the naming convention of:


Then the password hash is backed up as:


...the mobile account is skipped because it can be recreated when the user logs back in.

That is it for backing up! When the Restore script runs it creates the user account, restores the GeneratedUID (for the local accounts), then restores the home folders on the target for EVERY record in the folder that matches the Macs MAC address.

The implementation in DeployStudio is very easy. Simply take the scripts and wrap them around your workflow:

Thats it!

I'm releasing this code for all, please share, edit, use! Please report bugs and branches back to me at I'd love to hear what your doing!

Thanks to Pete Akins and his createUser.pkg from the instaDMG project. It was heavily relied upon for the backing up and creating of users. Seriously, Thank You Pete!

Thanks to the ##osx-server channel on the freenode IRC server. Everyone was very helpful in helping me work out the "bugs".

* Updated Monday December 20, 2010
- Resolved issue with accounts not showing in System Preferences after restore.
- Added support for restoring admin rights for network users


These scripts have been tested on 10.6 with local accounts and AD accounts. Machines are bound to AD via Directory Utility, with mobile accounts being created at login.

*Crappy generic name subject to change without notification


Data Scavenger said...

Excellent idea! I'm looking through the scripts now to see if they can be used at my job.

Observations so far:
1) You use "tar czf" to archive, but "tar -xf" to restore.
1a) That uses compression on the archive, but not the restore.
1b) Should that use the -p flag, to preserve permissions?
1c) Also just to nit-pick, why the lack of a "-" on the archive command but keep it in the restore command?

2) Why use tar at all? It won't get a number of things, such as resource forks. That could be an issue if they're using old software, like AppleWorks. The "ditto" command was specifically designed to handle Mac files. Something like "ditto -VXcz --rsrc $source $archive.cpio.gz" should do the trick. That will make a single file that is compressed with gzip (like tar -x) that contains the files with all their resource forks. If you prefer, the -j flag will compress it with bzip2 and -k will use PKZip.

Hope that helps. I'm looking forward to trying these scripts out!

Rusty Myers said...

Data Scavenger,
Great suggestions, get a hold of me rustymyers at gmail dot com.


Rusty Myers said...

Data Scavenger,

1) compression is for creating the archive only
1a) compression is for creating the archive only
1b) added that in beta0.4, yes it should.
1c) added - never noticed it.

2) Great suggestion, I've added it. Test it here:

Feel free to suggest other methods for backup.

Kostas said...

Hey, just found it! Is it alive?

Best regards


Rusty Myers said...

Hi Kostas,

This project should still work with the latest OS. I've not made updates to it in a while, nor have I had a need to use the code.

Feel free to let me know if something isn't working and I can try to help.