Looks like no one’s replied in a while. To start the conversation again, simply ask a new question.

Restore Multiple Partitions Through Disk Utility With AppleScript?

Hi!


I need an AppleScript to restore a bunch of partitions from a master drive to a stack of external target drives. I don't mind partitioning them 1st, if necessary.

The master drive has 16 partitions, all equally sized, and I'd like a script to run that will have Disk Utility to just go down the list restoring each partition to the matching 16 partitions on one of the target drives.

Anyone know of or have anything l could use for this? Any advice on making this script? I'm not an advanced AppleScripter. And finally, would there be any way to have the script partition the target drives as well?


Thanks in advance!

Mac OS X (10.7.3)

Posted on Apr 13, 2012 10:39 AM

Reply
20 replies

Apr 13, 2012 11:15 AM in response to aaanorton

unix has the diskutil utility which seems to be what you're looking for. Without knowing precisely what you're doing (are you trying to make exact copies of the master disk, or just copy the partition structure?) I can't give you precise code; can you give more details?


you can run this from applescript or a shell script, whichever you're more comfortable with.

Apr 13, 2012 11:59 AM in response to twtwtw

I'd like exact copies of the master disk. The master has 16 partitions, each (or most) of which contains data. I'd like to be able to plug in a FW drive and run a script that partitions the target with 16 equal partitions, and then restores each of the master's partion data to each of the target's.

If the partitioning portion of this is more work than necessary, I'd be happy to manually partition each new target drive before running a script to copy the data.

Make sense? I'll need to do this fairly regularly, so a script would really help.

More details:

Master and target drives are all FW 400/800 external drives.

I'd like some utuility for choosing the master and target drives when the script launches.

I'm pretty sure a shell script can be saved as a launchable application, right? So either would be fine. I would like to be able to easily share this with co-workers.

Apr 13, 2012 4:56 PM in response to aaanorton

You can use diskutil to do all of this.


You'll need to know the device id of the FW drive.

disk0 will be your boot drive. If your 'master' drive is a second drive (other than your boot) it'll typically be disk1, and an attached 'slave' drive would be disk2 - but you should verify that.


Once you've worked out your IDs you can partition the drive:

diskutil partitionDisk disk2 16 GPT [format name size]+

The last three parameters indicate the partition format (e.g. 'jhfs+' for Journaled Extended), partition name, and size) and should be repeated for each partition on the disk - e.g. 16 times in this instance. Sizes can be specified in bytes, kiloybtes, megabytes, gigabytes, terabytes or a percentage of disk space by appending the value with a 'B', 'K', 'M', 'G', 'T' or '%', respectively. You can also use 'R' (without any digits) to use the remaining space on the drive.


Once you've partitioned the disk you can replicate the 16 partitions via asr. For example, this AppleScript snippet will iterate through 16 partitions on the drives (although there's no error checking to make sure the partitions are valid:


repeat with part from 1 to 16

do shell script "/usr/sbin/asr restore --source disk1s" & part & " --target disk2s" & part & " --erase" with administrator privileges

end repeat

Apr 13, 2012 11:44 PM in response to Camelot

This looks very interesting! I should have a chance to check it out in a couple days. I will definitely report back.

It looks like the partitioning part would be easier to do manually in Disk Utility. There I can just specify 16 partitions and let the application figure out the details of size and names.

Will the repeat script as specified rename the target partitions to whatever the source partitions are named? I assume it will.

I know I can get the device IDs via the Get Info button once in Disk Utility, but is there an easier way?

And can you explain why why the IDs in the repeat script end in s, as in disk1s? I see that same convention in System Info: disk0s1 as the device and disk0s2 and disk0s3 as the volumes.


Thanks a ton! More soon.

Apr 14, 2012 8:39 PM in response to aaanorton

It looks like the partitioning part would be easier to do manually in Disk Utility. There I can just specify 16 partitions and let the application figure out the details of size and names.


If you know the size of the partitions you want/need, then it's easy to do. If you want them to all be the same size then a little AppleScript math before the diskutil command will do the trick, or you could use the % form:

diskutil partitionDisk disk2 16 GPT jhfs+ part1 6.25% ...


where 6.25% is the equivalent of 1/16 of the total disk space. Creating 16 partitions of this size will consume the entire disk (although you might want to use 'R' for the last partition to use the remaining space, just in case there are rounding issues.


Will the repeat script as specified rename the target partitions to whatever the source partitions are named? I assume it will.


You know, that's a good question. Off hand I don't recall whether asr restore copies the volume label or not. I suspect not so you'll either need to name that at the beginning (via the diskutil partitionDisk command) or after the data's copied, via standard AppleScript commands to rename disks, or via diskutil rename.


I know I can get the device IDs via the Get Info button once in Disk Utility, but is there an easier way?


You can get device IDs via diskutil info, but the trick is identifying which disk is which. A little experimentation might be in order.


And can you explain why why the IDs in the repeat script end in s, as in disk1s?


Sure - disk0 refers to the entire disk and all its partitions. The s denotes a slice (aka partition) on that disk.

So disk0s4 refers to the fifth slice/partition on disk0 (slices are 0-based, so disk0s0 is the first partition).

Note that the boot volume is commonly disk0s2 - the third partition since there's likely a partition map and a EFI boot partition before the actual boot volume. You'll need to look at the partition map of a sample disk to work out what the appropriate numbers are.

Apr 15, 2012 10:13 PM in response to Camelot

Camelot wrote:



And can you explain why why the IDs in the repeat script end in s, as in disk1s?


Sure - disk0 refers to the entire disk and all its partitions. The s denotes a slice (aka partition) on that disk.

So disk0s4 refers to the fifth slice/partition on disk0 (slices are 0-based, so disk0s0 is the first partition).

Note that the boot volume is commonly disk0s2 - the third partition since there's likely a partition map and a EFI boot partition before the actual boot volume. You'll need to look at the partition map of a sample disk to work out what the appropriate numbers are.


10-4. Thanks! I figured that was the case from the info I'm viewing about my disks. Makes perfect sense!


So I gave the script a whirl and here's the result I got in Apple Script Editor:


User uploaded file


Basically, it seems to be trying to restore part1 of source to slice1 (device) of target. Here are the results from Terminal shell:


[1] 84221

[2] 84222

[3] 84223

-bash: : command not found

-bash: part: command not found

[4] 84224

-bash: --target disk2s: command not found

-bash: part: command not found

-bash: --erase: command not found

[1] Exit 127 do shell script "/usr/sbin/asr restore --source disk1s"

[2] Exit 127 part

[3]- Exit 127 " --target disk2s"

[4]+ Exit 127 part


I'm thinking I should change the script to specify the 1st partition on disk2 like this:


do shell script "/usr/sbin/asr restore --source disk1s" & part & " --target disk2s2" & part & " --erase" with administrator privileges


Make sene? Please lemme know whatcha think!

And thanks!

Apr 15, 2012 11:38 PM in response to aaanorton

It isn't clear why the original script failed, but I don't think your solution is valid, either.


If you change the command in the way you describe, you have to consider how the script looks as it iterates through the loop from 1 to 16.


The first time through the loop, 'part' equates to 1, so the part of your script:


--target disk2s2" & part & " --erase"


will translate into:


--target disk2s21 --erase

I doubt there's 21 partitions on this disk. Of course, as you iterate through the loop and get towards the end - say, part = 16 - you'll get to --target disk2s216 and I know there isn't 216 partitions 🙂


So what I think this comes down to is a simple mismatch on the partition IDs - either there's some extra partition ID that I'm not seeing, or the disk hasn't been partitioned yet. Indeed - the error message in your screen shot - 'disk2s1 is not a volume' - implies that's the case. Either that, or the disk isn't mounted at disk2, so you might need to use disk3, or disk4 or some such. I did warn about this in my original post saying that you'd need to validate the disk IDs before trying this.

Apr 16, 2012 12:18 AM in response to Camelot

Camelot wrote:


It isn't clear why the original script failed, but I don't think your solution is valid, either.


If you change the command in the way you describe, you have to consider how the script looks as it iterates through the loop from 1 to 16.


The first time through the loop, 'part' equates to 1, so the part of your script:


--target disk2s2" & part & " --erase"


will translate into:


--target disk2s21 --erase


Hmmm... My thought was to give it a valid starting point, but I see now what you're saying.


I doubt there's 21 partitions on this disk. Of course, as you iterate through the loop and get towards the end - say, part = 16 - you'll get to --target disk2s216 and I know there isn't 216 partitions 🙂


Yes, I draw the line firmly at 199 partitions...


So what I think this comes down to is a simple mismatch on the partition IDs - either there's some extra partition ID that I'm not seeing, or the disk hasn't been partitioned yet. Indeed - the error message in your screen shot - 'disk2s1 is not a volume' - implies that's the case. Either that, or the disk isn't mounted at disk2, so you might need to use disk3, or disk4 or some such. I did warn about this in my original post saying that you'd need to validate the disk IDs before trying this.


OK, I'm willing to chalk this one up to a PEBKAC issue for now. I'll re-attempt it tomorrow and diligently (re)check my IDs.

Regarding that disk2s1 is not a volume message, If I had used the wrong ID, wouldn't it report something more like disk2s1 does not exist?

More soon!

Apr 17, 2012 9:39 PM in response to Camelot

Right on. But this seems to fail too. This from AppleScript Editor:


tell current application

do shell script "/usr/sbin/asr restore --source disk1s2 --target disk2s2 --erase" with administrator privileges

--> error " Validating target...

\"/disk2s2\" is not a volume

Could not validate target - No such file or directory" number 1

Result:

error " Validating target...

\"/disk2s2\" is not a volume

Could not validate target - No such file or directory" number 1

Apr 17, 2012 9:49 PM in response to aaanorton

according to man asr, asr can take volume mountpoints and /dev entries. it doesn't say anything about disk identifiers. You might want to try:


do shell script "/usr/sbin/asr restore --source /dev/disk1s2 --target /dev/disk2s2 --erase" with administrator privileges


However, I'm not sure if /dev entries understand the s* notation.

Restore Multiple Partitions Through Disk Utility With AppleScript?

Welcome to Apple Support Community
A forum where Apple customers help each other with their products. Get started with your Apple ID.