How can I enable Time Machine encryption on the command line?

Is is possible to enable encryption for a Time Machine target disk with a script or using the command line?

Is an encrypted Time Machine disk really just the same as a normal disk, full-disk-encrypted using FileVault?

I'd like to automate as much as is convenient when installing a Mac for a new user. This includes backups. We're using OS X Mountain Lion.

Additional discoveries:

  • You can ask a target to be encrypted from the Time Machine preferences GUI. This does not make it show up as such using the fdesetup command. It will however list as encrypted using diskutil cs list
  • If first encrypting a drive, the Time Machine prefs GUI will say "Encrypt backups" for that entry. This would support the method suggested below by Rene (except he suggests doing it on an encrypted image placed on a disk).

No, sorry chickpee, I believe you're incorrect.

Is an encrypted Time Machine disk really just the same as a normal disk, full-disk-encrypted using FileVault?

Yes. It is. This would be "FileVault 2" we are talking about, aka CoreStorage, Apple's newish logical volume manager. This is different that the previous TM and FileVault technologies, which are based on AES-encrypted sparse-bundle disk images (which are still used for network backups, etc.). The process that starts in System Preferences (these days) when you enable disk encryption (whether on an external disk, for Time Machine, or on the boot drive for FileVault), provided the disk is suitable, it does an online conversion from a traditional GPT Partition Table to a single monolithic data store, with a very small partition for the CS firmware. Logical volumes (in logical volume groups) are then carved out of this, and these (software) volumes are then HFS formatted and encrypted.

I believe the most straightforward method for doing this would be to:

  • Attach the disk you're going to use, wipe it. Free space or a single HFS+ partition.
  • diskutil cs create/convert (wasn't/was formatted; unimportant) to initialize and add a new LVG
  • diskutil cs createVolume, create a single LV. You could enable encryption at this point, with diskutil cs encryptVolume, if you know the passphrase you're going to use; if not, leave it unencrypted for now.
  • diskutil partitionDisk diskX -- see below -- CS volumes appear as if they are completely autonomous, separate disks, so you partitionDisk.

Then: mount and unlock the volume on your new user's machine. Once the disk is unlocked, there shouldn't be any trouble 'adopting' it for use there. If you want to put it into a config script, I believe it's just something like tmutil -a /Volumes/Foo, tmutil startbackup -ad disk.... This is the part I'm least sure about, but I'm also sure its easily doable. I haven't done this for Time Machine per se my self, but I pre-encrypt disks for FileVault like this all the time, and the OS sort of just knows what to do with if after that.

A properly suitable CS-enabled-disk is going to appear like this in diskutil (although you might not have the third partition on disk0 if it's knows it's not going to be a boot drive:

/dev/disk0
   #:                       TYPE NAME                    SIZE       IDENTIFIER
   0:      GUID_partition_scheme                        *251.0 GB   disk0
   1:                        EFI EFI                     209.7 MB   disk0s1
   2:          Apple_CoreStorage                         250.1 GB   disk0s2
   3:                 Apple_Boot Boot OS X               250.0 MB   disk0s3
/dev/disk1
   #:                       TYPE NAME                    SIZE       IDENTIFIER
   0:                  Apple_HFS Macintosh LV           *249.8 GB   disk1

disk0 will not appear as encrypted, ever, since this is what the volume manager basically has to 'boot' off of. disk1 will be encrypted and will require the passcode to mount.


I'll chance an answer.

Encrypted Time Machine backup isn't the same as FileVault, it's actually the same as selecting "Mac OS Extended ([Case-sensitive], Journaled, encrypted)" in Disk Utility.

So to automate it with an external drive, assuming it is "disk1", I think the following should work (sorry, don't have a spare USB drive around to test on):

diskutil partitionDisk disk1 1 GPT JHFS+ TimeMachine *X*G [X=size of partition]
sudo tmutil setdestination /Volumes/TimeMachine
diskutil cs convert disk1s2 -passphrase *xxxx* [or -stdinpassphrase if you prefer]