A step-by-step guide on how to back up the SD-card of your Raspberry Pi while it’s running

Making a backup of the SD card of your Raspberry Pi typically involves powering down the system, taking out the card, putting it into a card reader on your regular computer, and then entering the commands for backing up the disk.

However, if your Raspberry Pi is built into a device like a digital picture frame and it is difficult to access because you have to take it down from the wall, unplug the power and try to remove the SD card with freshly clipped fingernails, then you just don’t do it often, if at all.

This is where hot-cloning comes into play. Hot-cloning allows you to make a fully bootable copy of a remotely running system. This means you can back up e.g., the SD-card of your digital picture frame, which is operational 24/7, to your regular computer.

And you can even automate it to make it happen in the background once a week and keep only the last three backups.

In this article, I will explain to you step by step how to set up the hot-cloning using a compression algorithm, how to restore an image, and how to automate this task.

I will also explain some limitations of database-intensive applications where hot-cloning may not be the best choice.

Why not backing up your SD-card is not an option

The SD-card and the power supply of your Raspberry Pi are likely the only components that may potentially break.

I’ve seen SD-cards in a daily operation lasting five years, and others break after a few months.

If the power supply dwindles, you just get a new one.

But if the SD-card fails, you either have a saved image of the SD-card that you flash on your replacement card or you go through the more or less tedious routine of reinstalling everything from scratch.

And sometimes, you may have made a lot of modifications that you don’t remember or that are difficult to recreate.

Save yourself the trouble and make regular back-ups. It’s painless if you follow my instructions below.

Why hot-cloning can introduce discipline in the backing up process

Whenever the word “hot” is used in software it refers to an operation that is being conducted while the computer system is running. Think of hot-plugging or hot-swapping of external drives to avoid any system downtime.

Even if a digital picture frame is not a mission-critical tool, it’s good practice to have a near current backup image of your SD-card.

If it’s easy and automatic to make a backup of your card, you will do it. If it’s hard, you will not. Even if you swear otherwise.

When you clone the SD-card of your Raspberry Pi, you must save the resulting image to another computer as your SD-card will fill up to the last byte as the saved image will be almost as large as your entire SD-card.

How to save the cloned image directly on your regular computer

Open a Terminal window on your regular computer.

Enter the following command and replace “pi@192.168.178.81” with the user name and IP of your remote Raspberry Pi.

 ssh pi@192.168.178.81 "sudo dd if=/dev/mmcblk0 bs=1M status=progress | gzip -" | dd of=~/Desktop/$(date +%Y%m%d\_%H%M%S)\_pi_clone.gz

The “if” stands for ‘input file’, which means the SD-card. The “of” is the output file that will be stored on your desktop.

If you have Bonjour installed, you can simply enter the hostname followed by “.local”.

 ssh pi@pi4.local "sudo dd if=/dev/mmcblk0 bs=1M status=progress | gzip -" | dd of=~/Desktop/$(date +%Y%m%d\_%H%M%S)\_pi_clone.gz

The backup file will now appear on the Desktop of your regular computer where you started this command from.

It will take some time until it’s finished, but you will see a regular status update. For a 32GB SD-card where only 8 GB was filled, it took me about ninety minutes on a Raspberry Pi 4.

The saved file will be compressed right away during the cloning process.

The compression doesn’t make a huge difference, and I wonder if there are more effective ways. I only achieved 4GB in space savings compared to the full 32GB disk even though the disk had only 8GB of data.

How to automate hot-cloning and keep the three most recent versions

You could trigger the cloning of your Raspberry Pi manually whenever you have made changes to the system. But since humanity is lazy, you may want to automate this process and only keep the last three copies.

Create a specific directory where you upload your cloned Raspberry Pi image, like this one:

/Documents/Backups/Pi_SD_Card/

The command for finding only the last three files based on the Last Modified Date in this directory is:

find /Documents/Backups/Pi_SD_Card/ -mtime +4 -print

If this works fine, you can enter this command to delete all files in this directory except for the three most recent ones.

find /Documents/Backups/Pi_SD_Card/ -mtime +3 -exec rm -f {} \;

You can then add this to your crontab and have it run every day, week, or month depending on your backup rhythm.

How to restore a hot-cloned image

For restoring an image, you need to take out the SD-card and put it into a card reader. There is no way to restore remotely. It would expose your Raspberry Pi to a high-security risk. But the backup is rainy days only anyway, and hopefully, you will never have to use it.

Following your first backup, you should make a test restore onto a separate SD-card.

I would recommend using a tool like ApplePi Baker (macOS), or Balena Etcher  (macOS, Windows, or Linux)  for this task. Run a full restore on a spare SD card and insert it into your Raspberry Pi for testing.

It is much more comfortable with a user-friendly graphic interface, rather than having to remember the Terminal commands.

I made some tests with the SD card that is in my digital picture frame, and the hot-cloned image worked as advertised.

Limitations of hot cloning

Hot cloning may not work for every use case. However, it should work fine if there are no databases used. If you can cut the power of your Pi without any damage and it boots up again without any issue, then the method described above should be fine.

Databases can also be hot cloned while the OS is running, though it may require a subsequent clean-up. And some cache files may be inconsistent, but they should be recreated on the fly anyway.

But even with these limitations, hot-cloning may still be the more attractive option as your system can remain in service uninterrupted.

In the case of a digital picture frame, and if you use the software packages described on this blog, you should not encounter any issues.

Conclusion

Hot-cloning, especially when tied into an automated process, is an effortless way to make sure that you always have a current SD-card image of your Raspberry Pi applications.

No more excuses for not regularly doing backups!

Have you found an even better way? Let me know!

Was this article helpful?


Thank you for your support and motivation.


Scroll to Top