The Raspberry Pi has become one of the most popular microcomputers. Anyone can get one, since its price makes it accessible to any pocket. And the possibilities it offers us are unlimited, from setting up our own home server to turning it into a computer or a complete retro console. To make it work, we only need a micro-SD card with the operating system, since it has no internal storage. And all the data and all the settings will be saved on this card. But what if we do something wrong and it stops working? What if the card is erased by mistake or a problem?
Why the Raspberry Pi micro-sd might fail
There are many reasons why a storage device, such as a micro-sd card, can fail. If we do not have much experience within Linux operating systems, it is easy that, when making changes in the configuration, or when updating, the system is damaged and, by not knowing how to recover it, we lose everything we had. Generally, everything that refers to configuration errors of a Linux system has a solution, the thing is, how?
But software bugs are not the only ones that can give us headaches. There are also other reasons why we can lose the operating system of this microcomputer. For example, there may be a critical system error that causes the partition table to be erased and the memory card to appear “blank.” This can easily happen if we disconnect the Raspberry as we should not. It can also happen that, over time, the memory card stops working because it has exhausted the read and write cycles of its chips. This happens, especially, when we use low-quality SD cards. And there is no choice but to give up all the content and buy a new one.
The advantage of this storage system is that, if it breaks, or if we want to change to a higher capacity one, we can do it very easily, since it is enough to buy a new memory card, configure it and put it in the Raspberry Pi to start to use it. The only thing we will lose is the time necessary to leave everything as we had it at the beginning.
And, to avoid this, there are backup copies.
There are several ways to back up a Raspberry Pi card. Depending on whether we do it from Windows, or from Linux (even from the micro-computer itself) the process that we must follow will be one way or another. Let’s see it in detail.
To back up the memory card from Windows, the only thing we will need is that our computer has a memory card reader. Otherwise, we must buy one in any store (they can be around 10 euros, and even less) and connect the card to our computer through it. It is likely that we will see a message that tells us that the memory card is not formatted and that we have to format it. It is important not to do it and ignore the message. This appears because the micro-sd will be on a Linux filesystem (such as ext4) and therefore Windows will not be able to read its files.
Next, we will download a free and open source program called Win32 Disk Imager and install it on the computer. Once installed, we execute it and we will see a window like the following one.
We click on the folder button that appears in the ” Image File ” section and we will select the directory where we want the backup we are going to make to be saved. We will also select in the ” Device ” section the unit from which we are going to make the copy.
How is it possible to make a copy of the data if Windows does not recognize it? Because this program makes a ” RAW ” copy, that is, raw, of the 1 and 0 of the data. No matter the content, a literal copy of the drive is created (even of the free space on it) so that there is no problem.
Once everything is ready, we click on the ” Read ” button and the image creation process will begin. This process can take several minutes, and will occupy the same capacity as the original memory card.
When the reading process is finished, we will have the image (a file, with .img format) in the directory that we have specified. We can now remove the card from the computer, and reconnect it to the Raspberry Pi.
From Linux or the same Raspberry Pi
If we are going to do it from a Linux system (and even from the Raspberry Pi itself to an external drive that we have, for example, by USB or network), what we must do is connect the card to the computer (if we do not already have it ) and locate your mount point using the following command:
sudo fdisk -l
Assuming it is / dev / sdb (which will vary depending on the drives connected to the PC), then we will use the “dd” command to clone the RAW data from the main drive (the card) to the directory we want. This command has the following structure:
sudo dd if=/dev/sdb of=[directorio]/raspbackup.img
We must change [directory] to the path where we want to save the image. Either a network drive, or a different mount point. What is logical is that we should not create the backup on the same micro-sd, since, if the data is lost, and the backup, it is of little use to us.
Restore a copy of the SD
To restore the backup in case the original card fails, or we change it for a bigger one, literally what we have to do is follow the steps in reverse.
In the case of Windows, for example, we will use the Win32 Disk Imager program again, selecting in “Image File” the backup image, in “Device” the drive letter of the micro-sd where we are going to write the image, and we will click on the “write” button. The process will erase all the data, and when it is finished we will have the memory card ready to work again on the Raspberry Pi.
In the case of Linux, what we must do is use the dd command again, but in reverse. In other words, in the “if” parameter we can find the directory of the backup image (img), and in the “of” parameter, the mounting point of the memory card where we are going to dump said image.
sudo dd if=[directorio]/raspbackup.img of=/dev/sdb
In case we were wondering, copying the file “raspbackup.img” (or whatever we call it) to the memory card is of no use. A bit-by-bit RAW dump of the content has to be made, just as the copy was made.