From 6ef597701fea586fc41b7d871fd86d4dce655e0b Mon Sep 17 00:00:00 2001 From: maride Date: Wed, 7 Aug 2024 11:02:41 +0200 Subject: [PATCH] Init --- .gitignore | 1 + DEBIAN/conffiles | 1 + DEBIAN/control | 6 ++++++ DEBIAN/postinst | 10 ++++++++++ DEBIAN/postrm | 0 DEBIAN/preinst | 0 DEBIAN/prerm | 0 README.md | 21 ++++++++++++++++++++ etc/cron.daily/tupper | 4 ++++ etc/tupper.conf | 8 ++++++++ usr/sbin/tupper | 45 +++++++++++++++++++++++++++++++++++++++++++ 11 files changed, 96 insertions(+) create mode 100644 .gitignore create mode 100644 DEBIAN/conffiles create mode 100644 DEBIAN/control create mode 100755 DEBIAN/postinst create mode 100755 DEBIAN/postrm create mode 100755 DEBIAN/preinst create mode 100755 DEBIAN/prerm create mode 100644 README.md create mode 100755 etc/cron.daily/tupper create mode 100644 etc/tupper.conf create mode 100755 usr/sbin/tupper diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..c00df13 --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +*.deb diff --git a/DEBIAN/conffiles b/DEBIAN/conffiles new file mode 100644 index 0000000..87adf8e --- /dev/null +++ b/DEBIAN/conffiles @@ -0,0 +1 @@ +/etc/tupper.conf diff --git a/DEBIAN/control b/DEBIAN/control new file mode 100644 index 0000000..cbaa6e6 --- /dev/null +++ b/DEBIAN/control @@ -0,0 +1,6 @@ +Package: tupper +Version: 0.1 +Maintainer: Martin "maride" Dessauer +Architecture: all +Depends: cron (>= 3.0), borgbackup (<< 2), sshfs (>= 3) +Description: Some drops of glue between SSHFS, Borg and cron diff --git a/DEBIAN/postinst b/DEBIAN/postinst new file mode 100755 index 0000000..da34a8a --- /dev/null +++ b/DEBIAN/postinst @@ -0,0 +1,10 @@ +#!/bin/bash + +if [ "$1" == "configure" ]; then + chown root:root /etc/tupper.conf + chmod 600 /etc/tupper.conf + chown root:root /etc/cron.daily/tupper + chmod 755 /etc/cron.daily/tupper + chown root:root /usr/sbin/tupper + chmod 755 /usr/sbin/tupper +fi diff --git a/DEBIAN/postrm b/DEBIAN/postrm new file mode 100755 index 0000000..e69de29 diff --git a/DEBIAN/preinst b/DEBIAN/preinst new file mode 100755 index 0000000..e69de29 diff --git a/DEBIAN/prerm b/DEBIAN/prerm new file mode 100755 index 0000000..e69de29 diff --git a/README.md b/README.md new file mode 100644 index 0000000..5ae947d --- /dev/null +++ b/README.md @@ -0,0 +1,21 @@ +# tupper + +Drops of glue between [SSHFS](https://github.com/libfuse/sshfs), [Borg](https://www.borgbackup.org/) and [cron](https://github.com/vixie/cron) + +## Features + +- Auto-mounts specified SSHFS as required +- Encrypted, deduplicated backups thanks to Borg +- Backup of specified directories to SSHFS +- Daily cron scheduler + +## Usage + +Enter SSH credentials, host and destination directory in `/etc/tupper.conf`, as well as the directories to back up. + +Then either run `sudo tupper` or wait for the daily cron scheduler to kick in. + +## Building + +`dpkg-deb --build . tupper_0.1_any.deb` + diff --git a/etc/cron.daily/tupper b/etc/cron.daily/tupper new file mode 100755 index 0000000..824aac8 --- /dev/null +++ b/etc/cron.daily/tupper @@ -0,0 +1,4 @@ +#!/bin/bash + +/usr/sbin/tupper 2>&1 | tee --append /var/log/tupper.log + diff --git a/etc/tupper.conf b/etc/tupper.conf new file mode 100644 index 0000000..10a4ddc --- /dev/null +++ b/etc/tupper.conf @@ -0,0 +1,8 @@ +### tupper.conf - config file for tupper +# +## Example: +# SSH_URI='backupuser@yournas.local:/filez' +# SSH_PASS='p4$$w0rcl' +# BORG_PASSPHRASE='backuppass' +# TARGET='/var/lib/docker/volumes' + diff --git a/usr/sbin/tupper b/usr/sbin/tupper new file mode 100755 index 0000000..3b0d56a --- /dev/null +++ b/usr/sbin/tupper @@ -0,0 +1,45 @@ +#!/bin/bash -e + +# Inform user +echo "Welcome to tupper! Running on $(date)" + +# Check access rights of config file +CONF="/etc/tupper.conf" +if [ "$(stat -c '%U' $CONF)" != "root" ] && [ "$(stat -c '%A' $CONF)" = "*------" ]; then + echo "Config file $CONF must be owned by root without any rights for group and other (**00) set" 1>&2 + exit 1 +fi +# Include config file +source "$CONF" + +# Check if required variables are set +if [ "$SSH_URI$SSH_PASS$TARGET" = "" ]; then + echo "Config file $CONF must contain SSH_URI, SSH_PASS and TARGET" 1>&2 + exit 1 +fi + +# Check privileges +if [ "$UID" -ne 0 ]; then + echo "$0 must be run as root. Quitting." 1>2 + exit 1 +fi + +# Create temporary mountpoint +MOUNTPOINT="$(mktemp --directory)" + +# Check if SSHFS needs to be mounted +if [ "$(mount | grep $MOUNTPOINT | wc -l)" -eq 0 ]; then + echo "$SSH_PASS" | sshfs -o password_stdin "$SSH_URI" "$MOUNTPOINT" +fi + +# Backup +export BORG_PASSPHRASE +borg create --stats "$MOUNTPOINT::"'{now}' "$TARGET" +borg list "$MOUNTPOINT" + +# Unmount and clean up mountpoint +fusermount -u "$MOUNTPOINT" && \ +rm -rf "$MOUNTPOINT" + +# Inform user +echo "Goodbye from tupper! Finished on $(date)"