From b910951e9b08bba62b6956c5065e2d27d6bcb202 Mon Sep 17 00:00:00 2001 From: Derek Crudgington Date: Mon, 17 Apr 2023 15:45:47 +0000 Subject: [PATCH] Added backuptool and logs directory --- bin/.env | 7 +- bin/backuptool | 136 ++++++++++++++++++ ...install-federated.sh => install-federated} | 2 + bin/{start.sh => start} | 0 bin/{stop.sh => stop} | 0 bin/{upgrade.sh => upgrade} | 0 lib/functions.sh | 4 + logs/.gitignore | 0 8 files changed, 148 insertions(+), 1 deletion(-) create mode 100755 bin/backuptool rename bin/{install-federated.sh => install-federated} (99%) rename bin/{start.sh => start} (100%) rename bin/{stop.sh => stop} (100%) rename bin/{upgrade.sh => upgrade} (100%) create mode 100644 logs/.gitignore diff --git a/bin/.env b/bin/.env index bea61f3..3051759 100644 --- a/bin/.env +++ b/bin/.env @@ -4,9 +4,14 @@ # Company name #COMPANY="Fang Free Inc" -# Country +# Country #COUNTRY="US" # Administrator password #ADMINPASS="password123" +# Backblaze KeyID (Master Key) +B2_APPLICATION_KEY_ID="3239c6765fdc" + +# Backblaze Application Key +B2_APPLICATION_KEY="0050ac8837466cbca0e0aa574b5f8332f706a5e26c" diff --git a/bin/backuptool b/bin/backuptool new file mode 100755 index 0000000..039642d --- /dev/null +++ b/bin/backuptool @@ -0,0 +1,136 @@ +#!/bin/bash +# +# Federated Backup / Restore Tool to B2 Backblaze +. /federated/lib/functions.sh + +usage() { +cat << EOF +Backup to Backblaze +Usage: $0 [options] service +Arguments: + service Service to backup or 'all' for all services +Options + -l List files in a backup + -b Perform backup of a service + -d Delete backups of a service + -r Restore a backup of a service +EOF +exit 2; +} +createbucket() { + if [ ! -f "/federated/bin/.b2init" ]; then + UUID=`uuid` + B2_APPLICATION_KEY_ID="$B2_APPLICATION_KEY_ID" B2_APPLICATION_KEY="$B2_APPLICATION_KEY" /federated/bin/b2-linux create_bucket $UUID allPrivate + echo "$UUID" > /federated/bin/.b2init + fi +} +listbackup() { + echo "* Listing backup files in $SERVICE.." + PASSPHRASE=$GPG_PASSPHRASE duplicity list-current-files b2://$B2_APPLICATION_KEY_ID:$B2_APPLICATION_KEY@$UUID/$SERVICE/ +} +backupservice() { + echo "* Backing up $SERVICE.." + + if [ "$SERVICE" = "postgresql" ]; then + docker exec postgresql /bin/bash -c "pg_dumpall -c -U postgres | gzip -9 > /docker-entrypoint-initdb.d/dump_`date +%m-%d-%Y`.sql.gz" + PASSPHRASE=$GPG_PASSPHRASE duplicity /federated/apps/$SERVICE b2://$B2_APPLICATION_KEY_ID:$B2_APPLICATION_KEY@$UUID/$SERVICE/ + rm /federated/apps/postgresql/data/docker-entrypoint-initdb.d/dump_* + else + PASSPHRASE=$GPG_PASSPHRASE duplicity /federated/apps/$SERVICE b2://$B2_APPLICATION_KEY_ID:$B2_APPLICATION_KEY@$UUID/$SERVICE/ + fi +} +backupserviceall() { + echo "* Backing up all services.." + + for i in "${SERVICES[@]}"; do + echo "** Backing up $i.." + if [ "$SERVICE" = "postgresql" ]; then + docker exec postgresql /bin/bash -c "pg_dumpall -c -U postgres | gzip -9 > /docker-entrypoint-initdb.d/dump_`date +%m-%d-%Y`.sql.gz" + PASSPHRASE=$GPG_PASSPHRASE duplicity /federated/apps/$i b2://$B2_APPLICATION_KEY_ID:$B2_APPLICATION_KEY@$UUID/$i/ + rm /federated/apps/postgresql/data/docker-entrypoint-initdb.d/dump_* + else + PASSPHRASE=$GPG_PASSPHRASE duplicity /federated/apps/$i b2://$B2_APPLICATION_KEY_ID:$B2_APPLICATION_KEY@$UUID/$i/ + fi + done +} +deletebackup() { + mkdir -p /federated/tmp/empty + cd /federated/tmp/empty && /federated/bin/b2-linux sync --allowEmptySource --delete . b2://$UUID/$SERVICE/ + cd /federated && rm -rf /federated/tmp/empty +} +restorebackup() { + echo "* Restoring $SERVICE.." + PASSPHRASE=$GPG_PASSPHRASE duplicity --force b2://$B2_APPLICATION_KEY_ID:$B2_APPLICATION_KEY@$UUID/$SERVICE/ /federated/apps/$SERVICE +} +restorebackupall() { + echo "* Restoring all services.." + for i in "${SERVICES[@]}"; do + echo "** Restoring $i.." + PASSPHRASE=$GPG_PASSPHRASE duplicity --force b2://$B2_APPLICATION_KEY_ID:$B2_APPLICATION_KEY@$UUID/$i/ /federated/apps/$i + done +} +while getopts "lbdr" OPTION; do + case $OPTION in + l) + LIST_BACKUP="true"; + ;; + b) + RUN_BACKUP="true"; + ;; + d) + RUN_DELETE="true"; + ;; + r) + RUN_RESTORE="true"; + ;; + *) + usage; + exit 2; + esac +done + +if [ $# -ne 2 ]; then + usage; +fi +SERVICE=$2 + +if [ -f "/federated/bin/.gpg.backblaze" ]; then + GPG_PASSPHRASE=`cat /federated/bin/.gpg.backblaze` +else + GPG_PASSPHRASE=$(create_password); + echo "$GPG_PASSPHRASE" > /federated/bin/.gpg.backblaze +fi + +if [ -f "/federated/bin/.env" ]; then + . /federated/bin/.env + [ -z "$B2_APPLICATION_KEY_ID" ] && failcheck "/federated/bin/.env doesn't include B2_APPLICATION_KEY_ID for backups" + [ -z "$B2_APPLICATION_KEY" ] && failcheck "/federated/bin/.env doesn't include B2_APPLICATION_KEY for backups" +else + failcheck "Could not find an .env file." +fi + +[ -f "/federated/bin/.b2init" ] && UUID=`cat /federated/bin/.b2init` +[ $LIST_BACKUP ] && listbackup +[ $RUN_DELETE ] && deletebackup + +if [ $RUN_RESTORE ]; then + if printf '%s\0' "${SERVICES[@]}" | grep -Fxqz -- "$SERVICE"; then + restorebackup + elif [ "$SERVICE" = "all" ]; then + restorebackupall + else + failcheck "$SERVICE is not a valid service" + fi +fi + +if [ $RUN_BACKUP ]; then + if printf '%s\0' "${SERVICES[@]}" | grep -Fxqz -- "$SERVICE"; then + createbucket + backupservice + elif [ "$SERVICE" = "all" ]; then + createbucket + backupserviceall + else + failcheck "$SERVICE is not a valid service" + fi +fi diff --git a/bin/install-federated.sh b/bin/install-federated similarity index 99% rename from bin/install-federated.sh rename to bin/install-federated index 7994ff6..6d30490 100755 --- a/bin/install-federated.sh +++ b/bin/install-federated @@ -93,6 +93,8 @@ for i in dns postgresql ldap mail collabora proxy nextcloud matrix element listm start_$i done +add_cron + # Print out federated environment details print_details echo -ne "\n\n" diff --git a/bin/start.sh b/bin/start similarity index 100% rename from bin/start.sh rename to bin/start diff --git a/bin/stop.sh b/bin/stop similarity index 100% rename from bin/stop.sh rename to bin/stop diff --git a/bin/upgrade.sh b/bin/upgrade similarity index 100% rename from bin/upgrade.sh rename to bin/upgrade diff --git a/lib/functions.sh b/lib/functions.sh index df41d82..843dc78 100644 --- a/lib/functions.sh +++ b/lib/functions.sh @@ -27,6 +27,10 @@ spin() { done done } +add_cron() { + (crontab -l; echo "30 23 * * * /federated/bin/backup.sh >> /federated/logs/backup.log 2>&1") | sort -u | crontab - + (crontab -l; echo "0 2 * * * /federated/bin/upgrade.sh >> /federated/logs/upgrade.log 2>&1") | sort -u | crontab - +} install_federated() { [ -d "/federated" ] && fail "Directory /federated already exists. Already installed?" API_TOKEN="92d97f5aa371d420ebce7bc9a008ea8c6ec5d334" diff --git a/logs/.gitignore b/logs/.gitignore new file mode 100644 index 0000000..e69de29