lvm_self_backup: Add timeout and mount/umount tasks to systemd service

This patch adds a timeout to notice a failure, when the container hangs and does not do anything. This happened already and with moving the mount and unmount and snapshot management to the service file, we ensure that they are properly removed after a timeout.

Thanks to @Histalek for the service config (https://git.histalek.de/histalek-de/infrastructure/-/blob/main/roles/lvm_backup/templates/backup-lvm.service)
This commit is contained in:
saibotk 2021-10-05 16:49:34 +02:00
parent abe21edb72
commit d6f99396c3
Signed by: saibotk
GPG key ID: 67585F0065E261D5
2 changed files with 27 additions and 20 deletions

View file

@ -3,4 +3,31 @@ Description=Start lvm backup using duplicity.
[Service]
Type=oneshot
TimeoutSec=2h
# Creating snapshots
{% for item in backup_vols %}
ExecStartPre=/usr/sbin/lvcreate -L 1G -n {{ item.lv_name }}_snap -s {{ item.vg_name }}/{{ item.lv_name }}
{% endfor %}
# Mounting snapshots
{% for item in backup_vols %}
ExecStartPre=/usr/bin/mount -o ro,nosuid,noexec{% if item.fstype is defined and item.fstype == "xfs" %},nouuid{% endif %} -t {{ item.fstype | default("ext4") }} /dev/{{ item.vg_name }}/{{ item.lv_name }}_snap /backup_snapshots/{{ item.vg_name }}-{{ item.lv_name }}
{% endfor %}
ExecStart=/usr/local/bin/backup-lvm
# Make sure duplicity container is gone
ExecStopPost=/usr/bin/docker rm --force duplicity-backup-lvm
# Unmount snapshots
{% for item in backup_vols %}
ExecStopPost=/usr/bin/umount /dev/{{ item.vg_name }}/{{ item.lv_name }}_snap
{% endfor %}
# Remove snapshots
{% for item in backup_vols %}
ExecStopPost=/usr/sbin/lvremove -f {{ item.vg_name }}/{{ item.lv_name }}_snap
{% endfor %}

View file

@ -74,16 +74,6 @@ function backup () {(
echo "LVM backup started:"
echo "Creating snapshots:"
{% for item in backup_vols %}
lvcreate -L 1G -n {{ item.lv_name }}_snap -s {{ item.vg_name }}/{{ item.lv_name }}
{% endfor %}
echo "Mount snapshots:"
{% for item in backup_vols %}
mount -o ro,nosuid,noexec{% if item.fstype is defined and item.fstype == "xfs" %},nouuid{% endif %} -t {{ item.fstype | default("ext4") }} /dev/{{ item.vg_name }}/{{ item.lv_name }}_snap /backup_snapshots/{{ item.vg_name }}-{{ item.lv_name }}
{% endfor %}
backup
success=$?
if [ $success -gt 0 ]; then
@ -92,14 +82,4 @@ fi
echo $success > {{ backup_status_directory }}/backup.status
echo "Unmount snapshots:"
{% for item in backup_vols %}
umount /dev/{{ item.vg_name }}/{{ item.lv_name }}_snap
{% endfor %}
echo "Remove snapshots:"
{% for item in backup_vols %}
lvremove -f {{ item.vg_name }}/{{ item.lv_name }}_snap
{% endfor %}
exit $success