Create an account


Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Fedora - Automate backups with restic and systemd

#1
Automate backups with restic and systemd

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 backups. Then read on for more details.

Two systemd services are required to run in order to automate taking snapshots and keeping data pruned. The first service runs the backup command needs to be run on a regular frequency. The second service takes care of data pruning.

If you’re not familiar with systemd at all, there’s never been a better time to learn. Check out the series on systemd here at the Magazine, starting with this primer on unit files:



If you haven’t installed restic already, note it’s in the official Fedora repositories. To install use this command with sudo:

$ sudo dnf install restic

Backup


First, create the ~/.config/systemd/user/restic-backup.service file. Copy and paste the text below into the file for best results.

[Unit]
Description=Restic backup service
[Service]
Type=oneshot
ExecStartPre=restic unlock
ExecStart=restic backup --verbose --one-file-system --tag systemd.timer $BACKUP_EXCLUDES $BACKUP_PATHS
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
EnvironmentFile=%h/.config/restic-backup.conf

This service references an environment file in order to load secrets (such as RESTIC_PASSWORD). Create the ~/.config/restic-backup.conf 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.

BACKUP_PATHS="/home/rupert"
BACKUP_EXCLUDES="--exclude-file /home/rupert/.restic_excludes --exclude-if-present .exclude_from_backup"
RETENTION_DAYS=7
RETENTION_WEEKS=4
RETENTION_MONTHS=6
RETENTION_YEARS=3
B2_ACCOUNT_ID=XXXXXXXXXXXXXXXXXXXXXXXXX
B2_ACCOUNT_KEY=XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
RESTIC_REPOSITORY=b2:XXXXXXXXXXXXXXXXXX:/
RESTIC_PASSWORD=XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX

Now that the service is installed, reload systemd: systemctl –user daemon-reload. Try running the service manually to create a backup: systemctl –user start restic-backup.

Because the service is a oneshot, 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 restic-backup.service daily, create ~/.config/systemd/user/restic-backup.timer as follows. Again, copy and paste this text:

[Unit]
Description=Backup with restic daily
[Timer]
OnCalendar=daily
Persistent=true
[Install]
WantedBy=timers.target

Enable it by running this command:

$ systemctl --user start restic-backup.timer

Prune


While the main service runs the forget command to only keep snapshots within the keep policy, the data is not actually removed from the restic repository. The prune command inspects the repository and current snapshots, and deletes any data not associated with a snapshot. Because prune 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 ~/.config/systemd/user/restic-prune.service by copying and pasting this text:

[Unit]
Description=Restic backup service (data pruning)
[Service]
Type=oneshot
ExecStart=restic prune
EnvironmentFile=%h/.config/restic-backup.conf

Similarly to the main restic-backup.service, restic-prune is a oneshot service and can be run manually. Once the service has been set up, create and enable a corresponding timer at ~/.config/systemd/user/restic-prune.timer:

[Unit]
Description=Prune data from the restic repository monthly
[Timer]
OnCalendar=monthly
Persistent=true
[Install]
WantedBy=timers.target

That’s it! Restic will now run daily and prune data monthly.


Photo by Samuel Zeller on Unsplash.

Reply



Possibly Related Threads…
Thread Author Replies Views Last Post
  Fedora - systemd-resolved: introduction to split DNS xSicKxBot 0 1,552 10-16-2020, 07:28 PM
Last Post: xSicKxBot
  Fedora - Incremental backups with Btrfs snapshots xSicKxBot 0 1,597 09-15-2020, 10:20 AM
Last Post: xSicKxBot
  Fedora - Make free encrypted backups to the cloud on Fedora xSicKxBot 0 1,513 02-25-2020, 07:30 PM
Last Post: xSicKxBot
  Fedora - Fedora job opening: Fedora Community Action and Impact Coordinator (FCAIC) xSicKxBot 0 1,676 07-11-2019, 10:39 AM
Last Post: xSicKxBot
  Fedora - Contribute at the Fedora Test Day for Fedora Modularity xSicKxBot 0 1,821 03-28-2019, 11:12 PM
Last Post: xSicKxBot
  The End of the Road: systemd’s “Socket” Units xSicKxBot 0 1,799 10-02-2018, 03:00 AM
Last Post: xSicKxBot
  Systemd Timers: Three Use Cases xSicKxBot 0 1,961 08-06-2018, 02:26 PM
Last Post: xSicKxBot
  Setting Up a Timer with systemd in Linux xSicKxBot 0 1,988 07-24-2018, 03:50 PM
Last Post: xSicKxBot
  Systemd Services: Monitoring Files and Directories xSicKxBot 0 1,965 06-19-2018, 02:29 PM
Last Post: xSicKxBot
  Systemd Services: Reacting to Change xSicKxBot 0 1,800 06-13-2018, 06:21 AM
Last Post: xSicKxBot

Forum Jump:


Users browsing this thread:
1 Guest(s)

Forum software by © MyBB Theme © iAndrew 2016