From fb5552b88f3731576735edacd23dc3838d904493 Mon Sep 17 00:00:00 2001 From: Matt Molyneaux Date: Sun, 26 Sep 2021 23:43:59 +0100 Subject: [PATCH] Unit files for systemd --- .gitignore | 2 ++ Makefile | 30 +++++++++++++++++++++--------- backup.timer | 12 ++++++++++++ templates/backup.service | 19 +++++++++++++++++++ templates/clean-cache.service | 18 ++++++++++++++++++ 5 files changed, 72 insertions(+), 9 deletions(-) create mode 100644 .gitignore rewrite Makefile (82%) create mode 100644 backup.timer create mode 100644 templates/backup.service create mode 100644 templates/clean-cache.service diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..139fdba --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ +/backup.service +/clean-cache.service diff --git a/Makefile b/Makefile dissimilarity index 82% index b82e1a6..c7fdf54 100644 --- a/Makefile +++ b/Makefile @@ -1,9 +1,21 @@ -sources = $(shell find . -name \*.sh -type f) -prefix ?= /usr/local - -.PHONY: all -all: $(sources) - -.PHONY: install -install: $(sources) - install -t $(prefix)/bin $(sources) +scripts = $(shell find . -maxdepth 1 -name \*.sh -type f) +templates = $(shell find templates -maxdepth 1 -type f -printf "%f\n") +units = $(shell find -maxdepth 1 -name \*.service -or -name \*.timer) +export prefix ?= /usr/local + +define TEMPLATE_RULE +$(1): $(2) + cat $(2) | envsubst > $(1) +endef + +.PHONY: all +all: $(templates) + +.PHONY: install +install: $(scripts) $(units) + install -D -t $(prefix)/bin $(scripts) + install -D -t $(prefix)/lib/systemd/system $(units) + install -D -t $(prefix)/lib/systemd/user $(units) + + +$(foreach _tmpl,$(templates),$(eval $(call TEMPLATE_RULE,$(_tmpl),templates/$(_tmpl)))) diff --git a/backup.timer b/backup.timer new file mode 100644 index 0000000..c544b7a --- /dev/null +++ b/backup.timer @@ -0,0 +1,12 @@ +[Unit] +Description=Backup with restic +Requires=backup.service + +[Timer] +Unit=backup.service +OnCalendar=hourly +Persistent=true +RandomizedDelaySec=900 + +[Install] +WantedBy=timers.target diff --git a/templates/backup.service b/templates/backup.service new file mode 100644 index 0000000..b91d81d --- /dev/null +++ b/templates/backup.service @@ -0,0 +1,19 @@ +[Unit] +Description=Backup with restic +Wants=backup.timer +StartLimitInterval=200 +StartLimitBurst=5 + +[Service] +Nice=19 +IOSchedulingClass=idle +IOSchedulingPriority=7 +Type=oneshot +ExecStart=${prefix}/bin/backup.sh backup +Environment=BACKUP_CONFIG=/etc/moggers87/backuprc +Environment=HOME=/etc/moggers87 +Restart=on-failure +RestartSec=30 + +[Install] +WantedBy=multi-user.target diff --git a/templates/clean-cache.service b/templates/clean-cache.service new file mode 100644 index 0000000..f6be8b5 --- /dev/null +++ b/templates/clean-cache.service @@ -0,0 +1,18 @@ +[Unit] +Description=Clean cache in user home +Wants=clean-cache.timer +StartLimitInterval=200 +StartLimitBurst=5 + +[Service] +Nice=19 +IOSchedulingClass=idle +IOSchedulingPriority=7 +Type=oneshot +ExecStart=${prefix}/bin/clean-cache.sh clean +Environment=HOME=%h +Restart=on-failure +RestartSec=30 + +[Install] +WantedBy=multi-user.target -- 2.11.4.GIT