Automatic backup of XBMC databases, config files, and images on Linux (Ubuntu)

XBMC is an awesome media center application. I have previously written several posts on it. With all the customizations that I have been doing with my XBMC setup, it became more important than ever to backup my XBMC configuration files. But I wanted a simple and automatic solution. I could not find any addon to do this. After several hours of research I decided to write my own script for it. Described below is a solution that has worked for me whenever I had restore my settings.

In linux, the XBMC data folder is typically stored in the user's home directory under the name ".xbmc". Example:

/home/user/.xbmc

Within this folder there are several subfolders containing the following information:

  • Addons and Addon Settings
  • Databases (SQLite Videos and Music)
  • Playlists
  • Thumbnails (Posters, Fanarts, etc.)
  • Settings and Configuration Files

While XBMC offers exporting your library, I have always found that backing up the whole directory is the most convenient and foolproof way. Depending on your media library and addons, the .xbmc folder can range from few MBs to few GBs. If your .xbmc folder becomes huge with unncessary thumbnails, refer to this post on how to clean them.

As in my case, if space is not a limitation then backing up the whole .xbmc directory may be a good solution. In case of need, just restore the whole .xbmc directory and your XBMC will go back to how it was on the day the backup was made. Below is what I wanted to achieve:

  1. Automatic
  2. Simple - set it and forget it
  3. Periodic - every week in my case
  4. Automatically delete backups older than a certain date

Here is the bash script I came up with:

#!/bin/bash
DATE=`date +%Y%m%e-%H%M%S` 
find /home/user/backups/xbmc/ -type d -mtime +30 -exec rm -rf {} ;
mkdir /home/user/backups/xbmc/$DATE
rsync -rgop /home/USER/.xbmc/ /home/user/backups/xbmc/$DATE/

Save the above script as "xbmcweeklysync.sh" (or whatever) in folder of your choice.

NOTES:
DATE: stores the current date and time as YYYYMMDD-HHMMSS.
find: finds all folders older than 30 days and deletes them. To change the folder age limit, change "-mtime +30" to whatever age that works for you.
mkdir: creates a folder with the name YYYYMMDD-HHMMSS within your backups folder.
rsync: syncs the current .xbmc folder to the folder created above. Read the rsync manual to understand what "-rgop" means or to customize the options further.

Now with the script ready, setup up a cron job to run it automatically every day. I find it easier to setup a cron job through my Webmin interface. Below is a picture to show my setup:

Xbmc Backup Weekly Cron Job
Xbmc Backup Weekly Cron Job

You can customize the cron job to execute at a frequency you wish (my need was ever week). After having run the above script for couple months month now, this is how my backup folder looks:

Xbmc Backups
Xbmc Backups

Only the weekly backups within the last 30 days have been retained. There you go. Hope this works for you.

Be the 1 in 200,000. Help us sustain what we do.
31 / 150 by Dec 31, 2024
Join Us (starting from just $1.67/month)

Anand

Anand is a self-learned computer enthusiast, hopeless tinkerer (if it ain't broke, fix it), a part-time blogger, and a Scientist during the day. He has been blogging since 2010 on Linux, Ubuntu, Home/Media/File Servers, Smart Home Automation, and related HOW-TOs.