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. Alternatively, you could manually enter the container with sudo docker exec -it CONTAINER bash and then just run mysqldump -u USER --password=PASSWORD 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 CONTAINER bash
tar -czvf bookstack-files-backup.tar.gz .env public/uploads storage/uploads
exit
sudo docker cp CONTAINER:/var/www/bookstack/bookstack-files-backup.tar.gz /home/user/ftp/

If you used thisthe method,method above, 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 DATABASEcommands

To

# Move our backup the files, a similar command is ran, only this time we use tar to compress Bookstack files into athe tarballcontainers withinthat ourneed workingthem
directory. 

sudo

docker cp /home/bookadmin/ftp/backup/bookstack.backup.sql BOOKSTACK_MYSQL_CONTAINER:/ sudo docker cp /home/bookadmin/ftp/backup/bookstack-files-backup.tar.gz BOOKSTACK_CONTAINER:/var/www/bookstack/ # Enter MySQL container and restore the DB sudo docker exec -iit DOCKER-CONTAINER-NAMEBOOKSTACK_MYQL_CONTAINER bash mysql -czvfu bookstack-files-backup.tar.gz{mysql_user} .env-p public/uploads{database_name} storage/uploads

<

This{backup_file_name} willexit output# bookstack-files-backup.tar.gz which can be extracted back into a newEnter Bookstack docker container toand restore yourlocal data.data The command would be similar to the below - 

sudo docker exec -iit DOCKER-CONTAINER-NAMEBOOKSTACK_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' in a safe placesomewhere 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 Bookstackthe database within a docker container
################


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

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

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

# Restore
# catMove our backup files into the containers that need them
 # sudo docker cp /home/USER/ftp/backup/bookstack.backup.sql |BOOKSTACK_MYSQL_CONTAINER:/
 # sudo docker exec -i DOCKER-CONTAINER-NAMEcp /usr/bin/mysqlhome/USER/ftp/backup/bookstack-files-backup.tar.gz -uBOOKSTACK_CONTAINER:/var/www/bookstack/

USER# --password=PASSWORDEnter DATABASEMySQL container and restore the DB
 # sudo docker exec -iit DOCKER-CONTAINER-NAMEBOOKSTACK_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