Sunday, September 9, 2012

Howto : XBMC share and sync your Media library between multiple XBMC instances using Samba/NFS and Mysql




Last update:
09/19/2012: FAQ - deleting databases if required 


If you have multiple XBMC installations (or if you're planing to), you should be interested in sharing and syncing your Media Library between your installations.

When you have multiple XBMC running in your Network, and one central storage, this will really really simplify your life ^^

Advantages:

  • Share watched and unwatched status for your media on all devices
  • Stop watching a movie or TV show in one room then finish watching it in another room automatically
  • Only one library to maintain for all devices
  • Consolidate thumbnails and fanart to save hard drive space (only with XBMC Frodo v12) 

Main source:
http://wiki.xbmc.org/index.php?title=HOW-TO:Sync_multiple_libraries/Share_your_files

You'll find in this Wiki page all required information to achieve this, but i wanted to share my own experience to help you implementing this great feature!

I recommend you to use XBMC Frodo v12, even if not yet stable this version will work better in this configuration as it is able to deal itself with shared Library data like thumbnails and fanart.

With XBMC Eden (v11) sharing the Library works too but you have to synchronize yourself Thumbnails Datas.

Let's start ^^


1. Share your files



First consider your sharing protocol:

You may use 2 major file sharing protocols to share your Media Library, SMB protocol which is the most implemented (used by Windows, implemented in Linux through Samba) or NFS. (Network File System, more known in Unix World)

Choosing is simple, if you have or plan to have Windows XBMC clients, use SMB with Samba.
If you only have Linux Based XBMC, you should use NFS because of betters speed performances.

Note: NFS will also be supported in XBMC under Windows, but you could lack performances



To share your file with SMB using Samba:


If it's not already installed, you have to get samba up and running to share your Multimedia files between your XBMC instances:

On Ubuntu based systems:
$ sudo apt-get install samba


Share your files:

Setup simple shares for your media library, edit "/etc/samba/smb.conf" and add your share, example:
[MY_SHARE]
path = /media/MY_VOLUME
available = yes
browsable = yes
public = yes
writable = yes


Restart samba:
$ sudo service samba restart



To share your file with SMB using NFS:


On Ubuntu based systems, install NFS Server components:
$ sudo apt-get install nfs-kernel-server


Edit "/etc/exports" and configure your shared directory, example (adapt MY_VOLUME  and your Local Network IP):
/media/MY_VOLUME xxx.xxx.xxx.xxx/255.255.0.0(rw,all_squash,insecure,no_subtree_check,async)


Note: "async" option is not recommended for secured write operations, but an XBMC client should not expected to have write operations), this option may enhance NFS performances)
You can also choose "sync".

Restart NFS Server:

sudo /etc/init.d/nfs-kernel-server restart


2. Install and setup Mysql for your Database


Extracted from main XBMC Wiki page, follow these instructions:

Install Mysql:
$ sudo apt-get install mysql


- Create a main admin password when asked

- After installation has complete, edit "/etc/mysql/my.cnf" and do changes as follows:

Look for the line "bind-address" and change "127.0.01" with your host IP address.
You can also simply comment this line to allow access from anywhere.


Restart Mysql:
$ sudo service mysql restart



Login to Mysql using client (adapt password):
$ mysql -u root -p


Create the XBMC user,  set password and rights, enter commands as follows:
CREATE USER 'xbmc' IDENTIFIED BY 'xbmc';
GRANT ALL ON *.* TO 'xbmc';


Exit mysql client. (exit)


3. Set up XBMC


At this point, if you already have set your Media Library inside your main XBMC installation, you have 2 possibilities:

  • Converting your existing Media Library
  • Re-creating your Media Library from scratch

To be honest, even if you have a big Media Library, converting it will be enough complicated for not so much benefits.

This is my opinion, but i would recommend to completely re-create your Library from scratch, this is much easier than converting it, moreover that you won't loose your existing thumbnails and fanart.

If you want to convert it, follow instructions at:
http://wiki.xbmc.org/index.php?title=HOW-TO:Sync_multiple_libraries/Setting_up_XBMC

If you want to re-create it from scratch (my advice), do:

- In XBMC, Go to Video and delete each source you have previously set, say Yes when XBMC asks if you want to purge these media from your Library

- Go to Parameters > Video and Clean your Library

- Configure your mysql settings by editing your advancedsettings.xml (~/.xbmc/userdata/advancedsettings.xml) and adding:

Adapt "xxx.xxx.xxx.xxx" with your main IP Address:
    <videodatabase>
        <type>mysql</type>
        <host>xxx.xxx.xxx.xxx</host>
        <port>3306</port>
        <user>xbmc</user>
        <pass>xbmc</pass>
    </videodatabase> 

    <musicdatabase>
        <type>mysql</type>
        <host>xxx.xxx.xxx.xxx</host>
        <port>3306</port>
        <user>xbmc</user>
        <pass>xbmc</pass>
    </musicdatabase>


- Restart XBMC

- Go in Video and re-set your Media Sources using Samba/NFS shares instead of Local paths

This is very important, right now any source you want to add in your Media Library must be done using Samba shares, files paths must use UNC patchs to be able to be shared.

Here's some screenshots to explain:

When you will add your source, ensure your choose a SMB or NFS Share:




And navigate until your Media Share, and finally set the scrapper as always.

Any media found will automatically be added to the Mysql Database.


4- Setup other XBMC instances (clients)


- Stop XBMC

First thing to do with any XBMC client instance: Setup advancedsettings.xml

- Edit each of your client advancedsettings.xml and ensure your add the mysql section (adapt your IP Adress), this is exactly the same code than for the main instance:


    <videodatabase>
        <type>mysql</type>
        <host>xxx.xxx.xxx.xxx</host>
        <port>3306</port>
        <user>xbmc</user>
        <pass>xbmc</pass>
    </videodatabase> 

    <musicdatabase>
        <type>mysql</type>
        <host>xxx.xxx.xxx.xxx</host>
        <port>3306</port>
        <user>xbmc</user>
        <pass>xbmc</pass>
    </musicdatabase>


- Restart XBMC (on clients)


Then setup your sources on clients:

For any other XBMC instances, just copy your main XBMC instance sources.xm file to your clients.

This is very important as all of your XBMC instances have to use the same source settings, using SMB shares.

To achieve this, nothing more simple :-)

"sources.xml" is located in "~/.xbmc/userdata/sources.xml"

If you have playlists and favorites, you can also just copy them from your main XBMC instance to client instances:


  • Playlists are located in "~/.xbmc/userdata/playlists"
  • favourites.xml in "~/.xbmc/userdata/favourites.xml"


Final before launching your XBMC client:

About fanarts and other datas:

- If your clients are running XBMC Frodo (v12), then you don't have nothing to do.
XBMC will take care itself with fanart and other data.
Any future added Media will also be available in clients, including Thumbnails and fanart.

- If you have clients with XBMC Eden (v11), you have to copy the Thumbnails directory from your main XBMC instance: ~/.xbmc/userdata/Thumbnails/

For future media add, it will be available on both main XBMC and clients, but you won't get Thumbnails and fanart in clients.

For this to work, you will have to set scrappers in clients too (as in main XBMC instance) and scan for new medias each time you added some. (or enable scan at launch time)





And finally start XBMC.

Magical you'll get your full Media Library centralized and always synced :-)

Off course you main XBMC instance acts as the main server, clients won't be able to access to Media Library if the server is down...



FAQ



- How to re-initialize my database in case of trouble:

In case of trouble you may have to totally re-reinitialize your database. (if you're running thousands of hard tests ^^^).

To do so, simply delete all XBMC databases and re-create from scratch:^

Note: don't worry you won't loose nothing


Login to Mysql using client (adapt password):
$ mysql -u root -p



List XBMC Databases:
mysql > show databases;



Then for each database, delete it, example:
mysql > drop database xbmc_video;


And restart from importing your Media ^^