Follow by Email

Blog Archive

Search This Blog

Backup data to a USB stick Raspberry Pi

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.
Share it:
Backup data to a USB stick (Raspberry Pi)

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: Create a backup to a USB stick
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.
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.

Solution: Create a backup automatically
For such automatisms Linux brings along a tool. The so-called "Cron". "Cron" is a daemon for the time-based execution of processes and recurring tasks (Cronjobs) in Unix operating systems and thus Linux.
The instructions to be executed are stored in a user-defined table, the so-called "crontab". The "crontab" is a "timeline", basically a simple table, with five or six columns. The first five serve the time (minute, hour, day, month, day of the week). Then follows the user who is to execute the script and all other characters in a line serve the statement to be executed. For example, a shell or bash script. For the backup we have already created such a script. But you should only enter those scripts in the "Crontab", which has been tested manually.

The "Crontab" is a simple text file. There are two types of "crontab" that must be distinguished.
  • User crontab: Can be edited using the crontab -e command.
  • System crontab: Located in the file / etc / crontab.
They differ in principle in that the system crontab contains a column containing the user name that is to execute the command or the script.
We call the system crontab.
 sudo nano / etc / crontab

There you enter the following line at the end:
 0 1 * * * root bash /home/pi/backup-home-pi.sh
What does that mean? We want to run the backup script every day (month and year) at 1:00 am by the user "root".

Then just save and close: Ctrl + O, Return, Ctrl + X.
If you 've done everything right, then the backup should be automatically created at the specified time.
If something does not work
Serious errors are written to the "/ var / log / syslog". So if something does not work and you do not get straight to the mistake then that should be the first place to look for why it does not work. Maybe there is a hint here.
Share it:

Raspberry Pi

Post A Comment:

0 comments: