If you, like me, make a lot of changes to your Py Framboise, it might be a good idea to back it up.
And even better if the backups are restored
Yes, but how can I back up and restore my Pi Raspberry? We will see this in this complete guide.

The best way to back up your Raspberry Pi is to use a tool such as rsync and copy important files to another location on the network.
You can also make full backups on SD card to make sure everything is safe.

As is often the case, there is more than one way to do this. This strongly depends on how you use your Raspberry Pi and what is available on your network for backup storage.
In this tutorial I will show you different solutions:

  • Only back up important files (configuration, documents, MySQL databases).
  • Create a full image of the SD card on another computer
  • Or make a full backup of Raspberry Pi while you’re working.

We will now look in detail at the manufacture of each of these devices.

Back up only important files

The first method you can use if you have an easy installation is to back up only the files you need.

If you z. B. use a Raspberry Pi for a security camera, it’s a good idea to save your configuration file once, you don’t need it anymore.
So we’ll see it step by step:

  • How to create a scenario
  • To program it when folders are changed
  • How to send a file to another computer (in a script or manually).
  • And finally, how to restore files

Scenario

Before you start creating the script, you need to define the files you want to record.
I use in my script a file and a folder that you can customize to your wishes:

  • /etc/app1/file1.conf
  • /etc/app2

Then we need to create a backup directory to store the files:
mkdir /home/pi/backups

Use the nano to create a script:
nano /usr/local/bin/backup.sh

Finally, the first version of a simple scenario can look like this:

#! /bin/bash
/bin/cp /etc/app1/file1.conf /home/pi/backups
/bin/cp /etc/app2 /home/pi/backups/ -r

As you can see, this is a simple script that overwrites the old backup every time.
That is why we are now refining this scenario in a number of ways.

Use variables

The first good practice we use to improve scripts is to add variables.

You’ve got z. B. 200 files to store in /home/pi/backups, and tomorrow you want to store them in /media/nas/pi/.
You need to edit 200 lines in your script to change the destination folder.

The best way to do this is to create a variable at the beginning of the target directory path and use it for each line.
I do the same for the cp command, so if you want to change it to use rsync or another command, you only have to change one line.

Here’s a better scenario:

#! /bin/bash
DEST_FOLDER=’/home/pi/backups/’
BACKUP_CMD=’/bin/cp’.

$BACKUP_CMD /etc/app1/file1.conf $DEST_FOLDER
$BACKUP_CMD /etc/app2 $DEST_FOLDER -r

The result will be the same, but it will be easier to update.

Compressed files

In most cases we use compression to back up or at least archive the files.
I use tar to archive all files in one file and gzip to compress that file.

Here’s the new scenario:

#! /bin/bash

DEST_FOLDER=’/home/pi/backups/’
DEST_FILE=’backup.tar’
BACKUP_CMD=’/bin/tar -rvf’.

/bin/rm $DEST_FOLDER/$DEST_FILE.gz
$BACKUP_CMD $DEST_FOLDER/$DEST_FILE /etc/app1/file1.conf
$BACKUP_CMD $DEST_FOLDER/$DEST_FILE /etc/app2
/bin/gzip $DEST_FOLDER/$DEST_FILE

I add a new variable DEST_FILE to save the name of the backup file.
tar -rvf makes it possible to add multiple files to a tar file.
gzip makes it possible to compress the whole tar file.

Stop overwriting files

As you can see in the last script, each time we delete the previous backup.
It’s no good. If there are problems with the backup, you cannot get an older version.

It is good practice to name the backup file with the current time:

#! /bin/bash

DEST_FOLDER=’/home/pi/backups/’
DEST_FILE=’backup-$(dat+%F_%R).tar’
BACKUP_CMD=’/bin/tar -rvf’
$BACKUP_CMD $DEST_FOLDER/$DEST_FILE /etc/app1/file1.conf
$BACKUP_CMD $DEST_FOLDER/$DEST_FILE /etc/app2
/bin/gzip $DEST_FOLDER/$DEST_FILE

Nothing has changed except that we added a date to the DEST_FILE variable so we don’t delete the previous backup.

Each time you run the script, it creates a new file and saves all previous versions.

data-ezsrc=http://server.digimetriq.com/wp-content/uploads/2020/12/1608398820_992_How-to-Easily-Install-Apps-on-Raspberry-Pi-OS-5.jpg />

Course Raspberry Pi
Go to the next step.
I’m here to help you get started with your Raspberry piss and learn all the necessary skills in the right order.

Saving the calendar

After creating our scenario by following the previous steps, most of the work is done.

Simply program our scenario to run automatically every day.
We use a crontab:

  • Open the user crontab:
    crontab -e
  • Add this line to the end of the file:
    0 0 * * * * /usr/local/bin/backup.sh
    This cron runs your backup script every day at midnight, but you can change it if you want.
  • Backup and output (CTRL+O, Input, CTRL+X)

When backing up your files with the necessary rights, do not forget to program a script in the root crontab (sudo crontab -e).

If you don’t like it, feel free to read my tutorial on Raspberry Pi task scheduling.

Deleting old records

As you can see, a new file is created every day and is never deleted.
You can delete files older than eight days (or more, if necessary) to free up storage space.

To do this, you can add the following command to the end of the script:
/usr/bin/find $DEST_FOLDER -mtime +8 -delete

If you add this option, there will only be eight history files in your destination folder at any given time.
You can change the number of files you want to save.

It’s always a good idea to spend a few days in the past.
You can’t be sure you’ll see a problem with your records from day one.

Send a backup to the network

We now have a backup with a few days of history on the SD card, but that’s not enough.
If the SD card does not work tomorrow, you no longer need to save files.
To avoid such problems, we need to put the files in a safe place on another computer.

To a Linux or NAS computer

If you have another computer running Linux (or NAS), you can transfer your backup files to this computer to keep them safe.
This method also works for macros.

Personally, I use a Synology 4-bay NAS server, which is ideal for storing large amounts of critical data. It’s a bit expensive, but redundancy and security are hard to find on a standard computer (and many tools included).
For less important data, you can also change another Pi Raspberry to a NAS and sync your files with it (see my tutorial if you want to give it a try).

Manual

The first option is to do it manually.
If your files never change, or if they are not that important, you can do it once a week or once a month to be sure.

We use rsync to transfer files from the Raspberry Pi to the Linux computer.
So you need to install it on both machines:
sudo apt-get install rsync

Then create a backup folder on your computer:
mkdir /home/[USER]/backups_pi

You then have two possibilities to transfer files:

  • From your computer:
    rsync -auzr pi@[RASPBERRY_IP]:/home/pi/backups/* /home/[USER]/backups_pi/
  • From Crimson Pi:
    rsync -auzr /home/pi/backups/* [USER]@[COMPUTER_IP]:/home/[USER]/backups_pi/.

Don’t forget to replace the variables with the IP address of the raspberry Pi and your username.
And feel free to delete old files on your computer before or after the transfer.

automatic

Just like saving a raspberry Pi, it’s always a good idea to automate a task like this.
One problem you may have noticed is that you need to enter a password when you start the transfer.
To avoid this, you must first exchange the SSH keys between the two machines.

For this step, I’m assuming it’s your computer that’s performing the transfer.
If you prefer to do the opposite, you should do it the other way around (generate the keys on the Raspberry Pi and activate them on the PC).

Key transfer

  • First create a key on your computer (if you already have one, that’s fine, then go to the next step):
    ssh-keygen -t rsa
    Press the Enter-key for each question, leaving the passphrase empty.
  • Then pass on the public key of the raspberry Pi:
    rsync ~/.ssh/id_rsa.pub pi@[RASPBERRY_IP]:/home/pi/
    The last time you need to enter the password.
  • On the Raspberry Pi, add the public key to the authorized keys:
    cat ~/id_rsa.pub >> ~/.ssh/authorized_keys
  • Now, try to connect in silence. It should not ask for the password:
    ssh pi@[RASPBERRY_IP].

Example scenario

So you should be able to put the rsync command in a script that doesn’t ask for a password.
The scenario has to look like this:

#! /bin/bash
rsync -auzr pi@[RASPBERRY_IP]:/home/pi/backups/* /home/[USER]/backups_pi/
find /home/[USER]/backups_pi -mtime -8 -delete

Then program the script by adding it to the crontab as described above.

To a Windows computer

If your computer is running Windows, you can also back up, manually or automatically.

Manual

To manually transfer files from the Raspberry Pi to a Windows computer is the easiest way to install WinSCP on Windows.
It is a free file transfer software in SSH :

  • After installation, start WinSCP
  • Add a new raspberry Pi
    alt=winscp settings width=516 height=291 data-ezsrc=http://server.digimetriq.com/wp-content/uploads/2021/01/The-Ultimate-Guide-to-Backup-and-Restore-your-Raspberry-Pi.jpg data-ez= />
  • Click on Save and log in to the new site.
  • On the right side you will see the files of the Raspberry Pi and on the left side the files of your computer.
    You can now transfer files from one to another by dragging and dropping.
    Go to /home/pi/backups on your Raspberry Pi and download all or part of the files.
    You can then, for example, delete old files from your computer. B. to keep only the last eight days.

automatic

There is no way to program a transfer with WinSCP. And installing SSH on Windows is not that easy (at least not if you use it in a script).
So we need to find another method.
I suggest you share a folder in Windows and open it with your Pi Raspberry.

Share a file

Follow these steps to share a folder on Windows:

  • Create a new folder to store your backups in.
  • Right-click on this folder and select Properties from the context menu.
  • Click the Share tab and then click the Share button.
  • By default, your user has access to this resource, but you can add other users if you wish.
    You need a user with a password.
    If your Power User does not have a password, you need to set one.
    If you want, you can create another account with a password.
  • If everything is okay, click the release button.
  • That’s good. You now share a folder on the local network
    Save the network path for the next step
    For me it was something like this : \User [USER] Desktop herring.

Set the portion to pi raspberry.

Now we need to connect the Pi of the raspberry to the storage.
To make it permanent, follow the steps below:

Your shared folder is now available in /media/share, and when you create a file in this folder, it is available on your Windows computer:
sudo touch /media/share/test

Example scenario

Now that sharing is available, you can create a script and plan that z. For example, backup files are copied to this folder on a daily basis:

#! /bin/bash
cp /home/pi/backups/* /media/share/
find /media/share -mtime +8 -delete

Recoveries

This backup is the easiest to restore.
All you have to do is unpack the files from the archive and send them back to the original folder.

of Linux

Under Linux you can use the standard archiving software of your distribution.
Double-click on z. For example, on Ubuntu, click on a backup file to open it and see the files it contains.
Then click on Extract.

Once you have the desired files, send them back to Raspberry Pi using rsync as described above.

from Windows

On Windows I’m not sure if the default tool can open a .tar.gz file, but you can try it.
Otherwise, you need to install software such as 7zip or WinRAR.

Unpack the files with this tool and transfer them to the Raspberry Pi using WinSCP as described above.

Experience

It’s a good idea to go through the recovery process at least once.
So you can be sure it works and that you know how to do it.

Monitoring

With this method you can easily check if your backup is working every day as expected.

You can create a script that checks if there are current files in the backup folder.
Something like this:

#! /bin/bash

if [[ $[(find /home/pi/backups -type f -mtime -1) ]] ; then
echo Backup OK
//DO NOTHING
otherwise
echo Backup KO
//DO SOMETHING (MAIL?)
fi

Exporting MySQL data

If you have MySQL databases on your Raspberry Pi, the procedure is slightly different.

Fuse

The best practice for backing up MySQL data is to first export and then save this export.

Export the database with this command line:
mysqldump -uroot -p [PASSWORD] [DATABASE NAME] > export-[DATABASE NAME].sql

This command creates a file with all the SQL queries needed to create the database from scratch.

You can follow the same steps as before:

  • Scenario
  • map
  • Transfer files to another computer

If you need to back up more than one database, add as many lines as necessary to the script and change the database name and filename on each line.

data-ezsrc=http://server.digimetriq.com/wp-content/uploads/2020/12/1607451018_397_How-to-Install-VMWare-ESXi-on-a-Raspberry-Pi-Step.jpg />

Give your Pi raspberry within 30 days free
Sale: 25% discount today. Download the e-book.
Discover the secrets of the Raspberry Pipe in a 30 day challenge.

Recoveries

To restore the lost database, follow the same steps as for backing up the file.

Once the .sql file is back on the raspberry Pi, you can use this command to import the data into a new database:
mysql -uroot -p [PASSWORD] [FILE] < [SECURITY FILE].sql

The database must be empty to start the import.
Depending on your situation, you should use one of these two methods:

  • Import the backup into the database under a different name (and then copy only what you are interested in).
  • Or rename the damaged database, create it again (empty) and import the backup file.

As with making backups, you should test this process at least once.

Creating an SD memory card Figure

So in the previous paragraphs we have seen how we can save some files on the Raspberry Pi.
What if you have a complex facility that you want to book in full?

Fuse

The purpose of this backup method is to create an image that contains all your Pi’s Raspberry files.
You can then flash this image on another SD card to restore the raspberry ppi to the same state.

Linux

Under Linux you can use the dd command to create an image of the device:

  • Remove the SD card from the Pi of the raspberry
  • Insert the SD card into your computer
  • Find the name of the device:
    sudo fdisk -l
    This command displays all storage devices in your computer.
    You should see your hard drive and, somewhere at the bottom of the list, your SD card.
    Write down the name of the device (usually /dev/sdX or /dev/mmcblk0).
  • Create an image (replace device name):
    sudo dd bs=4M if=/dev/mmcblk0 or=backup.img

In a few minutes you have a full backup of the Raspberry Pi image.

This command should also work for macros, replace 4M with 4m in the last command.

Window

Under Windows, the best software for this purpose is the Win32 Disk Imager :

  • Download, install and run Win 32 Disk Imager.
  • You should get a window like this
    alt=win32 disk image width=482 height=342 data-ezsrc=http://server.digimetriq.com/wp-content/uploads/2021/01/1609823528_623_The-Ultimate-Guide-to-Backup-and-Restore-your-Raspberry-Pi.jpg data-ez= />
  • Enter the destination folder for the image and the file name (.img).
  • Select the letter of the SD card from the device list
  • Press the Play button
  • This tool creates an image in the selected folder

Recoveries

The easiest way to restore this backup is to flash the image on another SD card using Etcher :

  • Download Etcher on the official website
  • Installation and commissioning
  • Select a backup image on the left
  • Select the SD card
  • Press the flash button

You will receive a new SD card with the system in the state in which the backup was made.

Under Linux / Mac you can also use dd to make the SD card blink (inverted as and from):
sudo dd bs=4M if=backup.img or=dev/mmcblk0
If you have an empty SD card you can try this procedure to make sure it works in your case.

Clones of raspberries in action (pending)

The last road is the same as the previous one, but for a critical attitude.
If you can’t stop your Pi Raspberry from fully backing up an image, follow this procedure.

Condition

I’m going to share with you two ways to make a full backup of your Pi Framboise in action (using a desktop utility or from the command line).
In both cases, you must insert a second SD card into Pi Raspberry to copy the used SD card to the other.

If you don’t have one yet, I suggest you buy one of those cheap USB adapters from Amazon.
Prefer an adapter with a short cable (a long cable is not required, and the size of the USB stick may prevent access to other ports). So the one I’m connecting here is perfect for this.

PiClone (desktop version)

If your Raspberry Pi works with a desktop version, there is a tool called PiClone that you can use for this.
The name in the main menu is SD card copier.
You can find it in the Accessories submenu.

Using this software is simple:
alt=sd card copy raspbian width=474 height=238 data-ezsrc=http://server.digimetriq.com/wp-content/uploads/2021/01/The-Ultimate-Guide-to-Backup-and-Restore-your-Raspberry-Pi.png data-ez= />.

  • Select the SD card from the Copy from Device list.
  • Select the backup SD card from the Copy to Device list.
  • Press Start to start the transmission
  • Wait a few minutes and remove the backup SD card.

Feel free to try this backup SD card in another Pi Framboise to see if it works.

Scenario (light version)

If you want to use the Raspberry Pi OS Lite version or create a script, there is a shell script for that.

I will explain the basics of using this scenario.
But you can find all the information on the Github page if you need more.

  • To download the files, you need to install Git:
    sudo apt-get git
  • Start by downloading and installing the script:
    cd ~/Downloads
    git-clone https://github.com/billw2/rpi-clone.git
    cd rpi-clone
    sudo cp rpi-clone setup /usr/local/sbin
  • Then use fdisk to get the name of the backup SD card:
    sudo fdisk -l
    You should see the current SD card: sda or mmcblk0 usually.
  • And under the backup SD card: sdb or mmcblk1 normally
  • Finally, use rpi-clone:
    sudo rpi-clone sdb -v
    Replace sdb with the name of your backup SD card.

Completion

Here’s how.
You know how to secure your Raspberry Pi in different ways.

Try to program them, or if this is not possible, try to do it regularly.
Try to check them now and then.

There’s nothing worse than a backup that didn’t work or is unusable because you never tested it to see if it worked as expected.

raspberry pi os backup and restore,raspberry pi backup folder,linux raspberry pi backup,raspberry pi backup online,full backup of raspberry pi,how to backup raspberry pi image over network,raspberry pi system restore,raspberry pi nas/backup,backup raspberry pi sd card to cloud,raspberry pi clone,backing up your raspberry pi to a usb drive

You May Also Like

Google Pixel 2 Will Arrive this Spring in the Premium Segment

A 2017 Wall Street Journal study found that it is very, very…

How To Fix pps Closing By Themselves (Automatically)

Android phones are becoming more and more reliable. We’ve combined the best…

[pii_email_37f47c404649338129d6] Outlook error [ Fixed ] | 6 Different Ways

Should companies use Microsoft Outlook to send and receive email? Not only…

Chrome History By Date -Ways To Sort It

Learning to sort Chrome’s history by date is very important for those…