Perform the backup (and possible recovery) of the proprio Raspberry Pi

6 minutes of reading
Guide purposes:
  • Get an updated backup of the profirst Raspberry and eventually restore it
  • Difficulty level: bass
Concepts dealt with:
  • software usage
Software components used:
  • Miscellaneous (depending on the computer in use)
Prerequisites:
Physical devices used:
  • A computer (Window /Mac/ Linux)
GUIDE MOST INDICATED FOR:
Notes and disclaimer
  • qualsiasi eventuale modifica agli impianti domestici dev'essere progettata ed realizzata SOLO da personale qualificato;
  • any changes implemented in probefore is a propersonal responsibility as well as a profirst risk and danger (the contents of the present page are purely educational);
  • any changes implemented in proprior to a device it voids the guarantee, quality approvals and certifications.
Driving review: 1.1

Abstract

Il Raspberry Pi, sappiamo, is a tool irreplaceable: it is cheap, consumes very little electricity, is extremely versatile. Everything you need for who it is appkeen on home automation and more.

Raspberry, however, presents a small defect, namely the trend, over time and in the face of specific conditions (sudden changes in voltage, for example) in the corrupt improobviously the microSD on which it lies the operating system, our software and our configurations.
For corrupt we mean damage - permanently or not - the flash memory of the small card, making it unusable.

Corrupt the propria microSD means to find yourself, suddenly, with Rasberry (and therefore domotics, if it is the use that it makes of it) completely out of the game. This situation is usually indicated by the following message, at boot:

Kernel Panic: VFS: Unable to mount root fs on unknown-block

Basically, an unpleasant death sentence.

microSD


backup

"The importance of a backup until the first data loss is not understood"

It is so much an adage as banal as it is concrete. Once lost i proFirst of all, most of them are actually lost, so it will be late to cry over the corrupted microSD. It will therefore be the case to learn, before the irreparable happens, a good backup technique.

Usually, in the case of SD cards (SD, MiniSD, microSD, it's the same thing) the approis to connect them to a computer and create oneself image card status, or a single file that contains, bit by bit, the contents of the card. In fact it is not a matter of copying the files, but of literally copying the data blocks in a raw, raw way, for those that are.

It is therefore a good rule, from time to time, to turn off the profirst Raspberry and make the backup and then historicize it in a safe place. It will be useful in case of microSD abruptly abandon us.

Now let's see how to make a backup image.

Connect the microSD to the computer

First of all, turn off (via command "sudo shutdown nowvia SSH, or "shut down" from a graphical interface - may disconnecting the power supply) the Raspberry unit and remove the microSD.

To connect it to the computer an adapter is typically used. There are various types: some allow you to connect directly to the USB port; others instead "transfomano" from microSD format to SD (in this case the computer must have a port for the insertion of this format.

Backup with Windows

The software (free) most used to perform this type of backup via windows è Win32 Disk Imager, which works with any type of SD connected to the computer.

Let's start by downloading this software da SourceForge. The download starts a few moments after the page is opened.

Once installed, running the software apparirà a finestra like this:

Win32 Disk Imager

In the path "Image File"We will indicate the name of the image we want to create, typically using the extension". Img"; in the field "Device"We're going to choose the disk to back up, in this case the letter assigned to the SD we connected to the computer.

Click now on "Read"

When finished, disconnect the microSD with the usual one proprocedure Windows, like a flash drive or USB disk.

The backup has been made.

Backup with macOS

Users Appthey already have a powerful backup tool, that is the command "dd"

Launch, first of all, theapplation "terminal"
After connecting the SD to the computer, run the command:

diskutil list

to list and display the disks connected and recognized by the operating system:

diskutil list - Terminal MAC

The disk with a boot partition "Windows_FAT_32 "and with a" Linux "partition is typically the microSD disk we want to back up.

Before doing so, it is necessary to "disassemble" the disk using the command:

diskutil unmountDisk /dev/diskX

where the X is the number of disk to unmount (in the case of the example is the number 1).

We now perform the backup using the command:

sudo dd if=/dev/diskX of=~/SDCardBackup.dmg

where the X it is also in this case the number of disk to be removed (in the case of the example it is the number 1).
This command will create an image file called "SDCardBackup.dmg"In your user's folder macOS.

La proprocedure it can be long and does not give evidence of proyield. Simply let the computer work until the backup is complete.

Finally, unmount the microSD again, before disconnecting it:

diskutil unmountDisk /dev/diskX

Backup with Linux

Linux users have one prosimilar to that macOS.

First of all, before connecting the SD card execute the following command:

df -h

which will list connected disks and partitions. The output will be similar to the following:

Filesystem 1K-blocks Used Available Use% Mounted on
rootfs 29834204 15679020 12892692 55% /
/dev/root 29834204 15679020 12892692 55% /
devtmpfs 437856 0 437856 0% /dev
tmpfs 88432 284 88148 1% /run
tmpfs 5120 0 5120 0% /run/lock
tmpfs 176860 0 176860 0% /run/shm
/dev/mmcblk0p1 57288 14752 42536 26% /boot

Now connect the microSD and run the command again. The output will be similar to the following:

Filesystem 1K-blocks Used Available Use% Mounted on
rootfs 29834204 15679020 12892692 55% /
/dev/root 29834204 15679020 12892692 55% /
devtmpfs 437856 0 437856 0% /dev
tmpfs 88432 284 88148 1% /run
tmpfs 5120 0 5120 0% /run/lock
tmpfs 176860 0 176860 0% /run/shm
/dev/mmcblk0p1 57288 14752 42536 26% /boot
/dev/sda5 57288 9920 47368 18% /media/boot
/dev/sda6 6420000 2549088 3526652 42% /media/41cd5baa-7a62-4706-b8e8-02c43ccee8d9

for difference it will be possible to understand which is the dev assigned to the microSD.

The part indicated in red determines the disc name (our microSD) while the number on its right determines the partition (s). Since we want to create an image complete unit, we will only use the disc name.

Then execute the command:

sudo dd if=/dev/sda of=~/SDCardBackup.img

This command will create an image file called "SDCardBackup.dmg"In your user's folder macOS.

Restore

Il restore (or restore) and the proreverse backup procedure, or restore an image previously created on a new microSD (if the corrupted one no longer functions as a fault).

Restore with Windows

Always using the indicated utility in probackup procedure (or "Win32 Disk Imager") we can restore a previously created image.

Connect the microSD to the computer and then run theapplished here:

Win32 Disk Imager

In the head "Image File”At this point select the image to restore previously created.
In the field "Device”Select the letter corresponding to the microSD connected to the computer.

Click therefore "Write”To start recovery.

When finished, disconnect the microSD with the usual one proprocedure Windows - like a flash drive or USB disk.

The microSD now contains previously saved data on backup disk image and is available for use.

Restore with macOS

Launch, first of all, theapplation "terminal"
After connecting the SD to the computer, run the command:

diskutil list

to list and display the disks connected and recognized by the operating system:

diskutil list - Terminal MAC

The disk with a boot partition "Windows_FAT_32 "and with a" Linux "partition is typically the microSD disk on which to restore the backup.

Before making such procaesura, it is necessary to “disassemble” the disk using the command:

diskutil unmountDisk /dev/diskX

where the X is the number of disk to unmount (in the case of the example is the number 1).

We now perform the restore with the command:

sudo dd if=~/SDCardBackup.dmg of=/dev/diskx

where the X is the number of disk to be removed (in the case of the example is the number 1) and “~ / SDCardBackup.dmg"Is the path and name of the image file to be restored.

La proprocedure it can be long and does not give evidence of proyield. Simply let the computer work until the restore is complete.

Finally, unmount the microSD again, before disconnecting it:

diskutil unmountDisk /dev/diskX

Your microSD is prointended for use.

Recovery with Linux

First of all, before connecting the SD execute the following command:

df -h

which will list connected disks and partitions. The output will be similar to the following:

Filesystem 1K-blocks Used Available Use% Mounted on
rootfs 29834204 15679020 12892692 55% /
/dev/root 29834204 15679020 12892692 55% /
devtmpfs 437856 0 437856 0% /dev
tmpfs 88432 284 88148 1% /run
tmpfs 5120 0 5120 0% /run/lock
tmpfs 176860 0 176860 0% /run/shm
/dev/mmcblk0p1 57288 14752 42536 26% /boot

Now connect the microSD and run the command again. The output will be similar to the following:

Filesystem 1K-blocks Used Available Use% Mounted on
rootfs 29834204 15679020 12892692 55% /
/dev/root 29834204 15679020 12892692 55% /
devtmpfs 437856 0 437856 0% /dev
tmpfs 88432 284 88148 1% /run
tmpfs 5120 0 5120 0% /run/lock
tmpfs 176860 0 176860 0% /run/shm
/dev/mmcblk0p1 57288 14752 42536 26% /boot
/dev/sda5 57288 9920 47368 18% /media/boot
/dev/sda6 6420000 2549088 3526652 42% /media/41cd5baa-7a62-4706-b8e8-02c43ccee8d9

for difference it will be possible to understand which is the dev and the partitions assigned to the microSD.

The part indicated in red determine the names of the partitions attributed to our microSD.

In the example, sda5 e sda6 are the two disk partitions sDA.

Now, before restoring it is necessary to unmount the two partitions using the "unmount" command, which yes applica, in the case of the example, like this:

sudo umount /dev/sda5
sudo umount /dev/sda6

Remove the partitions, execute the command:

sudo dd bs=4M if=~/SDCardBackup.img of=/dev/sda

where the sDA is the disk name on which to restore (practically, the name of a partition, but without a number) and "~ / SDCardBackup.dmg"Is the path and name of the image file to be restored.

At the end of the procedura, execute the command:

sudo sync

and only after disconnecting the microSD, Now prointended for use.

inDomus telegram channel