Merge pull request #21 from ChuckPa/chuckpa/transfer-view-history-with-viewstate

Export View history with viewstate
This commit is contained in:
Chuck 2022-11-21 01:05:34 -05:00 committed by GitHub
commit cd9fe591a3
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -169,7 +169,7 @@ DoBackup() {
# Make a backup of the current database files and tag with TimeStamp # Make a backup of the current database files and tag with TimeStamp
MakeBackups() { MakeBackups() {
Output "Backup current databases with '-ORIG-$TimeStamp'" Output "Backup current databases with '-ORIG-$TimeStamp' timestamp."
for i in "db" "db-wal" "db-shm" "blobs.db" "blobs.db-wal" "blobs.db-shm" for i in "db" "db-wal" "db-shm" "blobs.db" "blobs.db-wal" "blobs.db-shm"
do do
@ -534,6 +534,9 @@ export TMP="$DBTMP"
# Work in the Databases directory # Work in the Databases directory
cd "$DBDIR" cd "$DBDIR"
# Get the owning UID/GID before we proceed so we can restore
Owner="$(stat $STATFMT '%u:%g' $CPPL.db)"
# Run entire utility in a loop until all arguments used, EOF on input, or commanded to exit # Run entire utility in a loop until all arguments used, EOF on input, or commanded to exit
while true while true
do do
@ -1112,71 +1115,95 @@ do
continue continue
fi fi
Output " "
WriteLog "Import - Attempting to import watch history from '$Input' " WriteLog "Import - Attempting to import watch history from '$Input' "
# Confirm our databases are intact # Confirm our databases are intact
if ! CheckDatabases; then if ! CheckDatabases "Import "; then
Output "Error: PMS databases are damaged. Repair needed. Refusing to import." Output "Error: PMS databases are damaged. Repair needed. Refusing to import."
WriteLog "Import - Verify main database - FAIL" WriteLog "Import - Verify main database - FAIL"
continue continue
fi fi
# Check the given database # Check the given database
Output "Checking database '$Input'"
if ! CheckDB "$Input"; then if ! CheckDB "$Input"; then
Output "Error: Given database is damaged. Repair needed. Database not trusted. Refusing to import." Output "Error: Given database '$Input' is damaged. Repair needed. Database not trusted. Refusing to import."
WriteLog "Import - Verify '$Input' - FAIL" WriteLog "Import - Verify '$Input' - FAIL"
continue continue
fi fi
WriteLog "Import - Verify '$Input' - PASS"
Output "Check complete. '$Input' is OK."
# Make a backup # Make a backup
Output "Backing up databases" Output "Backing up PMS databases"
if ! MakeBackups "Import "; then if ! MakeBackups "Import "; then
Output "Error making backups. Cannot continue." Output "Error making backups. Cannot continue."
WriteLog "Import - MakeBackups - FAIL" WriteLog "Import - MakeBackups - FAIL"
Fail=1 Fail=1
continue continue
else
WriteLog "Import - MakeBackups - PASS"
fi fi
WriteLog "Import - MakeBackups - PASS"
# Export viewstate from DB # Export viewstate from DB
Output "Exporting Viewstate / Watch history" Output "Exporting Viewstate & Watch history"
echo ".dump metadata_item_settings" | "$PLEX_SQLITE" "$Input" | grep -v TABLE | grep -v INDEX > "$TMPDIR/Viewstate.sql-$TimeStamp" echo ".dump metadata_item_settings metadata_item_views " | "$PLEX_SQLITE" "$Input" | grep -v TABLE | grep -v INDEX > "$TMPDIR/Viewstate.sql-$TimeStamp"
# Make certain we got something usable # Make certain we got something usable
if [ $(wc -l "$TMPDIR/Viewstate.sql-$TimeStamp" | awk '{print $1}') -lt 1 ]; then if [ $(wc -l "$TMPDIR/Viewstate.sql-$TimeStamp" | awk '{print $1}') -lt 1 ]; then
Output "No viewstates found to import." Output "No viewstates or history found to import."
WriteLog "Import - Nothing to import - FAIL"
continue continue
fi fi
# Make a working copy to import into # Make a working copy to import into
Output "Making backup copy of main database" Output "Preparing to import Viewstate and History data"
cp -p $CPPL.db "$TMPDIR/Viewstate.db-$TimeStamp" cp -p $CPPL.db $CPPL.db-$TimeStamp
Result=$?
if [ $Result -ne 0 ]; then
Output "Error $Result while making a working copy of the PMS main database."
Output " File permissions? Disk full?"
WriteLog "Import - Prepare: Make working copy - FAIL"
continue
fi
# Import viewstates into working copy # Import viewstates into working copy
Output "Importing Viewstate data" printf 'Importing Viewstate & History data...'
"$PLEX_SQLITE" $CPPL.db < "$TMPDIR/Viewstate.sql-$TimeStamp" 2> /dev/null "$PLEX_SQLITE" $CPPL.db-$TimeStamp < "$TMPDIR/Viewstate.sql-$TimeStamp" 2> /dev/null
# Make certain the resultant DB is OK # Make certain the resultant DB is OK
Output " done."
Output "Checking database following import" Output "Checking database following import"
if ! CheckDB $CPPL.db ; then if ! CheckDB $CPPL.db-$TimeStamp ; then
Output "Error $Result during import. Import corrupted database."
Output " Undoing viewstate import." # Import failed discard
Output "Error: Error code $Result during import. Import corrupted database."
Output " Discarding import attempt."
rm -f $CPPL.db-$TimeStamp
cp -p "$TMPDIR/Viewstate.db-$TimeStamp" $CPPL.db
WriteLog "Import - Import: $Input - FAIL" WriteLog "Import - Import: $Input - FAIL"
continue continue
fi fi
# Import successful; switch to new DB
Output "PMS main database is OK. Making imported database active"
WriteLog "Import - Import: Making imported database active"
# Move from tmp to active
mv $CPPL.db-$TimeStamp $CPPL.db
# We were successful # We were successful
Output "Viewstate import successful." Output "Viewstate import successful."
WriteLog "Import - Import: $Input - PASS" WriteLog "Import - Import: $Input - PASS"
# We were successful # We were successful
SetLast "Import" "$TimeStamp" SetLast "Import" "$TimeStamp"
continue
# 8. - Show Logfile # 8. - Show Logfile
elif [ $Choice -eq 8 ]; then elif [ $Choice -eq 8 ]; then