backup_bookstack.sh 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. #!/bin/bash
  2. # Modified from https://gist.github.com/ssddanbrown/3d5dbebc51ac6ca45837d8a030b07b65
  3. # Directory to store backups within
  4. # Should not end with a slash and not be stored within
  5. # the BookStack directory
  6. BACKUP_ROOT_DIR="$HOME"
  7. # Directory of the BookStack install
  8. # Should not end with a slash.
  9. BOOKSTACK_DIR="/var/www/bookstack"
  10. # Get database options from BookStack .env file
  11. export $(cat "$BOOKSTACK_DIR/.env" | grep ^DB_ | xargs)
  12. # Create an export name and location
  13. DATE=$(date "+%Y-%m-%d_%H-%M-%S")
  14. BACKUP_NAME="bookstack_backup_$DATE"
  15. BACKUP_DIR="$BACKUP_ROOT_DIR/$BACKUP_NAME"
  16. mkdir -p "$BACKUP_DIR"
  17. # Dump database to backup dir using the values
  18. # we got from the BookStack .env file.
  19. mysqldump --single-transaction \
  20. --no-tablespaces \
  21. -u "$DB_USERNAME" \
  22. -p"$DB_PASSWORD" \
  23. "$DB_DATABASE" > "$BACKUP_DIR/database.sql"
  24. # Copy BookStack files into backup dir
  25. cp "$BOOKSTACK_DIR/.env" "$BACKUP_DIR/.env"
  26. cp -a "$BOOKSTACK_DIR/storage/uploads" "$BACKUP_DIR/storage-uploads"
  27. cp -a "$BOOKSTACK_DIR/public/uploads" "$BACKUP_DIR/public-uploads"
  28. # Create backup archive
  29. tar -czpPf - $BACKUP_DIR | gpg -e -r 0 > "$BACKUP_DIR.tar.gz.gpg"
  30. # Cleanup non-archive directory
  31. rm -r "$BACKUP_DIR"
  32. # Upload archive
  33. rclone copy "$BACKUP_DIR.tar.gz.gpg" od-1:/upload -vP
  34. rm "$BACKUP_DIR.tar.gz.gpg"