From 62bca1b61296ac551574818c5021188913678168 Mon Sep 17 00:00:00 2001 From: "Jason D. McCormick" Date: Wed, 20 Dec 2023 16:47:37 -0500 Subject: updated mf-backup, add Makefiles --- Makefile | 47 ++++++++++++++++++++++++ bin/Makefile | 15 ++++++++ bin/mf-backup | 113 ++++++++++++++++++++++++++++++++++++++++++++++++++-------- etc/Makefile | 14 ++++++++ etc/mf-backup | 31 ++++++++++++++++ 5 files changed, 206 insertions(+), 14 deletions(-) create mode 100644 Makefile create mode 100644 bin/Makefile create mode 100644 etc/Makefile diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..d5b35d5 --- /dev/null +++ b/Makefile @@ -0,0 +1,47 @@ +# +# Build variables +# +RELVER = 1.0 +DEBVER = 1 + +BUILDABLES = \ + bin + +instconf ?= yes +ifeq ($(instconf),yes) +BUILDABLES += etc +endif + +ifdef ${DESTDIR} +DESTDIR=${DESTDIR} +endif + + +default: + @echo This does nothing + +install: $(ROOT_INSTALLABLES) + @echo DESTDIR=$(DESTDIR) + $(foreach dir, $(BUILDABLES), $(MAKE) -C $(dir) install;) + +deb: debclean debprep + debuild + +debchange: + debchange -r -v $(RELVER)-$(DEBVER) + + +debprep: debclean + (cd .. && \ + rm -f mfamily-scripts-$(RELVER) && \ + rm -f mfamily-scripts-$(RELVER).tar.gz && \ + rm -f mfamily-scripts_$(RELVER).orig.tar.gz && \ + ln -s Allmon3 mfamily-scripts-$(RELVER) && \ + tar --exclude=".git" -h -zvcf mfamily-scripts-$(RELVER).tar.gz mfamily-scripts-$(RELVER) && \ + ln -s mfamily-scripts-$(RELVER).tar.gz mfamily-scripts_$(RELVER).orig.tar.gz ) + +debclean: + rm -f ../mfamily-scripts_$(RELVER)* + rm -rf debian/mfamily-scripts + rm -f debian/files + diff --git a/bin/Makefile b/bin/Makefile new file mode 100644 index 0000000..1caaafc --- /dev/null +++ b/bin/Makefile @@ -0,0 +1,15 @@ +prefix ?= /usr +bindir ?= $(prefix)/bin + +BIN_FILES = $(filter-out Makefile , $(wildcard *)) +BIN_INSTALLABLES = $(patsubst %, $(DESTDIR)$(bindir)/%, $(BIN_FILES)) + +INSTALLABLES = $(BIN_INSTALLABLES) + +.PHONY: install +install: $(INSTALLABLES) + +$(DESTDIR)$(bindir)/%: % + install -D -m 0644 $< $@ + + diff --git a/bin/mf-backup b/bin/mf-backup index 8ce05d4..46307a8 100755 --- a/bin/mf-backup +++ b/bin/mf-backup @@ -1,22 +1,107 @@ -#!/bin/bash +#!/usr/bin/bash PATH=/usr/bin:/bin:/usr/sbin:/sbin +source /etc/mf-backup -### Backup Next Cloud +function check_for_error() { + [[ $1 -ne 0 ]] && GLOBAL_ERROR=$1 +} -/usr/local/bin/occ maintenance:mode --on -mysqldump -u root --all-databases > /var/lib/mysql-backup/dump.sql -kopia snapshot create /var/lib/mysql-backup -kopia snapshot create /data/nextcloud -/usr/local/bin/occ maintenance:mode --off +function backup_nextcloud() { + local GLOBAL_ERROR=0 + /usr/local/bin/occ maintenance:mode --on && \ + mysqldump -u root --all-databases > /var/lib/mysql-backup/dump.sql && \ + kopia $KOPIA_OPTS snapshot create /var/lib/mysql-backup && \ + kopia $KOPIA_OPTS snapshot create /data/nextcloud && \ + /usr/local/bin/occ maintenance:mode --off + check_for_error $? + return $GLOBAL_ERROR +} + +function backup_mariadb() { + local GLOBAL_ERROR=0 + mysqldump -u root --all-databases > /var/lib/mysql-backup/dump.sql + check_for_error $? + echo "kopia snapshot create /var/lib/mysql-backup" + kopia $KOPIA_OPTS snapshot create /var/lib/mysql-backup + check_for_error $? + return $GLOBAL_ERROR +} + +function log() { + echo "$1" >> $LOGFILE +} + +# Check B2 keys +if [ -z "$B2_KEY_ID" ]; then + echo "Missing B2_KEY_ID in /etc/mf-backup" + exit 1 +fi + +if [ -z "$B2_KEY" ]; then + echo "Missing B2_KEY in /etc/mf-backup" + exit 1 +fi + +if [ -z "$DIRS_TO_BACKUP" ]; then + echo "Missing DIRS_TO_BACKUP in /etc/mf-backup" + exit 1 +fi + +LOGFILE=`mktemp` +GLOBAL_ERROR=0 + +## Backup NextCloud +if [[ $BACKUP_NEXTCLOUD =~ ^[yY]$ ]]; then + log "" + log "-------------------------------------------------------------" + log " KOPIA NEXTCLOUD BACKUP" + log "-------------------------------------------------------------" + log "" + backup_nextclod >> $LOGFILE 2>&1 + check_for_error +fi + +## Backup MariaDB +if [[ $BACKUP_MARIADB =~ ^[yY]$ ]]; then + log "" + log "-------------------------------------------------------------" + log " KOPIA MARIADB BACKUP" + log "-------------------------------------------------------------" + log "" + backup_mariadb >> $LOGFILE 2>&1 + check_for_error +fi ## Backups -kopia snapshot create /etc -kopia snapshot create /var/www -kopia snapshot create /home -kopia snapshot create /root -kopia snapshot create /usr/local +log "" +log "-------------------------------------------------------------" +log " KOPIA STD FILESYSTEM SNAPSHOT " +log " ${DIRS_TO_BACKUP}" +log "-------------------------------------------------------------" +log "" + +for DIR in $DIRS_TO_BACKUP +do + echo "kopia snapshot create $DIR" >> $LOGFILE + kopia $KOPIA_OPTS snapshot create $DIR >> $LOGFILE 2>&1 + check_for_error $? +done ## Replicate -source /etc/mf-backup.sh -kopia repository sync-to b2 --bucket=mfamily-bu-cobalt --key-id=$B2_KEY_ID --key=$B2_KEY --delete +log "" +log "-------------------------------------------------------------" +log " KOPIA S3 TO BACKBLAZE B2 REPLICATION" +log "-------------------------------------------------------------" +log "" +kopia $KOPIA_OPTS repository sync-to b2 --bucket=${KOPIA_PREFIX}${X_HOSTNAME} \ + --key-id=$B2_KEY_ID --key=$B2_KEY --delete >> $LOGFILE 2>&1 + check_for_error $? + +## Mail log on error +if [[ $GLOBAL_ERROR -ne 0 ]]; then + MSUBJ="${X_HOSTNAME} Backup Job Errors" + mail -r $MAIL_FROM -s "$MSUBJ" $MAIL_TO < $LOGFILE +fi +rm $LOGFILE +exit 0 diff --git a/etc/Makefile b/etc/Makefile new file mode 100644 index 0000000..f2d6243 --- /dev/null +++ b/etc/Makefile @@ -0,0 +1,14 @@ +sysconfdir ?= /etc + +ETC_FILES = $(filter-out Makefile , $(wildcard *)) +ETC_INSTALLABLES = $(patsubst %, $(DESTDIR)$(sysconfdir)/%, $(ETC_FILES)) + +INSTALLABLES = $(ETC_INSTALLABLES) + +.PHONY: install +install: $(INSTALLABLES) + +$(DESTDIR)$(sysconfdir)/%: % + install -D -m 0644 $< $@ + + diff --git a/etc/mf-backup b/etc/mf-backup index 6bde9c1..98d6ff5 100644 --- a/etc/mf-backup +++ b/etc/mf-backup @@ -1,2 +1,33 @@ +#!/usr/bin/bash + +# Hostname to be used within the script. By default +# this should be set to $HOSTNAME from the system environment +# but can be overwritten with any other string as long +# as it makes sense to Kopia +X_HOSTNAME=$HOSTNAME + +# Basic directories to be backed up +DIRS_TO_BACKUP="/etc /home /root /usr/local" + +# Backup a MariaDB - Y or y for "yes", anything else no +# Make sure /var/lib/mysql-backup exists and has enough +# space to dump the full database with mysqldump +BACKUP_MARIADB=n + +# Backup a NextCloud instance - Y or y for "yes", anything else no +# Make sure /var/lib/mysql-backup exists and has enough +# space to dump the full database with mysqldump. Also ensure +# that /usr/local/bin/occ exists as a wraper for the PHP OCC interface +# for Nextcloud. Data assumed to be in /data/nextcloud. +# Note that BACKUP_MARIADB has no impact on BACKUP_NEXTCLOUD +BACKUP_NEXTCLOUD=m + +# B2 Key ID and Key for replication to Backblaze B2 B2_KEY_ID= B2_KEY= + +# Kopia Options +KOPIA_OPTS="--disable-color" + +# Kopia bucket prefix --> Bucket name is ${KOPIA_PREFIX}${X_HOSTNAME} +KOPIA_PREFIX="mfamily-bu-" -- cgit v1.2.3