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 #!/usr/bin/env bashio
# shellcheck shell=bash
set -euo pipefail
################## ##################
# SYMLINK CONFIG # # SYMLINK CONFIG #
@@ -7,46 +10,66 @@
DATA_LOCATION="$(bashio::config 'data_location')" DATA_LOCATION="$(bashio::config 'data_location')"
OLD_LOCATION="" OLD_LOCATION=""
if [ -L /config/Library ]; then is_populated() {
old_library_path="$(readlink -f /config/Library)" local d="$1"
if [ -n "$old_library_path" ]; then [[ -d "$d" ]] || return 1
OLD_LOCATION="$(dirname "$old_library_path")"
# 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
fi fi
if [ -z "$OLD_LOCATION" ] && [ -d "/share/plex" ]; then if [[ -z "${OLD_LOCATION}" && -d "/share/plex" ]]; then
OLD_LOCATION="/share/plex" OLD_LOCATION="/share/plex"
fi fi
if [ -n "$OLD_LOCATION" ] && [ "$DATA_LOCATION" != "$OLD_LOCATION" ] && [ -d "$OLD_LOCATION" ]; then if [[ -n "${OLD_LOCATION}" && "${DATA_LOCATION}" != "${OLD_LOCATION}" && -d "${OLD_LOCATION}" ]]; then
if [ -d "${DATA_LOCATION}" ] && [ "$(ls -A "${DATA_LOCATION}" 2>/dev/null)" ]; then if is_populated "${DATA_LOCATION}"; then
echo "Skipping migration: ${DATA_LOCATION} already contains data" bashio::log.info "Skipping migration: ${DATA_LOCATION} already has Plex data"
else 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}" mkdir -p "${DATA_LOCATION}"
cp -a "${OLD_LOCATION}/." "${DATA_LOCATION}/" || true cp -a "${OLD_LOCATION}/." "${DATA_LOCATION}/"
fi fi
fi fi
if [ ! -d "${DATA_LOCATION}" ]; then mkdir -p "${DATA_LOCATION}"
echo "Creating ${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 fi
if [ ! -d "${DATA_LOCATION}/Library" ]; then # Replace /config/Library with a symlink (safe handling)
echo "moving Library folder" if [[ -e /config/Library && ! -L /config/Library ]]; then
mv /config/Library "${DATA_LOCATION}" # At this point it should be absent or already moved; if it still exists as a directory, don't delete blindly
ln -s "${DATA_LOCATION}/Library" /config bashio::log.warning "/config/Library exists and is not a symlink; leaving it in place to avoid data loss"
echo "links done"
else else
rm -r /config/Library rm -f /config/Library || true
ln -s "${DATA_LOCATION}/Library" /config ln -s "${DATA_LOCATION}/Library" /config/Library
echo "Using existing config"
fi fi
# Adapt permissions if needed # 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" bashio::log.info "Updating permissions"
chown -R "$PUID:$PGID" "${DATA_LOCATION}" chown -R "$PUID:$PGID" "${DATA_LOCATION}"
chmod -R 777 "${DATA_LOCATION}" chmod -R u+rwX,go+rX,go-w "${DATA_LOCATION}"
fi fi