How to Backup and Restore an SD Card Using Linux or macOS

Linux

Introduction

Backing up an SD card is essential for preserving data or creating a duplicate for safety and testing purposes. This is particularly important if you're running a server on a Raspberry Pi, where maintaining a backup can save you from potential data loss.

Prerequisites

Before you start backing up or restoring your SD card, ensure you have the following:

  • An SD card reader
  • A computer running Linux or macOS
  • For Linux: Basic knowledge of command-line operations
  • For macOS: Familiarity with Disk Utility
  • Sufficient disk space to store the SD card image

Step-by-Step Instructions

Backup SD Card

Minimize the Image (Optional, Linux Only)

This step is optional but highly recommended as it minimizes the size of the image file.

  1. Defragment the File System:

    1. Connect the SD card to your Linux PC or Raspberry Pi using a USB card reader.
    2. Mount the SD card: sudo mount /dev/sdf2 /mnt
    3. Defragment the file system: sudo e4defrag /mnt
    4. Unmount the SD card: sudo umount /mnt
  2. Zero Out Free Space:

    1. Install zerofree if you don't have it: sudo apt-get install zerofree
    2. Zero out the free space: sudo zerofree -v /dev/sdf2

You can also shrink the image so that it can be written to a smaller SD card. For more details, refer to How to Copy an SD Card to a Smaller One.

Backup (Creating an Image File)

Backup with macOS

  1. Insert your SD card into the card reader and connect it to your macOS machine.
  2. Open Disk Utility (found in Applications > Utilities).
  3. Select the SD card from the list of available drives on the left.
  4. Click on "File" in the menu bar, then choose "New Image" > "Image from [Your SD Card Name]".
  5. In the pop-up window, choose a name and location for your backup image.
  6. Under "Format," select "compressed" to reduce the size of the backup image.
  7. Click "Save" to begin creating the backup image.

Backup with Linux

  1. Insert your SD card into the card reader and connect it to your Linux machine or Raspberry Pi.
  2. Identify the SD card device name by running sudo fdisk -l in the terminal. Look for a device like /dev/sdX (replace X with the actual letter of your SD card).
  3. Create a backup of the SD card by running:

    sudo dd if=/dev/sdX of=/path/to/backup.img bs=4M status=progress
    • Replace /dev/sdX with your SD card's device name.
    • Replace /path/to/backup.img with the desired location and filename for your backup.

Restore SD Card (Writing Image File to SD Card)

Restore with macOS

I recommend using balenaEtcher to restore the SD card. It’s available for macOS and Windows, and the process is straightforward. Disk Utility may also be used for this purpose, though I haven’t tested it.

Restore with Linux

To restore the SD card on Linux, follow these steps:

  1. Insert the SD card you want to restore into the card reader and connect it to your Linux machine.

  2. Identify the SD card device name by running:

    sudo fdisk -l

    Look for a device like /dev/sdX (replace X with the actual letter of your SD card).

  3. Restore the SD card by running:

    sudo dd if=/path/to/backup.img of=/dev/sdX bs=4M status=progress
    • Replace /path/to/backup.img with the location of your backup image.
    • Replace /dev/sdX with your SD card's device name.

Comments