Skip to main content

Backup & Restore

Preparations

  1. Make sure to have php installed on your system
  2. Get an API token_id and API token_secret from your user preferences in the webgui
  3. Create a directory, where you want to store the backups
    mkdir backup && cd backup
    mkdir zip md pdf
    
  4. Downlaod the export-books.php script from github
    curl https://raw.githubusercontent.com/BookStackApp/api-scripts/main/php-export-all-books/export-books.php > export-books.php
    
  5. Edit the export-books.php sciprt and enter the needed variables
    BS_URL=https://bookstack.example.com # Set to be your BookStack base URL
    BS_TOKEN_ID=abc123 # Set to be your API token_id
    BS_TOKEN_SECRET=123abc # Set to be your API token_secret
    
  6. Create a file backup.sh that specifies the kind of backup and deletes backup older than X days
    #!/bin/bash
    
    # go to the correct directory
    cd $(dirname "$0")
    
    CURRENTDATE=$(date +%Y-%m-%d_%H-%M-%S)
    
    # Create a new backup using the bookstack-system-cli command
    docker exec -it bookstack /app/www/bookstack-system-cli backup /backup/zip/bookstack_$(date +%Y-%m-%d_%H-%M-%S).zip
    
    # create a backup in pdf and md format
    mkdir -p ./pdf/$CURRENTDATE ./md/$CURRENTDATE
    php export-books.php pdf ./pdf/$CURRENTDATE
    php export-books.php markdown ./md/$CURRENTDATE
    
    # delete backups older than 10 days
    find ./pdf -maxdepth 1 -mtime +10 -exec rm -r -f {} \;
    find ./md  -maxdepth 1 -mtime +10  -exec rm -r -f {} \;
    find ./zip/*.zip  -mtime +10  -exec rm -f {} \;
    
    
  7. Make the added files executable
    chmod +x export-books.php backup.sh
    
  8. Make sure to bindmount your backup folder in the volumes section of the bookstack container by adding - ./backup:/backup

Backing up

With the above setup, pdf and markdown backups will be found inside the backups folder. Inside the zip backup/zip folder are the backups of the website.

To start backing up, simply create a cronjob that runs repeatedly. To save the books every day at 3am, simply add the following cronjob 0 3 * * * /path/to/bookstack/backup/backup.sh

Now simply backup the whole backup folder regularly to a different place and you should be good to go

Restoring

To restore the data simply start a container and run the following command

docker exec -it bookstack /app/www/bookstack-system-cli restore backup/<<nameofbackup>>.zip

See also: