From 64c6c0fee72110f2c6c701b03ce27dbe11ab5e64 Mon Sep 17 00:00:00 2001 From: Bertrand Benjamin Date: Mon, 5 Aug 2019 21:36:31 +0200 Subject: [PATCH] Feat: Create new task for automatic backup on dd --- files/dd_borg/40-backup.rules | 3 + files/dd_borg/automatic-backup.service | 4 + files/dd_borg/run.sh | 99 +++++++++++++++++++++++++ tasks/dd_backup.yml | 41 ++++++++++ 4 files changed, 147 insertions(+) create mode 100644 files/dd_borg/40-backup.rules create mode 100644 files/dd_borg/automatic-backup.service create mode 100644 files/dd_borg/run.sh create mode 100644 tasks/dd_backup.yml diff --git a/files/dd_borg/40-backup.rules b/files/dd_borg/40-backup.rules new file mode 100644 index 0000000..b172ac4 --- /dev/null +++ b/files/dd_borg/40-backup.rules @@ -0,0 +1,3 @@ +ACTION=="add", SUBSYSTEM=="bdi", DEVPATH=="/devices/virtual/bdi/*", +TAG+="systemd", ENV{SYSTEMD_WANTS}="automatic-backup.service" + diff --git a/files/dd_borg/automatic-backup.service b/files/dd_borg/automatic-backup.service new file mode 100644 index 0000000..c0209d2 --- /dev/null +++ b/files/dd_borg/automatic-backup.service @@ -0,0 +1,4 @@ +[Service] +Type=oneshot +ExecStart=/etc/backups/run.sh + diff --git a/files/dd_borg/run.sh b/files/dd_borg/run.sh new file mode 100644 index 0000000..106d487 --- /dev/null +++ b/files/dd_borg/run.sh @@ -0,0 +1,99 @@ +#!/bin/bash -ue + +# The udev rule is not terribly accurate and may trigger our service before +# the kernel has finished probing partitions. Sleep for a bit to ensure +# the kernel is done. +# +# This can be avoided by using a more precise udev rule, e.g. matching +# a specific hardware path and partition. +sleep 5 + +# +# Script configuration +# + +# The backup partition is mounted there +MOUNTPOINT=/mnt/backup + +# This is the location of the Borg repository +TARGET=$MOUNTPOINT/Combava + +# Archive name schema +DATE=$(date --iso-8601)-$(hostname) + +# This is the file that will later contain UUIDs of registered backup drives +DISKS=/etc/backups/backup.disks + +# Find whether the connected block device is a backup drive +for uuid in $(lsblk --noheadings --list --output uuid) +do + if grep --quiet --fixed-strings $uuid $DISKS; then + break + fi + uuid= +done + +if [ ! $uuid ]; then + echo "No backup disk found, exiting" + exit 0 +fi + +echo "Disk $uuid is a backup disk" +partition_path=/dev/disk/by-uuid/$uuid +# Mount file system if not already done. This assumes that if something is already +# mounted at $MOUNTPOINT, it is the backup drive. It won't find the drive if +# it was mounted somewhere else. +(mount | grep $MOUNTPOINT) || mount $partition_path $MOUNTPOINT +drive=$(lsblk --inverse --noheadings --list --paths --output name $partition_path | head --lines 1) +echo "Drive path: $drive" + +# +# Create backups +# + +# Options for borg create +BORG_OPTS="--stats --one-file-system --compression lz4 --checkpoint-interval 86400" + +# Set BORG_PASSPHRASE or BORG_PASSCOMMAND somewhere around here, using export, +# if encryption is used. + +# No one can answer if Borg asks these questions, it is better to just fail quickly +# instead of hanging. +export BORG_RELOCATED_REPO_ACCESS_IS_OK=no +export BORG_UNKNOWN_UNENCRYPTED_REPO_ACCESS_IS_OK=no + +# Log Borg version +borg --version + +echo "Starting backup for $DATE" + +# This is just an example, change it however you see fit +borg create $BORG_OPTS \ + --exclude /media/documents/games/ \ + --exclude /media/documents/musique/ \ + --exclude /media/documents/photos/ \ + $TARGET::$DATE-$$-documents \ + /media/documents/ + +# /home is often a separate partition / file system. +# Even if it isn't (add --exclude /home above), it probably makes sense +# to have /home in a separate archive. +borg create $BORG_OPTS \ + --exclude 'sh:/home/*/.cache' \ + $TARGET::$DATE-$$-home \ + /home/ + +echo "Completed backup for $DATE" + +# Just to be completely paranoid +sync + +if [ -f /etc/backups/autoeject ]; then + umount $MOUNTPOINT + hdparm -Y $drive +fi + +if [ -f /etc/backups/backup-suspend ]; then + systemctl suspend +fi + diff --git a/tasks/dd_backup.yml b/tasks/dd_backup.yml new file mode 100644 index 0000000..c206cc9 --- /dev/null +++ b/tasks/dd_backup.yml @@ -0,0 +1,41 @@ +--- +- name: Install autofs + pacman: + name: borg + state: present + +- name: copy udev rule in /etc/backups + copy: + src: files/dd_borg/40-backup.rules + dest: /etc/backups/40-backup.rules + backup: yes + +- name: Link it to /etc/udev + file: + src: /etc/backups/40-backup.rules + dest: /etc/udev/rules.d/40-backup.rules + state: link + +- name: copy service in /etc/backups + copy: + src: files/dd_borg/automatic-backup.service + dest: /etc/backups/automatic-backup.service + backup: yes + +- name: Link it to /etc/udev + file: + src: /etc/backups/automatic-backup.service + dest: /etc/udev/rules.d/automatic-backup.service + state: link + +- name: copy run scritp + template: + src: files/dd_borg/run.sh + dest: /etc/backups/run.sh + +- name: Reload systemctl + systemd: + daemon_reload: yes + +- name: Reload udev + shell: udevadm control --reload