We present here a method for making a light backup and restoring a Raspberry Pi. Creating a backup, an image of your Raspberry Pi, is essential in the event of SD card corruption or power failure. On Raspberry Pi and other single-board computers, the SD card often fails. You therefore need to make a backup in the form of an image or iso file, so that you can upload this image to a new SD card and recover your configuration.
The problem is that saving an image includes the entire memory of the SD card. So you can end up with several files of 32, 64 or 128Gb.
Hardware
- Raspberry Pi
- Two SD cards
- Internet connection
OS backup method
There’s no other method quite like it. The method I suggest consists of several steps, but will allow you to have a light backup of the Raspberry Pi and be able to update your projects easily.
The method consists of the following points
- Recover OS information
- Recover hardware info (optional)
- Recover configuration files
- Recover installed packages
- Recover installed Python packages
The list is by no means complete, but with it you can get close to an exact copy of what you had before.
Cleaning your system
sudo apt-get --purge -y autoremove
You can also delete any software you’ve never used.
Create a backup folder
It can take any form you like. The important thing is that it is clear so that you can find your way around.
- RPiBckp
- version
- projects
- config
Recover OS information
Depending on your project, it’s important to know the OS version, as compatibility issues may arise depending on the packages used.
cat /etc/os-release cat /etc/os-release > /home/pi/RPiBckp/version/os-version.txt
Another important piece of information is the kernel version and hardware configuration.
uname -a > /home/pi/RPiBckp/version/sys-version.txt
ls -l /lib/modules
N.B.: uname -r gives you the specific Kernel version. You can find the installation file in the firmware list
Recover installed packages
For a list of installed packages, enter the command
dpkg --get-selections
If you want the list of packages with the version number
apt list --installed
N.B.: for all these commands you can use grep to filter your search.
It’s important to note that there are a number of packages installed automatically and others that you install according to your needs.
The list of automatically and manually installed packages can be obtained with the respective commands
apt-mark showauto # paquets automatique apt-mark showmanual # paquets manuel
For my part, I keep a file with all installed packages to keep track of all versions.
apt list --installed > /home/pi/RPiBckp/version/apt-list-installed.txt
a file with manually installed packages
apt list --installed | grep -v automatic > /home/pi/RPiBckp/version/apt-list-manual.txt
and I create the file with just the names to launch the package installation
apt-mark showmanual > /home/pi/RPiBckp/version/packages.txt
I can then install the right versions individually if they don’t suit me.
Recover installed Python packages
python3 -m pip freeze
python3 -m pip freeze > /home/pi/RPiBckp/version/requirements.txt
Recover configuration files
Some configuration files can be interesting to retrieve, in particular the wifi configuration.
sudo nano /etc/wpa_supplicant/wpa_supplicant.conf
cp /etc/wpa_supplicant/wpa_supplicant.conf /home/pi/RPiBckp/versión/wpa_supplicant.conf
Creating a Git repository
It might be a good idea to create a git repository for each of your projects, either for configuring a Raspberry Pi, or for a set of Python scripts. A git repository is ideal for keeping your projects up to date, particularly in terms of package and OS versions.
Once you’ve recovered all your interest files, it’s time to restore your project.
Installing a Raspbian version
You can use the Raspberry Pi Imager software
or download the desired image from the site http://downloads.raspberrypi.org/raspbian/images/
N.B.: OS versions that are too old may not be compatible with new hardware and may cause security problems.
Installing a Kernel version
You can check the Kernel version installed with the command
ls -l /lib/modules
To install a specific version of Raspberry Pi firmware, you can find the list of firmware versions by version number.
You can then copy the Git hash corresponding to the desired version (e.g. 5.10.63: 64132d67d3e083076661628203a02d27bf13203c).
And install it using the command:
sudo rpi-update <git_hash>
For example:
sudo rpi-update 2ef601a50b68eebeeb4dc3c6c525855961891be6
Warning: be careful which version you install. The Kernel version should only be modified by experienced people who have a compatibility problem that cannot be solved in any other way.
Copy your backup folder
On a fresh installation of Raspbian, you can copy your backup folder and
Install the required packages
It is possible to install a set of APT packages from a text file containing the list of files to be installed
xargs -a packages.txt sudo apt-get install -y
You can do the same to install Python packages.
python3 -m pip install -r requirements.txt
Copy configuration files
For the wifi configuration file or any other file, you can use the copy or move command in superuser mode (sudo).
sudo cp wpa_supplicant.conf /etc/wpa_supplicant/
Conclusion
This method doesn’t produce a perfect copy of your project, but it does allow you to reproduce and maintain your project with very little memory space, which can be a huge advantage. You’ll end up with a set of configuration files that will enable you to quickly restore a corrupted SD card.
If you have any ideas for important files to back up or other methods of restoring an SD card, please feel free to leave a comment.