Mac OS X: Changing You’re Short Name Without Breaking Time Machine

April 22, 2009 · 5 comments

Today I got the urge to change my Mac OS X Leopard account short name (home directory) so that I would have less to type in Terminal. Here’s how I changed my short name without breaking my Time Machine backups.

A warning

This guide is meant to serve as a reference to those with systems administration experience, or those with an insatiable thirst for tinkering. There are a lot of things that could go horribly wrong, possibly even making your backups unusable. I make no claims that this method will work and I cannot provide any support.

I have not tested the final Ruby script provided below. It was originally two utility scripts that I wrote while working through things and I combined them for simplicity. Review the code, test, and then try at your own risk.

Prepare Time Machine

When you change your short name your username and possibly group will also change. In this event, Time Machine will start a new backup from scratch. This can be avoided by going through all of the Time Machine backups and performing the following actions:

  1. Change the name of the home directory to your new short name (username)
  2. Update the permissions to your new username and group

First, turn off Time Machine backups and make sure that a backup is not currently in progress. Next, disable ACLs on your Time Machine volume using the following command (making the appropriate substitution for <tm_volume>). Without this step, you will be denied access to the files, even as root.

1
sudo fsaclctl -p /Volumes/ -d

Next, copy the Ruby script below to a file named change_username_tm.rb and then modify it to suit your changes.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
#!/usr/bin/env ruby

dry_run      = true                  # Run in dry-run mode. Nothing will be changed.
# Change to 'false' when you're ready to roll.

account_name = "Jon Stacey’s iMac"    # Name of your Mac (found in Systems Preferences -&gt; Sharing)
drive_name   = "Macintosh HD"         # Name of your primary hard drive
new_username = "jon"                  # Your new username that you want
new_group    = "jon"                  # The new group you belong to
old_username = "jonstacey"            # Your old username that you detest
old_group    = "jonstacey"            # The old group that you used to belong to

time_machine = "Time Machine Backups" # The name of your Time Machine Backup drive

# End configuration

# Create array of all backups
paths = Dir.glob('/Volumes/' + time_machine + '/Backups.backupdb/' + account_name + '/*/' + drive_name + '/Users/' + old_username)

# Rename
paths.each do |old_path|
unless (old_path == '/Volumes/' + time_machine + '/Backups.backupdb/' + account_name + '/Latest/' + drive_name + '/Users/' + old_username)
new_path = File.join(File.dirname(old_path), new_username)

# Rename folder
puts "Changing #{old_path}\n to #{new_path}\n\n"
File.rename(old_path, new_path) unless (dry_run)

# Update permissions
command = "sudo chown -R #{new_username}:#{new_group} \"#{new_path}\""
system command unless (dry_run)
end
end

When ready, execute the script in dry-run mode and review everything. When you’re satisfied, set dry_run to false and run as root.

1
sudo ruby change_username_tm.rb

Note: There were a lot of files in Library that would error out when trying to change permissions. Everything seems to work, so I guess I’ll find out for sure the next time I attempt a restore (may that day never come). Worst case scenario, I have to restore my files by hand which is what I’ve been doing for years on Windows.

Finally, re-enable ACLs on the TM volume using the following command.

1
sudo fsaclctl -p /Volumes/ -e

* Don’t enable backups yet!

Change your short name

Follow Apple’s new instructions on changing your user short name under Leopard: http://support.apple.com/kb/HT1428

Finish up

Say a little prayer and give Time Machine a try to make sure you can still see all of your backups. If everything seems to be working, go with the hail marry and try running a Time Machine Backup Now (hint: there is an option if you click on the Time Machine status button in your menu bar). If the backup works, then you may be home free and it’s safe to re-enable automatic backups.

Breath a sigh of relief and promise yourself to plan ahead the next time you reinstall Mac OS X.

Your experience

If you have had success following this or another method, let me know by posting a comment! But remember, I can’t provide any serious technical support ;-)

{ 5 comments… read them below or add one }

1 Matt October 31, 2009 at 9:19 am

Thanks a ton! I installed Snow Leopard (clean install) but forgot to use the same username as my Leopard install. I ran your script and now Time Machine is working beautifully again.

Reply

2 Jon Stacey October 31, 2009 at 11:55 pm

@Matt, Great to hear!

A note to all: Keep in mind that I have not fully tested this. I’ve been blessed with a reliable iMac thus far. However, while I haven’t tried a full restore as part of a reinstall I have restored individual directories and files without any issues.

Reply

3 Stefan November 16, 2009 at 5:27 am

Doesn’t time machine start from scratch because you change the home folder, not because you change your short name (which you can do wihtout changing the home folder)?
I wonder, would it be possible to change the home folder to a directory which is a hardlink to your old home folder. Would that prevent time machine to start a new backup?

Reply

4 Jon Stacey November 18, 2009 at 10:55 am

@Stefan, The short name should match the home directory name. You might be able to get away with a hard link, but personally I wouldn’t want to because it adds unnecessary clutter.

Reply

5 Stefan November 18, 2009 at 11:15 am

You are right, and the only reason I would be temped to try the trick with the hard link is to leave the old home folder in place for time machine after a short name change. I just thought, maybe this would convince time machine that these are all actually the same files. But anyway, I forgot that you can’t create a hard link to a folder. Or can you?

Reply

Leave a Comment

Previous post:

Next post: