From b9601cf46a75b373f6b6b902d7ac2be38ab2ffe8 Mon Sep 17 00:00:00 2001 From: Ludovic Pouzenc Date: Tue, 15 Jun 2021 16:45:20 +0200 Subject: borg-family_0.2-1 --- borg-family-0.2/src/etc/bfhooks | 43 +++++++++++++++++++++++++++++++ borg-family-0.2/src/etc/confvars | 4 +++ borg-family-0.2/src/etc/excludes.d/home | 4 +++ borg-family-0.2/src/etc/excludes.d/rootfs | 5 ++++ borg-family-0.2/src/sbin/bfenv | 31 ++++++++++++++++++++++ borg-family-0.2/src/sbin/bfenv2 | 31 ++++++++++++++++++++++ borg-family-0.2/src/sbin/bfrun | 32 +++++++++++++++++++++++ borg-family-0.2/src/sbin/bfwhat | 24 +++++++++++++++++ 8 files changed, 174 insertions(+) create mode 100755 borg-family-0.2/src/etc/bfhooks create mode 100644 borg-family-0.2/src/etc/confvars create mode 100644 borg-family-0.2/src/etc/excludes.d/home create mode 100644 borg-family-0.2/src/etc/excludes.d/rootfs create mode 100755 borg-family-0.2/src/sbin/bfenv create mode 100755 borg-family-0.2/src/sbin/bfenv2 create mode 100755 borg-family-0.2/src/sbin/bfrun create mode 100755 borg-family-0.2/src/sbin/bfwhat (limited to 'borg-family-0.2/src') diff --git a/borg-family-0.2/src/etc/bfhooks b/borg-family-0.2/src/etc/bfhooks new file mode 100755 index 0000000..c7ce378 --- /dev/null +++ b/borg-family-0.2/src/etc/bfhooks @@ -0,0 +1,43 @@ +#!/bin/bash -e +mount_bind_rootfs() { + if ! mount | grep -q /rootfs; then + mkdir -p /rootfs + mount --bind / /rootfs + fi +} + +mount_efi() { + if ! mount | grep -q /boot/efi; then + mount /boot/efi + fi +} + +umount_bind_rootfs() { + if mount | grep -q /rootfs; then + umount /rootfs || lsof -n | grep /rootfs + rmdir /rootfs + fi +} + +umount_efi() { + if mount | grep -q /boot/efi; then + umount /boot/efi || lsof -n | grep /boot/efi + fi +} + +case $1 in + before) + echo "Start of backup: $2 to $BORG_REPO" + case $2 in + rootfs) apt-get clean || true; mount_bind_rootfs;; + boot-efi) mount_efi ;; + esac + ;; + after) + case $2 in + rootfs) umount_bind_rootfs ;; + boot-efi) umount_efi ;; + esac + echo "End of backup: $2 to $BORG_REPO" + ;; +esac diff --git a/borg-family-0.2/src/etc/confvars b/borg-family-0.2/src/etc/confvars new file mode 100644 index 0000000..4737195 --- /dev/null +++ b/borg-family-0.2/src/etc/confvars @@ -0,0 +1,4 @@ +# This file is used by bfenv and bfrun (bash scripts) +borg_init_opts=( -e repokey-blake2 --make-parent-dirs -v ) +borg_create_opts=( --one-file-system --compression zstd --exclude-caches --exclude-if-present=NOBACKUPDIR.TAG --keep-exclude-tags --list --filter=AMEi-x --stats --show-rc -v ) +quiet=0 diff --git a/borg-family-0.2/src/etc/excludes.d/home b/borg-family-0.2/src/etc/excludes.d/home new file mode 100644 index 0000000..c7aead3 --- /dev/null +++ b/borg-family-0.2/src/etc/excludes.d/home @@ -0,0 +1,4 @@ +**/.cache/* +/home/*/.mozilla/**/datareporting +/home/*/.thunderbird/**/global-messages-db.sqlite +/home/*/.thunderbird/**/ImapMail diff --git a/borg-family-0.2/src/etc/excludes.d/rootfs b/borg-family-0.2/src/etc/excludes.d/rootfs new file mode 100644 index 0000000..b61797b --- /dev/null +++ b/borg-family-0.2/src/etc/excludes.d/rootfs @@ -0,0 +1,5 @@ +/rootfs/var/cache/apt/* +**/.cache/* +/rootfs/home/*/.mozilla/**/datareporting +/rootfs/home/*/.thunderbird/**/global-messages-db.sqlite +/rootfs/home/*/.thunderbird/**/ImapMail diff --git a/borg-family-0.2/src/sbin/bfenv b/borg-family-0.2/src/sbin/bfenv new file mode 100755 index 0000000..756ef62 --- /dev/null +++ b/borg-family-0.2/src/sbin/bfenv @@ -0,0 +1,31 @@ +#!/bin/bash +umask 0077 +source /etc/borg-family/confvars +source /etc/borg-family/envvars +PATH="/etc/borg-family:$PATH" + +if [ \! -r /etc/borg-family/passphrase ]; then + echo "No passphrase (repokey) found, creating a new one" >&2 + touch /etc/borg-family/passphrase + chmod 600 /etc/borg-family/passphrase + pwgen 32 1 >> /etc/borg-family/passphrase + ls -l /etc/borg-family/passphrase >&2 + echo "You NEED to store it in a password manager to be able to restore backups" >&2 +fi + +if [ \! -r /etc/borg-family/id_rsa_borg ]; then + echo "No SSH key found, creating a new one" >&2 + ssh-keygen -N "" -C "$(id -un)_borg@$(hostname)" -f /etc/borg-family/id_rsa_borg \ + && cat /etc/borg-family/id_rsa_borg.pub +fi + +borg init "${borg_init_opts[@]}" 2>&1 | grep -vE '^A repository already exists' >&2 + +if ! borg check "${borg_check_opts[@]}"; then + echo "Showing BORG_* env variables (see /etc/borg-family/envvars) :" >&2 + env | grep ^BORG_ >&2 + echo "End of BORG_* env variables" >&2 + echo >&2 + echo "Can't access to or check the borg repository, exiting, no backup made" >&2 + exit 1 +fi diff --git a/borg-family-0.2/src/sbin/bfenv2 b/borg-family-0.2/src/sbin/bfenv2 new file mode 100755 index 0000000..6e5adeb --- /dev/null +++ b/borg-family-0.2/src/sbin/bfenv2 @@ -0,0 +1,31 @@ +#!/bin/bash +umask 0077 +source /etc/borg-family/confvars +source /etc/borg-family/envvars2 +PATH="/etc/borg-family:$PATH" + +if [ \! -r /etc/borg-family/passphrase2 ]; then + echo "No passphrase2 (repokey) found, creating a new one" >&2 + touch /etc/borg-family/passphrase2 + chmod 600 /etc/borg-family/passphrase2 + pwgen 32 1 >> /etc/borg-family/passphrase2 + ls -l /etc/borg-family/passphrase2 >&2 + echo "You NEED to store it in a password manager to be able to restore backups" >&2 +fi + +if [ \! -r /etc/borg-family/id_rsa_borg ]; then + echo "No SSH key found, creating a new one" >&2 + ssh-keygen -N "" -C "$(id -un)_borg@$(hostname)" -f /etc/borg-family/id_rsa_borg \ + && cat /etc/borg-family/id_rsa_borg.pub +fi + +LANG=C borg init "${borg_init_opts[@]}" 2>&1 | grep -vE '^A repository already exists' >&2 + +if ! borg list > /dev/null; then + echo "Showing BORG_* env variables (see /etc/borg-family/envvars) :" >&2 + env | grep ^BORG_ >&2 + echo "End of BORG_* env variables" >&2 + echo >&2 + echo "Can't access to or check the borg repository, exiting, no backup made" >&2 + exit 1 +fi diff --git a/borg-family-0.2/src/sbin/bfrun b/borg-family-0.2/src/sbin/bfrun new file mode 100755 index 0000000..b0f80dd --- /dev/null +++ b/borg-family-0.2/src/sbin/bfrun @@ -0,0 +1,32 @@ +#!/bin/bash +for e in bfenv bfenv2 +do + source $e + bfwhat | while IFS=' ' read -r mpe dev; do + mp=$(echo -e "$mpe") # mp: interpret escapings that may be present in /proc/mounts (\040 for space...) + name=${mp// /_}; # name: replace space by underscore + name=${mp//\//-}; name=${name/-/} # name: replace slash by dash, remove the leading one + if [[ "$mp $name $dev" =~ "--" || "$mp $name $dev" =~ ".." || "$mp $name $dev" =~ "[|&;()<>]" ]]; then + echo "Skipping $mpe because of shell unsafe characters" >&2 + continue + fi + + comment=$(blkid -- "$dev") + if [ -r "/etc/borg-family/excludes.d/$name" ]; then + runtime_args=( --comment="$comment" --exclude-from="/etc/borg-family/excludes.d/$name" ) + else + runtime_args=( --comment="$comment" ) + fi + + bfhooks before "$name" && \ + borg create "${runtime_args[@]}" "${borg_create_opts[@]}" "::{hostname}-$name-{now:%Y-%m-%d}" "$mp" + rc1=$? + bfhooks after "$name" + rc2=$? + if [ "$rc1" -ne 0 -o "$rc2" -ne 0 ]; then + echo "Errors during $name backup, return codes $rc1 (bfhook before && borg create) and $rc2 (bfhook after)" >&2 + else + [ "x$quiet" == "x1" ] || echo "Success for $name backup" + fi + done +done diff --git a/borg-family-0.2/src/sbin/bfwhat b/borg-family-0.2/src/sbin/bfwhat new file mode 100755 index 0000000..bf9cf4d --- /dev/null +++ b/borg-family-0.2/src/sbin/bfwhat @@ -0,0 +1,24 @@ +#!/bin/bash +what-from-proc-mounts() { + # Special case for "/", show it as /rootfs (see hook_* files). Helps saving initial /dev nodes files behind udev + awk '( $2 == "/" ) { print "/rootfs",$1 }' /proc/mounts + + # Backup all non "/", non-removable, mounted filesystems which device is also shown in /sys/block + tmp=$(mktemp) + grep 0 /sys/block/*/removable | sed -e 's#^/sys/block#^/dev#' -e 's#/removable:0$##' >> "$tmp" + grep -Ef "$tmp" /proc/mounts | awk '( $2 !~ /^\/(rootfs|$)/ ) { print $2,$1 }' + rm -- "$tmp" +} + +if [ -r /etc/borg-family/what.override ]; then + cat /etc/borg-family/what.override +else + if [ -r /etc/borg-family/what.include ]; then + cat /etc/borg-family/what.include + fi + if [ -r /etc/borg-family/what.exclude ]; then + what-from-proc-mounts | grep -vEf /etc/borg-family/what.exclude + else + what-from-proc-mounts + fi +fi -- cgit v1.2.3