Skip to main content

Backup Bookstacks

 

If you are running Bookstack in a docker container, run the following command to backup the database from outside the docker container. Navigate to the root of the running docker container, and run the following command. Don't forget to replace the LOUDNOISES with actual file names, users, or other information regarding your Bookstack instance.
sudo docker exec DOCKER-CONTAINER-NAME /usr/bin/mysqldump -u USER --password=PASSWORD DATABASE > DATABASE.backup.sql

This will output the file DATABASE.backup.sql into your working directory, move this file to a save place so you can restore the database should something go wrong in the future. If you used this method, the database can be easily restored using the following command
cat DATABASE.backup.sql | docker exec -i DOCKER-CONTAINER-NAME /usr/bin/mysql -u USER --password=PASSWORD DATABASE

To backup the files, a similar command is ran, only this time we use tar to compress Bookstack files into a tarball within our working directory. 

sudo docker exec -i DOCKER-CONTAINER-NAME -czvf bookstack-files-backup.tar.gz .env public/uploads storage/uploads

This will output bookstack-files-backup.tar.gz which can be extracted back into a new Bookstack docker container to restore your data. The command would be similar to the below - 

sudo docker exec -i DOCKER-CONTAINER-NAME tar -xvzf bookstack-files-backup.tar.gz

For safe keeping, toss this 'script' in a safe place and just copy it around to be ran whenever you need it. But once you run these commands a few times, you won't forget them, this might be more trouble than just typing the commands.

#!/bin/bash
##Backup script for backing up the database within docker container
################

# Backup Bookstack Database
sudo docker exec DOCKER-CONTAINER-NAME /usr/bin/mysqldump -u USER --password=PASSWORD DATABASE > DATABASE.backup.sql

# Backup Bookstack Files
sudo docker exec -i DOCKER-CONTAINER-NAME -czvf bookstack-files-backup.tar.gz .env public/uploads storage/uploads


# Restore
# cat backup.sql | docker exec -i DOCKER-CONTAINER-NAME /usr/bin/mysql -u USER --password=PASSWORD DATABASE
# sudo docker exec -i DOCKER-CONTAINER-NAME tar -xvzf bookstack-files-backup.tar.gz