From 893fac64bd1571984ed9b548f2706eb82d9042fc Mon Sep 17 00:00:00 2001 From: Michael Date: Sat, 5 Mar 2022 14:33:22 -0500 Subject: [PATCH] Refactor all the things! --- bash/backupBookstack.sh | 63 ++++++++++++++++++++++++++--------------- 1 file changed, 40 insertions(+), 23 deletions(-) diff --git a/bash/backupBookstack.sh b/bash/backupBookstack.sh index 6aa64e5..22e4d3f 100755 --- a/bash/backupBookstack.sh +++ b/bash/backupBookstack.sh @@ -1,19 +1,46 @@ #!/bin/bash -if [ $# -lt 3 ] && [ $# -gt 0 ]; then +# -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=- # +# USER CONFIGURED VARIABLES # +# -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=- # + +domain="bookstack.dismyserver.net" # The name of the directory in /var/www +sqlbackupuser="backupuser" # The user that has permission to dump DBs +sqlbackupuserpassword="hunter2" # Said user's password +termuser='rooot' # The user that you want to access the backup files with +backgroup='backupboi' # The group that can read the backup files + +# -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=- # + +arg1=$1 +[ $arg1 == "-h" ] && helpme=1;arg1=$2 +[ -z $arg1 ] || domain=$arg1 +application='' +if [ $domain != "bookstack.dismyserver.net" ]; then + if [[ $domain == *"."* ]] && [ -z $2 ]; then + application=`echo $domain | cut -d. -f2` + else + [ -z $2 ] && application=$domain || application=$2 + fi +fi + +if [ $# -gt 2 ] || [ $helpme ]; then cat << EOF -Incorrect number of arguments provided. Please either provide all - of the below arguments or provide none to use the default values - set in the script. +No arguments will follow the default values provided at the top of the script. + +Current defaults: + +domain: ${domain} +application: ${application} -1: The domain (or name of your folder in /var/www) -2: The name of the application (to be used in naming the files) -3: The name of the database to back up +Usage: -e.g. backupBookstack.sh docs.example.com docstack documentdb - ^ (1) ^ (2) ^ (3) +backupBookstack.sh [domain/folder] [name] + +- domain/folder is the directory in /var/www that your bookstack instance lives +- name is useful if your directory does not have any periods and you do not want to use the domain name as the application name. EOF exit 1 @@ -21,24 +48,13 @@ fi if [[ `id -u` -ne 0 ]]; then echo "This script must be run as root..." - exit 1 + exit fi -domain="bookstack.dismyserver.net" # The name of the directory in /var/www -application="" # Only populate if this not the default bookstack instance -appdatabase="bookstack" # seems important to get right - -sqlbackupuser="backupuser" # The user that has permission to dump DBs -sqlbackupuserpassword="hunter2" # Said user's password - -termuser='rooot' # The user that you want to access the backup files with -backgroup='backupboi' # The group that can read the backup files - [ -z $application ] || application="${application}-" -[ -z $1 ] || domain=$1 -[ -z $2 ] || application=$2 -[ -z $3 ] || appdatabase=$3 +appdatabase=`grep DB_DATABASE /var/www/${domain}/.env | cut -d= -f2` +[ -z $appdatabase ] && echo "Unable to determine database. Exiting.";exit 1 isodate=`date +%Y-%m-%d_%H-%M-%S%z` backupdir="/tmp/${application}bookstack-backup-${isodate}" backupenddir="/backups/${application}bookstack" @@ -106,6 +122,7 @@ EOF mysqldump -u${sqlbackupuser} -p${sqlbackupuserpassword} ${appdatabase} > $backupdir/${application}bookstack-$isodate.sql mkdir -p $backupenddir +chown -R $termuser:$backgroup $backupenddir 7z a $backupenddir/${application}bookstack_$isodate.7z $backupdir/* >/dev/null chown $termuser:$backgroup $backupenddir/${application}bookstack_$isodate.7z