How to backup the Raspberry Pi SD Card using Linux
Let’s face it, your hard drive will eventually fail and so will any storage device you own, especially something small like your Raspberry Pi SD card which not only can become corrupted but also lost if you use it outside your home transferring files at different locations. I am a big advocate of creating backups of your Raspberry Pi’s SD Card and create an image of the SD card that you can store on Google Drive or Dropbox.
I recently was using my Raspberry Pi as a server and had a ton of data and hours of software configurations that I have been using my Raspberry Pi for and during a thunderstorm the power got cut out, when power was restored and I booted up my server everything was corrupted and all my data was lost. If I had only created an image I would be able to insert a new SD card into my computer and restore my previously saved image and be back in business in 5 minutes.
Since all my computers in the house are running Linux I will be showing you how to create a backup image of your Raspberry Pi’s SD card and restoring your image back onto the SD card. The distro of Linux I am running is Linux Mint, which is a Debian Linux flavor. The steps for creating your Raspberry Pi image on Linux will work on all distributions of Linux since the command I will be using is a standard Linux command.
To create a backup image of the Raspberry Pi SD Card
You will need the following:
- Raspberry Pi
- 8GB Micro SD Card (Bigger SD card if you plan on saving the video to the SD card)
- 2.5A Power Supply
Instructions for Backing up the Raspberry Pi SD Card
Lets start off by opening a terminal window on your Linux computer. We will be able to do everything through the terminal window. Don’t put in your Raspberry Pi’s SD card into your computer yet, before putting in the SD card run the following command to display the amount of disk space available on our system. We really don’t care about the available disk space, what we will be looking for is all the partitions on our hard drive and we will be making note of what new partitions show up when we insert our Raspberry Pi SD card. Run the following command:
You should get a list of the current file system similar to the image below.
Now lets insert the Raspberry Pi SD card into your Linux computer and rerun the same command as above:
Note the 2 new lines that were added when running the ‘df’ command. The new device that wasn’t there the first time you ran the ‘df’ command is your SD card. Now that you have identified the Raspberry Pi SD card you will want to take note of the partitions name.
The first column gives you the name of your SD card as recognized by the file system, in my case it is ‘/dev/sdb1‘ and ‘/dev/sdb2‘. You can see that there are 2 partition on the SD card, SDB1 and SDB2, since we want to backup the whole Raspberry Pi SD card we will use SDB as our partitions name.
Type the following command into a terminal window to backup the Raspberry Pi SD card:
sudo dd if=/dev/sdb of=~/RaspberryPiBackup.img
Note: Change the /dev/sdb to whatever your file system name is. In my case above, my file systems name was /dev/sdb. You will also not get any progress on the backup being performed. It may take a few minutes to complete.
You can also rename the file name of the image you are creating to something else. Using the command above will save the Raspberry Pi SD card image to your home folder. Also note that the ‘dd’ command will not show you any progress so you will just get a blinking cursor. Depending on the size of the SD card you are backing up will determine how long it will take to backup, the larger the SD card the longer it will take.
Once the ‘dd’ command is finished you have successfully created a backup of the SD card for the Raspberry Pi. The backup image that has been created should be located in your home folder.
Restoring your Backup Image to the Raspberry Pi
If you would like to restore an image you have previously created you will need to discover which device is your SD card like in the steps above. Once you have discovered the file systems name of your Raspberry Pi SD card you will need to unmount it first. You will need to unmount each partition this time, in our case we have ‘/dev/sdb1‘ and ‘/dev/sdb2‘.
Use the following commands to unmount your SD card partitions:
sudo umount /dev/sdb1
sudo umount /dev/sdb2
Now write your backup image to the SD card with the following command:
sudo dd bs=4M if=~/RaspberryPiBackup.img of=/dev/sdb
Again, you will not get any progress of the image being written to your Raspberry Pi’s SD card. Notice that the command is just reversed from the back up as above. We just switch our input and output destinations.