I Regularly Back Up My Raspberry Pi — Here

With a huge community of tinkerers and developers backing the Raspberry Pi, the sky’s the limit when it comes to designing cool and intriguing projects with the SBC. That said, … Read more

Taylor Bell

Taylor Bell

Published on Apr 26, 2024

I Regularly Back Up My Raspberry Pi — Here

With a huge community of tinkerers and developers backing the Raspberry Pi, the sky’s the limit when it comes to designing cool and intriguing projects with the SBC. That said, if you only have a couple of microSD cards, you could run into a situation where deleting a project or two is the only way to make space for a new operating system.

Not to mention, even the best microSD cards aren’t immune to data corruption, which can render your data, and the OS, irrecoverable. Just like your PC, it’s a good idea to create frequent backups of your Raspberry Pi data. But if you’re new to the world of SBCs, here are some of the best methods to preserve all your Raspberry Pi projects.

A lifestyle image of the Raspberry Pi 5

Related

Raspberry Pi 5 review: The holy grail of DIY projects got even better (and rarer)

The Raspberry Pi 5 is one of the most powerful consumer-grade SBCs out there. Sadly, its limited stock means you’ll have a hard time finding one.

Backing up your Raspberry Pi using terminal commands

Like most Linux distros, the terminal in Raspberry Pi OS supports a host of powerful commands by default. This includes the data dupe (dd) command, which lets you back up the contents of your microSD card to any location of your choice.

1. Pick a USB drive with a larger storage capacity than the Raspberry Pi’s microSD card and format it to exFAT.

2. Plug the drive into the Raspberry Pi.

3. Open the terminal app.

The Raspberry Pi terminal app

4. Use the lsblk command to find the mountpoint of the storage drive.

lsblk
The lsblk command in the terminal

5. Clone all the contents of your microSD card to the USB drive with the help of the dd command:

sudo dd if=/dev/mmcblk0 of=USB_drive_mountpoint/backup_image_name.img
The dd command to clone the microSD card

Make sure the drive label doesn’t have any spaces, otherwise, the dd command will return an error when you try to create the backup image.

For modern drives, it’s better to speed up the process by setting the block size to 4096KB. You can do this by adding the bs=4M flag at the end of the command.

Backing up your Raspberry Pi using the SD Card Copier

If you’re someone who prefers a GUI-driven approach over running commands in the terminal, you can use the SD Card Copier app built into the Raspberry Pi OS to clone the contents of the microSD card.

1. Click on the Raspberry Pi button.

2. Hover your cursor over the Accessories tab and select the SD Card Copier tool.

The procedure to open the SD Card Copier tool

3. Set the mmcblk0 location of the microSD card as the Clone from device option and the USB drive as the Clone to device setting before hitting Start.

The SD Card Copier tool with certain settings highlighted

Backing up your Raspberry Pi using RonR RPi image utils and Cron

Although the dd command and the SD Card Clone utility are useful if you just wish to create a single backup image, you might want to look into RonR RPi image utils if you want to create incremental backups. Plus, this tool automatically compresses the image file, making it a great alternative for those who want to save space on the USB drive.

1. Inside the terminal, use the git command to clone the RonR RPi image utils repository:

git clone https://github.com/seamusdemora/RonR-RPi-image-utils.git
The command to clone the RonR RPi package

2. Paste the following command to install the package:

sudo install --mode=755 ~/RonR-RPi-image-utils/image-* /usr/local/sbin
The command to install the RonR RPi repository

3. Run the image-backup command to create a new image.

sudo image-backup
The command to create a new RonR RPi backup

4. Type the mountpoint of your USB drive followed by its name and press Enter.

The name of the RonR RPi backup image

5. Allocate the Root filesystem size and extra space for incremental updates.

The space allocated to the RonR RPi image files

6. Press Y and wait for RonRPi image utils to create the new backup file.

7. You can update this image file by running the sudo image-backup command followed by the drive mountpoint and the name of the backup file.

sudo image-backup mountpoint backup_image_name.img
The command to create an incremental backup using the RonR RPi tool

While RonR RPi is pretty useful on its own, you can even pair it with the Cron jobs scheduler present in the Raspberry Pi OS to automate the backup procedure.

1. Open the Cron job table with this command:

crontab -e

2. Press 1 to choose Nano as the editor.

3. Cron lets you add six components for each job, with the first five strings depicting the time and the last one being the actual script or service you wish to run. To update the backup file every day at midnight, you can insert the following code into the table containing the Cron job list:

0 0 * * * /usr/local/sbin/image-backup mountpoint/backup_image_name.img
The Cron Tasks table with the RonR RPi entry added

4. Tap Ctrl+X to close the buffer, press Y to save the file, and hit Enter to head back to the terminal.

Cloning your microSD card using Win32 Disk Imager

So far, all the methods we’ve used can be accessed from Raspberry Pi OS and other desktop-based operating systems. As such, you’ll have to look for another way to clone the microSD card if you’re running projects involving OpenWRT, MotionEyeOS, LibreELEC, or even Android and Windows 11. Fortunately, the Win32 Disk Imager lets you clone the microSD card with the help of your PC.

1. Install the Win32 Disk Imager application after downloading its setup file from the official link.

2. Insert the microSD card you wish to clone into your PC and launch Win32 Disk Imager as an admin.

Windows may prompt you to format the microSD card when you plug it into your system. Do not follow the formatting procedure, as you’ll lose all data on the card.

3. Pick the Boot partition of the microSD card as the Device.

The procedure to pick a drive inside Win32 Disk Imager

4. Enter the drive location and pick a name for the backup file before hitting Read.

The procedure to clone a drive using Win32 Disk Imager

Restoring your Raspberry Pi

Regardless of the method you used to create the backup image, you can follow the same procedure to restore the files to your microSD card. Although you can use the official Raspberry Pi Imager, Rufus, or Balena Etcher for this purpose, we’ll go with the Win32 Disk Imager to keep things simple.

1. Inside the Win32 Disk Imager app, click on the folder icon and select the backup image you created earlier.

The icon to pick the image file in the Win32 Disk Imager

2. Pick the microSD card where you wish to copy the image file.

The icon to pick the microSD card in the Win32 Disk Imager

3. Enter the Write button and wait for the tool to finish restoring all the files.

The Write button highlighted in the Win32 Disk Imager

Backing up all your precious Raspberry Pi project files

Once you’ve flashed the image files onto the microSD card, you can boot into the restored OS on your Raspberry Pi and continue where you left off.

With that, you’re free to modify the project files without worrying about starting from scratch in case things go wrong. If you’re running out of ideas, then you can check out our favorite Raspberry Pi projects to see all the cool and interesting stuff you could pull off with the SBC.

A person holding a Raspberry Pi 5 and a Raspberry Pi Zero W

Related

10 simple Raspberry Pi projects for beginners

You don’t need to be a DIY god to create these projects with your Raspberry Pi

Partager cet article

Inscrivez-vous à notre newsletter