Refactor symlink creation and data migration logic

This commit is contained in:
Alexandre
2026-01-28 15:32:35 +01:00
committed by GitHub
parent dbce691a6e
commit 0a9bbf8aa7

View File

@@ -1,4 +1,7 @@
#!/usr/bin/env bashio
# shellcheck shell=bash
set -euo pipefail
##################
# SYMLINK CONFIG #
@@ -7,46 +10,66 @@
DATA_LOCATION="$(bashio::config 'data_location')"
OLD_LOCATION=""
if [ -L /config/Library ]; then
old_library_path="$(readlink -f /config/Library)"
if [ -n "$old_library_path" ]; then
OLD_LOCATION="$(dirname "$old_library_path")"
is_populated() {
local d="$1"
[[ -d "$d" ]] || return 1
# Treat default/placeholder dirs as "empty"
# Use strong Plex indicators (adjust if your layout differs)
[[ -f "${d}/Library/Application Support/Plex Media Server/Preferences.xml" ]] && return 0
[[ -f "${d}/Library/Application Support/Plex Media Server/Plug-in Support/Databases/com.plexapp.plugins.library.db" ]] && return 0
return 1
}
if [[ -L /config/Library ]]; then
old_library_path="$(readlink -f /config/Library || true)"
if [[ -n "${old_library_path}" ]]; then
OLD_LOCATION="$(dirname "${old_library_path}")"
fi
fi
if [ -z "$OLD_LOCATION" ] && [ -d "/share/plex" ]; then
if [[ -z "${OLD_LOCATION}" && -d "/share/plex" ]]; then
OLD_LOCATION="/share/plex"
fi
if [ -n "$OLD_LOCATION" ] && [ "$DATA_LOCATION" != "$OLD_LOCATION" ] && [ -d "$OLD_LOCATION" ]; then
if [ -d "${DATA_LOCATION}" ] && [ "$(ls -A "${DATA_LOCATION}" 2>/dev/null)" ]; then
echo "Skipping migration: ${DATA_LOCATION} already contains data"
if [[ -n "${OLD_LOCATION}" && "${DATA_LOCATION}" != "${OLD_LOCATION}" && -d "${OLD_LOCATION}" ]]; then
if is_populated "${DATA_LOCATION}"; then
bashio::log.info "Skipping migration: ${DATA_LOCATION} already has Plex data"
else
echo "Migrating existing ${OLD_LOCATION} data to ${DATA_LOCATION}"
bashio::log.warning "Migrating existing ${OLD_LOCATION} data to ${DATA_LOCATION}"
mkdir -p "${DATA_LOCATION}"
cp -a "${OLD_LOCATION}/." "${DATA_LOCATION}/" || true
cp -a "${OLD_LOCATION}/." "${DATA_LOCATION}/"
fi
fi
if [ ! -d "${DATA_LOCATION}" ]; then
echo "Creating ${DATA_LOCATION}"
mkdir -p "${DATA_LOCATION}"
mkdir -p "${DATA_LOCATION}"
# Ensure /config/Library points to ${DATA_LOCATION}/Library
if [[ ! -d "${DATA_LOCATION}/Library" ]]; then
bashio::log.info "Moving /config/Library to ${DATA_LOCATION}/Library"
# If /config/Library is a symlink, don't mv it
if [[ -L /config/Library ]]; then
rm -f /config/Library
mkdir -p "${DATA_LOCATION}/Library"
elif [[ -d /config/Library ]]; then
mv /config/Library "${DATA_LOCATION}/Library"
else
mkdir -p "${DATA_LOCATION}/Library"
fi
fi
if [ ! -d "${DATA_LOCATION}/Library" ]; then
echo "moving Library folder"
mv /config/Library "${DATA_LOCATION}"
ln -s "${DATA_LOCATION}/Library" /config
echo "links done"
# Replace /config/Library with a symlink (safe handling)
if [[ -e /config/Library && ! -L /config/Library ]]; then
# At this point it should be absent or already moved; if it still exists as a directory, don't delete blindly
bashio::log.warning "/config/Library exists and is not a symlink; leaving it in place to avoid data loss"
else
rm -r /config/Library
ln -s "${DATA_LOCATION}/Library" /config
echo "Using existing config"
rm -f /config/Library || true
ln -s "${DATA_LOCATION}/Library" /config/Library
fi
# Adapt permissions if needed
if ! bashio::config.true "skip_permissions_check" && [ "${PUID:-0}" != "0" ] && [ "${PGID:-0}" != "0" ]; then
if ! bashio::config.true "skip_permissions_check" && [[ "${PUID:-0}" != "0" && "${PGID:-0}" != "0" ]]; then
bashio::log.info "Updating permissions"
chown -R "$PUID:$PGID" "${DATA_LOCATION}"
chmod -R 777 "${DATA_LOCATION}"
chmod -R u+rwX,go+rX,go-w "${DATA_LOCATION}"
fi