Backup Plex For Ubuntu Reliably

It’s no secret that I like watching TV. From movies to TV shows, I like ’em all. I’m also a very happy cord cutter. This means I use a combination of services ranging from Netflix to Amazon Prime for my video content. I also have a fair bit of media on Plex. Like a lot of people, I use Plex to store digital copies of my media. Doing so, let’s me watch my DVDs in a format that doesn’t require me to leave my couch.

Unfortunately, Plex is dependent on me to maintain a working storage option here at home. In my case, that would be a USB connected hard drive attached to an old computer. For you, perhaps it’s a NAS with multiple drives.

On paper, this sounds fine…until one of the hard drives fail. Remember, this isn’t a redundant NAS setup at my house – this is a basic hard drive that could crash given enough time. This means if my internal hard drive crashes, my configuration files for Plex are toast. I’d still have my media, but I’d lose my place instantly if I’m part way through a movie or if I’m watching a TV series. It’s even worse if I lose the external drive with the media. The configuration files would be safe on the internal drive, but tons of media files would now be gone with the wind. Both situations aren’t okay with me! This is why I run automatic backups.

Introducing cp and rsync

Two of my favorite backup tools for media centers are cp and rsync. I utilize cp because I can use it to overwrite files each and every time. This ensures that there is no odd-ball outcomes with configuration files. This leaves me with rsync, which I find very helpful in dealing with larger media files.

Now some of you might be wondering why I don’t simply run rsync for both media and my configuration, let me break it down as follows.

When I use cp, I know for a fact that my previous backup is completely wiped and rewritten. Since my configuration is is MUCH smaller than my media, doing a complete overwrite is perfectly plausible. It also ensures that there is no question that my configuration is intact and up to date.

This brings me to rsync. For backing up my media, using rsync makes the most sense. It provides two key points of functionality. The first point is that all backups are incremental. This means that I’m not backing up the same media over and over, instead, only new media is backed up. And the last point is rsync handles interruptions really well. So if you’re rsync backup is interrupted for some reason, no biggie – it’ll resume from where it left off during the next backup. For large media transfers back and forth, this is a tremendously helpful.

Password-less sudo commands

The next step is to implement cron. This automates a bash script that handles the Plex backups for you. To do this however, we need to do the following:

1) Enable your sudo capabilities to turn off and on Plex services without a password prompt (and do so safely).
2) Run a script that handles all the necessary commands.
3) Schedule the script to run on a set schedule automatically.

Now before we get into editing your sudo powers, I need to clarify something. Adding privileges to your sudo powers needs to be done with care. Don’t use wild cards with permissions, keep things locked down as much as possible. Okay, end of lecture. Let’s get started.

The editor to be used for handling your sudoers file may vary. Ubuntu users will find that nano will be the default editor, others may find it’s something else like vi. Both editors, in my opinion are a matter of taste. I will suggest however, that nano is a far better choice for those not familiar with more advanced editors such as vi. Arch users can learn more about using vi or nano for editing their sudoer configuration here. Ubuntu users, as previously stated, will find nano is the default option.

In a terminal:

sudo visudo

This will bring up nano with your configuration file. Look for the following heading:

# Allow members of group sudo to execute any command

Next, you will want to give Plex permission to stop and start its services. Mind you, this is being done on an Ubuntu 14.04 machine. Regardless, full path for the commands are needed, not merely what you would run manually to start and stop a service.

Remember: Your destination paths will reflect your NAS destination or your attached USB storage location. The example is merely reflective of what I’m using.

# Allow members of group sudo to execute any command
%adm ALL=(ALL) NOPASSWD: /sbin/start plexmediaserver, /sbin/stop plexmediaserver, /bin/cp -av /var/lib/plexmediaserver /media/username/recovery/laptopbackup-plex/configuration

The key entries we made here grant specific commands no-password access to running without a password prompt. This includes starting & stopping Plex services, plus copying the configuration files using the cp command. Do not wildcard (*) anything with the NOPASSWD option. You want to limit this access as much as possible. Also be sure to note how each command combination is separated by a comma – without using a comma, it would appear to be one large single command.

The next headline ‘we’re going to look for is:

#includedir /etc/sudoers.d

You’ll want to add this line immediately underneath the above “includedir” heading:

#username ALL=NOPASSWD: /home/username/

Now save your work by pressing Ctrl+Z then Y.

Pro tip: Make sure the username matches the one you’ll be using. In case, the username would be matt for example.

Important note: I found the provided combination of the username line and the previous adm to be 100% effective. Other configurations I tested did not work as desired. So my advise is to use the setup I’m sharing in this article for success. In short, I make no claims that this is the cleanest way to do this, rather this is the layout that worked for me based on countless configuration tests. In the end, the result is my configuration works properly and 100% of the time.

The automation script

Now I’m not going to pretend like I’m a great creator of bash scripts. Obviously, this could be formatted in a cleaner fashion. That said, like with my sudoer configuration, this has been proven to work flawlessly.

Open up a text file, then save the following to your user’s home directory – just give it a name like backup-schedule.

sudo /sbin/stop plexmediaserver
sleep 5
sudo /bin/cp -av /var/lib/plexmediaserver /media/username/recovery/laptopbackup-plex/configuration
sleep 20
rsync -a /media/username/VideoMedia /media/username/recovery/laptopbackup-plex/video-files --progress
sleep 10
sudo /sbin/start plexmediaserver

Now save the file and make it executable from the terminal (You do NOT need to add a .sh to the script – adding .sh is totally up to you):

cd ~/
chmod +x backup-schedule

Important: There are a few things in this script you’ll need to change. The sections that start with /media represent my personal drives and their mount points. Your setup will likely differ, so make sure the script reflects your samba mount points or attached storage mount points.

You may also notice that I appended -progress to my rsync command. I always use this option because I need to visually see what the progress is and how long is going to be left. Your first run is going to likely be a long one, so seeing how long this is going to take is critical.

Also be sure to notice how I’m using sudo for certain commands and not for others. Those commands with sudo attached need super user privileges, hence my work earlier with my sudoer configuration. And of course, for the sudo commands – make sure you match the full paths listed. To your sudoer, there’s a world of difference between start plexmediaserver and /sbin/start plexmediaserver.

Setting up a schedule

Usually setting up a script to run would be done via cron. If you’d like to learn to use cron via the terminal, this guide will get you going. If however, you simply want a GUI to do the time calculations for you, I’ve had success using GNOME Schedule. Simply install it from your software repository, then it should appear in your launch menu under Schedule Tasks.

Using GNOME Schedule is incredibly simple. Just launch the application, then choose if you’d like to use a one time schedule or a repeating one. In your case, you’re going to want to setup a reoccurring task. With the reoccurring task window open, choose the following options.

1) Provide a description (Plex Backup).
2) In the command section, place the path and file name for your Plex backup script. If it’s in your home directory, it might look something like ~/backup-schedule or /home/YourUserName/backup-schedule.
3) Click Advanced.
4) Choose a minute reflecting either 0 for on the hour or 30 for the thirty minute mark (at the exact minute option selected). I chose 0.
5) When selecting the hour to run, I recommend late at night or otherwise when you’re going to be sleeping. I chose (at the exact hour) 4 to represent 4am. Remember the time is military time.
6) As for Day, Month and Weekday, I left these with the * wild card as I want this to run daily.

Once you’ve made your selections, you can check your settings by looking at the Preview heading at the bottom of the window. It should read “On every day at 04:00” if you set everything correctly. Click Add, then you’ll be notified that this will be running out of your home directory – since this is a user-specific crontab we’re setting up. Just click OK and we’re done.

And that’s it! You’re now ready to have your Plex configuration backed up automatically to a backup drive or NAS.

Closing thoughts

I realize that I will be getting some “friction” regarding my choice of using cp over rsync for backing up my config. I want to be clear – it’s entirely possible that rsyncing my Plex configuration might work great. That said, I opted to use cp -av because I know for certain that all the files will be overwritten with fresh copies, plus the original permissions will always be left intact.

Photo by alexisgallisa

FTC required disclosure of Material Connection: The Amazon product links in the post above are “affiliate links.” This means if you click on the link and purchase the item, Freedom Penguin will receive an affiliate commission.

Matt Hartley
Freedom Penguin’s founder & talking head – Matt has over a decade working with Linux desktops, his operating system experience consists of both Windows and Linux operating platforms. In addition to writing articles on Linux and open source technology for and, Matt also once served as a co-host for a popular Linux-centric podcast.

Matt has written about various software titles, such as Moodle, Joomla, WordPress, openCRX, Alfresco, Liferay and more. He also has additional Linux experience working with Debian based distributions, openSUSE, CentOS, and Arch Linux.

Leave a Comment