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
  • 363
  • 3
  • 16
  • You may use "[SD card backup for MacOS](https://github.com/r-ionescu/SD-card-backup-for-MacOS/blob/main/sd-backup.sh)" as well :) – Raul Jan 02 '22 at 22:38

3 Answers3

9

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
  • 54,718
  • 26
  • 92
  • 182
  • 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 06: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 07: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'
Jacobm001
  • 11,797
  • 7
  • 45
  • 56
  • 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 06:23
  • By the way, is there any particular reason for using `bs=4M` or could it be anything? – Will Feb 22 '16 at 06: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
  • Nevermind. In osx you can access disks as raw by prefixing the disk with 'r'. `man hdiutil` search 'DEVICE SPECIAL FILES' – jmathew Sep 23 '16 at 14:41
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"