Commit 7ec35b21 authored by nimrod's avatar nimrod
Browse files

Simpler backup script.

Backups across the different services have been changed. There are now
snapshots for each services with copies saved for a few weeks. All of
them under /var/backups. The backup script doesn't need to work around
btrfs subvolumes. Instead just snapshotting /var/backups and rsync'ing
to a removable media. The old script is kept in source for reference.
parent c10d7210
Loading
Loading
Loading
Loading
+62 −0
Original line number Diff line number Diff line
#!/bin/sh
set -eu

is_btrfs() {
    [ "$(df --output=fstype "$1" | tail +2)" = 'btrfs' ]
}

is_subvolume() {
    btrfs subvolume show "$1" >/dev/null 2>&1
}

root_subvolume() {
    path="$1"
    until is_subvolume "$path" || [ "$path" = '/' ]
    do
        path="$(dirname "$path")"
    done
    echo "$path"
}

cleanup() {
    exit_code="$?"
    if [ -n "${snapshot:-}" ]
    then
        btrfs subvolume delete "$snapshot"
    fi
    sync --file-system "$destination"
    if [ "$exit_code" -eq 0 ]
    then
        curl -so /dev/null "https://notify.shore.co.il/send?message=Backup%20finished%20successfully%20on%20$(hostname -s)."
    else
        curl -so /dev/null  "https://notify.shore.co.il/send?message=Backup%20failed%20on%20$(hostname -s)."
    fi
}

if [ "$#" -ne 1 ]
then
    echo "Usage: $0 destination" >&2
    exit 1
fi

if ! is_btrfs /var/backups
then
    echo '/var/backups is not on btrfs filesystem. Exiting.' >&2
    exit 1
fi

trap 'cleanup' INT QUIT EXIT TERM

destination="$1"
volume="$(root_subvolume /var/backups)"
snapshot="$(mktemp --dry-run "--tmpdir=$volume")"
btrfs subvolume snapshot -r "$volume" "$snapshot"
source="/var/backups"
source="${snapshot}${source#$volume}/"

sync --file-system /var/backups
rsync --archive \
      --delete \
      --info=progress2 \
      "$source" \
      "$destination"
+2 −2
Original line number Diff line number Diff line
@@ -9,11 +9,11 @@

- name: Copy btrfs backup script
  ansible.builtin.copy:
    dest: /usr/local/sbin/btrfs-backup
    dest: /usr/local/sbin/offsite-backup
    group: root
    mode: preserve
    owner: root
    src: btrfs-backup
    src: offsite-backup

- name: Install btrfs scrub services and timers
  loop: &loop