{"id":1596,"date":"2024-02-14T12:40:08","date_gmt":"2024-02-14T11:40:08","guid":{"rendered":"https:\/\/mpr-projects.com\/?p=1596"},"modified":"2024-02-27T07:37:45","modified_gmt":"2024-02-27T06:37:45","slug":"rpi-nas-borg-backup","status":"publish","type":"post","link":"https:\/\/mpr-projects.com\/index.php\/2024\/02\/14\/rpi-nas-borg-backup\/","title":{"rendered":"RPi NAS: Borg Backup"},"content":{"rendered":"\n<p>In this post we&#8217;re going to use the software packages <a href=\"https:\/\/www.borgbackup.org\/\" target=\"_blank\" rel=\"noreferrer noopener\">Borg Backup<\/a> and <a href=\"https:\/\/torsion.org\/borgmatic\/\" target=\"_blank\" rel=\"noreferrer noopener\">Borgmatic<\/a> to do system backups of both our Raspberry Pi and our main computer. We&#8217;ll also schedule automatic backups using both cron and systemd.<\/p>\n\n\n<p>This post is part of a series about building a Network-Attached Storage (NAS) with redundancy using a Raspberry Pi (RPi). See <a href=\"https:\/\/mpr-projects.com\/index.php\/2023\/11\/13\/building-a-raspberry-pi-nas-with-data-redundancy-part-1-overview\/#RPi_NAS_Post_List\" data-type=\"post\" data-id=\"8\">here<\/a> for a list of all posts in this series.<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\" style=\"margin-top:var(--wp--preset--spacing--30);margin-bottom:var(--wp--preset--spacing--30)\"\/>\n\n\n\n<p>During the main part of this series we set up a network-attached storage solution (NAS) using a Raspberry Pi. The software we used to provide data redundancy, Greyhole, is not suitable for storing lots of small files like those in a system backup (because of Greyhole&#8217;s overheads, see <a href=\"https:\/\/mpr-projects.com\/index.php\/2023\/12\/13\/rpi-nas-extras-greyhole\/\" data-type=\"post\" data-id=\"1210\">here<\/a> for more information). So in this post we&#8217;ll set up a proper system backup.<\/p>\n\n\n\n<p>We&#8217;ll be using Borg Backup, which is available on Linux and Mac. Running it on Windows is only experimental. The advantage of such a backup software (vs just manually copying data) is that it works incrementally. So only files that have changed are saved during the backup, which makes the backup faster and require less space<sup data-fn=\"0b18ed8e-ef34-4ee6-a479-271bd1c1d3be\" class=\"fn\"><a href=\"#0b18ed8e-ef34-4ee6-a479-271bd1c1d3be\" id=\"0b18ed8e-ef34-4ee6-a479-271bd1c1d3be-link\">1<\/a><\/sup>. It also allows us to easily encrypt our backups and we can browse through them if we just need to restore a few files. Our actual interaction with Borg Backup will happen through a helper software called <a href=\"https:\/\/torsion.org\/borgmatic\/\" target=\"_blank\" rel=\"noreferrer noopener\">borgmatic<\/a>.<\/p>\n\n\n\n<p>Let&#8217;s get on with it. First we&#8217;ll do a simple setup on the Raspberry Pi that runs our NAS. Then we&#8217;ll set up a slightly more complex version on my main computer.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Borg on the Raspberry Pi<\/h3>\n\n\n\n<p>This is going to be a very simple backup. All we&#8217;re going to back up is our configuration files and scripts so we don&#8217;t have to redo the Samba and Greyhole setup if the SD card of the Raspberry Pi stops working. There&#8217;s a great description of the installation process of borgmatic <a href=\"https:\/\/torsion.org\/borgmatic\/docs\/how-to\/set-up-backups\/\" target=\"_blank\" rel=\"noreferrer noopener\">here<\/a>. Below I&#8217;ll go through the steps for the specific setup on the Raspberry Pi.<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">Basic Setup<\/h4>\n\n\n\n<p>First we need to install the software with<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>sudo apt install borgmatic<\/code><\/pre>\n\n\n\n<p>This command will automatically install all required software, such as Borg Backup. Next we create a sample configuration file. On the version currently (in Jan-24) available on Raspberry Pi OS we do that with the following command.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>sudo generate-borgmatic-config<\/code><\/pre>\n\n\n\n<p>This command generates the file <em>\/etc\/borgmatic\/config.yaml<\/em>. Newer versions of borgmatic require a slightly different command, which we&#8217;ll use in the next section. Let&#8217;s open the configuration file with<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>sudo vim \/etc\/borgmatic\/config.yaml<\/code><\/pre>\n\n\n\n<p>It&#8217;s quite a long file with lots of options for customization. For this simple backup we&#8217;re only using the following lines:<\/p>\n\n\n\n<figure data-wp-context=\"{&quot;imageId&quot;:&quot;69e795d2745f0&quot;}\" data-wp-interactive=\"core\/image\" data-wp-key=\"69e795d2745f0\" class=\"wp-block-image size-full wp-lightbox-container\"><img loading=\"lazy\" decoding=\"async\" width=\"908\" height=\"656\" data-wp-class--hide=\"state.isContentHidden\" data-wp-class--show=\"state.isContentVisible\" data-wp-init=\"callbacks.setButtonStyles\" data-wp-on--click=\"actions.showLightbox\" data-wp-on--load=\"callbacks.setButtonStyles\" data-wp-on-window--resize=\"callbacks.setButtonStyles\" src=\"https:\/\/mpr-projects.com\/wp-content\/uploads\/2024\/01\/Borg_RPi_config_1.png\" alt=\"\" class=\"wp-image-1601\" srcset=\"https:\/\/mpr-projects.com\/wp-content\/uploads\/2024\/01\/Borg_RPi_config_1.png 908w, https:\/\/mpr-projects.com\/wp-content\/uploads\/2024\/01\/Borg_RPi_config_1-300x217.png 300w, https:\/\/mpr-projects.com\/wp-content\/uploads\/2024\/01\/Borg_RPi_config_1-768x555.png 768w\" sizes=\"auto, (max-width: 908px) 100vw, 908px\" \/><button\n\t\t\tclass=\"lightbox-trigger\"\n\t\t\ttype=\"button\"\n\t\t\taria-haspopup=\"dialog\"\n\t\t\taria-label=\"Enlarge\"\n\t\t\tdata-wp-init=\"callbacks.initTriggerButton\"\n\t\t\tdata-wp-on--click=\"actions.showLightbox\"\n\t\t\tdata-wp-style--right=\"state.imageButtonRight\"\n\t\t\tdata-wp-style--top=\"state.imageButtonTop\"\n\t\t>\n\t\t\t<svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" width=\"12\" height=\"12\" fill=\"none\" viewBox=\"0 0 12 12\">\n\t\t\t\t<path fill=\"#fff\" d=\"M2 0a2 2 0 0 0-2 2v2h1.5V2a.5.5 0 0 1 .5-.5h2V0H2Zm2 10.5H2a.5.5 0 0 1-.5-.5V8H0v2a2 2 0 0 0 2 2h2v-1.5ZM8 12v-1.5h2a.5.5 0 0 0 .5-.5V8H12v2a2 2 0 0 1-2 2H8Zm2-12a2 2 0 0 1 2 2v2h-1.5V2a.5.5 0 0 0-.5-.5H8V0h2Z\" \/>\n\t\t\t<\/svg>\n\t\t<\/button><\/figure>\n\n\n\n<p>The <em>source_directories<\/em> section specifies the folders we want to back up<sup data-fn=\"c8587e72-3ecd-4215-8fb8-dcfe89c6ad79\" class=\"fn\"><a href=\"#c8587e72-3ecd-4215-8fb8-dcfe89c6ad79\" id=\"c8587e72-3ecd-4215-8fb8-dcfe89c6ad79-link\">2<\/a><\/sup>. Under <em>repositories<\/em> we can choose one or more locations where the backup should be saved (we can easily store them in a redundant way by adding additional locations). Section <em>exclude_patterns<\/em> allows us to define rules to skip certain files or folders<sup data-fn=\"57572f17-e236-4a11-b601-8839c0dc2340\" class=\"fn\"><a href=\"#57572f17-e236-4a11-b601-8839c0dc2340\" id=\"57572f17-e236-4a11-b601-8839c0dc2340-link\">3<\/a><\/sup>.<\/p>\n\n\n\n<p>Finally, it&#8217;s usually not necessary to keep all backups we&#8217;ve ever made. Typically, we care most about the latest backups (occasionally older backups can be useful, e.g. if we want to recover a specific, older file). To save some space we can specify how many daily, monthly, weekly and yearly backups should be kept. That&#8217;s done in section <em>retention<\/em>. Borg\/borgmatic will automatically remove backups that are in excess of what we specify there. I don&#8217;t expect the configuration files to change very often so I&#8217;m only keeping very few backups.<\/p>\n\n\n\n<p>After closing the file we can create the repository for our backups with<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>sudo borgmatic init --encryption none<\/code><\/pre>\n\n\n\n<p>You&#8217;ll notice that I&#8217;ve not set up encryption for this repository. That&#8217;s because I don&#8217;t think it&#8217;s necessary for this specific backup (there&#8217;s nothing private on there). In the next section, which backs up our main system, we <em>will<\/em> encrypt our backup.<\/p>\n\n\n\n<p>Now let&#8217;s create our initial backup:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>sudo borgmatic create --verbosity 1 --list --stats<\/code><\/pre>\n\n\n\n<p>On a larger system this command can take quite a while. On our Raspberry Pi, however, we&#8217;ve only specified a small number of folders to back up and there&#8217;s not much data in them so the initialization won&#8217;t take long.<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">Automatic Backups with Cron<\/h4>\n\n\n\n<p>Since we don&#8217;t want to run the command above manually (we&#8217;ll probably forget to do it + we&#8217;d have to log into the Raspberry Pi all the time) we automate them with cron. Borgmatic provides a <a href=\"https:\/\/torsion.org\/borgmatic\/docs\/how-to\/set-up-backups\/#cron\" target=\"_blank\" rel=\"noreferrer noopener\">cron script<\/a> which you can download but I prefer to add the line below to the crontab file<sup data-fn=\"741b5fa7-21a4-4803-9137-9581e8085235\" class=\"fn\"><a href=\"#741b5fa7-21a4-4803-9137-9581e8085235\" id=\"741b5fa7-21a4-4803-9137-9581e8085235-link\">4<\/a><\/sup>. Let&#8217;s open it with<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>sudo crontab -e<\/code><\/pre>\n\n\n\n<p>and add the following line<\/p>\n\n\n\n<figure data-wp-context=\"{&quot;imageId&quot;:&quot;69e795d274cf4&quot;}\" data-wp-interactive=\"core\/image\" data-wp-key=\"69e795d274cf4\" class=\"wp-block-image size-large wp-lightbox-container\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"53\" data-wp-class--hide=\"state.isContentHidden\" data-wp-class--show=\"state.isContentVisible\" data-wp-init=\"callbacks.setButtonStyles\" data-wp-on--click=\"actions.showLightbox\" data-wp-on--load=\"callbacks.setButtonStyles\" data-wp-on-window--resize=\"callbacks.setButtonStyles\" src=\"https:\/\/mpr-projects.com\/wp-content\/uploads\/2024\/01\/Borg_RPi_config_2-1024x53.png\" alt=\"\" class=\"wp-image-1600\" srcset=\"https:\/\/mpr-projects.com\/wp-content\/uploads\/2024\/01\/Borg_RPi_config_2-1024x53.png 1024w, https:\/\/mpr-projects.com\/wp-content\/uploads\/2024\/01\/Borg_RPi_config_2-300x15.png 300w, https:\/\/mpr-projects.com\/wp-content\/uploads\/2024\/01\/Borg_RPi_config_2-768x39.png 768w, https:\/\/mpr-projects.com\/wp-content\/uploads\/2024\/01\/Borg_RPi_config_2-1536x79.png 1536w, https:\/\/mpr-projects.com\/wp-content\/uploads\/2024\/01\/Borg_RPi_config_2.png 1556w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><button\n\t\t\tclass=\"lightbox-trigger\"\n\t\t\ttype=\"button\"\n\t\t\taria-haspopup=\"dialog\"\n\t\t\taria-label=\"Enlarge\"\n\t\t\tdata-wp-init=\"callbacks.initTriggerButton\"\n\t\t\tdata-wp-on--click=\"actions.showLightbox\"\n\t\t\tdata-wp-style--right=\"state.imageButtonRight\"\n\t\t\tdata-wp-style--top=\"state.imageButtonTop\"\n\t\t>\n\t\t\t<svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" width=\"12\" height=\"12\" fill=\"none\" viewBox=\"0 0 12 12\">\n\t\t\t\t<path fill=\"#fff\" d=\"M2 0a2 2 0 0 0-2 2v2h1.5V2a.5.5 0 0 1 .5-.5h2V0H2Zm2 10.5H2a.5.5 0 0 1-.5-.5V8H0v2a2 2 0 0 0 2 2h2v-1.5ZM8 12v-1.5h2a.5.5 0 0 0 .5-.5V8H12v2a2 2 0 0 1-2 2H8Zm2-12a2 2 0 0 1 2 2v2h-1.5V2a.5.5 0 0 0-.5-.5H8V0h2Z\" \/>\n\t\t\t<\/svg>\n\t\t<\/button><\/figure>\n\n\n\n<p>With this line there will be a backup every day at 3am and a summary will be written to the log-file saved in <code>nas_user<\/code>&#8216;s home directory (you could also write the log file to one of the NAS&#8217;s Samba shares so you don&#8217;t have to log into the RPi to view it).<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Backing up to a Remote System<\/h3>\n\n\n\n<p>Now that we&#8217;ve done a simple setup for our Raspberry Pi let&#8217;s look at something a bit more complex. I want my main computer to do a backup every 3 hours. Again the data will be saved on the storage drives of the NAS (but you could use any local or remote repository).<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">Basic Setup<\/h4>\n\n\n\n<p>First we need to install Borg. If you&#8217;re on a system that uses <em>apt<\/em> then you can run the same command as in the previous section. I use Arch Linux on my main system so the following command installs Borg and borgmatic (and all other requirements):<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>sudo pacman -S borgmatic<\/code><\/pre>\n\n\n\n<p>The backup command will be executed by the root user<sup data-fn=\"d0b0cc80-a858-4ab4-b704-c1bca5e1cec2\" class=\"fn\"><a href=\"#d0b0cc80-a858-4ab4-b704-c1bca5e1cec2\" id=\"d0b0cc80-a858-4ab4-b704-c1bca5e1cec2-link\">5<\/a><\/sup> and it will run over ssh, so we have to make sure that the root user can access the Raspberry Pi via ssh. First we create a separate ssh key for our root user and save it under <em>\/root\/.ssh\/key_xps17_root_user<\/em> (using <code>sudo ssh-keygen<\/code>). Then we create or update the file <em>\/root\/.ssh\/config<\/em> with the following contents:<\/p>\n\n\n\n<figure data-wp-context=\"{&quot;imageId&quot;:&quot;69e795d275277&quot;}\" data-wp-interactive=\"core\/image\" data-wp-key=\"69e795d275277\" class=\"wp-block-image size-large wp-lightbox-container\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"141\" data-wp-class--hide=\"state.isContentHidden\" data-wp-class--show=\"state.isContentVisible\" data-wp-init=\"callbacks.setButtonStyles\" data-wp-on--click=\"actions.showLightbox\" data-wp-on--load=\"callbacks.setButtonStyles\" data-wp-on-window--resize=\"callbacks.setButtonStyles\" src=\"https:\/\/mpr-projects.com\/wp-content\/uploads\/2024\/01\/Borg_XPS_config_0-1024x141.png\" alt=\"\" class=\"wp-image-1634\" srcset=\"https:\/\/mpr-projects.com\/wp-content\/uploads\/2024\/01\/Borg_XPS_config_0-1024x141.png 1024w, https:\/\/mpr-projects.com\/wp-content\/uploads\/2024\/01\/Borg_XPS_config_0-300x41.png 300w, https:\/\/mpr-projects.com\/wp-content\/uploads\/2024\/01\/Borg_XPS_config_0-768x106.png 768w, https:\/\/mpr-projects.com\/wp-content\/uploads\/2024\/01\/Borg_XPS_config_0.png 1192w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><button\n\t\t\tclass=\"lightbox-trigger\"\n\t\t\ttype=\"button\"\n\t\t\taria-haspopup=\"dialog\"\n\t\t\taria-label=\"Enlarge\"\n\t\t\tdata-wp-init=\"callbacks.initTriggerButton\"\n\t\t\tdata-wp-on--click=\"actions.showLightbox\"\n\t\t\tdata-wp-style--right=\"state.imageButtonRight\"\n\t\t\tdata-wp-style--top=\"state.imageButtonTop\"\n\t\t>\n\t\t\t<svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" width=\"12\" height=\"12\" fill=\"none\" viewBox=\"0 0 12 12\">\n\t\t\t\t<path fill=\"#fff\" d=\"M2 0a2 2 0 0 0-2 2v2h1.5V2a.5.5 0 0 1 .5-.5h2V0H2Zm2 10.5H2a.5.5 0 0 1-.5-.5V8H0v2a2 2 0 0 0 2 2h2v-1.5ZM8 12v-1.5h2a.5.5 0 0 0 .5-.5V8H12v2a2 2 0 0 1-2 2H8Zm2-12a2 2 0 0 1 2 2v2h-1.5V2a.5.5 0 0 0-.5-.5H8V0h2Z\" \/>\n\t\t\t<\/svg>\n\t\t<\/button><\/figure>\n\n\n\n<p>Now we copy the public key we&#8217;ve just created into the <em>authorized_keys<\/em> file on the Raspberry Pi. With this setup our root user will be able to ssh into the Raspberry Pi without being asked for a password (as long as you didn&#8217;t set a password during the generation of the ssh key).<\/p>\n\n\n\n<p>You could also reuse the ssh key of your regular user<sup data-fn=\"e037fd8f-0503-40b3-99c8-03d638a67b9e\" class=\"fn\"><a href=\"#e037fd8f-0503-40b3-99c8-03d638a67b9e\" id=\"e037fd8f-0503-40b3-99c8-03d638a67b9e-link\">6<\/a><\/sup>. Whichever key you use, make sure that it&#8217;s listed in the <em>authorized_keys<\/em> file on the Raspberry Pi (<em>\/home\/nas_user\/.ssh\/authorized_keys<\/em>) so root can connect<sup data-fn=\"064410fc-5917-401f-8a20-a45847c9e638\" class=\"fn\"><a href=\"#064410fc-5917-401f-8a20-a45847c9e638\" id=\"064410fc-5917-401f-8a20-a45847c9e638-link\">7<\/a><\/sup>.<\/p>\n\n\n\n<p>Next we generate a configuration file. On my Arch System the latest version of borgmatic is available. With this version the command to generate a config file is:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>sudo borgmatic config generate<\/code><\/pre>\n\n\n\n<p>The configuration file will be saved in <em>\/etc\/borgmatic\/config.yaml<\/em> <sup data-fn=\"84d78769-162f-4cd0-9144-4ccac757c005\" class=\"fn\"><a href=\"#84d78769-162f-4cd0-9144-4ccac757c005\" id=\"84d78769-162f-4cd0-9144-4ccac757c005-link\">8<\/a><\/sup>. We&#8217;ll start with a simple file and then we&#8217;ll add a few useful features to it.<\/p>\n\n\n\n<figure data-wp-context=\"{&quot;imageId&quot;:&quot;69e795d27577a&quot;}\" data-wp-interactive=\"core\/image\" data-wp-key=\"69e795d27577a\" class=\"wp-block-image size-large wp-lightbox-container\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"415\" data-wp-class--hide=\"state.isContentHidden\" data-wp-class--show=\"state.isContentVisible\" data-wp-init=\"callbacks.setButtonStyles\" data-wp-on--click=\"actions.showLightbox\" data-wp-on--load=\"callbacks.setButtonStyles\" data-wp-on-window--resize=\"callbacks.setButtonStyles\" src=\"https:\/\/mpr-projects.com\/wp-content\/uploads\/2024\/01\/Borg_XPS_config_1-1024x415.png\" alt=\"\" class=\"wp-image-1610\" srcset=\"https:\/\/mpr-projects.com\/wp-content\/uploads\/2024\/01\/Borg_XPS_config_1-1024x415.png 1024w, https:\/\/mpr-projects.com\/wp-content\/uploads\/2024\/01\/Borg_XPS_config_1-300x122.png 300w, https:\/\/mpr-projects.com\/wp-content\/uploads\/2024\/01\/Borg_XPS_config_1-768x311.png 768w, https:\/\/mpr-projects.com\/wp-content\/uploads\/2024\/01\/Borg_XPS_config_1.png 1514w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><button\n\t\t\tclass=\"lightbox-trigger\"\n\t\t\ttype=\"button\"\n\t\t\taria-haspopup=\"dialog\"\n\t\t\taria-label=\"Enlarge\"\n\t\t\tdata-wp-init=\"callbacks.initTriggerButton\"\n\t\t\tdata-wp-on--click=\"actions.showLightbox\"\n\t\t\tdata-wp-style--right=\"state.imageButtonRight\"\n\t\t\tdata-wp-style--top=\"state.imageButtonTop\"\n\t\t>\n\t\t\t<svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" width=\"12\" height=\"12\" fill=\"none\" viewBox=\"0 0 12 12\">\n\t\t\t\t<path fill=\"#fff\" d=\"M2 0a2 2 0 0 0-2 2v2h1.5V2a.5.5 0 0 1 .5-.5h2V0H2Zm2 10.5H2a.5.5 0 0 1-.5-.5V8H0v2a2 2 0 0 0 2 2h2v-1.5ZM8 12v-1.5h2a.5.5 0 0 0 .5-.5V8H12v2a2 2 0 0 1-2 2H8Zm2-12a2 2 0 0 1 2 2v2h-1.5V2a.5.5 0 0 0-.5-.5H8V0h2Z\" \/>\n\t\t\t<\/svg>\n\t\t<\/button><\/figure>\n\n\n\n<p>You&#8217;ll notice a few changes to the previous section. First, the repository is now remote and we connect to it via ssh. Second, we&#8217;ve added an <em>encryption_passphrase<\/em>. As a result our backups will be encrypted and can only be accessed with the password (and the key but that&#8217;s stored with the repository). <\/p>\n\n\n\n<p>Make sure to also save the encryption password in another, secure location! Otherwise, if your computer breaks and you don&#8217;t have access to the configuration file, you won&#8217;t be able to decrypt your backups! Borg\/borgmatic also recommend saving a copy of the key<sup data-fn=\"9d604407-149f-4bd0-b23f-93fe0e0179e2\" class=\"fn\"><a href=\"#9d604407-149f-4bd0-b23f-93fe0e0179e2\" id=\"9d604407-149f-4bd0-b23f-93fe0e0179e2-link\">9<\/a><\/sup>.<\/p>\n\n\n\n<p>After you&#8217;ve adjusted the configuration file to your needs we can create the repository with:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>sudo borgmatic rcreate --encryption repokey<\/code><\/pre>\n\n\n\n<p>Then we export the key of the repository to the current directory so we can securely save it somewhere.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>sudo borg key export ssh:\/\/nas_user@192.168.50.210\/nas_mounts\/hdd0\/borg_backups\/demo .\/borg_demo_key_backup<\/code><\/pre>\n\n\n\n<p>Before we do our first backup let&#8217;s add <em>exclude_patterns<\/em>, which allow us to ignore certain files or folders:<\/p>\n\n\n\n<figure data-wp-context=\"{&quot;imageId&quot;:&quot;69e795d275cb7&quot;}\" data-wp-interactive=\"core\/image\" data-wp-key=\"69e795d275cb7\" class=\"wp-block-image aligncenter size-medium wp-lightbox-container\"><img loading=\"lazy\" decoding=\"async\" width=\"300\" height=\"150\" data-wp-class--hide=\"state.isContentHidden\" data-wp-class--show=\"state.isContentVisible\" data-wp-init=\"callbacks.setButtonStyles\" data-wp-on--click=\"actions.showLightbox\" data-wp-on--load=\"callbacks.setButtonStyles\" data-wp-on-window--resize=\"callbacks.setButtonStyles\" src=\"https:\/\/mpr-projects.com\/wp-content\/uploads\/2024\/01\/Borg_XPS_config_2-300x150.png\" alt=\"\" class=\"wp-image-1614\" srcset=\"https:\/\/mpr-projects.com\/wp-content\/uploads\/2024\/01\/Borg_XPS_config_2-300x150.png 300w, https:\/\/mpr-projects.com\/wp-content\/uploads\/2024\/01\/Borg_XPS_config_2.png 472w\" sizes=\"auto, (max-width: 300px) 100vw, 300px\" \/><button\n\t\t\tclass=\"lightbox-trigger\"\n\t\t\ttype=\"button\"\n\t\t\taria-haspopup=\"dialog\"\n\t\t\taria-label=\"Enlarge\"\n\t\t\tdata-wp-init=\"callbacks.initTriggerButton\"\n\t\t\tdata-wp-on--click=\"actions.showLightbox\"\n\t\t\tdata-wp-style--right=\"state.imageButtonRight\"\n\t\t\tdata-wp-style--top=\"state.imageButtonTop\"\n\t\t>\n\t\t\t<svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" width=\"12\" height=\"12\" fill=\"none\" viewBox=\"0 0 12 12\">\n\t\t\t\t<path fill=\"#fff\" d=\"M2 0a2 2 0 0 0-2 2v2h1.5V2a.5.5 0 0 1 .5-.5h2V0H2Zm2 10.5H2a.5.5 0 0 1-.5-.5V8H0v2a2 2 0 0 0 2 2h2v-1.5ZM8 12v-1.5h2a.5.5 0 0 0 .5-.5V8H12v2a2 2 0 0 1-2 2H8Zm2-12a2 2 0 0 1 2 2v2h-1.5V2a.5.5 0 0 0-.5-.5H8V0h2Z\" \/>\n\t\t\t<\/svg>\n\t\t<\/button><\/figure>\n\n\n\n<p>We also enable the following options<\/p>\n\n\n\n<figure data-wp-context=\"{&quot;imageId&quot;:&quot;69e795d2760af&quot;}\" data-wp-interactive=\"core\/image\" data-wp-key=\"69e795d2760af\" class=\"wp-block-image size-full wp-lightbox-container\"><img loading=\"lazy\" decoding=\"async\" width=\"1052\" height=\"294\" data-wp-class--hide=\"state.isContentHidden\" data-wp-class--show=\"state.isContentVisible\" data-wp-init=\"callbacks.setButtonStyles\" data-wp-on--click=\"actions.showLightbox\" data-wp-on--load=\"callbacks.setButtonStyles\" data-wp-on-window--resize=\"callbacks.setButtonStyles\" src=\"https:\/\/mpr-projects.com\/wp-content\/uploads\/2024\/01\/Borg_XPS_config_3.png\" alt=\"\" class=\"wp-image-1616\" srcset=\"https:\/\/mpr-projects.com\/wp-content\/uploads\/2024\/01\/Borg_XPS_config_3.png 1052w, https:\/\/mpr-projects.com\/wp-content\/uploads\/2024\/01\/Borg_XPS_config_3-300x84.png 300w, https:\/\/mpr-projects.com\/wp-content\/uploads\/2024\/01\/Borg_XPS_config_3-1024x286.png 1024w, https:\/\/mpr-projects.com\/wp-content\/uploads\/2024\/01\/Borg_XPS_config_3-768x215.png 768w\" sizes=\"auto, (max-width: 1052px) 100vw, 1052px\" \/><button\n\t\t\tclass=\"lightbox-trigger\"\n\t\t\ttype=\"button\"\n\t\t\taria-haspopup=\"dialog\"\n\t\t\taria-label=\"Enlarge\"\n\t\t\tdata-wp-init=\"callbacks.initTriggerButton\"\n\t\t\tdata-wp-on--click=\"actions.showLightbox\"\n\t\t\tdata-wp-style--right=\"state.imageButtonRight\"\n\t\t\tdata-wp-style--top=\"state.imageButtonTop\"\n\t\t>\n\t\t\t<svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" width=\"12\" height=\"12\" fill=\"none\" viewBox=\"0 0 12 12\">\n\t\t\t\t<path fill=\"#fff\" d=\"M2 0a2 2 0 0 0-2 2v2h1.5V2a.5.5 0 0 1 .5-.5h2V0H2Zm2 10.5H2a.5.5 0 0 1-.5-.5V8H0v2a2 2 0 0 0 2 2h2v-1.5ZM8 12v-1.5h2a.5.5 0 0 0 .5-.5V8H12v2a2 2 0 0 1-2 2H8Zm2-12a2 2 0 0 1 2 2v2h-1.5V2a.5.5 0 0 0-.5-.5H8V0h2Z\" \/>\n\t\t\t<\/svg>\n\t\t<\/button><\/figure>\n\n\n\n<p>which allow us to manually exclude certain directories from being backed up (that can be quite useful in situations where some of your custom code creates a lot of temporary data).<\/p>\n\n\n\n<p>When I set up my repository I started with a small number of directories and gradually expanded them (so I could make sure that no large, unnecessary files were saved). So let&#8217;s now create an initial backup with the command below (depending on your source directories, this may take a while):<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>sudo borgmatic create --verbosity 1 --list --stats<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\">Additional Features<\/h4>\n\n\n\n<p>There are a few more features I&#8217;d like to have. First, I only want the backup to run when the remote server can be reached (e.g. there&#8217;s no point in attempting a backup if you&#8217;re on a laptop and away from home). We can achieve that by ping&#8217;ing the Raspberry Pi&#8217;s IP address and checking for a response.<\/p>\n\n\n\n<figure data-wp-context=\"{&quot;imageId&quot;:&quot;69e795d2765c8&quot;}\" data-wp-interactive=\"core\/image\" data-wp-key=\"69e795d2765c8\" class=\"wp-block-image aligncenter size-medium is-resized wp-lightbox-container\"><img loading=\"lazy\" decoding=\"async\" width=\"300\" height=\"33\" data-wp-class--hide=\"state.isContentHidden\" data-wp-class--show=\"state.isContentVisible\" data-wp-init=\"callbacks.setButtonStyles\" data-wp-on--click=\"actions.showLightbox\" data-wp-on--load=\"callbacks.setButtonStyles\" data-wp-on-window--resize=\"callbacks.setButtonStyles\" src=\"https:\/\/mpr-projects.com\/wp-content\/uploads\/2024\/01\/Borg_XPS_config_4-300x33.png\" alt=\"\" class=\"wp-image-1636\" style=\"width:500px\" srcset=\"https:\/\/mpr-projects.com\/wp-content\/uploads\/2024\/01\/Borg_XPS_config_4-300x33.png 300w, https:\/\/mpr-projects.com\/wp-content\/uploads\/2024\/01\/Borg_XPS_config_4-1024x112.png 1024w, https:\/\/mpr-projects.com\/wp-content\/uploads\/2024\/01\/Borg_XPS_config_4-768x84.png 768w, https:\/\/mpr-projects.com\/wp-content\/uploads\/2024\/01\/Borg_XPS_config_4.png 1188w\" sizes=\"auto, (max-width: 300px) 100vw, 300px\" \/><button\n\t\t\tclass=\"lightbox-trigger\"\n\t\t\ttype=\"button\"\n\t\t\taria-haspopup=\"dialog\"\n\t\t\taria-label=\"Enlarge\"\n\t\t\tdata-wp-init=\"callbacks.initTriggerButton\"\n\t\t\tdata-wp-on--click=\"actions.showLightbox\"\n\t\t\tdata-wp-style--right=\"state.imageButtonRight\"\n\t\t\tdata-wp-style--top=\"state.imageButtonTop\"\n\t\t>\n\t\t\t<svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" width=\"12\" height=\"12\" fill=\"none\" viewBox=\"0 0 12 12\">\n\t\t\t\t<path fill=\"#fff\" d=\"M2 0a2 2 0 0 0-2 2v2h1.5V2a.5.5 0 0 1 .5-.5h2V0H2Zm2 10.5H2a.5.5 0 0 1-.5-.5V8H0v2a2 2 0 0 0 2 2h2v-1.5ZM8 12v-1.5h2a.5.5 0 0 0 .5-.5V8H12v2a2 2 0 0 1-2 2H8Zm2-12a2 2 0 0 1 2 2v2h-1.5V2a.5.5 0 0 0-.5-.5H8V0h2Z\" \/>\n\t\t\t<\/svg>\n\t\t<\/button><\/figure>\n\n\n\n<p>Next I want to be informed when a backup is taking place. For that we&#8217;ll use <a href=\"https:\/\/wiki.archlinux.org\/title\/Desktop_notifications\" target=\"_blank\" rel=\"noreferrer noopener\">Desktop Notifications<\/a>. If you&#8217;re on a system like Ubuntu then you should already have a notification daemon installed. Since Arch Linux is quite bare bones<sup data-fn=\"a5c5e524-c096-4cf2-a1ec-6c320497371c\" class=\"fn\"><a href=\"#a5c5e524-c096-4cf2-a1ec-6c320497371c\" id=\"a5c5e524-c096-4cf2-a1ec-6c320497371c-link\">10<\/a><\/sup> I had to set it up myself (I use <a href=\"https:\/\/github.com\/emersion\/mako\" target=\"_blank\" rel=\"noreferrer noopener\">mako<\/a> which works on Wayland\/Sway). You can test if you have a working daemon by sending a test message:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>notify-send -t 0 \"hi there\"<\/code><\/pre>\n\n\n\n<p>With the option <em>-t 0<\/em> your message will not automatically disappear after a few seconds but stay on screen until the user clicks on it.<\/p>\n\n\n\n<p>This works well if the message is sent and received by the same user. In our case the message will be sent by the root user (who executes the backup) and received by our regular user. That makes it a bit more difficult because the correct session bus needs to be used when sending the message. I wrote the helper script below, which works well on Wayland<sup data-fn=\"bd31459e-310a-4c01-af6d-45f0201e671f\" class=\"fn\"><a href=\"#bd31459e-310a-4c01-af6d-45f0201e671f\" id=\"bd31459e-310a-4c01-af6d-45f0201e671f-link\">11<\/a><\/sup>. The script is saved under <em>\/root\/notify_send_to_user<\/em> (the file is owned by root which has execute permissions on it).<\/p>\n\n\n\n<figure data-wp-context=\"{&quot;imageId&quot;:&quot;69e795d276a64&quot;}\" data-wp-interactive=\"core\/image\" data-wp-key=\"69e795d276a64\" class=\"wp-block-image size-large wp-lightbox-container\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"72\" data-wp-class--hide=\"state.isContentHidden\" data-wp-class--show=\"state.isContentVisible\" data-wp-init=\"callbacks.setButtonStyles\" data-wp-on--click=\"actions.showLightbox\" data-wp-on--load=\"callbacks.setButtonStyles\" data-wp-on-window--resize=\"callbacks.setButtonStyles\" src=\"https:\/\/mpr-projects.com\/wp-content\/uploads\/2024\/01\/Borg_XPS_config_5-1024x72.png\" alt=\"\" class=\"wp-image-1619\" srcset=\"https:\/\/mpr-projects.com\/wp-content\/uploads\/2024\/01\/Borg_XPS_config_5-1024x72.png 1024w, https:\/\/mpr-projects.com\/wp-content\/uploads\/2024\/01\/Borg_XPS_config_5-300x21.png 300w, https:\/\/mpr-projects.com\/wp-content\/uploads\/2024\/01\/Borg_XPS_config_5-768x54.png 768w, https:\/\/mpr-projects.com\/wp-content\/uploads\/2024\/01\/Borg_XPS_config_5-1536x108.png 1536w, https:\/\/mpr-projects.com\/wp-content\/uploads\/2024\/01\/Borg_XPS_config_5-2048x145.png 2048w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><button\n\t\t\tclass=\"lightbox-trigger\"\n\t\t\ttype=\"button\"\n\t\t\taria-haspopup=\"dialog\"\n\t\t\taria-label=\"Enlarge\"\n\t\t\tdata-wp-init=\"callbacks.initTriggerButton\"\n\t\t\tdata-wp-on--click=\"actions.showLightbox\"\n\t\t\tdata-wp-style--right=\"state.imageButtonRight\"\n\t\t\tdata-wp-style--top=\"state.imageButtonTop\"\n\t\t>\n\t\t\t<svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" width=\"12\" height=\"12\" fill=\"none\" viewBox=\"0 0 12 12\">\n\t\t\t\t<path fill=\"#fff\" d=\"M2 0a2 2 0 0 0-2 2v2h1.5V2a.5.5 0 0 1 .5-.5h2V0H2Zm2 10.5H2a.5.5 0 0 1-.5-.5V8H0v2a2 2 0 0 0 2 2h2v-1.5ZM8 12v-1.5h2a.5.5 0 0 0 .5-.5V8H12v2a2 2 0 0 1-2 2H8Zm2-12a2 2 0 0 1 2 2v2h-1.5V2a.5.5 0 0 0-.5-.5H8V0h2Z\" \/>\n\t\t\t<\/svg>\n\t\t<\/button><\/figure>\n\n\n\n<p>If the root user wants to send a non-disappearing message to user <em>mpr<\/em> then it would run<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\/root\/notify_send_to_user mpr 0 \"message\"<\/code><\/pre>\n\n\n\n<p>You can test if it works for you by temporarily becoming root with <code>sudo su<\/code> (or using <em>sudo<\/em>) and running the command above. A notification should pop up on your desktop.<\/p>\n\n\n\n<p>Now we adjust borgmatic&#8217;s configuration file so it informs us about the update process (replace the previous <em>before_everything<\/em> section with the one below):<\/p>\n\n\n\n<figure data-wp-context=\"{&quot;imageId&quot;:&quot;69e795d276ee3&quot;}\" data-wp-interactive=\"core\/image\" data-wp-key=\"69e795d276ee3\" class=\"wp-block-image size-large wp-lightbox-container\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"111\" data-wp-class--hide=\"state.isContentHidden\" data-wp-class--show=\"state.isContentVisible\" data-wp-init=\"callbacks.setButtonStyles\" data-wp-on--click=\"actions.showLightbox\" data-wp-on--load=\"callbacks.setButtonStyles\" data-wp-on-window--resize=\"callbacks.setButtonStyles\" src=\"https:\/\/mpr-projects.com\/wp-content\/uploads\/2024\/01\/Borg_XPS_config_6-1024x111.png\" alt=\"\" class=\"wp-image-1630\" srcset=\"https:\/\/mpr-projects.com\/wp-content\/uploads\/2024\/01\/Borg_XPS_config_6-1024x111.png 1024w, https:\/\/mpr-projects.com\/wp-content\/uploads\/2024\/01\/Borg_XPS_config_6-300x33.png 300w, https:\/\/mpr-projects.com\/wp-content\/uploads\/2024\/01\/Borg_XPS_config_6-768x83.png 768w, https:\/\/mpr-projects.com\/wp-content\/uploads\/2024\/01\/Borg_XPS_config_6-1536x167.png 1536w, https:\/\/mpr-projects.com\/wp-content\/uploads\/2024\/01\/Borg_XPS_config_6-2048x222.png 2048w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><button\n\t\t\tclass=\"lightbox-trigger\"\n\t\t\ttype=\"button\"\n\t\t\taria-haspopup=\"dialog\"\n\t\t\taria-label=\"Enlarge\"\n\t\t\tdata-wp-init=\"callbacks.initTriggerButton\"\n\t\t\tdata-wp-on--click=\"actions.showLightbox\"\n\t\t\tdata-wp-style--right=\"state.imageButtonRight\"\n\t\t\tdata-wp-style--top=\"state.imageButtonTop\"\n\t\t>\n\t\t\t<svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" width=\"12\" height=\"12\" fill=\"none\" viewBox=\"0 0 12 12\">\n\t\t\t\t<path fill=\"#fff\" d=\"M2 0a2 2 0 0 0-2 2v2h1.5V2a.5.5 0 0 1 .5-.5h2V0H2Zm2 10.5H2a.5.5 0 0 1-.5-.5V8H0v2a2 2 0 0 0 2 2h2v-1.5ZM8 12v-1.5h2a.5.5 0 0 0 .5-.5V8H12v2a2 2 0 0 1-2 2H8Zm2-12a2 2 0 0 1 2 2v2h-1.5V2a.5.5 0 0 0-.5-.5H8V0h2Z\" \/>\n\t\t\t<\/svg>\n\t\t<\/button><\/figure>\n\n\n\n<p>This will send a message when the backup starts and when it has finished. If the RPi couldn&#8217;t be reached then we get a notification and the backup stops.<\/p>\n\n\n\n<p>Finally, I have multiple WiFi networks at home and sometimes I&#8217;m not connected to the one with the Raspberry Pi. In that case the backup fails and I get a notification saying <em>Couldn&#8217;t connect to rpi-nas.<\/em> The options below tell borgmatic to try again three minutes later, which gives me some time to connect to the correct WiFi.<\/p>\n\n\n\n<figure data-wp-context=\"{&quot;imageId&quot;:&quot;69e795d277287&quot;}\" data-wp-interactive=\"core\/image\" data-wp-key=\"69e795d277287\" class=\"wp-block-image size-large wp-lightbox-container\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"245\" data-wp-class--hide=\"state.isContentHidden\" data-wp-class--show=\"state.isContentVisible\" data-wp-init=\"callbacks.setButtonStyles\" data-wp-on--click=\"actions.showLightbox\" data-wp-on--load=\"callbacks.setButtonStyles\" data-wp-on-window--resize=\"callbacks.setButtonStyles\" src=\"https:\/\/mpr-projects.com\/wp-content\/uploads\/2024\/01\/Borg_XPS_config_7-1024x245.png\" alt=\"\" class=\"wp-image-1622\" srcset=\"https:\/\/mpr-projects.com\/wp-content\/uploads\/2024\/01\/Borg_XPS_config_7-1024x245.png 1024w, https:\/\/mpr-projects.com\/wp-content\/uploads\/2024\/01\/Borg_XPS_config_7-300x72.png 300w, https:\/\/mpr-projects.com\/wp-content\/uploads\/2024\/01\/Borg_XPS_config_7-768x184.png 768w, https:\/\/mpr-projects.com\/wp-content\/uploads\/2024\/01\/Borg_XPS_config_7-1536x368.png 1536w, https:\/\/mpr-projects.com\/wp-content\/uploads\/2024\/01\/Borg_XPS_config_7.png 1562w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><button\n\t\t\tclass=\"lightbox-trigger\"\n\t\t\ttype=\"button\"\n\t\t\taria-haspopup=\"dialog\"\n\t\t\taria-label=\"Enlarge\"\n\t\t\tdata-wp-init=\"callbacks.initTriggerButton\"\n\t\t\tdata-wp-on--click=\"actions.showLightbox\"\n\t\t\tdata-wp-style--right=\"state.imageButtonRight\"\n\t\t\tdata-wp-style--top=\"state.imageButtonTop\"\n\t\t>\n\t\t\t<svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" width=\"12\" height=\"12\" fill=\"none\" viewBox=\"0 0 12 12\">\n\t\t\t\t<path fill=\"#fff\" d=\"M2 0a2 2 0 0 0-2 2v2h1.5V2a.5.5 0 0 1 .5-.5h2V0H2Zm2 10.5H2a.5.5 0 0 1-.5-.5V8H0v2a2 2 0 0 0 2 2h2v-1.5ZM8 12v-1.5h2a.5.5 0 0 0 .5-.5V8H12v2a2 2 0 0 1-2 2H8Zm2-12a2 2 0 0 1 2 2v2h-1.5V2a.5.5 0 0 0-.5-.5H8V0h2Z\" \/>\n\t\t\t<\/svg>\n\t\t<\/button><\/figure>\n\n\n\n<h4 class=\"wp-block-heading\">Automatic Backups with Systemd<\/h4>\n\n\n\n<p>In the previous section we used cron to automate backups. In this section we&#8217;ll use systemd. Borgmatic already <a href=\"https:\/\/torsion.org\/borgmatic\/docs\/how-to\/set-up-backups\/#systemd\" target=\"_blank\" rel=\"noreferrer noopener\">provides us<\/a> with the two files we need:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><a href=\"https:\/\/projects.torsion.org\/borgmatic-collective\/borgmatic\/raw\/branch\/main\/sample\/systemd\/borgmatic.service\" target=\"_blank\" rel=\"noreferrer noopener\">borgmatic.service<\/a><\/li>\n\n\n\n<li><a href=\"https:\/\/projects.torsion.org\/borgmatic-collective\/borgmatic\/raw\/branch\/main\/sample\/systemd\/borgmatic.timer\" target=\"_blank\" rel=\"noreferrer noopener\">borgmatic.timer<\/a><\/li>\n<\/ul>\n\n\n\n<p>The first file describes the actual backup process. The second file allows us to specify when the service should be run. Download and move them to <em>\/etc\/systemd\/system<\/em>.<\/p>\n\n\n\n<p>If you set up notifications as described above then you&#8217;ll have to comment out the following line in <em>borgmatic.service<\/em><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code># May interfere with running external programs within borgmatic hooks.\n# CapabilityBoundingSet=CAP_DAC_READ_SEARCH CAP_NET_RAW<\/code><\/pre>\n\n\n\n<p>because it does interfere with our notification-sending script. You can try if the backup process works by running the service with<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>sudo systemctl start borgmatic.service<\/code><\/pre>\n\n\n\n<p>Then we&#8217;ll edit the file <em>borgmatic.timer<\/em>. I want a backup to happen every three hours so I use the following settings<sup data-fn=\"9ed81526-0af8-4efa-b187-bb619195a3f9\" class=\"fn\"><a href=\"#9ed81526-0af8-4efa-b187-bb619195a3f9\" id=\"9ed81526-0af8-4efa-b187-bb619195a3f9-link\" class=\"broken_link\">12<\/a><\/sup>:<\/p>\n\n\n\n<figure data-wp-context=\"{&quot;imageId&quot;:&quot;69e795d277768&quot;}\" data-wp-interactive=\"core\/image\" data-wp-key=\"69e795d277768\" class=\"wp-block-image aligncenter size-medium wp-lightbox-container\"><img loading=\"lazy\" decoding=\"async\" width=\"300\" height=\"165\" data-wp-class--hide=\"state.isContentHidden\" data-wp-class--show=\"state.isContentVisible\" data-wp-init=\"callbacks.setButtonStyles\" data-wp-on--click=\"actions.showLightbox\" data-wp-on--load=\"callbacks.setButtonStyles\" data-wp-on-window--resize=\"callbacks.setButtonStyles\" src=\"https:\/\/mpr-projects.com\/wp-content\/uploads\/2024\/01\/Borg_XPS_config_8-300x165.png\" alt=\"\" class=\"wp-image-1625\" srcset=\"https:\/\/mpr-projects.com\/wp-content\/uploads\/2024\/01\/Borg_XPS_config_8-300x165.png 300w, https:\/\/mpr-projects.com\/wp-content\/uploads\/2024\/01\/Borg_XPS_config_8.png 734w\" sizes=\"auto, (max-width: 300px) 100vw, 300px\" \/><button\n\t\t\tclass=\"lightbox-trigger\"\n\t\t\ttype=\"button\"\n\t\t\taria-haspopup=\"dialog\"\n\t\t\taria-label=\"Enlarge\"\n\t\t\tdata-wp-init=\"callbacks.initTriggerButton\"\n\t\t\tdata-wp-on--click=\"actions.showLightbox\"\n\t\t\tdata-wp-style--right=\"state.imageButtonRight\"\n\t\t\tdata-wp-style--top=\"state.imageButtonTop\"\n\t\t>\n\t\t\t<svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" width=\"12\" height=\"12\" fill=\"none\" viewBox=\"0 0 12 12\">\n\t\t\t\t<path fill=\"#fff\" d=\"M2 0a2 2 0 0 0-2 2v2h1.5V2a.5.5 0 0 1 .5-.5h2V0H2Zm2 10.5H2a.5.5 0 0 1-.5-.5V8H0v2a2 2 0 0 0 2 2h2v-1.5ZM8 12v-1.5h2a.5.5 0 0 0 .5-.5V8H12v2a2 2 0 0 1-2 2H8Zm2-12a2 2 0 0 1 2 2v2h-1.5V2a.5.5 0 0 0-.5-.5H8V0h2Z\" \/>\n\t\t\t<\/svg>\n\t\t<\/button><\/figure>\n\n\n\n<p>See <a href=\"https:\/\/www.freedesktop.org\/software\/systemd\/man\/latest\/systemd.timer.html\" target=\"_blank\" rel=\"noreferrer noopener\">here<\/a> for details about all the options we have when configuring our timer. Finally, to activate the timer we run<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>sudo systemctl enable --now borgmatic.timer<\/code><\/pre>\n\n\n\n<p>We&#8217;ll do one last change to our setup. In the<em> <\/em>file <em>\/home\/nas_user\/.ssh\/authorized_keys<\/em> we&#8217;ll replace the ssh key of our root user with the following line:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>command=\"borg serve --restrict-to-repository \/nas_mounts\/hdd0\/borg_backups\/demo\",restrict ssh-ed25519 &lt;&lt;sshKey&gt;&gt; someDescription<\/code><\/pre>\n\n\n\n<p>This line only allows the ssh key of our root user to be used to update the Borg repository. It can&#8217;t be used to log into the RPi or do anything else but change the repo, which should add a little additional security (see <a href=\"https:\/\/borgbackup.readthedocs.io\/en\/stable\/deployment\/hosting-repositories.html\" target=\"_blank\" rel=\"noreferrer noopener\">here<\/a> for more information).<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Browsing and Restoring Backups<\/h3>\n\n\n\n<p>To mount a backup we have to make sure that <a href=\"https:\/\/wiki.archlinux.org\/title\/FUSE\" target=\"_blank\" rel=\"noreferrer noopener\">FUSE<\/a> is installed. On many flavours of Linux it will already be there. If it&#8217;s not (like in Arch) then search for and install an implementation of it. On my system I used<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>sudo pacman -S python-llfuse<\/code><\/pre>\n\n\n\n<p>Now we can create a mount point, e.g. with <em>mkdir \/home\/mpr\/mnt<\/em>, and easily mount the latest backup by running<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>sudo borgmatic mount --archive latest --mount-point \/home\/mpr\/mnt<\/code><\/pre>\n\n\n\n<p>You don&#8217;t have to use <em>latest<\/em>, you can mount any backup you like (you can find their names by running <code>sudo borgmatic rlist<\/code>).<\/p>\n\n\n\n<p>There are quite a lot of options regarding mounting and browsing through backups. They are explained very well <a href=\"https:\/\/torsion.org\/borgmatic\/docs\/how-to\/extract-a-backup\/\" target=\"_blank\" rel=\"noreferrer noopener\">here<\/a> so for more information do have a read at borgmatic&#8217;s how-to-guides (I&#8217;m not going into a lot of detail here because the documentation I linked to above is really good so there&#8217;s no point in me repeating it &#8230;).<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">About Hardware<\/h3>\n\n\n\n<p>If you&#8217;ve been following the main series of posts then you know that we&#8217;ve chosen HDDs for storing data. While they&#8217;re fast enough for our our main purpose of storing data, they have to spin up if they haven&#8217;t been used for a while. Since I&#8217;m doing a backup every three hours there would be a lot of spinning up and down. So I&#8217;ve decided to add the 1TB SSD (which I tested during the main series) to the Raspberry Pi, which will be used for Borg system backups and for holding Samba shares. It is not used by Greyhole as a storage drive.<\/p>\n\n\n\n<p>So, there we have it. We&#8217;re done with another piece of the back-up puzzle. The only major functionality that&#8217;s still missing is duplicating the NAS to a remote location (so we have a proper 3-2-1 backup system). But that will have to wait for a little while &#8230;<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\" style=\"margin-top:var(--wp--preset--spacing--30);margin-bottom:var(--wp--preset--spacing--30)\"\/>\n\n\n\n<p>Footnotes:<\/p>\n\n\n<ol class=\"wp-block-footnotes\"><li id=\"0b18ed8e-ef34-4ee6-a479-271bd1c1d3be\">On the website they call it de-duplicating instead of incremental. <a href=\"#0b18ed8e-ef34-4ee6-a479-271bd1c1d3be-link\" aria-label=\"Jump to footnote reference 1\">\u21a9\ufe0e<\/a><\/li><li id=\"c8587e72-3ecd-4215-8fb8-dcfe89c6ad79\"><em>\/etc<\/em> contains Samba and Greyhole configuration files, <em>\/usr\/local\/bin<\/em> is where we&#8217;ve been saving some helper scripts and the home directory on this Raspberry Pi contains some additional scripts I use for other tasks. <a href=\"#c8587e72-3ecd-4215-8fb8-dcfe89c6ad79-link\" aria-label=\"Jump to footnote reference 2\">\u21a9\ufe0e<\/a><\/li><li id=\"57572f17-e236-4a11-b601-8839c0dc2340\">This section allows for <a href=\"https:\/\/en.wikipedia.org\/wiki\/Glob_(programming)\">globbing<\/a> as shown by the asterisks *. <a href=\"#57572f17-e236-4a11-b601-8839c0dc2340-link\" aria-label=\"Jump to footnote reference 3\">\u21a9\ufe0e<\/a><\/li><li id=\"741b5fa7-21a4-4803-9137-9581e8085235\">The script provided by borgmatic works for Debian-based systems. So it does work for Raspberry Pi OS but it may not work for other Linux flavours. I prefer to use the more general way described  above. <a href=\"#741b5fa7-21a4-4803-9137-9581e8085235-link\" aria-label=\"Jump to footnote reference 4\">\u21a9\ufe0e<\/a><\/li><li id=\"d0b0cc80-a858-4ab4-b704-c1bca5e1cec2\">Our backup includes system files to which only the root user has access. <a href=\"#d0b0cc80-a858-4ab4-b704-c1bca5e1cec2-link\" aria-label=\"Jump to footnote reference 5\">\u21a9\ufe0e<\/a><\/li><li id=\"e037fd8f-0503-40b3-99c8-03d638a67b9e\">The ssh key of my user is managed by <a href=\"https:\/\/keepassxc.org\/\" target=\"_blank\" rel=\"noreferrer noopener\">KeePassXC<\/a>. When KeePassXC is unlocked then the key is added to the ssh agent. When it&#8217;s locked then the key is removed from the ssh agent. Having to unlock KeePassXC everytime my system wants to do an update seems cumbersome so I&#8217;ve created a separate ssh key for the root user. The permissions on this key only allow access to the root user.  <a href=\"#e037fd8f-0503-40b3-99c8-03d638a67b9e-link\" aria-label=\"Jump to footnote reference 6\">\u21a9\ufe0e<\/a><\/li><li id=\"064410fc-5917-401f-8a20-a45847c9e638\">I&#8217;m going a bit fast here, I know, but I&#8217;m assuming that you are familiar with how ssh works. If not then have a look online, there are loads of good descriptions available. <a href=\"#064410fc-5917-401f-8a20-a45847c9e638-link\" aria-label=\"Jump to footnote reference 7\">\u21a9\ufe0e<\/a><\/li><li id=\"84d78769-162f-4cd0-9144-4ccac757c005\">You can specify the location of the config file with option <em>-d<\/em>. You can also have multiple configuration files used for different purposes (see <a href=\"https:\/\/torsion.org\/borgmatic\/docs\/how-to\/make-per-application-backups\/\" target=\"_blank\" rel=\"noreferrer noopener\">here<\/a>). For example, one could be used for the system backup, another one for backing up your user data (that update process wouldn&#8217;t require root access rights). For this post we&#8217;re just going to use one file though (it&#8217;s easier to read this way). <a href=\"#84d78769-162f-4cd0-9144-4ccac757c005-link\" aria-label=\"Jump to footnote reference 8\">\u21a9\ufe0e<\/a><\/li><li id=\"9d604407-149f-4bd0-b23f-93fe0e0179e2\">The key is saved in the repository so I&#8217;m not sure how much benefit there is from an extra copy. According to <a href=\"https:\/\/github.com\/borgbackup\/borg\/issues\/5285\" target=\"_blank\" rel=\"noreferrer noopener\">this<\/a> it&#8217;s to protect against software\/hardware bugs. I guess it&#8217;s better to be safe than sorry&#8230; <a href=\"#9d604407-149f-4bd0-b23f-93fe0e0179e2-link\" aria-label=\"Jump to footnote reference 9\">\u21a9\ufe0e<\/a><\/li><li id=\"a5c5e524-c096-4cf2-a1ec-6c320497371c\">Its minimal nature is my main motivation for using Arch Linux. It makes it much easier to have a small system without unnecessary programs. <a href=\"#a5c5e524-c096-4cf2-a1ec-6c320497371c-link\" aria-label=\"Jump to footnote reference 10\">\u21a9\ufe0e<\/a><\/li><li id=\"bd31459e-310a-4c01-af6d-45f0201e671f\">If you&#8217;re on X.Org you may have to adjust the script. I don&#8217;t have a machine with X.Org available so I didn&#8217;t test how the script has to be changed. <a href=\"#bd31459e-310a-4c01-af6d-45f0201e671f-link\" aria-label=\"Jump to footnote reference 11\">\u21a9\ufe0e<\/a><\/li><li id=\"9ed81526-0af8-4efa-b187-bb619195a3f9\">You may have to add <code>Wants=borgmatic.service<\/code> in the [Unit] section of the .timer file. There seems to be a bug in systemd that sometimes prevents timers from being run, see <a href=\"https:\/\/github.com\/systemd\/systemd\/issues\/6680\" target=\"_blank\" rel=\"noreferrer noopener\">here<\/a> for the bug report. <a href=\"#9ed81526-0af8-4efa-b187-bb619195a3f9-link\" aria-label=\"Jump to footnote reference 12\">\u21a9\ufe0e<\/a><\/li><\/ol>","protected":false},"excerpt":{"rendered":"<p>In this post we&#8217;re going to use the software packages Borg Backup and Borgmatic to do system backups of both our Raspberry Pi and our main computer. We&#8217;ll also schedule automatic backups using both cron and systemd.<\/p>\n","protected":false},"author":1,"featured_media":1673,"comment_status":"open","ping_status":"open","sticky":false,"template":"wp-custom-template-single-with-sidebar-1","format":"standard","meta":{"_eb_attr":"","footnotes":"[{\"content\":\"On the website they call it de-duplicating instead of incremental.\",\"id\":\"0b18ed8e-ef34-4ee6-a479-271bd1c1d3be\"},{\"content\":\"<em>\/etc<\/em> contains Samba and Greyhole configuration files, <em>\/usr\/local\/bin<\/em> is where we've been saving some helper scripts and the home directory on this Raspberry Pi contains some additional scripts I use for other tasks.\",\"id\":\"c8587e72-3ecd-4215-8fb8-dcfe89c6ad79\"},{\"content\":\"This section allows for <a href=\\\"https:\/\/en.wikipedia.org\/wiki\/Glob_(programming)\\\">globbing<\/a> as shown by the asterisks *.\",\"id\":\"57572f17-e236-4a11-b601-8839c0dc2340\"},{\"content\":\"The script provided by borgmatic works for Debian-based systems. So it does work for Raspberry Pi OS but it may not work for other Linux flavours. I prefer to use the more general way described  above.\",\"id\":\"741b5fa7-21a4-4803-9137-9581e8085235\"},{\"content\":\"Our backup includes system files to which only the root user has access.\",\"id\":\"d0b0cc80-a858-4ab4-b704-c1bca5e1cec2\"},{\"content\":\"The ssh key of my user is managed by <a href=\\\"https:\/\/keepassxc.org\/\\\" target=\\\"_blank\\\" rel=\\\"noreferrer noopener\\\">KeePassXC<\/a>. When KeePassXC is unlocked then the key is added to the ssh agent. When it's locked then the key is removed from the ssh agent. Having to unlock KeePassXC everytime my system wants to do an update seems cumbersome so I've created a separate ssh key for the root user. The permissions on this key only allow access to the root user. \",\"id\":\"e037fd8f-0503-40b3-99c8-03d638a67b9e\"},{\"content\":\"I'm going a bit fast here, I know, but I'm assuming that you are familiar with how ssh works. If not then have a look online, there are loads of good descriptions available.\",\"id\":\"064410fc-5917-401f-8a20-a45847c9e638\"},{\"content\":\"You can specify the location of the config file with option <em>-d<\/em>. You can also have multiple configuration files used for different purposes (see <a href=\\\"https:\/\/torsion.org\/borgmatic\/docs\/how-to\/make-per-application-backups\/\\\" target=\\\"_blank\\\" rel=\\\"noreferrer noopener\\\">here<\/a>). For example, one could be used for the system backup, another one for backing up your user data (that update process wouldn't require root access rights). For this post we're just going to use one file though (it's easier to read this way).\",\"id\":\"84d78769-162f-4cd0-9144-4ccac757c005\"},{\"content\":\"The key is saved in the repository so I'm not sure how much benefit there is from an extra copy. According to <a href=\\\"https:\/\/github.com\/borgbackup\/borg\/issues\/5285\\\" target=\\\"_blank\\\" rel=\\\"noreferrer noopener\\\">this<\/a> it's to protect against software\/hardware bugs. I guess it's better to be safe than sorry...\",\"id\":\"9d604407-149f-4bd0-b23f-93fe0e0179e2\"},{\"content\":\"Its minimal nature is my main motivation for using Arch Linux. It makes it much easier to have a small system without unnecessary programs.\",\"id\":\"a5c5e524-c096-4cf2-a1ec-6c320497371c\"},{\"content\":\"If you're on X.Org you may have to adjust the script. I don't have a machine with X.Org available so I didn't test how the script has to be changed.\",\"id\":\"bd31459e-310a-4c01-af6d-45f0201e671f\"},{\"content\":\"You may have to add <code>Wants=borgmatic.service<\/code> in the [Unit] section of the .timer file. There seems to be a bug in systemd that sometimes prevents timers from being run, see <a href=\\\"https:\/\/github.com\/systemd\/systemd\/issues\/6680\\\" target=\\\"_blank\\\" rel=\\\"noreferrer noopener\\\">here<\/a> for the bug report.\",\"id\":\"9ed81526-0af8-4efa-b187-bb619195a3f9\"}]"},"categories":[3,10],"tags":[9,4,19,5],"class_list":["post-1596","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-linux","category-projects","tag-data-safety","tag-linux","tag-project","tag-raspberry-pi"],"_links":{"self":[{"href":"https:\/\/mpr-projects.com\/index.php\/wp-json\/wp\/v2\/posts\/1596","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/mpr-projects.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/mpr-projects.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/mpr-projects.com\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/mpr-projects.com\/index.php\/wp-json\/wp\/v2\/comments?post=1596"}],"version-history":[{"count":25,"href":"https:\/\/mpr-projects.com\/index.php\/wp-json\/wp\/v2\/posts\/1596\/revisions"}],"predecessor-version":[{"id":1705,"href":"https:\/\/mpr-projects.com\/index.php\/wp-json\/wp\/v2\/posts\/1596\/revisions\/1705"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/mpr-projects.com\/index.php\/wp-json\/wp\/v2\/media\/1673"}],"wp:attachment":[{"href":"https:\/\/mpr-projects.com\/index.php\/wp-json\/wp\/v2\/media?parent=1596"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/mpr-projects.com\/index.php\/wp-json\/wp\/v2\/categories?post=1596"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/mpr-projects.com\/index.php\/wp-json\/wp\/v2\/tags?post=1596"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}