backup_bookstack.sh 1.1 KB

1234567891011121314151617181920212223242526272829303132333435
  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. # Use loaded .env variables to create DB dump
  18. mysqldump -u "$DB_USERNAME" -p"$DB_PASSWORD" "$DB_DATABASE" > "$BACKUP_DIR/database.sql"
  19. # Create backup archive
  20. tar -czpPf - "$BACKUP_DIR/database.sql" "$BOOKSTACK_DIR/.env" "$BOOKSTACK_DIR/storage/uploads" "$BOOKSTACK_DIR/public/uploads" "$BOOKSTACK_DIR/themes" \
  21. | gpg -e -r 0 > "$BACKUP_DIR.tar.gz.gpg"
  22. # Cleanup non-archive directory
  23. rm -r "$BACKUP_DIR"
  24. # Upload archive
  25. rclone copy "$BACKUP_DIR.tar.gz.gpg" od-1:/upload -vP
  26. rm "$BACKUP_DIR.tar.gz.gpg"