Feat: Create new task for automatic backup on dd

This commit is contained in:
Bertrand Benjamin 2019-08-05 21:36:31 +02:00
parent 28562e79f4
commit 64c6c0fee7
4 changed files with 147 additions and 0 deletions

View File

@ -0,0 +1,3 @@
ACTION=="add", SUBSYSTEM=="bdi", DEVPATH=="/devices/virtual/bdi/*",
TAG+="systemd", ENV{SYSTEMD_WANTS}="automatic-backup.service"

View File

@ -0,0 +1,4 @@
[Service]
Type=oneshot
ExecStart=/etc/backups/run.sh

99
files/dd_borg/run.sh Normal file
View File

@ -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

41
tasks/dd_backup.yml Normal file
View File

@ -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