{"version":"https://jsonfeed.org/version/1.1","title":"Vic Demuzere - systemd","home_page_url":"https://vic.demuzere.be/articles/tags/systemd/","feed_url":"https://vic.demuzere.be/articles/tags/systemd/feed.json","description":"Blog posts tagged systemd by Vic Demuzere","language":"en","items":[{"id":"https://vic.demuzere.be/articles/backups-restic-systemd/","content_html":"<p>Since moving away from Digital Ocean (and their snapshots) I've been looking for a good way to create backups for my servers.\nI've recently stumbled upon the <a href=\"https://restic.github.io/\" title=\"Restic\">Restic</a> project and have been using it ever since.</p>\n<ul>\n<li>Restic is very <strong>simple to use</strong>. It's a cli-tool with a few easy to understand commands and has good documentation including examples.</li>\n<li>You <strong>don't need a special backup service</strong>. Restic runs on the host being backed up, and is happy to store its backups on a simple file server or cloud service.</li>\n<li>Security is important and the developers have written a <strong>decent <a href=\"https://restic.readthedocs.io/en/latest/100_references.html#threat-model\" title=\"Restic threat model\">threat model</a></strong>: trust only the host where restic runs, which makes sense as it contains the data anyway. The place where you store your backups is always considered insecure.</li>\n</ul>\n<h2 id=\"mounting-storage-space\">Mounting storage space<a href=\"#mounting-storage-space\" aria-label=\"Link to heading 'Mounting storage space'\" data-heading-content=\"Mounting storage space\" class=\"anchor\"></a></h2>\n<p><a href=\"https://www.transip.be/\" title=\"TransIP\">TransIP</a> is giving 1TB of free storage on their <a href=\"https://www.transip.be/stack/\" title=\"TransIP Stack\">Stack storage service</a>, which is more than enough for my backup needs.\nThe downside is that the only useful way to access it is a painfully slow webdav mount.\nIf you're backing up data that changes fast and need a consistent state, I would suggest choosing a different place to store your data.\nFor most of my servers I don't really care about backup speeds, though.</p>\n<p>Mounting the volume using an entry in <code>/etc/fstab</code> would work, but as I want to specify dependencies later I opt to create a <em><a href=\"https://www.freedesktop.org/software/systemd/man/systemd.mount.html\" title=\"systemd Mount Unit\">Mount Unit</a></em> instead.\nThis is basically the systemd version of a single fstab entry.</p>\n<pre><code># /etc/systemd/systemd/mnt-backups.mount\n[Unit]\nDescription=Backup storage\nAfter=network-online.target\nWants=network-online.target\n\n[Mount]\nType=davfs\nWhat=https://something.stackstorage.com/remote.php/webdav/\nWhere=/mnt/backups\nOptions=uid=root,gid=root,file_mode=644,dir_mode=755,nosuid,noexec,rw\nTiemoutSec=15\n\n[Install]\nWantedBy=multi-user.target\n</code></pre>\n<p>If you're using webdav (using davfs2) like me, you'll also have to set your username and password in the secrets file <code>/etc/davfs2/secrets</code>.\nBe sure to restrict the permissions on this file as it contains passwords.</p>\n<pre><code>https://something.stackstorage.com/remote.php/webdav/ username password\n</code></pre>\n<p>We can now tell systemd to mount our volume:</p>\n<pre><code>systemctl start mnt-backups.mount\n</code></pre>\n<p>And also set it to be mounted every time our machine boots:</p>\n<pre><code>systemctl enable mnt-backups.mount\n</code></pre>\n<h2 id=\"initialize-the-repository\">Initialize the repository<a href=\"#initialize-the-repository\" aria-label=\"Link to heading 'Initialize the repository'\" data-heading-content=\"Initialize the repository\" class=\"anchor\"></a></h2>\n<p>Before our first backup we have to initialize a repository.\nThis creates the directory structure and configuration that restic will use to store our backups.\nNote that, as restic does not trust our backup volume, any configuration stored there is also encrypted!</p>\n<pre><code>restic init --repo /mnt/backups/myserver\n</code></pre>\n<p>Do not forget the password you enter during this step.\nEvery single file on the backup volume is encrypted and without the password your backups become useless.</p>\n<h2 id=\"backup-service\">Backup service<a href=\"#backup-service\" aria-label=\"Link to heading 'Backup service'\" data-heading-content=\"Backup service\" class=\"anchor\"></a></h2>\n<p>The <em><a href=\"https://www.freedesktop.org/software/systemd/man/systemd.service.html\" title=\"systemd Service Unit\">Service Unit</a></em> shown below describes a service that creates a single backup and stops.</p>\n<pre><code># /etc/systemd/system/backup.service\n[Unit]\nDescription=Backup\nAfter=mnt-backups.mount\nWants=mnt-backups.mount\n\n[Service]\nType=simple\nEnvironment=RESTIC_PASSWORD=mysupers3cr3tpassw0rd!\nEnvironment=RESTIC_REPOSITORY=/mnt/backups/myserver\nExecStart=/usr/local/bin/restic backup /home /srv/web\n</code></pre>\n<p>In the <code>[Unit]</code> section we specify that the mount we created earlier is a requirement for making backups (<code>Wants</code>).\nThe <code>After</code> setting is used to order the units when both are set to start when booting the system.\nIt's a bit redundant to specify that the <em>Service</em> should be after the <em>Mount</em> as we already specified a dependency, but it's a common thing to do.</p>\n<p>Note that I only backup my <code>/home</code> and <code>/srv/web</code> folders as everything else can either be recreated or is managed by Ansible on my servers.\nIf you prefer to have a copy of your entire filesystem remember to change the path!</p>\n<p>We can now create a new backup manually by running <code>systemctl start backup</code>.\nThis isn't really useful by itself as we could have run <code>restic backup</code> manually just as easy, with the exception that we don't have to type the password anymore.\nOnce we combine this with a systemd timer, things become interesting!</p>\n<p>Remember that we set the backup volume to be mounted on boot? That isn't really required.\nAs we have specified a dependency on the volume, systemd will check if it's mounted before starting the backup service.\nThe free <a href=\"https://www.transip.be/stack/\" title=\"TransIP Stack\">Stack</a> volume I'm using is a bit wonky though, so I prefer to keep it mounted as that's a bit more stable.</p>\n<h2 id=\"creating-backups-automatically\">Creating backups automatically<a href=\"#creating-backups-automatically\" aria-label=\"Link to heading 'Creating backups automatically'\" data-heading-content=\"Creating backups automatically\" class=\"anchor\"></a></h2>\n<p>Now that our backup works, it's time to have it create a backup automatically every night.\nCreating a <em><a href=\"https://www.freedesktop.org/software/systemd/man/systemd.mount.html\" title=\"systemd Timer Unit\">Timer Unit</a></em> with the same name as the service we want to start does exactly that!</p>\n<pre><code># /etc/systemd/system/backup.timer\n[Unit]\nDescription=Backup daily\n\n[Timer]\nOnCalendar=daily\nPersistent=true\n\n[Install]\nWantedBy=timers.target\n</code></pre>\n<p>This simple timer is set to run daily at midnight.\nSetting the <code>Persistent</code> attribute tells systemd that if our host was down at midnight, it has to run the backup as soon as it comes up again.\nAs servers are usually online 24/7 this doesn't really matter but it is a great setting for desktops and laptops.</p>\n<p>Enable the timer using systemctl:</p>\n<pre><code>systemctl enable backup.timer\n</code></pre>\n<p>Now enjoy having a fresh backup of your server every night!\nSimilar to the backup service, you can also create a service to <a href=\"https://restic.readthedocs.io/en/latest/060_forget.html\" title=\"Remove old snapshots\">remove your old snapshots</a>.\nBy default <a href=\"https://restic.github.io/\" title=\"Restic\">Restic</a> just keeps adding data and never removes anything, this may become a problem pretty fast if you're backup up a lot of data.</p>\n<h2 id=\"restoring\">Restoring<a href=\"#restoring\" aria-label=\"Link to heading 'Restoring'\" data-heading-content=\"Restoring\" class=\"anchor\"></a></h2>\n<p>As with any backup system: remember to test restoring your backups regularly.\nRestic gives us two great options in case we fuck up and need our data back:</p>\n<ul>\n<li><a href=\"https://restic.readthedocs.io/en/latest/050_restore.html#restoring-from-a-snapshot\" title=\"Restore a snapshot using restic\">Restore a complete snapshot</a> if you know to which point in time you need to return and if you need a copy of all your files.</li>\n<li><a href=\"https://restic.readthedocs.io/en/latest/050_restore.html#restore-using-mount\" title=\"Mount all your snapshots using restic\">Mount your backup repository</a> as a fuse filesystem, allowing you to browse all your snapshots and the files within as if they're all just there on your server.</li>\n</ul>\n<div class=\"footer\"><hr><p>Thanks for subscribing to my feed!</p><p>Feel free to e-mail comments or corrections to <a href=\"mailto:vic@demuzere.be?subject=RE%3A%20Using%20Restic%20and%20systemd%20to%20backup%20a%20linux%20host\">vic@demuzere.be</a>.</p></div>","url":"https://vic.demuzere.be/articles/backups-restic-systemd/","date_published":"2017-11-24T21:20:00Z","date_modified":"2023-11-28T19:55:32Z","language":"en"},{"id":"https://vic.demuzere.be/articles/using-systemd-user-units/","content_html":"<p>Systemd is mainly used to manage services on modern linux distributions, but it also allows non-root users to manage services running under their own account.\nThis makes systemd a great alternative to services like supervisord as it is able to detect crashes and automatically restarts the service.</p>\n<h2 id=\"configuring-a-user-unit\">Configuring a user unit<a href=\"#configuring-a-user-unit\" aria-label=\"Link to heading 'Configuring a user unit'\" data-heading-content=\"Configuring a user unit\" class=\"anchor\"></a></h2>\n<p>The directory structure that holds unit files is similar to the systemwide configuration, but is located in a user's home directory:</p>\n<pre><code>$ mkdir -p ~/.config/systemd/user\n</code></pre>\n<p>In the <code>user</code> folder we can create service units with a <code>.service</code> extension. A unit is a ini-style file describing a service that has to be managed.</p>\n<p>Let's say we have a <a href=\"https://laravel.com/docs/5.4/queues#running-the-queue-worker\">Laravel PHP queue worker</a> that we want to be managed by systemd, called <code>worker.service</code>:</p>\n<pre><code>[Unit]\nDescription=Laravel PHP queue worker\n\n[Service]\nExecStart=/usr/bin/env php /path/to/my/project/artisan queue:work --sleep=3 --tries=3\n\n[Install]\nWantedBy=default.target\n</code></pre>\n<p>Run <code>$ systemctl --user daemon-reload</code> to make systemd recognize your changes.</p>\n<p>We can now manage this service with systemd:</p>\n<ul>\n<li><code>$ systemctl --user start worker</code> starts the service</li>\n<li><code>$ systemctl --user stop worker</code> stops the service</li>\n<li><code>$ systemctl --user restart worker</code> restarts the workers</li>\n<li><code>$ systemctl --user status worker</code> shows us the service status</li>\n</ul>\n<p>If we want this service to start automatically <em>when our user logs in</em>, we have to enable it:</p>\n<ul>\n<li><code>$ systemctl --user enable worker</code></li>\n<li><code>$ systemctl --user disable worker</code></li>\n</ul>\n<h2 id=\"viewing-logs\">Viewing logs<a href=\"#viewing-logs\" aria-label=\"Link to heading 'Viewing logs'\" data-heading-content=\"Viewing logs\" class=\"anchor\"></a></h2>\n<p>Everything our PHP script writes to stdout and stderr will be recorded by journald. We can view the logs for this specific service:</p>\n<pre><code>$ journalctl -u worker\n</code></pre>\n<p>On older versions of systemd (like the one shipped with Debian Jessie), you might need to use a flag specific to user units:</p>\n<pre><code>$ journalctl --user-unit worker\n</code></pre>\n<h2 id=\"automatically-restarting-on-failure\">Automatically restarting on failure<a href=\"#automatically-restarting-on-failure\" aria-label=\"Link to heading 'Automatically restarting on failure'\" data-heading-content=\"Automatically restarting on failure\" class=\"anchor\"></a></h2>\n<p>Let's be honest: things go wrong. When our worker crashes we want to make sure it's started again. Fortunately, we can configure systemd to restart on failure:</p>\n<p>Add the following to the <code>[Service]</code> section of the unit file:</p>\n<pre><code>RestartSec=10s\nRestart=on-failure\n</code></pre>\n<p>This makes systemd restart the service 10 seconds after it has stopped with a non-zero exit code. If the process exited cleanly, systemd considers the work as done and won't restart the service until you manually start it later.</p>\n<h2 id=\"starting-user-services-at-boot\">Starting user services at boot<a href=\"#starting-user-services-at-boot\" aria-label=\"Link to heading 'Starting user services at boot'\" data-heading-content=\"Starting user services at boot\" class=\"anchor\"></a></h2>\n<p>By default, systemd starts <em>enabled</em> user units when the user logs in, and <strong>stops them once the user has logged out</strong>. This is great for desktop pcs where a user wants to run things while he's using the computer. On servers, however, we may want to run those units all the time.</p>\n<pre><code># loginctl enable-linger USERNAME\n</code></pre>\n<p>You can check the status for each user by:</p>\n<pre><code># loginctl user-status USERNAME\n</code></pre>\n<p>It should show that the user is lingering:</p>\n<pre><code>vic (1000)\n           Since: Fri 2017-09-01 21:54:29 CEST; 3s ago\n           State: lingering\n            Unit: user-1000.slice\n</code></pre>\n<p>When the user is not logged in and does not have lingering enabled, loginctl doesn't know about any user sessions so it will return an error:</p>\n<pre><code>Failed to get user: No user 1000 known or logged in\n</code></pre>\n<div class=\"footer\"><hr><p>Thanks for subscribing to my feed!</p><p>Feel free to e-mail comments or corrections to <a href=\"mailto:vic@demuzere.be?subject=RE%3A%20Managing%20services%20for%20non-root%20users%20with%20systemd\">vic@demuzere.be</a>.</p></div>","url":"https://vic.demuzere.be/articles/using-systemd-user-units/","date_published":"2017-09-01T19:36:00Z","date_modified":"2023-11-28T19:55:32Z","language":"en"}]}