Skip to content

Instantly share code, notes, and snippets.

@ssddanbrown
Last active October 30, 2024 15:51
Show Gist options
  • Save ssddanbrown/3d5dbebc51ac6ca45837d8a030b07b65 to your computer and use it in GitHub Desktop.
Save ssddanbrown/3d5dbebc51ac6ca45837d8a030b07b65 to your computer and use it in GitHub Desktop.
bookstack-backup

BookStack Backup Script

This is a simple BookStack backup script, to dump the database, copy uploaded files, and zip it all up into a timestamped archive. This is designed for an on-system install, not a docker setup. Database credentails are automatically read from your BookStack config.

This script will copy uploads before zipping, so you'll need more free space on your system than your BookStack directory already consumes.

Usage

  1. Copy the script down to a file (bookstack-backup.sh).
  2. Tweak the configu variables at the top of the script.
  3. Make the script executable (chmod +x bookstack-backup.sh).
  4. Run the script (./bookstack-backup.sh).
#!/bin/bash
# Directory to store backups within
# Should not end with a slash and not be stored within
# the BookStack directory
BACKUP_ROOT_DIR="$HOME"
# Directory of the BookStack install
# Should not end with a slash.
BOOKSTACK_DIR="/var/www/bookstack"
# Get database options from BookStack .env file
export $(cat "$BOOKSTACK_DIR/.env" | grep ^DB_ | xargs)
# Create an export name and location
DATE=$(date "+%Y-%m-%d_%H-%M-%S")
BACKUP_NAME="bookstack_backup_$DATE"
BACKUP_DIR="$BACKUP_ROOT_DIR/$BACKUP_NAME"
mkdir -p "$BACKUP_DIR"
# Dump database to backup dir using the values
# we got from the BookStack .env file.
mysqldump --single-transaction \
--no-tablespaces \
-u "$DB_USERNAME" \
-p"$DB_PASSWORD" \
"$DB_DATABASE" > "$BACKUP_DIR/database.sql"
# Copy BookStack files into backup dir
cp "$BOOKSTACK_DIR/.env" "$BACKUP_DIR/.env"
cp -a "$BOOKSTACK_DIR/storage/uploads" "$BACKUP_DIR/storage-uploads"
cp -a "$BOOKSTACK_DIR/public/uploads" "$BACKUP_DIR/public-uploads"
# Create backup archive
tar -zcf "$BACKUP_DIR.tar.gz" \
-C "$BACKUP_ROOT_DIR" \
"$BACKUP_NAME"
# Cleanup non-archive directory
rm -rf "$BACKUP_DIR"
echo "Backup complete, archive stored at:"
echo "$BACKUP_DIR.tar.gz"
@fliptoback
Copy link

Thanks Man-in-Black for that. At the moment I just use a bash script to automatically delete the zip file first, then run the docker backup script to create a new bookstack.zip, so that the hyperbackup program on my synology can pick up the bookstack backup files.

Your script is a much better and more elegant way of doing this file rotation. I will try to adapt to my system.
Thanks again for that. This is much appreciated.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment