As is often the case with backups, you usually only think about making one when it's too late. That's what happened to me recently with one of mine RaspberryPis. The curse and blessing with 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 may 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 or a lot, it can make sense to create a complete backup of the SD card you are using.
How you can create a complete backup of an inserted SD card using a USB stick or an external hard drive is described in the following article.
Safety instructions
I know the following notes are always kind of annoying and seem unnecessary. Unfortunately, many people who knew "better" have lost eyes, fingers or other things due to carelessness or injured themselves. Data loss is almost negligible in comparison, but even these can be really annoying. Therefore, please take five minutes to read the safety instructions. Because even the coolest project is not worth injury or other trouble.
https://www.nerdiy.de/sicherheitshinweise/
Affiliate links/advertising links
The links to online shops listed here are so-called affiliate links. If you click on such an affiliate link and make a purchase via this link, Nerdiy.de will receive a commission from the relevant online shop or provider. The price does not change for you. If you make your purchases via these links, you support Nerdiy.de in being able to offer other useful projects in the future. 🙂
Requirements
Helpful articles:
Before you start with this article, you should have prepared a RaspberryPi 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 RaspberryPi.
- RaspberryPi – setup for nerdiys!
- RaspberryPi – The first configuration!
- RaspberryPi – Control the RaspberryPi via SSH
In the following lists you will find all the parts you need to implement this article.
Required tool:
Number | Link |
---|---|
1x | Screwdriver set Buy at Amazon |
1x | SD card reader Buy at Amazon |
Required material:
Number | Link |
---|---|
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 to the RaspberryPi via SSH
To start, you must first log in to the Rasp Pi with Putty via SSH. How to do this is in the article RaspberryPi - Control the RaspberryPi via SSH described.
Mount external drive
Before you can create the image - i.e. an exact image of the inserted SD card - you need a storage area into 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 doesn't work because the image created will be the same size 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 that has at least as much storage space as the size of the Raspberry PI's SD card.
So connect a corresponding USB stick or external hard drive to your running Raspberry PI and integrate it so that data can be written to it. How to mount a drive under Linux is also in the article Raspberry Pi – mount drive under Linux described.
You can check whether the connected drive is mounted correctly and available by entering the command
df -h
check.
Prepare recovery script
In addition to the external storage, you have to prepare another little thing before the image can be created.
In order for the image that has been created to be adapted to the (perhaps larger SD card) after it has been written to a new SD card, you need a corresponding script. You must install this on your Raspberry PI before creating the image. It is automatically included in the image created afterwards.
You can find more information about this script in the associated GIT repository (by Drewsif) here:
- https://github.com/Drewsif/PiShrink
- or here in the Nerdiy GIT repository: https://github.com/Nerdiyde/PiShrink
To download the script, set the appropriate rights and install it, you just have to execute the following commands (on the Raspberry Pi you want to create a backup of).
wget https://raw.githubusercontent.com/Drewsif/PiShrink/master/pishrink.sh sudo chmod +x pishrink.sh sudo mv pishrink.sh /usr/local/bin
Create image
After you have made all the preparations, you can start creating the image.
To do this, all you have to do is enter the following command into the console of your Raspberry PI.
Before that, however, you would have to adapt the part “/media/pi/Volume/sd_card_backup.img” to the mount point of your external drive. For example, if your external drive is mounted 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 starting, it can 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 image
The actual image has now been created. However, in order to compress it a little (i.e. reduce the required storage space) and make it easier to transfer it to a new SD card, you should now process the image using the prepared script.
To do this, go 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 could take a little longer.
As soon as the processing of the image is completed, you will see a corresponding message.
Write image to a new SD card
You can now use the created image with the Raspberry Pi Imager write to a (different) SD card
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 on the “Write” button…
... and confirmed the warning message...
... the writing process starts.
As soon as this is completed, the message shown will be displayed.
You can now insert the SD card described into a Raspberry PI. This should then boot without any problems.
More articles on the topic
In the following category you will find more links about Rasperry PI.
Have fun with the project
I hope everything worked as described for you. If not or you have questions or suggestions please let me know in the comments. I will then add this to the article if necessary.
Ideas for new projects are always welcome. 🙂
PS 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 think it's cool that I share the information with you, I would be happy about a small donation to the coffee fund. 🙂