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.
-
Defragment the File System:
- Connect the SD card to your Linux PC or Raspberry Pi using a USB card reader.
- Mount the SD card:
sudo mount /dev/sdf2 /mnt
- Defragment the file system:
sudo e4defrag /mnt
- Unmount the SD card:
sudo umount /mnt
-
Zero Out Free Space:
- Install
zerofree
if you don't have it:sudo apt-get install zerofree
- Zero out the free space:
sudo zerofree -v /dev/sdf2
- Install
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
- Insert your SD card into the card reader and connect it to your macOS machine.
- Open Disk Utility (found in
Applications > Utilities
). - Select the SD card from the list of available drives on the left.
- Click on "File" in the menu bar, then choose "New Image" > "Image from [Your SD Card Name]".
- In the pop-up window, choose a name and location for your backup image.
- Under "Format," select "compressed" to reduce the size of the backup image.
- Click "Save" to begin creating the backup image.
Backup with Linux
- Insert your SD card into the card reader and connect it to your Linux machine or Raspberry Pi.
- Identify the SD card device name by running
sudo fdisk -l
in the terminal. Look for a device like/dev/sdX
(replaceX
with the actual letter of your SD card). -
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.
- Replace
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:
-
Insert the SD card you want to restore into the card reader and connect it to your Linux machine.
-
Identify the SD card device name by running:
sudo fdisk -l
Look for a device like
/dev/sdX
(replaceX
with the actual letter of your SD card). -
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.
- Replace
Comments