[TUTORIAL] How to backup and restore your entire system + data like a snapshot with raspBackup.sh

Hi,

First of all, thanks to he Yunohost team.
Yunohost is great and allows me to do great stuff and learn a lot.
Bravo !

(version française ici)

So, I’m using Yunohost for more than a year now with the following apps installed :

  • Nextcloud (and some of its app like calendar, the phone tracker, notes, contacts)
  • Wallabag
  • ttrss
  • Emails (that are pushed to my phone, that’s cool!)
  • Etherpad
  • Matrix-Synapse (in order to replace Whatsapp)
  • The Wifi Hotspot

But I also have some specific needs that are not provided by Yunohost and that I installed myself :

  • An OpenVpn client that supports the standard client.ovpn config file (I use it with the Wifi Hotspot to get a VPNised Wifi in the house)

  • A program/service that plays my favorites radios (the audio is being sent to the audio output of the raspberry and is streamed into my house with a small FM streamer)

  • A music server named MPD : Very nice program that you can control with a phone.

  • A small graphical menu in the command line style that is displays when I (or my wife) connect my phone with the ssh client so we can change the radio, reboot the raspberry, etc. in literally 3 seconds.

In order to do that, I kind of managed myself with the ressources I found online to write some very simple scripts, create some services, etc.
It doesn’t look much, but it still takes few couple of hours in extra to set it up entirely after the install of the Yunohost.

The backup system that is offered in Yunohost is good but somehow did not fit all of my requirements because :

  1. In case of a big crash that forces you to re-install everything (a corrupted SD card or the administrator that just screw up the system with some shitty commands :slight_smile: ), you first have to download the Yunohost image, flash it, update the system, etc. before you can restore a backup.

  2. It would not save any of my little scripts, services, extra Yunohost settings that take a few hours to install and set up / tune.

So I looked online and found raspiBackup

You can read the official page for all the details, but here is what I liked about it :

  • It backups the all system, absolutely everything : The data, the system itself, its settings… Well everything, exactly as if you do a dd image of the SD card.

  • You can choose between 3 or 4 types of backup, including rsync (there is dd and tar as well) which allows you to 1) realize incremental incremental backup in order to save some time and storage space, and 2) to restore any single file / directory very easily with one command (cp /backup/location/file /place/to/restore/file)

  • The backup and restore! of the whole system + data in one single command. And this is what interests me the most : I screw up my system with a wrong command, my SD card is corrupt, or anything else that forces me ro restore the entire system, well the script does it and restores your backup by flashing everything on a SD card in one single command ! Then, you just have to put the freshly flashed SD card into the raspepberry, to turn it on and the magic happens : The system is running exactly as it was when the backup was launched.

The cons :

  • Backups are not encrypted
  • In order to backup the entire system, the script needs to stop most of the services / programs running, so it makes your Yunohost unavailable during the backup

How does it work ?

**First of all : Backup an image of your SD card before installing the script, in case you fuck up your system or something.

The up to date documentation for the installation is here.
But to make it short :

  • You download and execute the installation script

  • choose the backup type (rsync, dd, or tar…)

  • set the location of the backup (external disc, nfs mount, etc.)

  • decide which services will be stopped / restart and most important in which order it’s gonna be stopped and restarted.
    To set the order properly, I performed a reboot and then went to check the syslog logs with a tail -n 500 /var/log/syslog and looked for the reboot logs.

It looks like this :

Jan 13 07:56:48 box systemd[1]: Starting A high performance web server and a reverse proxy server…
Jan 13 07:56:48 box systemd[1]: Starting Fail2Ban Service…
Jan 13 07:56:48 box systemd[1]: Starting YunoHost Firewall…
Jan 13 07:56:48 box systemd[1]: Starting Synapse Matrix homeserver…
Jan 13 07:56:48 box systemd[1]: Starting YunoHost VPN Client…
Jan 13 07:56:48 box systemd[1]: Started Etherpad-lite, the collaborative editor…
Jan 13 07:56:48 box systemd[1]: Starting The PHP 7.0 FastCGI Process Manager…
Jan 13 07:56:48 box systemd[1]: Starting coturn…
Jan 13 07:56:48 box systemd[1]: Started Music Player Daemon.
Jan 13 07:56:48 box systemd[1]: Starting MariaDB 10.1.38 database server…
Jan 13 07:56:48 box systemd[1]: Reached target Network is Online.
Jan 13 07:56:48 box systemd[1]: Mounting /mnt/podcasts…
Jan 13 07:56:48 box systemd[1]: Starting /etc/rc.local Compatibility…
Jan 13 07:56:48 box systemd[1]: Started Just plays FIP.
Jan 13 07:56:48 box systemd[1]: Started client OpenVPN.
Jan 13 07:56:48 box systemd[1]: Starting dnsmasq - A lightweight DHCP and caching DNS server…
Jan 13 07:56:48 box systemd[1]: Started YunoHost API Server.
Jan 13 07:56:48 box systemd[1]: Starting Advanced key-value store…
Jan 13 07:56:48 box systemd[1]: Started /etc/rc.local Compatibility.
Jan 13 07:56:48 box systemd[1]: Started OpenBSD Secure Shell server.
Jan 13 07:56:48 box systemd[1]: Stopped VNC Server in Service Mode daemon.
Jan 13 07:56:48 box systemd[1]: Started VNC Server in Service Mode daemon.

Then, according to these logs, I can set the proper order for the services to be stopped and restarted by the script (it’s the trickiest part of the script’s tuning).
Also, it has to be updated every time you install a new application on Yunohost (a new application = a new service)

Once everything is set, just launch the script with
sudo raspiBackup.sh

And that’s it.
The first backup is quite long because it needs to backup all your system + data. The next backup will be way shorter.

Once the first backup is done, restore it in order to check in details that everything works properly.

Once everything is set as you like, you can ask cron to perform a backup as your convenience (in my case once a night).

Also, I’m using 2 USB stick : One is permanently plugged on the yunohost and the other one stays safe at my job (and I switch the 2 keys every couple of days).

Fee free to comment or tell me what you think about it.

See you !

1 Like

Nice, thanks for sharing!

Thanks for sharing your method. It seems implementing our own backup systems (instead of the default Yunohost setup) is not uncommon. I wonder if this represents an opportunity to improve the backup capability of Yunohost??? I see you already found my thread. I’ll post a reply to your comment there…

V-