Skip to content

Instantly share code, notes, and snippets.

@lbrooney
Last active January 3, 2025 04:48
Show Gist options
  • Select an option

  • Save lbrooney/a39d1a8a62251a69df09f3af4ff9b373 to your computer and use it in GitHub Desktop.

Select an option

Save lbrooney/a39d1a8a62251a69df09f3af4ff9b373 to your computer and use it in GitHub Desktop.
audiobookshelf install/update script for TrueNAS
#!/bin/sh
# Function to display messages in green color
green_echo() {
echo -e "\033[0;32m$1\033[0m"
}
# Function to display messages in yellow color
yellow_echo() {
echo -e "\033[0;33m$1\033[0m"
}
# Check if required packages are installed, install or update if necessary
check_install_update() {
pkg update
for pkg in "$@"; do
if ! pkg info "$pkg" > /dev/null 2>&1; then
# Package is not installed, install it
green_echo "Installing $pkg..."
pkg install -y "$pkg"
else
# Package is installed, check for updates and update if necessary
yellow_echo "Checking for updates to $pkg..."
pkg upgrade -q -y "$pkg"
fi
done
}
check_install_update "curl" "ffmpeg" "node20" "npm-node20" "nano"
# Set the installation directory
INSTALL_DIR="/root/audiobookshelf"
# Set the configuration path within the installation directory
CONFIG_PATH="$INSTALL_DIR/config"
# Set the metadata path within the installation directory
METADATA_PATH="$INSTALL_DIR/metadata"
# Set the backup path within the installation directory
BACKUP_PATH="$INSTALL_DIR/backup"
# Define the GitHub repository
REPO="advplyr/audiobookshelf"
# Ensure the installation directory exists
mkdir -p $INSTALL_DIR
# Ensure configuration, metadata, and backup folders exist
mkdir -p "$CONFIG_PATH"
mkdir -p "$METADATA_PATH"
mkdir -p "$BACKUP_PATH"
# Navigate to the installation directory
cd $INSTALL_DIR
# Check if Audiobookshelf is already installed
CURRENT_VERSION=$(cat .version 2>/dev/null)
# Fetch the latest release version from GitHub Releases API
LATEST_VERSION=$(curl -s "https://api.github.com/repos/$REPO/releases/latest" | grep '"tag_name":' | sed -E 's/.*"([^"]+)".*/\1/')
if [ -z "$LATEST_VERSION" ]; then
yellow_echo "Failed to retrieve the latest version. Exiting."
exit 1
fi
if [ "$CURRENT_VERSION" = "$LATEST_VERSION" ]; then
green_echo "Audiobookshelf is already up to date. Exiting."
exit 0
fi
if pm2 list | grep -q "audiobookshelf-$CURRENT_VERSION"; then
yellow_echo "Stopping audiobookshelf $CURRENT_VERSION in PM2..."
pm2 stop "audiobookshelf-$CURRENT_VERSION"
fi
if [ -n "$CURRENT_VERSION" ] && [ -d "$CONFIG_PATH" ] && [ -d "$METADATA_PATH" ]; then
yellow_echo "Creating a backup of Audiobookshelf folder..."
mkdir -p "$BACKUP_PATH/audiobookshelf-$CURRENT_VERSION"
cp -r "$CONFIG_PATH" "$BACKUP_PATH/audiobookshelf-$CURRENT_VERSION/config"
cp -r "$METADATA_PATH" "$BACKUP_PATH/audiobookshelf-$CURRENT_VERSION/metadata"
fi
# Set the temporary directory
TMP_DIR="$INSTALL_DIR/tmp"
# Ensure the temporary directory exists
mkdir -p "$TMP_DIR"
# Download the latest release into the temporary directory
green_echo "Downloading Audiobookshelf version $LATEST_VERSION..."
curl -L -o "$TMP_DIR/$LATEST_VERSION.zip" "https://github.com/$REPO/archive/refs/tags/$LATEST_VERSION.zip"
# Extract the contents into the install directory
unzip -o "$TMP_DIR/$LATEST_VERSION.zip" -d "$INSTALL_DIR"
# Remove the temporary directory
green_echo "Cleaning up temporary files..."
rm -rf "$TMP_DIR"
# Enter the Audiobookshelf directory within the install directory
cd "$INSTALL_DIR/audiobookshelf-${LATEST_VERSION#?}"
# Update npm to the latest version before running npm commands
green_echo "Updating npm to the latest version..."
npm install -g npm
# Install dependencies and build the app
yellow_echo "Installing dependencies and building the app..."
npm run client
npm ci --omit=dev
# Update PM2
green_echo "Updating PM2..."
npm install -g pm2
pm2 update
# Indicate to the user that they may need to run pm2 unstartup and pm2 startup
yellow_echo "Note: If you have updated Node versions, consider running the following commands to ensure PM2 compatibility:"
echo "pm2 unstartup"
echo "pm2 startup"
# Ensure the rc.d folder exists (sometimes it doesn't generate and PM2 doesn't account for this)
mkdir -p /usr/local/etc/rc.d/
# Run PM2 startup
if ! service -e | grep -q "pm2"; then
pm2 startup
fi
# Create a PM2 configuration file with the new version as part of the app name
cat <<EOF > pm2config.json
{
"apps": [
{
"name": "audiobookshelf-$LATEST_VERSION",
"script": "index.js",
"env": {
"NODE_ENV": "production",
"HOST": "0.0.0.0",
"CONFIG_PATH": "$CONFIG_PATH",
"METADATA_PATH": "$METADATA_PATH",
"SOURCE": "FreeBSD"
}
}
]
}
EOF
# Start the Audiobookshelf app with PM2
green_echo "Starting Audiobookshelf with PM2..."
pm2 start pm2config.json
# Wait for 3 seconds
sleep 3
# Check the status of the application
STATUS=$(pm2 list | grep "audiobookshelf-$LATEST_VERSION" | awk '{print $18}')
# Check if the app is running successfully
if [ "$STATUS" = "errored" ]; then
yellow_echo "Audiobookshelf $LATEST_VERSION failed to start. Please check logs for details..."
yellow_echo "Starting audiobookshelf $CURRENT_VERSION..."
pm2 start "audiobookshelf-$CURRENT_VERSION"
elif [ "$STATUS" = "online" ]; then
# Remove the existing Audiobookshelf app from PM2 if it exists
pm2 delete "audiobookshelf-$CURRENT_VERSION" 2>/dev/null
green_echo "audiobookshelf $LATEST_VERSION started successfully..."
# Save the PM2 configuration
green_echo "Saving PM2 configuration..."
pm2 save
echo "$LATEST_VERSION" > "$INSTALL_DIR/.version" # Save the version number
else
echo yellow_echo "Something went wrong at the PM2 stage. Please check logs for details..."
exit 2
fi
# Display PM2 status
green_echo "Audiobookshelf updated to version $LATEST_VERSION and running with PM2:"
pm2 status
@jakepog
Copy link

jakepog commented Oct 26, 2024

Thanks for the excellent script! Unfortunately this no longe works for Audiobookshelf v2.15.x as it includes nusqlite3, for which a binary is not available for FreeBSD - according to the error logs.

ERROR: [Binary] Failed to download library libnusqlite3 version 1.2 to /root/audiobookshelf/audiobookshelf-2.15.1 Error: [NunicodeDownloader] Platform freebsd-x64 not supported

I tried to compile it myself, but cannot find a sqlite3 development package. Suggestions?

@lbrooney
Copy link
Author

lbrooney commented Oct 26, 2024

I haven't used those script in a while myself nor do I have a FreeBSD/TrueNAS Core installation currently.

PR #3468 first implemented these changes. They grab binaries compiled with musl from this repo. I can't speak for whether you can compile the binaries on FreeBSD, but you may be able to copy the build steps they take in the GitHub workflow and compile on your local FreeBSD machine.

You might also be able to get around this by launching with env var SKIP_BINARIES_CHECK set to 1. See here. As libnusqlite3 is set as not required, this might be the simplest solution, but maybe not the safest.

@jakepog
Copy link

jakepog commented Oct 29, 2024

The nucnicode binaries don't appear to be compatible with FreeBSD/TrueNAS 13.3 at least. Perhaps with some mucking around a Linux build could be used. Setting SKIP_BINARIES_CHECK = 1 appears to be worked fine, thanks!

@Baenwort
Copy link

Baenwort commented Jan 3, 2025

The nucnicode binaries don't appear to be compatible with FreeBSD/TrueNAS 13.3 at least. Perhaps with some mucking around a Linux build could be used. Setting SKIP_BINARIES_CHECK = 1 appears to be worked fine, thanks!

You might try contacting the blog author at: https://blog.brendans-bits.com/posts/2024/audiobookshelf-2.11-on-freebsd/ as he seems to be still using FreeBSD? That was the original recipe I followed and since I got lucky with not updating at the wrong time I'm still OK myself.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment