Kodi Community Forum
How I sync my library between two kodis - Printable Version

+- Kodi Community Forum (https://forum.kodi.tv)
+-- Forum: Support (https://forum.kodi.tv/forumdisplay.php?fid=33)
+--- Forum: General Support (https://forum.kodi.tv/forumdisplay.php?fid=111)
+---- Forum: OS independent / Other (https://forum.kodi.tv/forumdisplay.php?fid=228)
+---- Thread: How I sync my library between two kodis (/showthread.php?tid=354033)



How I sync my library between two kodis - MorkZaDork - 2020-04-30

Hello all, I spent a good number of hours banging my head on this one yesterday so now that I have it working, I figured it'd be good to post out there in case anybody runs into the troubles I had. I'm not saying this is the best method to do this, but it seemed like it was my only option given my constraints and it ended up working so it's an idea. 

Background
I have two raspberry pi zero w's running openelec and kodi krypton. One has the media attached via a USB hard drive and the other one reads that hard drive over the network via samba. All in all, it works relatively well, except progress syncing. Since both kodis store their watched status locally, they would only keep track of what you watched on that specific kodi device. So I set out to try and make something work, and found out it is a massive pain in the butt.

The main problem I had was no matter what I could find on the internet for a recommended way to do this, openelec just wouldn't allow it. You can't apt-get with it, and their add-on repository is very pared down. I kept running into roadblock after roadblock. I also didn't want to have to rely on a third device to do the syncing, I wanted the two kodi devices to be able to do this on their own, amongst themselves.

After hours and hours of fiddling, I finally go it working, and here's what I did.

Notes
1. This assumes you know what you're doing with SSH, linux, samba, nano, crontab, etc.
2. Both kodi devices need to have static IP addresses setup (can be done in the GUI).
3. This will probably not work if you're watching two different kodi devices at the same time. This assumes you'll only watch one at a time, then the other kodi will continuously sync up the progress from that until such a time that you stop watching the first device. When you start watching on the second device, then the first device will start syncing up to it.
4. This writeup ONLY includes video database. I don't use my kodi for music, but you could easily modify it to also do the same thing for the music database.
5. Your version of kodi may use a different database file, so adjust MyVideos107.db as necessary.
6. This is a LONG writeup but actually only takes a few minutes to implement. It may be one of the "easier" options for that reason.

The way that it works is the kodi devices will regularly compare the timestamp of its local video database file with that of the one on the other kodi. If the remote kodi has a newer database (meaning you've been watching it), it will copy the database file over from that kodi to itself. They will both do this in staggered four minute intervals, only copying the database over when it's found to be newer.

How to Implement

STEP 1
On the main device that has the USB HDD attached, you cannot watch media directly from the USB HDD. The database keeps track of the full path in addition to the file to know if you've watched it or not. So if one device references sda1-USB-blah\file.ext and the other one references smb:ip.address\share\file.ext, it will treat them as two different files and the syncing won't work. To get around this, you have to essentially watch the media from your USB HDD from a samba share instead. Except that samba share just points back to itself.

Go into video sources and create a new samba share to the media (on the same ip address of the kodi you're using). Then from there, set that video source as the favorite and always watch media from it instead of the local sda1 device. This will basically force the database file to use the full samba share path, which will be required for syncing up the other kodi device that's using a samba share instead of a local HDD.


STEP 2
Have each kodi device mount a new, separate samba share on bootup. This is specifically ONLY to compare the local database to the remote database on the other kodi, this isn't for sharing media. 

Do this on both kodi devices, SSH in and create a ~/.config/autostart.sh file (just nano it)

In autostart.sh put the following:
mkdir /media/kodimount
mount -t cifs -o username=root //ip.address.of.otherkodi/Userdata/Database /media/kodimount
Then CTRL-X, Y, ENTER to save and exit

(I am aware I could have done this in fstab, what can I say, this worked well enough for me to not have to figure out yet another thing)

Lastly, make autostart.sh executable:
chmod +x autostart.sh

NOTE - ip.address.of.otherkodi will be different depending on the device, because you want to put the IP address of the OTHER kodi on the network here.


STEP 3
We'll use crontab to set up the interval of when to compare the two files and copy over when necessary:

Go into crontab -e and:

For the first kodi, paste this in, then CTRL-X, Y, ENTER to save
0,4,8,12,16,20,24,28,32,36,40,44,48,52,56 * * * * cp -p -u -f /media/kodimount/MyVideos107.db ~/.kodi/userdata/Database

For the second kodi, do the same but use this:
2,6,10,14,18,22,26,30,34,38,42,46,50,54,58 * * * * cp -p -u -f /media/kodimount/MyVideos107.db ~/.kodi/userdata/Database

The reason I have two different crontab entries here is to stagger them. What I found is if both kodis try to sync at the same time, they end up corrupting the database file and copying over a blank one, losing all of your progress. This sets them up on a 4 minute interval, but staggered so every second minute only one of them is running, giving it time to do its work and complete the copy (it doesn't take 2 minutes to copy, this is just an extra safety precaution) before the other kodi tries to sync. You can NOT do */4 for the minute in crontab here because that'll just make them run on the same minute, creating the same problem. You have to spell out every explicit minute of the hour you want them to run on.

If you're curious about the cp switches here:
-p: this is necessary to preserve the timestamp coming from the remote database file. If you don't use this, the newly copied file will get a new timestamp and then they will be constantly vollying the database file back and forth every 2 minutes. Not necessarily the worst thing, but it's a lot of writes to your SD card that aren't actually necessary.
-u: only copy the file when the remote is newer
-f: force copy (I think)

DONE: That's actually it, it should be working now.

I am by no means a linux or kodi expert, I'm just sharing what worked for me.


RE: How I sync my library between two kodis - Karellen - 2020-04-30

(2020-04-30, 21:18)MorkZaDork Wrote: I have two raspberry pi zero w's running openelec
OpenELEC is a dead fork of kodi that has not been maintained for over 3 years. I am surprised you have not come across any issues while using it.

Change to LibreELEC.


RE: How I sync my library between two kodis - Klojum - 2020-04-30

(2020-04-30, 21:18)MorkZaDork Wrote: I have two raspberry pi zero w's running openelec and kodi krypton.

Syncing libraries between those two versions will never happen. Their data layouts have too many differences by now.
So I'm hoping you meant LibreELEC all along, and not OpenELEC which was abandoned some years ago.

(2020-04-30, 21:18)MorkZaDork Wrote: I am by no means a linux or kodi expert, I'm just sharing what worked for me.

That in itself is good, but using a centralized MySQL database (running on a separate Raspberry Pi 3/4) would be my preference from the beginning.


RE: How I sync my library between two kodis - scott967 - 2020-04-30

I see this would work for keeping in progress/watched in sync, but not sure what happens when you update the library.  Seems like you also need to sync Textures13.db?

scott s.
.


RE: How I sync my library between two kodis - DarrenHill - 2020-05-01

Thread moved to OS independent, as discussions isn't for support.