6

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!

8

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

#!/bin/bash
# script to backup Pi SD card
# 2017-06-05
# 2018-11-29    optional name
# DSK='disk4'   # manual set disk
OUTDIR=~/temp/Pi

# Find disk with Linux partition (works for Raspbian)
# Modified for PINN/NOOBS
export DSK=`diskutil list | grep "Linux" | sed 's/.*\(disk[0-9]\).*/\1/' | uniq`
if [ $DSK ]; then
    echo $DSK
    echo $OUTDIR
else
    echo "Disk not found"
    exit
fi

if [ $# -eq 0 ] ; then
    BACKUPNAME='Pi'
else
    BACKUPNAME=$1
fi
BACKUPNAME+="back"
echo $BACKUPNAME

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 > $OUTDIR/Piback.img.gz

#rename to current date
echo compressing completed - now renaming
mv -n $OUTDIR/Piback.img.gz $OUTDIR/$BACKUPNAME`date +%Y%m%d`.img.gz
  • 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 '16 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 '16 at 7:21
4

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'
  • 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 '16 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 '16 at 6:23
  • By the way, is there any particular reason for using bs=4M or could it be anything? – Will Feb 22 '16 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 '16 at 15:59
  • @Milliways By 'raw disk mode' do you mean when the disk is unmounted? – jmathew Sep 23 '16 at 14:34
1

Note the bs=4M will not work on Mac (at least Sierra), use lower case 'm' that will work like "dd bs=4m if=/dev/rdisk2 of=~/Documents/abc.img"

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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