Taillieu.Info

More Than a Hobby..

Warning: "continue" targeting switch is equivalent to "break". Did you mean to use "continue 2"? in /customers/3/2/5/taillieu.info/httpd.www/templates/taillieuinfo_joomla3_rev1/functions.php on line 194

How to Backup your Raspberry Pi SD Card

In this guide, we will be showing you various ways on how to backup your Raspberry Pi and also how to restore it. Backing up is a crucial task that you should be doing often, especially if you make many changes or are storing data on it.

One thing to note is that some backup methods are going to be way more thorough the others, for instance backing up your Raspberry Pi SD Card image is going to be more reliable than just backing up all the files to a USB device since the image is a replication of all partitions on the SD Card.

We will be exploring two different methods of backing up your Raspberry Pi in this guide, the two different methods that we will be showing you how to do is the following:

 Backing up your Raspberry Pi to a computer using a SD Card Reader
 Backup the Raspberry Pi to a USB device

Personally, we recommend backup to a computer as it is the most robust method. Mainly since the Raspberry Pi doesn’t have the best USB speeds or network speeds so creating backups can take considerable time and put a fair bit of stress on the Raspberry Pi’s limited resources. As an added bonus the backup we make using a tool like win32diskimager is a 1 to 1 copy of the SD Card meaning every byte should be the same.

Backing up the Raspberry Pi is an important step in making sure you don’t lose any data when you go to make big changes to your system. This is important if you have stuff that you would prefer not to lose if you’re looking at doing a complicated Pi project, upgrade or something else to the operating system.

 Backing up your Raspberry Pi SD Card

To start off we are first going to show you how to backup your Raspberry Pi SD Card as an image. To do this tutorial you will need to have a SD Card reader handy.

To begin this tutorial, please first turn off your Raspberry Pi by running the following command on your Pi’s terminal:

sudo shutdown now

Once it has shut down, disconnect the power and remove your SD Card. Place the SD Card into your SD Card reader and proceed onwards with this section of the guide.

This section of the guide will be split into 3 parts, one for Windows, one for Mac OS X and one for Linux based systems.

 Backing up your Raspberry Pi SD Card on Windows

1. To backup our Raspberry Pi’s SD Card on Windows we will be utilizing the imaging tool that is called win32diskimager. Win32diskimager is an incredibly useful tool that can read and write images to USB Sticks or SD/CF Cards. It is a tool many will be familiar with since it is used often to write the initial Raspbian image to your Raspberry Pi.

We will start off this section of the guide by downloading and installing this tool by going to win32diskimager’s homepage.

2. With win32diskimager now installed, open it up. You should be greeted with a screen like we have shown below.

For now we need to make sure the right drive has been selected by clicking the dropdown box under “Device:” (1.) and ensuring the correct drive letter of our SD Card is selected.

Afterwards, click the folder icon (2.) that is directly to the left of the device dropdown box.

Selecting drive to backuph

 

3. You will now be shown a file selection screen, however we won’t be selecting any file. Instead navigate to the folder you want to keep your backup file. Once within this folder, type in the name you want to give your backup file in the textfield (1.) at the bottom as shown in the screenshot below.

Make sure you have .img at the end of the name you choose to use. For example, we named our backup file RaspberryPiMyLifeUp.img

Once you have typed in your file name you want to utilize, click the Open button.

Backing up Raspberry Pi Selecting a disk image

 

4. Finally with our device set and our new filename set we can begin the backup process. To do this we simply just need to click the “Read” button. Please note that this can take some time as it is a complete backup of your SD Card, meaning every single byte is replicated.

Raspberry Pi Backup SD Card

 Restoring your Raspberry Pi Backup on Windows

1. Now when it comes time that you need to make use of your full SD Card backup we will need to once again make use of win32diskimager. Before opening the software, make sure you have inserted your SD Card into a SD Card Reader that is plugged into your computer.

With the win32diskimager software now opened, lets click the dropdown box that is located under the “Device” (1.) header and ensuring that the correct drive letter of our SD Card is selected.

 

After you have set the device we can now, click the folder icon (2.) that is located to the left of the dropdown box.

Selecting drive to backup

2. Now that we have the file browser loaded up, let’s find the backup file that we created earlier. Once you have found it either double click the file, or single click it and click the “Open” button.

3. With our backup image now selected, we can now proceed with writing the backup file to the SD Card. Please note this will wipe everything currently on the SD Card so make sure that you have the correct device selected. When you are happy you have everything correct, click the “Write” button.

Raspberry Pi write backup image

 

4. Once the writing process has completed you should now have a SD Card that is in the exact state of when you originally made the backup. Thanks to the full image backup it means all partitions on the card are restored.

 Backup up your Raspberry Pi SD Card on OS X

1. With your SD Card inserted into a card reader on your Mac we can begin the process of making a full image backup of your Raspberry Pi. Unlike Windows, we can easily just use the terminal to do this.

To proceed with this tutorial, start off by opening the Terminal application.

2. With the Terminal application now open on your Mac device, we need to utilize the following command. This command will display all available disks on your device.

diskutil list

Within this list, look for your SD Card by looking for a disk that is about the size of your SD Card. For instance, with a 16gbSD Card you should be looking at a "_partition_scheme" of about 16gb. You will also notice that there is likely a partition called "boot".

Once you have found your SD Card in this list, take notice of the mount location. For instance, my own SD Card was under /dev/disk1.

3. Now still within the terminal on your Mac, we need to utilize the following command. This command will basically create a copy of your SD Cards image, and save it to your home directory as PiSDBackup.dmg (The file format being a disc image)

Make sure you swap out /dev/disk1 with whatever you found when using the diskutil list command.

sudo dd if=/dev/disk1 of=~/PiSDBackup.dmg

This command can take some time to complete as it requires reading the entire SD Card to the disk. The command also provides no feedback on how far along it is, so please be patient and wait for the prompt to enter another command to reappear before removing your SD Card from your Mac.

 Restoring your Raspberry Pi Backup on OS X

1. Now that you need to restore your Raspberry Pi to its backup we need to again use the diskutil list command to find our SD Card. Remember to as before, take note of partition sizes to find your SD Card.

diskutil list

2. Now before we can write to the SD Card we will need to unmount it. The reason for this is that OSX will attempt to write to it at the same time, unmounting the SD Card prevents this from happening.

Run the following command on your Mac device to unmount the card, again making sure that you replace /dev/disk1 with the one you found using the diskutil list command.

diskutil unmountDisk /dev/disk1

3. Finally we can now write the image back to the SD Card, please be prepared for this to take some time as it involves rewriting the entire SD Card. Remember to change out /dev/disk1 with the mount location you grabbed using the diskutil list command.

sudo dd if=~/PiSDBackup.dmg of=/dev/disk1

Like reading the SD Card to a disk image file, the process of writing the image also takes a long time. As an added note, the dd tool doesn't show any write progress so please be patient and wait till the enter command prompt reappears.

4. Once the write process has completed we can now eject the SD Card from the Mac so we can continue using our Raspberry Pi Backup. To eject the SD Card, we will need to utilize the command below.

Your Raspberry Pi's SD Card should now be in the exact same state as when you made the original backup.

sudo diskutil eject /dev/rdisk3

 Backing up your Raspberry Pi SD Card on Linux

1. Before starting to backup up your Raspberry Pi's SD Card on Linux we will first run a command with the reader not plugged in. The reason for this is makes it much easier to see which device is which.

df -h

This command will return something like what is shown below.

Filesystem      Size  Used Avail Use% Mounted on
/dev/root        15G  4.0G  9.6G  29% /
devtmpfs        458M     0  458M   0% /dev
/dev/mmcblk0p1   41M   21M   21M  52% /boot

2. Now insert your SD Card reader back into your Linux computer and again run the following command again, but this time take note of the additional entries.

df -h

This command will return something like what you got above, but with an additional entry, in our case /dev/sda1 is that additional entry, with your entry, remove the partition number. For instance, /dev/sda1 will become /dev/sda. We need to do this as we want to write to the entire drive and not just a singular partition.

Filesystem      Size  Used Avail Use% Mounted on
/dev/root        15G  4.0G  9.6G  29% /
devtmpfs        458M     0  458M   0% /dev
/dev/mmcblk0p1   41M   21M   21M  52% /boot
/dev/sda1       3.7G   75M  3.6G   3% /media/boot

3. We can now utilize dd, the same tool we make use of on Mac OS X since it is also built on Linux. We can utilize the following command to begin dumping the SD Cards image to our home directory.

Make sure you replace /dev/sda with the filesystem that you grabbed using the df -h command.

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

The process of backing up your Raspberry Pi can take some serious time, so be patient and wait. The dd tool provides no feedback, so you will have to wait until the input command returns to your terminal.

Once it reappears you will have successfully backed up your Raspberry Pi.

 Restoring your Raspberry Pi Backup on Linux

1. Now that you have made a backup of your Raspberry Pi you will want to at some stage make use of this. To do this we will need to again go through the process of finding out the location of our filesystems.

Use the df -h command like we did in the first segment of this guide, though this time you might have more than one partition pop up for your SD Card such as /dev/sda1 and /dev/sda2. Take note of all new entries as you will need to unmount all of them.

2. Now that we have all our partition locations ready, we can unmount each of them by running the following command for each one. Switching out /dev/sda1 for the locations that you got in the previous step.

sudo unmount /dev/sda1

3. With all the partitions now unmounted let's write our backup image to the SD Card. We can do that by running the following command. Remember swap out /dev/sda with your devices mount location.

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

 Backing up your Raspberry Pi to a USB Drive

1. Before plugging in your USB device that you want to keep the backups on let's first run the following command to find out the current filesystems that are available to us.

df -h

This command will return something like what is shown below.

Filesystem      Size  Used Avail Use% Mounted on
/dev/root        15G  4.0G  9.6G  29% /
devtmpfs        458M     0  458M   0% /dev
/dev/mmcblk0p1   41M   21M   21M  52% /boot

2. Now insert your USB Device into your Raspberry Pi and run the following command, take note of any new entries that pop up.

df -h

This command will return something like what is shown below.

Filesystem      Size  Used Avail Use% Mounted on
/dev/root        15G  4.0G  9.6G  29% /
devtmpfs        458M     0  458M   0% /dev
/dev/mmcblk0p1   41M   21M   21M  52% /boot
/dev/sda1       3.7G   75M  3.6G   3% /media/pi/MYBACKUPDRIVE

3. With our USB device now showing on the list, we need to look at the "Mounted on" location. In our case this is /media/pi/MYBACKUPDRIVE. This will be the location that we will write our backup images to. So, make note of it for later in the tutorial.

Now with our backup location now handy we can download the backup script that we are going to use, this script was written by a user on the Raspberry Pi forums called Jinx.

Let's clone the script to our Raspberry Pi by running the following two commands.

cd ~
git clone https://github.com/lzkelley/bkup_rpimage.git

4. With the script now saved to the Raspberry Pi we can start to make use of it. We can do an initial backup by running the command below on our Raspberry Pi. Make sure you replace /media/pi/MYBACKUPDRIVE with your own mount location that you grabbed in the previous step.

sudo sh bkup_rpimage.sh start -c /media/pi/MYBACKUPDRIVE/rpi_backup.img

Basically this script will create a dummy image then launch an rsync process to copy all the files from the system to the dumby image. Please note that the initial backup can take up to an hour to complete.

5. Now that we have created our initial backup file and know that the script is working as intended we can move onto automating the backup. To do this we will be making use of cronjobs.

One thing to decide on how is whether you want an incremental backup or multiple backups. An incremental backup just updates the original backup and doesn't generate a new file.

Run the following command on your Raspberry Pi to begin editing the crontab.

sudo crontab -e

6. In the crontab editor, add one of the following lines to the bottom of the file. This will make a backup every day. If you want to edit the cron timings you can use the Crontab guru website to easily work them out.

Incremental Backup

0 0 * * * sudo sh ./home/pi/bkup_rpimage/bkup_rpimage.sh start -c /media/pi/MYBACKUPDRIVE/rpi_backup.img

Multiple Backup

0 0 * * * sudo sh ./home/pi/bkup_rpimage/bkup_rpimage.sh start -c /media/pi/MYBACKUPDRIVE/rpi_$(date +%Y-%m-%d).img

Now save the file by pressing Ctrl + X then pressing Y and then hitting Enter.

7. You should now have an automated backup system up and running that will continually backup your Raspberry Pi to your USB device. To restore these images follow our Restoring guides located in the SD Card section of this guide.

I hope that you now know how to backup your Raspberry Pi SD card on both your main system or backing up directly to a USB device. You can now continue on with working on some Raspberry Pi projects or just using your Pi as you would normally. If you run into any issues or have any feedback feel free to drop a comment below.