How to backup Raspberry Pi

A big number of Raspberry Pi users love to experiment with the device. For many, that was the motivation of purchasing it in the first place. It is not uncommon to lose some files or corrupt an operating system on your Raspberry Pi every now and then, as a result of experimentation. But this is never much of a problem – that is, as long as you have made proper backups.

There really is no excuse for not making backups. The process to do so is very easy, and you are about to learn how. Restoring backups is also easy to do on the Raspberry Pi, so you face little downtime if the OS stops working or you lose some files. In this tutorial, we will go through the step by step instructions to back up individual files on the Raspberry Pi, as well as how to make a full backup of the entire OS installation.

In this tutorial you will learn:

  • How to back up individual files with rsync
  • How to create full system backups with rpi-clone
How to backup Raspberry Pi
How to backup Raspberry Pi
Software Requirements and Linux Command Line Conventions
Category Requirements, Conventions or Software Version Used
System Raspberry Pi
Software N/A
Other Privileged access to your Linux system as root or via the sudo command.
Conventions # – requires given linux commands to be executed with root privileges either directly as a root user or by use of sudo command
$ – requires given linux commands to be executed as a regular non-privileged user

How to back up individual files on Raspberry Pi




Backing up individual files is not a unique process on the Raspberry Pi. In other words, the same method you use on any ordinary Linux system can also be used on Raspberry Pi. There are many different ways to back up your important files, but the one that we recommend and will be covering in this tutorial is the rsync command.

DID YOU KNOW?
Backing up individual files means that the operating system itself, the installed applications, and the system configuration files will not be saved. Instead, just your important, irreplaceable files will make it onto the backup. Everything else will need to be reinstalled or redownloaded later in the case that you need to do a full system recovery later on.

Some users mistakenly think of rsync as a file copying tool, like cp or scp. While there’s some overlap, rsync excels in synchronization, specifically. In other words, it can take a source directory and make an identical destination directory. And when a file changes in the source directory, rsync can efficiently synchronize the contents to the destination directory, only transferring the bits that have changed. It’s also a very secure utility, utilizing SSH for remote file transfers. This makes rsync work very well as a backup tool, on top of file copying.

  1. The most common options to use with the rsync command are probably -a and -v. We can also use the --delete option to delete the extraneous files from the destination directory. Our basic syntax, then, will look like this:
    $ rsync -av --delete /src/ /dst/
    
  2. One of rsync’s most powerful features is that it can also be used with remote systems. To run rsync through SSH, we can add the -e ssh option in our command. Specify the remote SSH user and destination directory in the command as well. You’ll be prompted for the SSH password after entering the command.
    $ rsync -av -e ssh /src/ user@remote:/path/to/dst/
    

To learn more about rsync, check out our other tutorials which go more in depth:



How to back up the full Raspberry Pi system

rpi-clone is a Bash script that will clone your entire SD card to a destination disk (such as another SD card or USB drive). Your Raspberry Pi can be running during the process, so there is no need to take it offline.

Using this method, the operating system, your installed applications, configuration files, and all other data (including any personal files) will be backed up. This method is superior for users that apply many custom configurations to their system and do not want to go through the painstaking process of setting up another Raspberry Pi installation with all of the same settings that were previously applied. The recovery will do all of that for you.

Follow the step by step instructions below to use rpi-clone to create a backup copy of the SD card on your Raspberry Pi:

  1. First, make sure that git is installed on your Raspberry Pi by executing the following commands:
    $ sudo apt update
    $ sudo apt install git
    
  2. The rpi-clone script is hosted on GitHub (click on that link to see more information). Use the following commands to download the scripts:
    $ git clone https://github.com/billw2/rpi-clone.git 
    $ cd rpi-clone
    $ sudo cp rpi-clone rpi-clone-setup /usr/local/sbin
    
  3. Use a tool like fdisk to check the device path to the card (or USB device, disk, etc) that you wish to back up to.
    $ sudo fdisk -l
    

    For most cases, the operating system will most likely reside on /dev/sda. If you only have one other storage device inserted, then it will probably default to /dev/sdb, but be sure to check the output of the previous command to make sure.

  4. Now we can invoke the rpi-clone command and supply the path to the device that we want to clone our main drive to. As an example, we will use a generic /dev/sdX path:
    $ sudo rpi-clone sdX -v
    

Other method to clone installation

We thought it worth mentioning that the ddrescue utility can also be used to create a full clone of your Raspberry Pi system, in case you do not want to use rpi-clone. Check out our other tutorial on How to repair and clone disk with ddrescue for step by step instructions.



Closing Thoughts

In this tutorial, we saw how to back up the Raspberry Pi. This included using rsync to back up individual files and directories, as well as using the rpi-clone script from GitHub to create a full clone of the OS installation and all files. As mentioned, yet another alternative would be the ddrescue command.



Comments and Discussions
Linux Forum