Systemd (Auto)mounts & Scheduling Backups

Intro

This post continues the unoffocial series on systemd. This time I got to know the systemd mount and how it relates to fstab, and the automounts, which wait until a mount path is requested and then mount the device. Skip to the next section for the important stuff.

I’m whittling down to the essentials as I’m moving (to Whitehorse, YK!) with my wife-to-be in the near future. Part of that includes making my laptop my primary computing device. Formerly, I used it just for data/development projects, and left certain things like accounting, photo editing, multimedia, etc for my desktop. So there were a few small things to do, like install homebank (try it!). My desktop was also the file server and NAS for me and the missus, to which we just rsync’d our laptops. A job on the desktop backed them up to an external drive.

Two things were key in the migration to the laptop, to keep it from going up in flames:

  1. designate some non storage as copy-on-write for torrent files, e.g. here which I was alerted to by Chris Irwin’s excellent BTRFS presentation at KWLUG
  2. setup external storage for, and schedule, system backups

For both I ended up utilizing systemd options for file system mounts, and for 2 I extended my experience with systemd services and timers (i.e. here and there).

I am still considering remote backup solutions (as a usb backup that is usually with my laptop only offers slight protection against data loss). Duplicity + amazons3 sounds good, but could get pricey. Any suggestions out there?

Systemd Mount Settings

I was going to do an interesting btrfs setup and backup with btrfs sync, but it turned out the dedicated external drive I had for that was fried. The ext4 drive had a pretty recent backup of my laptop and other media on it; so I just continued rsync’ing to that device. Here’s how it looks in the fstab (the preferred place to setup systemd mounts). An explanation of the mount options is given below.

cat /etc/fstab | grep ADATA

UUID=7c76879e-ef10-4da7-bf75-93e03ea5f9d9   /media/ADATA_HV620  ext4    noauto,x-systemd.automount,x-systemd.device-timeout=5,x-systemd.idle-timeout=60 0   2
  • noauto
    • do not mount the device at boot
  • x-systemd.automount
    • create an .automount unit for this
  • x-systemd.device-timeout
    • stop the unit if the device is unavailable
  • x-systemd.idle-timeout
    • stop the unit if it is inactive

I also changed the device label should I want to refer to device in other ways in the future.

sudo e2label /dev/sdb1 ADATA_HV620

Setup a backup service

Here’s the service file, which runs the backup job.

[Unit]
Description=Backup system to external drive
OnFailure=status-email-jotham@%i.service
Requires=media-ADATA_HV620.mount
After=media-ADATA_HV620.mount
# could use the RequiresMountsFor=/media/ADATA_HV620, which 
# implies requires and after, but only for
# mounts where the 'noauto' option is not used in fstab

[Service]
Type=simple
ExecStart=/usr/bin/rsync -avzr --exclude=snapshot* --exclude=/dev* --exclude=/proc* --exclude=/sys* --exclude=/tmp* --exclude=/run* --exclude=/mnt* --exclude=/media* --exclude=/var/lib/pacman/sync* / /media/archLenFlex_backup/

[Install]
WantedBy=multi-user.target

Recall from a recent post that the OnFailure activates another systemd unit, which emails me the status of the failed job. This template unit has been very convenient, and could also be used as the value for the ExecStartPost element of unit files, emailing the status of successful jobs. Note also the Requires and After lines. Both enforce a dependency of the servie on the mount unit being active. However, without After, the backup service and mount unit could be started in parallel instead of serially.

Test it out as follows

sudo systemctl start backup-external-disk.service

Great, that works. And here’s the timer file for the service.

[Unit]
Description=External backup timer

[Timer]
OnCalendar=*-*-* 08,11,14,17,20,23:00:00
Persistent=True

[Install]
WantedBy=timers.target

Now just enable and start it.

sudo systemctl start backup-external-disk.timer
sudo systemctl enable backup-external-disk.timer

Outstanding questions

I am having one issue with the automounts. It is described on the Arch forums, here. Briefly, if I remove or stop an automounted device and try to start it again (manually, or by acessing the mount path), the mount service fails. If I disbable fsck for the device in the fstab, I can get the device to mount again provided I reload the systemd daemon. It’d be nice if neither of these (daemon-reload or disabling fsck) were necessary. Suggestions are welcome!

Go Top
comments powered by Disqus