how to backup raspberry pi sd card to usb drive
Making a backup makes sense. But doing it all by hand makes less sense. What if you forget it? That’s exactly when you need it.

So you automate the backup. For example, once a night, at a time when the system to be backed up has nothing to do.
Task
- Create a backup of your user directory “/ home/pi” on a USB stick.
- Create an executable script file that runs the backup.
- The backup should be created automatically every day at one o’clock.
Solution: backup raspberry pi sd card to usb drive
Creating a backup usually only makes sense if you save the backup outside the system. For example, on a USB stick. For this we have such a stick when plugged in via “usbmount” automatically on “/ media / usb” integrated into the system.
- Mount/unmount volumes, drives, and file systems
- Automatically mount/unmount USB stick with “usbmount”
- Automatically mount/unmount USB stick and USB hard disks with “fstab”
Alternatively, the backup can also be done on “/ dev / sda1”. Only then you have to make sure that the USB stick is actually there to find.
Now it’s time to create a backup on the USB stick. Basically there are several possibilities. In this case we choose a fairly old program with “tar”, but it is very common and exists in every Linux.
“tar” creates archives with the file extension “.tar”. These are summaries of files. In addition to the directory structure, rights and owners are also stored. This means that when you play back the fuse, everything is back. To create a backup, “tar” is perfect.
The parameter “c” instructs “tar” to create an archive file.
With the parameter “P” “tar” remembers the original location of the archived files and directories. But this also has the consequence that you can restore the files only to their original location. If you want it flexible, then you should do without the parameter “P”, but you have to pay close attention to where you extract the archive.
The parameter “v” lists all files during archive generation. So if it takes a bit longer then you can see where “tar” is.
The parameter “f” instructs “tar” to redirect the data to a file. Originally, “tar” writes the data to a tape drive.
How to create an archive:
tar -cPvf /media/usb/backup-home-pi.tar/home/pi
If the archive should already exist, then it is simply overwritten.
When “tar” is done, the command line returns to the input.
If you want to know if the file was actually created and how big it is, enter:
cd /media/usb
Is-all
And we can still see the archive content:
tar -tvf /media/usb/backup-home-pi.tar
The “tar” command not only creates an archive file but can also extract it again.
A restore would look like this:
tar -xvf /media/usb/backup-home-pi.tar
Solution: Backup by script
Switch to your user directory:
cd /home/pi
Create the following file:
sudo nano backup-home-pi.sh
with the following content:
#! /Bin/bash
tar -cPf /media/usb/backup-home-pi.tar /home/pi
exit 0
Three things are different here compared to direct command input. The first line instructs the interpreter. In the second line we dispense with the screen output by omitting the parameter “v”. We do not need this for automatic backup. Finally, there is an exit code in the third line. This tells the system that the script ran successfully.
Save and close: Ctrl + O, Return, Ctrl + X.
Then you have to make the file executable.
sudo chmod + x backup-home-pi.sh
To test whether the file can be executed and the backup is created, simply enter the name of the file:
bash backup-home-pi.sh
Note: When running “tar” in the Bash file, we did not use the parameter “v”. Therefore nothing is displayed now.
If “tar” has created the archive, the command line returns to the input.
Now you should check if the backup was really successful.
ls -all /media/usb0
If the file “backup-home-pi.tar” is displayed with the current time, the backup was successful.
Leave a Reply