Backup Bookstacks

Don't drive as root, create a BookStack administrator account by following the adduser instructions.

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 -p DATABASE > DATABASE.backup.sql

This will output the file DATABASE.backup.sql into your working directory, move this file to a safe place so you can restore the database should something go wrong in the future. Alternatively, you could manually enter the container with sudo docker exec -it CONTAINER bash and then just run mysqldump -u USER -p DATABASE > DATABASE.backup.sql && exit followed by sudo docker cp CONTAINER:/container/path/DATABASE.backup.sql local/path to copy the SQL backup onto our container's host.

This is all that needs to be done to backup the base content of BookStack, but there are some important configurations and upload directories you'll want to zip up, too. To zip these directories, enter your docker container and run the following commands

sudo docker exec -it BOOKSTACK_CONTAINER bash
tar -czvf bookstack-files-backup.tar.gz /var/www/html/.env /var/www/html/public/uploads /var/www/html/storage/uploads
exit
sudo docker cp BOOKSTACK_CONTAINER:/var/www/html/bookstack/bookstack-files-backup.tar.gz /home/USER/ftp/

If you used the method above, the database can be easily restored using the following commands

# Move our backup files into the containers that need them
sudo docker cp /home/USER/ftp/backup/bookstack.backup.sql BOOKSTACK_MYSQL_CONTAINER:/
sudo docker cp /home/USER/ftp/backup/bookstack-files-backup.tar.gz BOOKSTACK_CONTAINER:/var/www/html/bookstack/

# Enter MySQL container and restore the DB
sudo docker exec -it BOOKSTACK_MYQL_CONTAINER bash
mysql -u {mysql_user} -p {database_name} < {backup_file_name}
exit

# Enter Bookstack container and restore local data
sudo docker exec -it BOOKSTACK_CONTAINER bash
tar -xvzf bookstack-files-backup.tar.gz
exit

If you are restoring to a new version of BookStack you will have to run php artisan migrate after restore to perform any required updates to the database. For safe keeping, toss this 'script' somewhere 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_MYSQL_CONTAINER /usr/bin/mysqldump -u USER --password=PASSWORD DATABASE > DATABASE.backup.sql

# Backup Bookstack Files
sudo docker exec -it DOCKER_CONTAINER bash
tar -czvf bookstack-files-backup.tar.gz .env public/uploads storage/uploads
exit
sudo docker cp CONTAINER:/var/www/html/bookstack/bookstack-files-backup.tar.gz /home/USER/ftp/

# Or manually copy them...
 # sudo docker cp BOOKSTACK_CONTAINER:/var/www/html/bookstack/.env /home/USER/ftp/
 # sudo docker cp BOOKSTACK_CONTAINER:/var/www/html/bookstack/storage/uploads /home/USER/ftp/
 # sudo docker cp BOOKSTACK_CONTAINER:/var/www/html/bookstack/public/uploads /home/USER/ftp/

# Restore
# Move our backup files into the containers that need them
 # sudo docker cp /home/USER/ftp/backup/bookstack.backup.sql BOOKSTACK_MYSQL_CONTAINER:/
 # sudo docker cp /home/USER/ftp/backup/bookstack-files-backup.tar.gz BOOKSTACK_CONTAINER:/var/www/html/bookstack/

# Enter MySQL container and restore the DB
 # sudo docker exec -it BOOKSTACK_MYQL_CONTAINER bash
 # mysql -u USER -p DATABASE < DATABASE.backup.sql
 # exit

# Enter Bookstack container and restore local data
 # sudo docker exec -it BOOKSTACK_CONTAINER bash
 # tar -xvzf bookstack-files-backup.tar.gz
 # exit