分享Plex数据包时,数据库及配置信息的脱敏及媒体路径替换方法
侧边栏壁纸
  • 累计撰写 32 篇文章
  • 累计收到 11 条评论

分享Plex数据包时,数据库及配置信息的脱敏及媒体路径替换方法

翔翎
2022-04-18 / 0 评论 / 67 阅读 / 正在检测是否收录...
温馨提示:
本文最后更新于2022年04月18日,已超过948天没有更新,若内容或图片失效,请留言反馈。

本文转自 https://www.emengweb.com/p/%E9%85%8D%E7%BD%AE%E6%96%87%E4%BB%B6%E8%AE%B0%E5%BD%95

Plex基本配置文件

内含多处唯一设备id及账号信息,删除文件系统会自动生成,但是将变为重新安装状态。
/plex/config/Library/Application Support/Plex Media Server/Preferences.xml

Plex媒体数据库

数据库文件位置:
/plex/config/Library/Application Support/Plex Media Server/Plug-in Support/Databases/com.plexapp.plugins.library.db
以下为关键表结构

敏感信息,需要清空:

  • accounts - 内含账户信息,需要清空表
  • devices - 内含登录设备及id,需要清空表
  • external_metadata_sources - 内含账户关联的好友分享服务器及uri地址,需要清空表
  • play_queues - 内含播放媒体的客户端id记录,需要清空表

媒体路径信息,需要替换路径:

  • media_parts - 内含媒体详细文件信息,其中file列包含文件路径,可用于批量替换
  • media_streams - 内含媒体字幕信息,其中uri列包含文件路径,可用于批量替换
  • section_locations - 内含媒体库路径,其中root_path列包含文件路径,可用于批量替换

无需改动:

  • library_sections - 内含plex媒体库的名称等信息
  • media_items - 内含媒体封面及缩略图位置信息,user_thumb_url及user_art_url存放路径类似media://0/xoxixo.bundle/Cont...

媒体附加信息

建议遍历此目录所有.xml文件,替换包含路径的文字内容
  • /plex/config/Library/Application Support/Plex Media Server/Media/localhost/*.xml

    例:
/Library/Application Support/Plex Media Server/Media/localhost/0/0cd1d3b24102303ab2b954d7a37e8f7f7ebf8b7.bundle/Contents/Plex Movie/Subtitles.xml

内含字幕信息,可批量替换。字幕文件路径存储在:Subtitles/Language/Subtitle/@file字段下。

媒体库替换步骤

删除 /plex/config/Library/Application Support/Plex Media Server/Preferences.xml
批量替换数据库删除敏感信息 ,替换媒体路径信息 /plex/config/Library/Application Support/Plex Media Server/Plug-in Support/Databases/com.plexapp.plugins.library.db
批量查找此目录下的xml文件 /plex/config/Library/Application Support/Plex Media Server/Media/localhost/*.xml,替换媒体路径信息

替换删除并替换数据库中的媒体库路径

使用此命令打开sql数据库,输入以下sql命令

sqlite3 "/plex/config/Library/Application Support/Plex Media Server/Plug-in Support/Databases/com.plexapp.plugins.library.db"

# 数据库脱敏
delete from accounts;
delete from devices;
delete from external_metadata_sources;
delete from play_queues;

# 批量替换表中文件目录路径字符
update media_parts set file = replace(file,'/home','/newPath');
update media_streams set uri = replace(uri,'/home','/newPath');
update section_locations set root_path = replace(root_path,'/home','/newPath');

手动替换媒体附加信息的路径(有问题,弃用)
注意将 /PATH 替换为新的媒体挂载目录(正则中路径的/字符需要改为/进行字符转义),将 /plex/config 替换为pelx媒体库的路径。

sed -i "s/\/home/\/PATH/g" `grep abc -rl "/root/plex/config/Library/Application Support/Plex Media Server/Media/localhost/0"`
# 有问题,无法查找到文件

find "/root/plex/config/Library/Application Support/Plex Media Server/Media/localhost/0" -name '*xml' | xargs -i echo mv \"{}\" \"{}\" | sed 's/\/home/\/PATH/g' | sh

脚本(sql执行时有问题,弃用)

deal_plex_data.sh

#!/bin/bash

# Plex 应用数据的 config 目录
#PLEX_DIR=/plex/config
PLEX_DIR=/plex/config
# 当前默认的媒体库目录
MEIDA_PATH_DEF=/home
# 被替换的媒体库目录
MEIDA_PATH=/home
# 默认是否替换数据路径
place_path=n

read -p "# 是否对数据库中媒体路径进行替换? (y/N):" place_path
if [ "$place_path" = "y" ] || [ "$place_path" = "Y" ]; then
    read -p "# 当前根目录为 '$MEIDA_PATH' , 请输入新的根目录路径:" MEIDA_PATH
    echo "# Plex 数据库中媒体根目录将被替换为 '${MEIDA_PATH}'"
fi

echo '# 删除配置文件 Preferences.xml'
rm -f "${PLEX_DIR}/Library/Application Support/Plex Media Server/Preferences.xml"

echo '# 处理媒体库脱敏及路径替换'
sudo apt-get install sqlite3 -y
SQL_PATH="${PLEX_DIR}/Library/Application Support/Plex Media Server/Plug-in Support/Databases/com.plexapp.plugins.library.db"
# 删除表中所有数据 db.execSQL("delete from table")
sqlite3 ${SQL_PATH} "delete from accounts; delete from devices; delete from external_metadata_sources;  delete from play_queues;"
# 批量替换表中文件目录路径字符
if [ "$place_path" = "y" ] || [ "$place_path" = "Y" ]; then
update media_parts set file = replace(file,"'${MEIDA_PATH_DEF}'","'${MEIDA_PATH}'");
update media_streams set uri = replace(uri,"'${MEIDA_PATH_DEF}'","'${MEIDA_PATH}'");
update section_locations set root_path = replace(root_path,"'${MEIDA_PATH_DEF}'","'${MEIDA_PATH}'");
fi
.quit
echo '# 数据库处理完毕'

# 批量替换媒体附加信息中的文件目录路径字符
echo '# 媒体附加信息替换'
if [ "$place_path" = "y" ] || [ "$place_path" = "Y" ]; then
echo '# 您还需要手动处理媒体附加信息中的路径替换'
fi

修改版脚本(待测试)

#!/bin/bash
# Plex 应用数据的 config 目录
#PLEX_DIR=/plex/config
PLEX_DIR=/var/lib/plexmediaserver 
# 当前默认的媒体库目录
#MEIDA_PATH_DEF=/home/plex
#MEIDA_PATH_DEF1=/home/gdrive
#MEIDA_PATH_DEF2=/nongjiale
# 被替换的媒体库目录
read -p "请输入数据库中媒体路径,如选择默认路径请直接回车:" MEIDA_PATH_DEF_IN
if [[ $MEIDA_PATH_DEF_IN == "" ]]; then
    MEIDA_PATH_DEF=/home/gdrive
else
    MEIDA_PATH_DEF=$MEIDA_PATH_DEF_IN
fi
# 默认是否替换数据路径
read -p "# 是否对数据库中媒体路径进行替换? (y/N):" place_path
if [ "$place_path" = "y" ] || [ "$place_path" = "Y" ]; then
    read -p "当前数据库中记录的媒体根目录为 '$MEIDA_PATH_DEF' , 请输入新的根目录路径:" MEIDA_PATH
    echo "# Plex 数据库中媒体根目录将被替换为 '${MEIDA_PATH}'"
fi

echo '# 删除配置文件 Preferences.xml'
rm -f "${PLEX_DIR}/Library/'Application Support'/'Plex Media Server'/Preferences.xml"

echo '# 处理媒体库脱敏及路径替换'
apt-get install sqlite3 -y||yum install sqlite3 -y
SQL_PATH="${PLEX_DIR}/Library/'Application Support'/'Plex Media Server'/'Plug-in Support'/Databases/com.plexapp.plugins.library.db"
if [ "$place_path" = "y" ] || [ "$place_path" = "Y" ]; then
# 删除表中所有数据 db.execSQL("delete from table")
sqlite3 ${SQL_PATH} "delete from accounts; delete from devices; delete from external_metadata_sources;  delete from play_queues;"
# 批量替换表中文件目录路径字符
update media_parts set file = replace(file,"'${MEIDA_PATH_DEF}'","'${MEIDA_PATH}'");
update media_streams set uri = replace(uri,"'${MEIDA_PATH_DEF}'","'${MEIDA_PATH}'");
update section_locations set root_path = replace(root_path,"'${MEIDA_PATH_DEF}'","'${MEIDA_PATH}'");
.quit
echo '# 您还需要手动处理媒体附加信息中的路径替换'
else
    sqlite3 ${SQL_PATH} "delete from accounts; delete from devices; delete from external_metadata_sources;  delete from play_queues;"
fi
echo '# 数据库处理完毕'
exit 0
0

评论 (0)

取消