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"
@Man-in-Black
Copy link

You're welcome ;)

@fliptoback
Copy link

After running for 2 days i come to realize that it doesnt backup anymore - it has this error message

Target ZIP output location at [/backup/bookstack.zip] already exists.

Is there a way i can force the backup to overwrite the previous one?

@Man-in-Black
Copy link

Man-in-Black commented Sep 9, 2023

did you use the bookstack-system-cli? In this case you need to put something like the date in it:
docker exec -it bookstack /app/www/bookstack-system-cli backup /sicherung/bookstack_$(date "+%Y-%m-%d_%H-%M-%S").zip
and of course a job to rotate the backups since it will fill you disk if you never clean them up ;)

@fliptoback
Copy link

Thanks. Once I add the time stamp to the bookstack.zip file it works. I just need to figure out how to rotate the backups to avoid the database filling up the disk.

Thanks again.

@Man-in-Black
Copy link

a simple solution to that: find /sicherung/bookstack/ -mtime +31 -name bookstack_*.zip -exec rm -f {} \;
this will delete all files older than 31 days.

@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