Refactor all the things!

This commit is contained in:
Some guy named Michael 2022-03-05 14:33:22 -05:00
parent ead9f85a10
commit 893fac64bd

View File

@ -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.
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
Current defaults:
e.g. backupBookstack.sh docs.example.com docstack documentdb
^ (1) ^ (2) ^ (3)
domain: ${domain}
application: ${application}
Usage:
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