Many people pour hours and hours into developing a project on their Raspberry Pi and never put any though in how to back up their work. A Raspberry Pi is a used a lot for prototyping and electronics learning projects. It’s a super fantastic computer for these purposes and even includes a full set of GPIO pins for connecting all sorts of sensors, buttons, I2C serial communications, and other devices. Unfortunately, even with the protections offered in the Raspberry Pi for shorts and overloads, it is still very common for fry the device by wiring something incorrectly. That’s why backups are important and so in this tutorial we’re going to talk about how to backup a Raspberry Pi properly and some different ways to do it.
Things to Consider Before Backing Up a Raspberry Pi
I know a lot of people will comment below saying things like “I keep a copy of my code on my PC/Dropbox/Smartphone.” and that’s awesome. Keeping a copy of your code on another device is absolutely a great thing to do. Honestly, for some projects it might be enough. However, may people fail to remember that a Raspberry Pi project isn’t just about the code they wrote.
When developing a projecting, you’ll likely need to make many configuration changes to the Raspbian operating system. You’ll also likely install new software libraries and packages onto the Pi for your code to take advantage of. When you restore your code from a folder on your PC none of those configurations or libraries will be installed. You’ll have to remember what they all were and add them back one at a time. That’s a mess.
In this how to backup a Raspberry Pi tutorial, we’re going to cover two main methods of backing up your Pi.
- Backing up your development over the network (good)
- Backing up a copy of the SD-Card to your PC (best)
For the astute in the room, you’re probably thinking right about now “Why not do both?”. And you couldn’t be more right! I use both methods to backup my R2-D2 build project. I make a nightly copy of my development files and a weekly image of the SD Card.
Backing up Your Files to the Network
So let’s first go over how to backup your files to the network. This is a pretty simple task. I’d highly recommend using a dedicated NAS appliance for backing things up as they usually have superior redundancy and backup options for their own data. However, a Windows 10 share will do in a pinch. If you need help setting up a Windows 10 share, check out this article.
The first thing you’ll need to do is mount the shared folder on your Raspberry Pi. Everything we do in this article is going to be command-line since the vast majority of people will be working with their Pi from the command-line. Feel free to ask questions in the comments if you need help doing this in the GUI. We’re pretty fast to answer.
Before you can mount Samba (or SMB) shares on your Raspberry Pi you’re going to need to install the Samba client tools. Do that by running these commands:
sudo apt-get update sudo apt-get install samba samba-common-bin smbclient cifs-utils
Create a Network Mount Folder
You’ll want to create a folder on your Raspberry Pi that will serve as the mount point for your network share. In my R2-D2 build, I created a folder in the root of my Pi called simply NAS and gave it full read write permissions.
sudo mkdir /NAS sudo chmod 777 /NAS
Mount the Windows Folder on Your Raspberry Pi
The next order of business is super simple! You’re ready to mount your NAS or Windows share to the newly created folder on your Raspberry Pi. To that you’ll need to run the following command. Keep in mind you’ll need the username and password of your NAS or Windows PC to do this.
sudo mount.cifs //nas1.home/Dropbox /Dropbox/ -o user=yourusername,password=yourpassword
You can also put this command into a file called “mountme.sh” or whatever name you prefer, allowing you to run it quickly without typing it in every time. However, remember that this means a password is stored in plain text for anyone to see if others have access to your Raspberry Pi. Example mountme.sh file:
#!/bin/bash sudo mount.cifs //nas1.home/Dropbox /Dropbox/ -o user=yourusername,password=yourpassword
Syncing your Raspberry Pi Code to Your PC
The next thing you need to do is copy your code to your Windows or NAS shared folder. You could do this with the CP (copy) command. However, I much prefer to use rsync. It is faster, keeps all of the file attributes intact, and will only waste time and network bandwidth on files that have changed. If you have a lot of files this could take a while on the first sync, but on subsequent copies, only changed files get transferred and it will be significantly faster. The command to do that is below.
rsync -rvh /home/pi/Documents/MyCode /NAS/MyCode/
If you’d prefer to just copy the files you can use this command. Keep in mind every file will be copied and overwritten.
cp -r /home/pi/Documents/MyCode /NAS/MyCode/
How to Backup a Raspberry Pi SD Card to Windows
The absolute best way to ensure you have a copy of your Raspberry Pi and all of its data is to routinely image the SD card. You can make a copy of the entire SD card and store it on your PC, or you can copy the entire contents to another SD card. I prefer to make an image. This allows me to keep a copy on my NAS which syncs the file to my Dropbox folder in the cloud. Not only do I have protection from a PC failure or home fire, but I also have a copy I can access remotely when I am traveling.
My preferred tool for imaging an SD card is Balena Etcher. It’s a super easy to use tool and does a great job. Unfortunately, it only copies files too the SD card and won’t back them up to an image. Since that’s what we need to do, we’ll be use Win32 Disk Imager.. If you’re on a Mac you can use Apple Pi Baker. Win32 disk imager is what we’ll use in this how-to, but Apple Pi Baker works very similarly.
The first thing you need to do is safely shutdown your Raspberry Pi. Do this at the command line by running the following command:
sudo shutdown now
Move the SD Card to Your PC and Create an Image
After a few minutes your Raspberry Pi will be shutdown. Disconnect it from power by unplugging the 5V micro-USB cable. You can now safely remove the micro-SD card from the Pi and place it into a USB card reader connected to your PC (or Mac).
Next, open Win32 Disk Imager and click the folder icon next to “Image File” and enter the name of the backup file. In my case I chose “The Geek Pub RPi Backup.img”, but any name will do.
On the far right top side of the imager, click the Device drop-down and select the drive letter that represents your micro-SD card (or the reader it is plugged into). In my case that is the F:\ drive, but yours will likely be different. You can go to My Computer to see a list of all of your drives if you have any concerns or have multiple drives listed and you are not sure which one to choose. Your Raspberry Pi micro-SD card will most likely be named “boot” on that screen.
Now click on the Read button. Win32 Disk Imager will begin reading the contents of your SD card and writing it to the file name you chose. Be patient, this is probably going to take between 5 and 10 minutes depending on how much data you’ve copied to the Pi’s SD card.
Restoring Your Raspberry Pi’s SD Card
In the future, if you decide to restore your SD card, just follow the same steps. Only this time select the image and click the Write button. The process will operate in reverse. For an even simpler restore we recommend using Balena Etcher.
One thing we hear a lot from people is that their RPi SD card will not mount on their PC when they try to restore it. This is a common problem. The fastest way to fix this is to format the micro-SD card before you try to restore data to it. Honestly, its not a bad idea to do this anyway as your micro-SD card could have been corrupted at some point, hence why you’re restoring it!
How to Backup a Raspberry Pi SD Card to MacOS
Backing up your Raspberry Pi SD card to MacOS can be done without any additional software. Since MacOS is a Linux(ish) variant, many of the Linux (or BSD) tools are included with the operating system. In this case, the tool we need is DD. DD stand for “Copy and Convert” and if that makes you say “Huh?” you’re not alone. The CC command was already taken by the C compiler when DD was invented, so the author had a little fun and just called Copy and Convert DD.
Just like in our Windows example, you’ll need to plug your Raspberry Pi micro-SD card into your Mac before we begin. Once that’s done, open a new terminal window and type the following command:
You should see something similar to the following output. Look for a disk called “boot” that is similar in size to that of your Raspberry Pi’s micro-SD card. If your micro-SD card is 64GB you’ll likely see a drive called boot in the 63.x GB range. Once you’ve found it, make a note of the drives name. In my case it is /dev/disk3.
With the name of your drive now known, all you need to do is run the DD command to backup that drive to a file.
sudo dd if=/dev/disk3 of=/Users/yourusername/Documents/TheGeekPubPiBackup.img
The weird thing about this utility is that shows zero progress after it starts running. Because this backup will take 5 to 10 minutes to complete, many people assume its hung and not working. But there is a neat trick to get the status. Just press CTRL+T while DD is running to get a nice little output of what is happening.
Final Thoughts on Backing Up Your Raspberry Pi
Now that you know how to backup your Raspberry Pi to your PC, Mac, NAS or cloud storage folder, there are some additional things you should consider.
Reducing Win32Disk Imager File Size
Win32 Disk Imager is a byte-for-byte copy of your Raspberry Pi’s micro-SD card. This means if your SD card is 64GB in size, the backup will also be 64GB in size. If you make or keep a lot of backups the file sizes will likely become unmanageable. There are a couple of ways to deal with this.
The first way is to simply use Windows 10’s built in compression utility to compress the file into a .zip file. This works great and in most cases will significantly reduce the file size of your backup. In my case, I was able to get about a 65% reduction in file size. Unfortunately, its very slow and will take just about as long as the original backup to complete.
Another option is to use HDRawCopy. This utility is similar to Win32DiskImager but also compresses the file during execution. It’s not as well supported as other imaging utilities, but it does the job well enough.
Well now that you know how to backup a Raspberry Pi the right way, we hope you’ll take this task seriously and to it on a regular basis! If you run into any trouble leave a comment below and we will do our best to help you out!