{"id":92534,"date":"2019-04-25T08:00:36","date_gmt":"2019-04-25T08:00:36","guid":{"rendered":"https:\/\/fedoramagazine.org\/?p=27506"},"modified":"2019-04-25T08:00:36","modified_gmt":"2019-04-25T08:00:36","slug":"automate-backups-with-restic-and-systemd","status":"publish","type":"post","link":"https:\/\/sickgaming.net\/blog\/2019\/04\/25\/automate-backups-with-restic-and-systemd\/","title":{"rendered":"Automate backups with restic and systemd"},"content":{"rendered":"<p>Timely backups are important. So much so that <a href=\"https:\/\/restic.net\/\">backing up software<\/a> is a common topic of discussion, even <a href=\"https:\/\/fedoramagazine.org\/?s=backup\">here on the Fedora Magazine<\/a>. This article demonstrates how to automate backups with <strong>restic<\/strong> using only systemd unit files.<\/p>\n<p>For an introduction to restic, be sure to check out our article <a href=\"https:\/\/fedoramagazine.org\/use-restic-encrypted-backups\/\">Use restic on Fedora for encrypted backups<\/a>. Then read on for more details.<\/p>\n<p> <span id=\"more-27506\"><\/span> <\/p>\n<p>Two systemd services are required to run in order to automate taking snapshots and keeping data pruned. The first service runs the <em>backup<\/em> command needs to be run on a regular frequency. The second service takes care of data pruning.<\/p>\n<p>If you&#8217;re not familiar with systemd at all, there&#8217;s never been a better time to learn. Check out <a href=\"https:\/\/fedoramagazine.org\/series\/systemd-series\/\">the series on systemd here at the Magazine<\/a>, starting with this primer on unit files:<\/p>\n<figure class=\"wp-block-embed is-type-rich is-provider-fedora-magazine\">\n<div class=\"wp-block-embed__wrapper\">\n<blockquote class=\"wp-embedded-content\" data-secret=\"t4XltaCCoC\"><p><a href=\"https:\/\/fedoramagazine.org\/systemd-getting-a-grip-on-units\/\">systemd unit file basics<\/a><\/p><\/blockquote>\n<\/div>\n<\/figure>\n<p>If you haven&#8217;t installed restic already, note it&#8217;s in the official Fedora repositories. To install use this command <a href=\"https:\/\/fedoramagazine.org\/howto-use-sudo\/\">with sudo<\/a>:<\/p>\n<pre class=\"wp-block-preformatted\">$ sudo dnf install restic<\/pre>\n<h2>Backup<\/h2>\n<p>First, create the <em>~\/.config\/systemd\/user\/restic-backup.service<\/em> file. Copy and paste the text below into the file for best results.<\/p>\n<pre class=\"wp-block-preformatted\">[Unit]<br \/>Description=Restic backup service<br \/>[Service]<br \/>Type=oneshot<br \/>ExecStartPre=restic unlock<br \/>ExecStart=restic backup --verbose --one-file-system --tag systemd.timer $BACKUP_EXCLUDES $BACKUP_PATHS<br \/>ExecStartPost=restic forget --verbose --tag systemd.timer --group-by \"paths,tags\" --keep-daily $RETENTION_DAYS --keep-weekly $RETENTION_WEEKS --keep-monthly $RETENTION_MONTHS --keep-yearly $RETENTION_YEARS<br \/>EnvironmentFile=%h\/.config\/restic-backup.conf<br \/><\/pre>\n<p>This service references an environment file in order to load secrets (such as <em>RESTIC_PASSWORD<\/em>). Create the <em>~\/.config\/restic-backup.conf<\/em> file. Copy and paste the content below for best results. This example uses BackBlaze B2 buckets. Adjust the ID, key, repository, and password values accordingly.<\/p>\n<pre class=\"wp-block-preformatted\">BACKUP_PATHS=\"\/home\/rupert\"<br \/>BACKUP_EXCLUDES=\"--exclude-file \/home\/rupert\/.restic_excludes --exclude-if-present .exclude_from_backup\"<br \/>RETENTION_DAYS=7<br \/>RETENTION_WEEKS=4<br \/>RETENTION_MONTHS=6<br \/>RETENTION_YEARS=3<br \/>B2_ACCOUNT_ID=XXXXXXXXXXXXXXXXXXXXXXXXX<br \/>B2_ACCOUNT_KEY=XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX<br \/>RESTIC_REPOSITORY=b2:XXXXXXXXXXXXXXXXXX:\/<br \/>RESTIC_PASSWORD=XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX<br \/><\/pre>\n<p>Now that the service is installed, reload systemd: <em>systemctl &#8211;user daemon-reload<\/em>. Try running the service manually to create a backup: <em>systemctl &#8211;user start restic-backup<\/em>.<\/p>\n<p>Because the service is a <em>oneshot<\/em>, it will run once and exit. After verifying that the service runs and creates snapshots as desired, set up a timer to run this service regularly. For example, to run the <em>restic-backup.service<\/em> daily, create <em>~\/.config\/systemd\/user\/restic-backup.timer<\/em> as follows. Again, copy and paste this text:<\/p>\n<pre class=\"wp-block-preformatted\">[Unit]<br \/>Description=Backup with restic daily<br \/>[Timer]<br \/>OnCalendar=daily<br \/>Persistent=true<br \/>[Install]<br \/>WantedBy=timers.target<br \/><\/pre>\n<p>Enable it by running this command:<\/p>\n<pre class=\"wp-block-preformatted\">$ systemctl --user start restic-backup.timer<\/pre>\n<h2>Prune<\/h2>\n<p>While the main service runs the <em>forget<\/em> command to only keep snapshots within the keep policy, the data is not actually removed from the restic repository. The <em>prune<\/em> command inspects the repository and current snapshots, and deletes any data not associated with a snapshot. Because <em>prune<\/em> can be a time-consuming process, it is not necessary to run every time a backup is run. This is the perfect scenario for a second service and timer. First, create the file <em>~\/.config\/systemd\/user\/restic-prune.service<\/em> by copying and pasting this text:<\/p>\n<pre class=\"wp-block-preformatted\">[Unit]<br \/>Description=Restic backup service (data pruning)<br \/>[Service]<br \/>Type=oneshot<br \/>ExecStart=restic prune<br \/>EnvironmentFile=%h\/.config\/restic-backup.conf<br \/><\/pre>\n<p>Similarly to the main <em>restic-backup.service<\/em>, <em>restic-prune<\/em> is a oneshot service and can be run manually. Once the service has been set up, create and enable a corresponding timer at <em>~\/.config\/systemd\/user\/restic-prune.timer<\/em>:<\/p>\n<pre class=\"wp-block-preformatted\">[Unit]<br \/>Description=Prune data from the restic repository monthly<br \/>[Timer]<br \/>OnCalendar=monthly<br \/>Persistent=true<br \/>[Install]<br \/>WantedBy=timers.target<br \/><\/pre>\n<p>That&#8217;s it! Restic will now run daily and prune data monthly.<\/p>\n<hr class=\"wp-block-separator\" \/>\n<p><em>Photo by&nbsp;<\/em><a href=\"https:\/\/unsplash.com\/photos\/JuFcQxgCXwA?utm_source=unsplash&amp;utm_medium=referral&amp;utm_content=creditCopyText\"><em>Samuel Zeller<\/em><\/a><em>&nbsp;on&nbsp;<\/em><a href=\"https:\/\/unsplash.com\/search\/photos\/archive?utm_source=unsplash&amp;utm_medium=referral&amp;utm_content=creditCopyText\"><em>Unsplash<\/em><\/a><em>.<\/em><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Timely backups are important. So much so that backing up software is a common topic of discussion, even here on the Fedora Magazine. This article demonstrates how to automate backups with restic using only systemd unit files. For an introduction to restic, be sure to check out our article Use restic on Fedora for encrypted [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[48],"tags":[45,61,46,47],"class_list":["post-92534","post","type-post","status-publish","format-standard","hentry","category-fedora-os","tag-fedora","tag-fedora-project-community","tag-magazine","tag-news"],"_links":{"self":[{"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/posts\/92534","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/comments?post=92534"}],"version-history":[{"count":0,"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/posts\/92534\/revisions"}],"wp:attachment":[{"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/media?parent=92534"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/categories?post=92534"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/tags?post=92534"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}