7

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!

Will
  • 371
  • 3
  • 16

3 Answers3

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
Milliways
  • 62,573
  • 32
  • 113
  • 225
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'
Jacobm001
  • 11,904
  • 7
  • 47
  • 58
2

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"