#!/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