-
-
Notifications
You must be signed in to change notification settings - Fork 46
Raspberry Pi Backup & Restore
Throughout the process of setting things up, you may want to have a backup of your Raspberry Pi so you don’t have to start from scratch if something goes wrong (or you want to redo something altogether). It's also helpful to create a backup after each successful step so you can return to it whenever you need to. There are multiple ways to backup your Raspberry Pi's MicroSD card, including software and manually using a Mac or Raspberry Pi console (the latter of which this guide will not cover).
Note: Creating an image of your Raspberry Pi's MicroSD card, regardless of the method, will require that amount of disk space on your Mac. That means if you're using a 64GB MicroSD card for your Pi, you'll need at least 64GB of free space on your Mac's hard drive to create a backup. You may want to stick with a smaller MicroSD card for this reason. The only workaround, which I haven't tested, is using the macOS Disk Utility to create a backup image from just the boot partition, which is reasonably small. That said, I have yet to try and restore from this type of image.
The easiest way to backup and restore your Raspberry Pi when using a Mac is with the ApplePi-Baker app. That said, the current version (2.2.3) would not run on macOS 12.6 (Monterey), so I had to install the beta (2.3.0) instead. Use this software at your own risk!
To backup your Raspberry Pi installation from your Mac, we'll need to pull the MicroSD card out and insert it into the Mac, then use Apple-Pi Baker to handle the rest.
First, shut down your Raspberry Pi with the following command:
sudo halt
or the more verbose:
sudo shutdown -h now
and then unplug your Raspberry Pi. If you have a Pi Switch, you can turn off the Raspberry Pi without unplugging it. Remove the SD card from the Raspberry Pi and insert it into a supported SD card reader (like the Anker 2-in-1 card reader mentioned earlier), and connect to your Mac.
When you open the ApplePi-Baker app, you should be able to select the SD card from the Select a Disk options (make sure you select your MicroSD card), then choose the Backup option.
Note: If ApplePi-Baker's console shows Disk Access Failed
, you'll need to change your Security & Privacy settings in macOS System Preferences. Check the box, quit and restart ApplePi-Baker, and everything should work as expected.
The IMG format allows ApplePi-Baker to shrink the Linux partition on backup and expand it on restore, saving you from having to backup the entire size of the SD card rather than just the actual contents. You can also use the Enable Linux Partition Resize option (it's the box icon with arrows at all 4 corners, second from the left) before clicking the Backup option above.
Note: The IMG file format is a raw copy of every single byte of the source disk. This means that if your MicroSD card is 32GB, even though you may be using just 1 byte of data, your backup file will still be 32GB. With the Enable Linux Partition Resize option selected, you'll need 2 to 3 times the disk space of your MicroSD card to make room for the expanding and shrinking of the partition. This may limit your ability to use ApplePi-Baker for backups...
In the file choose window, select IMG from the Format selections at the bottom of the window, and then give the backup file a name.
Creating the backup image will take some time (25 minutes or so is normal), be prepared to wait.
Another way to backup your MicroSD card on a Mac is using the command line. Be careful using any commands that modify your disk images, as you could irreparably damage your computer's file system.
From the macOS Terminal app, type:
diskutil list
to see a list of drives attached to your Mac. It should be relatively clear which one is the MicroSD card.
To create a Disk Image of the MicroSD card:
sudo dd if=/dev/disk2 of=/Users/patrick/Desktop/RaspberryPiBackup.dmg
where /dev/disk2
is the path to your MicroSD card’s disk and /Users/patrick/Desktop/RaspberryPiBackup.dmg
is the path and filename on your Mac to save the Disk Image. Enter your Mac user’s password when prompted. This may take a while depending on the size of your SD card and what you have installed on it.
While you wait, you can use Control + T
to see the current progress:
load: 3.28 cmd: dd 66405 uninterruptible 0.22u 3.51s
997633+0 records in
997633+0 records out
510788096 bytes transferred in 22.436327 secs (22766119 bytes/sec)
Note: If you see any input/output errors, try using Disk Utility instead.
Open the macOS Disk Utility application, and choose View > Show All Devices. Right click on the device name of the SD card (not the individual partition(s)!) and choose the Image from ‘DEVICE NAME’ option. Give the file a name and choose a location to save it. Choose the DVD/CD master option for Format, and no Encryption. Disk Utility will create a .CDR file with your SD card’s files.
If you’ve made a backup of your Raspberry Pi’s SD card previously, you can restore it any time and overwrite your current configuration. It's always a good idea to make a new backup of your current setup before restoring from a previous backup.
Using the ApplePi-Baker app on macOS, restoring from a backup is a fairly simple task.
Again, shut down your Raspberry Pi first:
sudo halt
and remove the MicroSD card from your Raspberry Pi. Using a supported MicroSD card reader, insert the card from your Raspberry Pi into your Mac. Launch the ApplePi-Baker app and select the correct MicroSD card disk, then choose the Restore option. ApplePi-Baker will expand the backup file and overwrite the complete contents of your card with the backup file's contents, so make sure you really want to do this.
You can also use the macOS Terminal application to this by typing:
diskutil list
to see a list of drives attached to your Mac. You'll need to unmount the MicroSD card to proceed (your Mac can still see the files, but you won't see the mounted disk(s) anymore):
diskutil unmountDisk /dev/disk2
where /dev/disk2
is the path to your MicroSD card’s disk. Restore the card from backup using:
sudo dd if=~/RaspberryPiBackup.dmg of=/dev/disk2
where /dev/disk2
is the path to your MicroSD card’s disk and ~/RaspberryPiBackup.dmg
is the path and filename on your Mac to save the Disk Image. This may take a while depending on the size of your MicroSD card backup
Note: If you backed up the MicroSD card as a .CDR, you can rename it to .ISO and restore it to any card with the free Etcher application for macOS.