Skip to content

Instantly share code, notes, and snippets.

@caseyanderson
Last active August 27, 2019 22:32
Show Gist options
  • Save caseyanderson/31b615045332a6ab3f4028c696920f57 to your computer and use it in GitHub Desktop.
Save caseyanderson/31b615045332a6ab3f4028c696920f57 to your computer and use it in GitHub Desktop.
write or backup raspbian to or from a microSD

Writing, Backing Up, and Cloning Raspbian (RPi) Images

Materials

  • laptop (with internet access)
  • microSD card (ex: 8GB)
  • Raspbian Buster Lite Image File

Pre-flight

microSD prep

  1. Download and Install the SDFormatter
  2. Insert the microSD card into an SD card adapter, plug the adapter into your laptop
  3. Open SDFormatter, which should automatically identify your microSD card, select Overwrite Format, then click the Format button. After a while the application will tell you that it is done erasing all data from the microSD card

write .img from RPi Foundation

  1. Download the Raspbian Buster Lite Image (.img), from the Raspberry Pi Foundation's website, to the Downloads folder on your laptop (note: the same procedure works for Raspbian Buster with Desktop .img)

  2. In the Terminal change directories (cd) to Downloads: cd Downloads

  3. Get the formatted microSD card's address (identified by size [8GB]): diskutil list

  4. unmount the microSD card so we can write our .img to it: diskutil unmountDisk /dev/disk2 (replace 2 in /dev/disk2 with whatever number you got in step 3.)

  5. In the Finder unzip the Raspbian Buster Lite .img

  6. In the Terminal use dd to write the Raspbian Buster Lite .img to the microSD card: sudo dd bs=10m if=2019-07-10-buster-stretch-lite.img of=/dev/rdisk2 (replace 2 in /dev/rdisk2 with whatever number you got in step 3.). This takes a VERY LONG TIME, so go get a cup of coffee or take a walk or something

  7. Eventually you should see something like this in the Terminal confirming that dd is done writing the .img to the microSD card:

    3833+0 records in
    3833+0 records out
    4019191808 bytes transferred in 1642.354445 secs (2447213 bytes/sec)
  8. Eject the microSD card from your laptop: diskutil unmountDisk /dev/disk2

  9. Put the microSD card into your Pi, plug power cable in, and confirm that it boots up (if the ACT LED is flashing green then it has successfully booted)

Backup .img

  1. Shut down your RPi: sudo shutdown now
  2. Remove the microSD card from the RPi, insert it into an SD card adapter, plug the microSD into your laptop
  3. Get the formatted microSD card's address : diskutil list
  4. unmount the disk: diskutil unmountDisk /dev/rdisk2 (replace 2 in /dev/rdisk2 with whatever number you got in step 3.)
  5. Pipe the output of dd to gzip, archiving and compressing the image in one line, with the following: sudo dd bs=10m if=/dev/rdisk[DISK# FROM DISKUTIL] | gzip > rasbian.img.gz (this takes a VERY LONG TIME, so go get a cup of coffee or take a walk or something)

Restoring/Cloning from Backup .img

  1. Repeat steps for microSD prep
  2. Repeat steps 3. and 4. from write .img from RPi Foundation
  3. The following will extract and restore a backup .img to another microSD card: gunzip --stdout rasbian.img.gz | sudo dd bs=4m of=/dev/rdisk2 (replace 2 in /dev/rdisk2 with whatever number you got in step 3. of microSD prep) (this takes a VERY LONG TIME, so go get a cup of coffee or take a walk or something)
  4. Remove microSD from laptop, insert into RPi, confirm successful startup
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment