Raspberry Pi Stack Exchange is a question and answer site for users and developers of hardware and software for Raspberry Pi. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

Is there a simple way using Mac OS X (shell preferred) to clone an entire SDCard from the SDCard slot (byte-for-byte)), to a file? I would then want to reverse the process and restore the image from the file to the SDCard.

I was considering using dd, but I'm wondering if boot partition / MBR data would be properly replicated. I don't want to copy the files, but rather the entire SDCard. Thanks!

share|improve this question
up vote 7 down vote accepted

I use the following script to backup SD cards on OS X:-

#!/bin/bash
# script to backup Pi SD card
# 2015-11-28
#DSK='disk4'
# Find disk with LInux partition (works for Raspbian)
export DSK=`diskutil list | grep "Linux" | cut -c 69-73`
if [ $DSK ]; then
    echo $DSK
else
    echo "Disk not found"
    exit
fi
diskutil unmountDisk /dev/$DSK
echo please wait - This takes some time
echo Ctl+T to show progress!
time sudo dd if=/dev/r$DSK bs=4m | gzip -9 > ~/temp/Pi/Piback.img.gz
#rename to current date
echo compressing completed - now renaming
mv -n ~/temp/Pi/Piback.img.gz ~/temp/Pi/Piback`date +%Y%m%d`.img.gz
share|improve this answer
    
Thanks! I like this. I think I'm going to modify this script a bit and use it. Perhaps I'll make it more general and put it on GitHub (I'll give you credit if I do). This seems awesomely-useful! – Will Feb 22 at 6:26
1  
@Will You are welcome. All of my code is freely available to all. The code is hardly original and I drew on many sources. The code to automatically identify the SD card is my own - to minimise the risk of picking the wrong one, which would be catastrophic. I did have more elegant code, but it would not work for named partitions and does not work with NOOBS. At least it fails safely if you try. – Milliways Feb 22 at 7:21

You can use dd to backup the drive. The trick is to use the device as the input, not its partitions. Taking an example from the RPi forum, you can backup and even auto compress the information in one action.

To backup the device:

sudo dd bs=4M if=/dev/rdisk2 | gzip > /Users/`whoami`/image`date +%d%m%y`.gz

To restore the device:

sudo bash -c 'gzip -dc /Users/`whoami`/image.gz | dd bs=4M of=/dev/rdisk2'
share|improve this answer
    
OS X does not use /dev/sdb for its internal card reader. Also this can be VERY slow if you you don't use raw disk mode. – Milliways Feb 22 at 4:55
    
Thanks! I'm doing this now with xz, but xz was the wrong choice as it takes hours. gzip will probably be the way to go. Also, you can use $HOME instead of /Users/`whoami` . – Will Feb 22 at 6:23
    
By the way, is there any particular reason for using bs=4M or could it be anything? – Will Feb 22 at 6:28
1  
@Will bs can be pretty much anything, though the default value (512) slows down copying considerably. Anything from 1M to 10% of your system RAM should be good. – Dmitry Grigoryev Feb 22 at 15:59

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.