Skip to main content

Part 6 - Making it all come together

Well done, after finishing Part 5 - Mounting the drive(s) you are ready to roll!

But, wait, isn't there a lot more to do? No... not really. You can now visit https://app.plex.tv/web - your brand new Plex server will be there. Bet you didn't know it would be this easy, did you?

Now I'm not going to teach you how to set up Plex, there is ample information on the Plex website for that. What you do need to know though is where to find your content.


So let's add a library and click "Browse for Media Folder".


You will see the services you created on the left hand side of your selection. If you just want to use Rclone, all you need to do is click Plex and add the folder(s) of your choice to your library.


That's all... You can stop sweating and start watching as soon as Plex has finished scanning your content!

Wait, before you go. The following sections are optional... but I found those make my Plex life a lot easier.

Creating a backup


Once the Plex server is installed, all content is scanned, your plugins are running and history is being made what has been watched and what not, the most scary part is losing everything in a crash. Don't think it can't happen... even with my very reliable provider I once experienced a corrupted server instance - and I had to start from scratch again.

Luckily, thanks to my good friend "kelinger" (who provided most of the scripts presented in this tutorial, my eternal gratitude to him!), I now have a backup script that I run once a week to make sure that whenever I need to reinstall Plex, it will be up and running in a matter of hours - even in case I ever decide to switch providers.

Note: for this script it is vital that Rclone is correctly configured (this is covered in part 4 of this tutorial).

Creating the script directory (only if you haven't created it yet):

mkdir ~/bin

The backup script:

nano ~/bin/plex-backup

Paste the following:

#!/bin/bash
sudo service plexmediaserver stop
sudo -s tar -cvjf /tmp/$(hostname).$(date +%F).tar.bz2 /var/lib/plexmediaserver
sudo service plexmediaserver start
sudo rclone copy /tmp/$(hostname).* Gdrive:/Backup -v --checksum --drive-chunk-size=64M
sudo rm /tmp/$(hostname).*

Save with Ctrl-O, Enter, then Ctrl-X.

This will create a dated backup of your Plex server and copy it to a folder named "Backup" in Google. You can edit the part in red to upload to any folder you wish. Make sure you create this folder on your cloud service before the first backup!

Make the script executable:

chmod +x ~/bin/plex-backup

You can run the script whenever you want by typing "plexbackup". Important: your server will be unavailable during the backup process.

If you wish to make this an automatic weekly event, type the following command:

crontab -e

If you are asked which editor you want to use, choose 2 to select the nano editor. Scroll to the bottom and add a new line:

15 1 * * Sun /home/plexuser/bin/plex-backup > /dev/null 2>&1

Change plexuser to your own of course. Save with Ctrl-O, Enter, then Ctrl-X.

In the example above, the backup will run every Sunday morning at 1:15am. If you want to edit the schedule to suit your own, search for "cron job scheduling", there is ample information available on the Internet how to adapt this to your own wishes.

Note: the name of the backup file is based on the Hostname of your server. This is set in the client area of your VPS provider's website.


You can change the name there if you wish. I named mine Plex, but if you gave yours a different name, your backup files will be named accordingly. If you run multiple Plex servers, you will always know which backup belongs to which server.

Restoring a backup


It happened... you lost your server, and now you need to restore the backup.

First of all, follow parts 1 to 5 of this tutorial (and the switch script of part 6, if you are using that). That means you now have a Plex "vanilla" server including your mounted drive(s), just as you had before. There is no need to recreate your libraries or install any plugins, the backup took care of that.

We'll place the restore files in the tmp directory. Advantage is after a reboot, all superfluous files will be gone automatically - which is exactly what you want!

Visit your Google Drive folder and review your backup files. If you have been creating backups regularly, there might be quite a few! Check the exact date of the file you want to restore.


Type

mkdir /tmp/restore

Then

sudo rclone copy Gdrive:/Backup/$(hostname).2017-06-25.tar.bz2 /tmp --checksum --drive-chunk-size=64M -v

If you are using another backup folder in Google Drive, edit the first part in red. The second red part should reflect the exact date of the file you want to restore.

This could take a few minutes, depending on the size of your Plex database and the download speed. Then type

tar -xvf /tmp/$(hostname).2017-06-25.tar.bz2 -C /tmp/restore

Again, make sure to enter the exact name and date of the file. This too will take a little while to finish.

Type the following commands one by one:

sudo service plexmediaserver stop
sudo mv /var/lib/plexmediaserver /var/lib/plexmediaserver-vanilla
sudo mv /tmp/restore/var/lib/plexmediaserver /var/lib/
sudo chown -R plex:plex /var/lib/plexmediaserver

Wait for the prompt to come back, this may take a minute.

sudo service plexmediaserver start

Check if your server is restored and working fine... then you can delete the old files:

sudo rm -r /var/lib/plexmediaserver-vanilla
sudo rm -r /tmp/restore
sudo rm /tmp/$(hostname).*

Your Plex server is now restored to the date you created your backup.

Creating a switch script


Now that Rclone is every bit as good as Plexdrive, the switch script has become a legacy feature. You may still use it of course, but it's no longer necessary. If you are setting up a new server, you can safely skip this section.

What if you can't choose between Rclone and Plexdrive, or if you want to be able to switch seamlessly between the two? With this switch script, your content is always mounted in the same location - and the beauty is you can add other cloud and/or mount services whenever you wish!

You will need to be logged into PuTTY as your user. Creating the script directory (only if you haven't created it yet):

mkdir ~/bin

Then the script itself:

nano ~/bin/switch

Paste the following:

#!/bin/bash
OPTION=${1}
case "${OPTION}" in
 [12])
  echo Stopping services
  sudo service plexmediaserver stop
  sudo rm /home/Plex
  ;;&
 1)
  echo Making Plex use Rclone
  sudo ln -s /media/Plex /home/Plex
  ;;&
 2)
  echo Making Plex use Plexdrive
  sudo ln -s /media/Plexdrive /home/Plex
  ;;&
 [12])
  echo Starting services
  sudo service plexmediaserver start
  ;;&
 [012])
  echo Plex is using $(readlink /home/Plex)
  exit 0
  ;;
esac
echo Usage:
echo " ${0} [0|1|2]"
echo "  0 = Show current Plex drive mapping"
echo "  1 = Switch Plex to use Rclone for streaming"
echo "  2 = Switch Plex to use Plexdrive for streaming"
exit 1

Save with Ctrl-O, Enter, then Ctrl-X. Make the script executable:

chmod +x ~/bin/switch

Now all you have to do is type "switch 1" to switch to Rclone, or "switch 2" to switch to Plexdrive. Typing "switch 0" will show you which drive is currently mounted.

Once this is done and you selected your mounted drive, you will need to point your Plex libraries to the new location.

Instead of picking either Plexdrive or Gdrive, select the "slash forward" / and then click "home":


Click Plex and from there, select your content.




You only need to set all your libraries once... no matter how many changes you make to your cloud services. This script is quite future proof!

Bonus 1: Rclone update script


This is a script to quickly update Rclone to the latest version. It is not vital to install, but it makes it easy to switch between Rclone's release and beta without any hassle.

Creating the script directory (only if you haven't created it yet):

mkdir ~/bin

To create the script:

nano ~/bin/rclone-update

Copy and paste the following:

#!/bin/bash
# Rclone updater
cd /tmp
if [[ ${1} = 'beta' ]]
then
  curl -O https://beta.rclone.org/rclone-beta-latest-linux-amd64.zip > /dev/null 2>&1
  unzip rclone-beta-latest-linux-amd64.zip > /dev/null 2>&1
else
  curl -O https://downloads.rclone.org/rclone-current-linux-amd64.zip > /dev/null 2>&1
  unzip rclone-current-linux-amd64.zip > /dev/null 2>&1
fi
cd rclone-*-linux-amd64
A=$(sudo rclone --version)
B=$(sudo ./rclone --version)
if [[ ${A} = ${B} ]]
then
  echo ${A} is already current
else
  echo Installing new ${B}
  #stop services
  sudo service plexmediaserver stop
  sudo service rclone stop
  sudo service plexdrive stop
  #update rclone
  sudo cp rclone /usr/sbin/
  sudo chown root:root /usr/sbin/rclone
  sudo chmod 755 /usr/sbin/rclone
  #install manpage
  sudo mkdir -p /usr/local/share/man/man1
  sudo cp rclone.1 /usr/local/share/man/man1/
  sudo mandb > /dev/null 2>&1
  #start services
  sudo service rclone start
  sudo service plexdrive start
  sudo service plexmediaserver start
fi
cd /tmp
sudo rm -r rclone-*

If you did not install Plexdrive, remove the lines in red. Save with Ctrl-O, Enter, then Ctrl-X.

Make the script executable:

chmod +x ~/bin/rclone-update

All you need to do is type "rclone-update" to update Rclone to the latest release version, or "rclone-update beta" to update to the latest beta version.

Bonus 2: Plexdrive update script


This is a script to quickly update Plexdrive to the latest release. Due to a limitation of GitHub, it is currently only possible to update to a stable version.
Make sure you do research what changes you need to make to get a new stable version to work... this tutorial is based on version 5.0.0.

Creating the script directory (only if you haven't created it yet):

mkdir ~/bin

To create the script:

nano ~/bin/plexdrive-update

Copy and paste the following:

#!/bin/bash
# Plexdrive updater
cd /tmp
if [[ ${1} = 'beta' ]]
then
  echo This script only supports updating to stable versions right now.
  exit
else
  sudo wget $(curl -s https://api.github.com/repos/dweidenfeld/plexdrive/releases/latest | grep 'browser_' | cut -d\" -f4 | grep plexdrive-linux-amd64) -O plexdrive-linux-amd64  > /dev/null 2>&1
  sudo mv plexdrive-linux-amd64 plexdrive
  sudo chown root:root /tmp/plexdrive
  sudo chmod 755 /tmp/plexdrive
fi
cd /usr/local/bin
A=$(plexdrive --version)
B=$(/tmp/plexdrive --version)
if [[ ${A} = ${B} ]]
then
  echo ${A} is already current
else
  echo Installing new ${B}
  #stop services
  sudo service plexmediaserver stop
  sudo service rclone stop
  sudo service plexdrive stop
  #install plexdrive
  sudo mv /tmp/plexdrive /usr/local/bin/
  sudo chown root:root /usr/local/bin/plexdrive
  sudo chmod 755 /usr/local/bin/plexdrive
  #start services
  sudo service rclone start
  sudo service plexdrive start
  sudo service plexmediaserver start
fi
cd /tmp
sudo rm plexdrive 2> /dev/null

If you did not install Rclone, remove the lines in red. Save with Ctrl-O, Enter, then Ctrl-X.

Make the script executable:

chmod +x ~/bin/plexdrive-update

All you need to do is type "plexdrive-update" to update Plexdrive to the latest release version.

That's really all

Hope you enjoyed my tutorial on how to set up your own Plex server on a VPS with unlimited cloud storage to host your content. Don't hesitate to hit the comments, I'd love to hear if this helped you set up your own!

Comments