Bookstack doesn’t have a backup functionally by default. This tutorial assumes the following:
- Your Bookstack is fully setup and you have SSH access to the server it’s on.
- You have another server/vm/pc/instance/nas/whatever where the backups will live (we’ll call it Backup server from now on) and it runs a flavor of Linux.
- You have SSH access to the backup server.
Step 1 (Backup Server):
Create a new user (we’ll name it bookstack) with its home directory set to the folder where the backups will live. Take note of the user password as it’s needed for Step 2 (if the folder already exists, do the next step first):
sudo adduser --home /thisiswhere/thebackups/willlive bookstack
If the directory exists already, you can run adduser
command with a --no-create-home
option appended and set permissions manually:
sudo adduser --no-create-home /thisiswhere/thebackups/willlive bookstack && chown bookstack:bookstack /thisiswhere/thebackups/willlive && chmod 755 /thisiswhere/thebackups/willlive
Step 2 (Bookstack Server):
Run the following command to install SSHPASS (this is so the Cronjob we set later on can run the script and not need interaction to specify ssh credentials), create the backup script and edit it (change USER
for the actual user folder and USER:USER
for permissions).
sudo apt install sshpass -y && cd /home/USER && sudo touch bookstack_bkup.sh && sudo chown -R USER:USER bookstack_bkup.sh && sudo nano bookstack_bkup.sh
Copy the following scrip into the file, edit the ssh PASSWORD
(it assumes the user you created on the Backup Server is bookstack
), address (BACKUP_SERVER_IP_or_DOMAIN
) and the folder structure (/thisiswhere/thebackups/willlive
) accordingly, save and exit.
#!/bin/bash
###
date=$(date +"%d-%m-%Y@%H-%M")
cd /home/USER/
mysqldump -u bookstack bookstack > bookstack_db_backup.sql
sudo tar cvf bookstack_db_bkup_$date.tar.gz bookstack_db_backup.sql
sudo tar cvf bookstack_files_bkup_$date.tar.gz /var/www/bookstack
sshpass -p PASSWORD scp -oStrictHostKeyChecking=no bookstack_db_bkup_* bookstack@BACKUP_SERVER_IP_or_DOMAIN:/thisiswhere/thebackups/willlive
sshpass -p PASSWORD scp -oStrictHostKeyChecking=no bookstack_files_bkup_* bookstack@BACKUP_SERVER_IP_or_DOMAIN:/thisiswhere/thebackups/willlive
rm -rf bookstack_files_bkup_* bookstack_db_bkup_* bookstack_db_backup.sql
Run the following to create and edit the db credentials file for the script automation (change USER
for the actual local user):
cd /home/USER && touch .my.cnf && nano .my.cnf
Copy the following scrip into the file, edit the password (WHATEVER_THE_PASSWORD_FOR_THE_BOOKSTACK_DB_IS
) accordingly, save and exit.
[mysqldump]
user = bookstack
password = WHATEVER_THE_PASSWORD_FOR_THE_BOOKSTACK_DB_IS
Now, create a Cronjob to automate the previous script. Open the Crontab file:
sudo nano /etc/crontab
and add the following line at the end (change USER
for the actual local user):
* 12 * * 1 root bash /home/USER/bookstack_bkup.sh
This line specifies that it’ll do a backup every Monday at midday. You can however set it to whatever you’d like. Here’s a quick reference table:
So, when the Cron is executed, it will run the script which will do a MySQL database dump and compress it, compress the Bookstack folder and send both to the Backup server. If you want to create an initial backup and not wait for the Cronjob, (to make sure all is OK) run this command (change USER
for the actual local user):
cd /home/USER && sudo bash bookstack_bkup.sh
Final annotations:
- Depending on the size of your backups and needs, you could set up a small script on the Backup Server that only keeps files from the last X period (so you don’t end up running out of space).
- Depending on your setup, if the backup server is located at home and the Bookstack server isn’t (or the other way around), router settings, port forwarding etc or even dynamic IPs might be an issue. To make things simpler, you could install Logmein Hamachi (free version for 5 devices) on both servers so they both have a permanent, virtual lan ip (and you won’t need to fiddle with settings in routers or anything else).