Easily back up your Raspberry Pi SD card using simple Linux commands.

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 back up 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 straightforward. 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 back up 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 the full memory capacity of your Raspberry Pi SD card after restoring an image file onto it. Use this DIY Hacking tutorial to safeguard all your precious work on a Raspberry Pi.

How Does Backing Up the Raspberry Pi Work?

Here, we are using simple dd commands to create an image (.img) of the SD card we want to back up. This image can then be zipped and stored in your PC for the 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 you can only restore an .img file onto 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 who haven't gotten 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:

  • umount /dev/sdc1
  • umount /dev/sdc2

Next, perform the "fsck" operation to rectify the errors in the memory card:

  • 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 with some data in it, then you'll 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 in the card reader. Use the "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. So, for an SD card with no partitions:

  • umount /dev/sdc

And for a used SD card with multiple partitions:

  • umount /dev/sdc1
  • umount /dev/sdc2

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 this:

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 into your Raspberry Pi and it will be just like it was before.

In cases when you are using a new SD card of greater memory capacity than the original card, you will notice when you perform the "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 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 you to reboot when you exit by selecting "Finish". After rebooting, perform the "df -h" command, and you will notice now that the maximum size of the SD card is at 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 into the PC. Having a Raspberry Pi backup will make your life a lot easier.

Arvind Sanjeev
An interaction designer and engineer. Yahoo-Accenture had also awarded him as the "Most Promising Innovator".

Maker Pro Logo
Continue to site
Quote of the day

-