#!/bin/bash # -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=- # # 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 No arguments will follow the default values provided at the top of the script. Current defaults: 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 fi if [[ `id -u` -ne 0 ]]; then echo "This script must be run as root..." exit fi [ -z $application ] || application="${application}-" 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" 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 chown -R $termuser:$backgroup $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