commit 9c0be458fde1577549ac916ebfd40a5ce4259dec Author: Jacob R. Edwards <jacob@jacobedwards.org> Date: Thu, 27 Apr 2023 19:32:47 -0700 Add srvbackup script and Makefile Diffstat:
A | Makefile | | | 7 | +++++++ |
A | srvbackup | | | 81 | +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
2 files changed, 88 insertions(+), 0 deletions(-)
diff --git a/Makefile b/Makefile @@ -0,0 +1,7 @@ +install: + install -o root -g bin -m 0755 srvbackup /usr/local/bin/srvbackup + +uninstall: + rm -f /usr/local/bin/backup + # You may also wish to run + # # rm -rf /var/backup diff --git a/srvbackup b/srvbackup @@ -0,0 +1,81 @@ +#!/bin/sh +# Copyright 2023 Jacob R. Edwards +# srvbackup, a server backup script +# +# This script is to be put on the server and is meant to be used +# by both the server to generate the backup and by clients connecting +# through ssh(1) to retrive the backup. +# +# The backup is available to everyone (permissions-wise) but will +# in the future be able to be encrypted with an age(1) key if +# /etc/backup/key exists. +# +# Example usage: +# +# Make a backup at the beginning of every day using +# a crontab(5): +# 0 0 * * * srvbackup make +# +# Update 'local-backup.tar.age' from the 'server.dom': +# ssh unprivledged@server.dom srvbackup get "$(sha256 < local-backup.tar.age)" > tmp +# (test -s tmp && mv tmp local-backup.tar.age) || rm tmp + +set -e + +dir=/var/backup +backup="$dir"/backup.tar +checksum="$dir"/sha256 + +createdir() { + ! test -d "$dir" && + mkdir -m 0755 "$dir" # not -p +} + +update() ( + IFS=' + ' + cd / + pax -w -uf "$backup" $(</etc/backup/list) +) + +if test $# -eq 0; then + c=make +else + c="$1" + shift +fi + +case "$c" in +(make) + createdir + update "$backup" + sha256 < "$backup" > "$checksum" + ;; +(path) + echo "$backup" + ;; +(sha256) + cat "$checksum" + ;; +(get) + if test "$1" -a "$1" = -c + then + shift + write=compress + else + write=cat + fi + test "$1" -a "$1" = "$(<"$checksum")" && + exit 0 + $write < "$backup" + ;; +(*) + { + echo "$c: Command not known" + echo "usage: srvbackup make + path + sha256 + get [-c] [sha256] # (compress(1) if -c is given, only if checksums differ)" + } 1>&2 + exit 1 +esac