HowTo: Raspberry Pi – Create Image from system SD card

As is so often the case with backups, you usually only think about making one when it's too late for it. That's what happened to me recently with one of my RaspberryPis. The curse and blessing of these is that they use an SD card as the main drive.

This makes installation very easy. Unfortunately, an SD card only has a limited number of write cycles. This means that if you often write a lot of data to this SD card, then it can happen that this SD card fails at some point. But even if you don't write a lot of data to this SD card often, it can make sense to create a complete backup of the SD card used.

The following article describes how you can create a complete backup of an inserted SD card using a USB stick or an external hard drive.


Safety instructions

I know the following hints are always a bit annoying and seem unnecessary. But unfortunately, many people who knew it "better" from carelessness lost their eyes, fingers or other things or hurt themselves. In comparison, a loss of data is almost not worth mentioning, but even these can be really annoying. Therefore, please take five minutes to read the safety instructions. Even the coolest project is worth no injury or other annoyance. https://www.nerdiy.de/en/sicherheitshinweise/

Affiliate links / advertising links

The links to online stores listed here are so-called affiliate links. If you click on such an affiliate link and store via this link, Nerdiy.de receives a commission from the online store or provider concerned. The price doesn't change for you. If you do your purchases via these links, you will support Nerdiy.de in being able to offer further useful projects in the future. 🙂


Requirements

Helpful articles:
Before you start this article, you should have prepared a Raspberry Pi so that it can be reached via the network and controlled via SSH.

The following articles describe what needs to be done to prepare the Raspberry Pi.

In the following lists you will find all the parts you need to implement this article.

Required tool:

NumberLink
1xScrewdriver set  Buy at Amazon
1xSD card reader  Buy at Amazon

Required material:

NumberLink
1x Raspberry Pi  Buy at Amazon
1x Raspberry Pi power supply  Buy at Amazon
1x Raspberry Pi case  Buy at Amazon
1x Micro SD card 64GB  Buy at Amazon


Log in via SSH on the RaspberryPi

To get started, you first need to log in to RasPi with SSH on Putty. How to do it is described in the article

After entering your username and password you can start to enter the first commands.

Mount external drive

Before you can create the image - an exact copy of the inserted SD card - you need a memory area in which you can write this data. Unfortunately, it is not possible to create a backup or image of the SD card and write this image to this SD card at the same time. This does not work because the image created will be as large as the total capacity of the SD card used.

If you have installed the operating system of your Raspberry PI on a 32GB SD card, the image of this SD card will also be 32GB in size.

So you need (for example) a USB stick or external hard drive with at least as much storage space as the SD card of the Raspberry PI is large.

So connect a corresponding USB stick or external hard drive to your running Raspberry PI and integrate it in such a way that data can be written to it. How to mount a drive under Linux is also described in the article Raspberry Pi – Mount drive under Linux.

You can check whether the connected drive is correctly mounted and available by entering the command

df -h 


Prepare recovery script

In addition to the external memory, you have to prepare another little thing before the image can be created.

You need a corresponding script so that the image once created is also adapted to the (perhaps larger SD card) after it has been written to a new SD card. You have to install this on your Raspberry PI before creating the image. So it is automatically included in the image created afterwards.

More information about this script can be found in the associated GIT repository (by Drewsif) here:

To download the script, set the appropriate rights and install it, all you have to do is run the following commands (on the Raspberry Pi you want to back up).

wget https://raw.githubusercontent.com/Drewsif/PiShrink/master/pishrink.sh sudo chmod +x pishrink.sh sudo mv pishrink.sh /usr/local/bin


Createimage

After you have made all the preparations, you can start creating the image.

All you have to do is enter the following command in the console of your Raspberry PI.

Before doing this, however, you would have to adapt the "/media/pi/Volume/sd_card_backup.img" part to the mount point of your external drive. For example, if your external drive is integrated under "/tmp/mountPoint/", the path in the command is "/tmp/mountPoint/sd_card_backup.img".

sudo dd if=/dev/mmcblk0 of=/media/pi/Volume/sd_card_backup.img bs=4M status=progress

After startup, it may take quite a while for the image to be written.

As soon as the image has been created, a summary of the amount of data written is output.


Post process and compress the image

The actual image is now complete. However, in order to compress it a bit (ie to reduce the required storage space) and to facilitate the transfer to a new SD card, you should now post-process the image with the prepared script.

To do this, change to the directory in which the image file is located and execute the following command.

sudo pishrink.sh -z sd_card_backup.img

The image will now be checked and compressed, which again could take a little longer.

As soon as the processing of the image is complete, you will be shown a corresponding message.


Write image to a new SD card

You can now write the created image to a (different) SD card with the Raspberry Pi Imager

To do this, click on "Select OS"...

... "Own Image" ...

… and selects the previously created image.

Then click on “Select SD card” and…

… selects the inserted SD card.

After you click the "Write" button...

…and acknowledged the warning message…

… the writing process should start.

As soon as this is completed, you will see the message shown.

You can now insert the described SD card into a Raspberry PI. It should then boot without any problems.


More articles on the topic

In the following category you will find further links on the subject of Rasperry PI.


Have fun with the project

I hope everything worked as described. If not or you have any other questions or suggestions, please let me know in the comments. Also, ideas for new projects are always welcome. 🙂

P.S. Many of these projects - especially the hardware projects - cost a lot of time and money. Of course I do this because I enjoy it, but if you appreciate that I share this information with you, I would be happy about a small donation to the coffee box. 🙂

Buy Me a Coffee at ko-fi.com   

Kommentar hinterlassen

Your email address will not be published. Erforderliche Felder sind mit * markiert