Raspberry Pi Backup: The Best Backup Tutorial
For all those who have worked on a raspberry pi, you might have experienced the frustration associated with having a corrupted SD card. Especially, when all the work you have done is stored in it. But, it doesn’t have to be that way. Using a couple of really simple commands in Linux , you will be easily able to backup your Raspberry Pi SD card. And, when required, to restore the raspberry pi backup on to a new SD card.
The process is really simple and straight forward. Once you have restored the image of your raspberry pi backup to your new SD card, you will be able to just plug it in and use it like your old system. This post also covers general dilemmas such as: how to backup a raspberry pi SD card having multiple partitions and also the solution to the infamous dd command error: “dd: writing to `/dev/sdc’: Input/output error”. It also addresses the problem of not being able to use your full memory capacity of your raspberry pi SD card after restoring an image file onto it. So, use this DIY Hacking tutorial to safeguard all your precious work on a raspberry pi. Get started with this Raspberry Pi Backup tutorial!
What are the stuff required to do this ?
- Raspberry Pi model B or Raspberry pi 2 with memory card preloaded with an OS.
- A new SD card having a memory capacity equal to or greater than the SD card you would like to backup.
- A linux pc with a memory card reader.
How does it work?
Here, we are using simple dd commands to create an image (.img) of the SD card we want to backup. This image can then be zipped and stored in your pc for future. While restoring the raspberry pi backup , you again use the dd command in the reverse order and prepare the new SD card. However, please note that restoration of an .img file can only be done on to a memory card having equal or greater memory capacity than the original backed up SD card. Take extra precaution while performing the dd command , as it can cause you to corrupt your hard disk if you enter improper destination or source addresses in it.
Step 1 : Backing up your SD card
First of all, insert your raspberry pi SD card into the memory card reader / slot of your PC. Next, use the dd command to create an image file of the SD card. To correctly identify your card, perform “df -h” before and after inserting the SD card and observe the new paths . These, new paths represent your SD card. You might notice that you have more than one partition for the memory card, for example: when you perform “df -h” , it might list your SD card as /dev/sdc1 and /dev/sdc2. In this case, use just “/dev/sdc” while performing the dd command, to include both partitions. Like so :
sudo dd if=/dev/sdc of=/home/ars/backup.img
The memory card should be specified under “if” and the destination of the backup under “of”.You can use “bs=1M” or “bs=4M” to specify writing speed with the command too.
However, after performing the above command some of you might experience an error like : dd: writing to `/dev/sdc’: Input/output error. For those haven’t got this error, please proceed to step two. This error is usually caused due to some errors encountered while reading the SD card. It is normally experienced by people who see a warning while booting the pi , saying that they should perform an fsck operation manually. It is caused due to improper unmounting of the SD card. To get over this error, you have to perform the fsck operation on the SD card. Before doing it, unmount the SD card as so :
Next, perform the “fsck” operation to rectify the errors in the memory card. Like so:
sudo fsck -C /dev/sdc1
sudo fsck -C /dev/sdc2
Now, remove the card reader or the SD card and re insert it. And again perform the dd command like below, it should work without any errors. If it again shows dd errors , repeat the above steps.
sudo dd if=/dev/sdc of=/home/ars/backup.img
Step 2: Restoring your Raspberry Pi backup to a new SD card
Now, we will restore the Raspberry Pi backup from the first step. If you have a used SD card having some data in it, then you would have to format it first. If you have a brand new card, it might not be necessary to format, even though it wouldn’t hurt to. Insert the new card to the card reader. Use “df -h” command to identify your new memory card. If you have multiple partitions on the card like /dev/sdc1 and /dev/sdc2 , format the card by using /dev/sdc to include both partitions.
Before formatting the card, unmount the SD card. Like so, for an SD card with no partitions:
Like so, for a used SD card having multiple partitions:
Next , format the card using this command :
sudo mkdosfs -I -F32 /dev/sdc
After this, you will have a completely blank and formatted SD card. Now, remove the card reader or the SD card and re insert it into the PC. Next , perform the dd command to restore your raspberry pi backup on this new card. Use the location of the image file created using the first step under the “if” section and the new SD card location under the “of” section. Like so:
sudo dd if=/home/ars/backup.img of=/dev/sdc
Now, you have created an identical copy of your original raspberry pi SD card. Plug it in your raspberry pi and it will be just like how it used to be before :)
In cases, when you are using a new SD card of greater memory capacity than the original card. You will notice that, when you perform “df -h” command, the size of the new card is identical to the original SD card even though it is actually a bigger SD card. In order to utilize the entire space of the new SD card, simply plug in the card into your raspberry pi and start it up. Enter the command “sudo raspi-config” and select “Expand root partition” option. It will ask to reboot when you exit by selecting “Finish”. After rebooting, perform “df -h” command, you will notice now that the maximum size of the SD card is its full capacity.
In any case , if you experience the dd error : dd: writing to `/dev/sdc’: Input/output error , in the second step. Please perform the fsck operation like in the first step and perform the dd command again after removing and re inserting the raspberry pi SD card or the memory card reader onto the PC. Having a Raspberry Pi backup will make your life a lot easier :)