Add open_backup.sh

Add open_backup, a script to mount the encrypted backup drive created in
arch_backup.
This commit is contained in:
ducoterra
2022-02-04 13:49:02 -05:00
parent c6db306a0e
commit 672371887c
2 changed files with 55 additions and 0 deletions

View File

@@ -18,6 +18,14 @@
group: root
mode: '0744'
become: yes
- name: Template open_backup.sh
ansible.builtin.template:
src: open_backup.sh.j2
dest: /usr/local/scripts/open_backup.sh
owner: root
group: root
mode: '0744'
become: yes
- name: Ensure hourly backups of each item in backups
ansible.builtin.cron:
name: "hourly backup of {{ item }}"

View File

@@ -0,0 +1,47 @@
# Backup info
export BACKUP_DRIVE_UUID={{ disk.uuid }}
export BACKUP_DRIVE_PASSWORD={{ disk.password }}
export BACKUP_DRIVE_NAME=luks-$BACKUP_DRIVE_UUID
export BACKUP_DRIVE_MNT=/mnt/$BACKUP_DRIVE_NAME
# For notifications
export DBUS_SESSION_BUS_ADDRESS=unix:path=/run/user/{{ notifications.user.uid }}/bus
export USER={{ notifications.user.name }}
function log {
LEVEL=$1
MESSAGE=$2
echo "$LEVEL: $MESSAGE"
}
# Unlock backup drive
if [ -L /dev/disk/by-uuid/$BACKUP_DRIVE_UUID ]; then
cryptsetup luksOpen /dev/disk/by-uuid/$BACKUP_DRIVE_UUID $BACKUP_DRIVE_NAME --key-file=$BACKUP_DRIVE_PASSWORD
cryptsetup status /dev/mapper/$BACKUP_DRIVE_NAME
else
log "ERROR" "Drive $BACKUP_DRIVE_UUID could not be found."
exit 1
fi
if [ $? = 0 ]; then
log "INFO" "Drive $BACKUP_DRIVE_UUID unlocked"
else
log "ERROR" "Drive $BACKUP_DRIVE_UUID could not be unlocked."
exit 1
fi
# Create /mnt/uuid
log "INFO" "Creating $BACKUP_DRIVE_NAME"
mkdir -p $BACKUP_DRIVE_MNT
# Mount /mnt/uuid
log "INFO" "Mounting /dev/mapper/$BACKUP_DRIVE_NAME"
mount -t btrfs -o compress=zstd /dev/mapper/$BACKUP_DRIVE_NAME $BACKUP_DRIVE_MNT
if [ $? = 0 ]; then
log "INFO" "Drive $BACKUP_DRIVE_UUID mounted at $BACKUP_DRIVE_MNT"
exit 0
else
log "ERROR" "Drive $BACKUP_DRIVE_NAME could not be mounted."
exit 1
fi