From ead9f85a102cf54fee545e3070d6b1a37fdf1578 Mon Sep 17 00:00:00 2001 From: Michael Date: Fri, 4 Mar 2022 22:26:04 -0500 Subject: [PATCH] Added Bookstack backup script. --- README.md | 3 ++ bash/backupBookstack.sh | 113 ++++++++++++++++++++++++++++++++++++++++ 2 files changed, 116 insertions(+) create mode 100755 bash/backupBookstack.sh diff --git a/README.md b/README.md index 7e4f57a..90473da 100644 --- a/README.md +++ b/README.md @@ -5,6 +5,9 @@ **aperture** and funperture A simple little script that echos one of two things. Either the aperture logo, or how the aperture logo got into the script. I like it. +**backupBookstack.sh** +A script to back up a Bookstack instance (https://bookstackapp.com). I run it weekly. It works well enough for me to post it here so... + **backupProgramFiles.sh** This is a script that I created to zip up my Program Files folders on my C: drive as those are permanently excluded by the backup software I use. I could probably dive in and pick specific applications that I know store their configutaions in the Program Files folders but for now I just do the whole thing. This script also clears out backups older than 2 weeks (I run it weekly) and emails a summary to the provided address through the specified mail relay server. Be sure to edit smtp-creds.sh to include your information. diff --git a/bash/backupBookstack.sh b/bash/backupBookstack.sh new file mode 100755 index 0000000..6aa64e5 --- /dev/null +++ b/bash/backupBookstack.sh @@ -0,0 +1,113 @@ +#!/bin/bash + +if [ $# -lt 3 ] && [ $# -gt 0 ]; 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. + +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 + +e.g. backupBookstack.sh docs.example.com docstack documentdb + ^ (1) ^ (2) ^ (3) + +EOF +exit 1 +fi + +if [[ `id -u` -ne 0 ]]; then + echo "This script must be run as root..." + exit 1 +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 + +isodate=`date +%Y-%m-%d_%H-%M-%S%z` +backupdir="/tmp/${application}bookstack-backup-${isodate}" +backupenddir="/backups/${application}bookstack" + +mkdir -p $backupdir/public +mkdir -p $backupdir/storage + +phpver=`php -v` +apachever=`apache2 -v` +apachemod=`apache2ctl -M 2>/dev/null` +serverver=`grep -h DISTRIB_DESCRIPTION /etc/*release | cut -d= -f2 | tr -d \"` +sqlver=`mysql --version` + +cp -r /var/www/${domain}/public/uploads $backupdir/public/ +cp -r /var/www/${domain}/storage/uploads $backupdir/storage/ +cp /var/www/${domain}/.env* $backupdir/ +cat > ${backupdir}/restore-notes.txt << EOF +Backup for ${isodate} + +Backed up the following directories: + +- /var/www/${domain}/public/uploads +- /var/www/${domain}/storage/uploads +- /var/www/${domain}/.env* + +Backed up the following database: + +- ${appdatabase} + +7zip does not save file permissions or users/groups. +Be sure to change these manually if a restore is +performed. + +https://www.bookstackapp.com/docs/admin/backup-restore/ + +-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=- + The Boring Stuff +-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=- + +Server Version + +${serverver} + +PHP Version + +${phpver} + +SQL Version + +${sqlver} + +Apache Version + +${apachever} + +Enabled Apache Modules + +static = compiled into Apache +shared = dynamically loaded + +${apachemod} +EOF + +# https://bencane.com/2011/12/12/creating-a-read-only-backup-user-for-mysqldump/ +mysqldump -u${sqlbackupuser} -p${sqlbackupuserpassword} ${appdatabase} > $backupdir/${application}bookstack-$isodate.sql + +mkdir -p $backupenddir + +7z a $backupenddir/${application}bookstack_$isodate.7z $backupdir/* >/dev/null +chown $termuser:$backgroup $backupenddir/${application}bookstack_$isodate.7z +chmod 640 $backupenddir/${application}bookstack_$isodate.7z +rm -rf $backupdir