PMS Database backup to external location - Ubuntu

Server Version#: 1.24.4.5081
Hi all,
I have a HP Proliant N54 running PMS Ubuntu 20.04.2 LTS.
I believe that Plex does backups internally every three days. What I want to do is to set up backup to a remote NAS. Seems pointless doing backups to the same disk, if the disk dies the backup is gone with it.
Is there a script of some sort to do this? I don’t think I can just add the location in to the ‘Backup Directory’ under Scheduled Tasks. I have tried that and I don’t see any backup files appearing in the remote directory.

Please note that I am only looking to back up the Plex database and settings, the media is stored somewhere else totally different.

I’m a Linux newbie so please be gentle.

Thanks in advance.

There is a very easy way to do this which doesn’t involve a lot of hackery.

  1. We setup a script to run at whatever schedule you want.
  2. In that script
    a. Stop Plex
    b. Make the full backup of everything (includes any customization of posters, etc)
    c. Start Plex

In that script, you can designate another drive or even on your NAS if you wish; choice is yours.

Thanks
I guess I was hoping for a little more detail to get me going i.e. sample script.
Any chance of that?

Oh sure, I can write you the whole procedure… But what would you learn? :smiling_imp:

LOL

Key points.

  1. Run the script using crontab (as root)
  2. In the script (customize/finish as needed):
#!/bin/bash

# Note:  This script assumes running as root in order to make the backup


# Mount NAS folders here if needed (fill in the details)
# mount  host:/sharename  /mountpoint

# Stop Plex
systemctl stop plexmediaserver



# Perform backup (set the output path from '/nas/backups' to whatever you need)
cd /var/lib/plexmediaserver
tar cf "/nas/backups/PlexBackup-$(date +'%F').tar" ./Library

# All done, start plex
systemctl start plexmediaserver

Read up on crontab.
you’ll want sudo crontab to edit the root user’s crontab jobs file.

The output format for the files will be: PlexBackup-YYYY-MM-DD.tar

Will that work?

Note: This is verification of what date +'%F' will return

[chuck@lizum ~.2013]$ date +'%F'
2022-04-09

Excellent! That is exactly what I was hoping for. Something to get me started.
Thanks

There are a lot of little tricks there, like $(command) . (put any command sequence inside the () )

Play with it.

echo   Hi,  Today is $(date)

Now add

echo  Hi, Today is $(date '%F')

Thanks, I think I have some experimenting to do.

Hi,
I have had a go at this and of course, I am looking for some more advice.

Here is what I did for a script:
leif@HP-ProLiant-MicroServer:~$ cat pmsbackup-external
#!/bin/bash

systemctl stop plexmediaserver

cd /var/lib/plexmediaserver
tar cf 192.168.1.5:/Multimedia/Backup_files-$(date +’%F’).tar .Library

systemctl start plexmediaserver

Problem No 1:
Here is what happen when try and run that script:
leif@HP-ProLiant-MicroServer:~$ pmsbackup-external
pmsbackup-external: command not found

I have to use sh to get the script to run. I thought that having the #!/bin/bash in the script would fix that?
Problem No 2: How can I get around the problem with ‘Authentication is required’ if I want to automate this?

Problem No 3: I have created a user ‘leif’ in the NAS but the authentication fails saying ‘Permission Denied’. I have no idea why it says that…I know that this is not Plex related so I am looking for good ideas why this can be? I know that the user is valid, I can log in to the NAS via the web page no problems and that use has Read / Write access to the backup folder.

Thanks heaps in advance…

Leif

leif@HP-ProLiant-MicroServer:~$ sh pmsbackup-external
==== AUTHENTICATING FOR org.freedesktop.systemd1.manage-units ===
Authentication is required to stop ‘plexmediaserver.service’.
Multiple identities can be used for authentication:

  1. leif, (leif)
  2. L Lundberg, (llundberg)
    Choose identity to authenticate as (1-2): 1
    Password:
    ==== AUTHENTICATION COMPLETE ===
    leif@192.168.1.5’s password:
    Permission denied, please try again.
    leif@192.168.1.5’s password:
    Permission denied, please try again.
    leif@192.168.1.5’s password:

You can’t specify a UNC NFS path like that. Tar doesn’t know what to do with it.

Consider:

mount 192.168.1.5:/Multimedia  /mnt
tar cf /mnt/Backup_files-$(date +’%F’).tar .Library
umount /mnt
  1. Mount the NFS shared folder /Multimedia at the /mnt directory (assuming it’s not used)

  2. Write the tar backup file

  3. Unmount /mnt now that we’re done with it.

===

The ‘command not found’ is because the file isn’t in your PATH string.

The most common solution is to give the full path (e.g.)

/home/chuck/bin/pmsbackup-external

-or- (cheating with fancy stuff)

~/bin/pmsbackup-external

(the ~ character is a shell shortcut which expands to the path of your home directory. For me, that’s /home/chuck. )

study up on adding directories to your PATH variable. It’ll save you typing in the future as you start collecting scripts in ~/bin :wink:

For reference, here’s the most common stuff in my ~/bin that I’ve written for daily use.

[chuck@lizum ~.2001]$ ls ~/bin
avi2mkv*          diff            MakeMovies*         No-parens*    rar*            SetEng-mkv*              start-pms-syno.zip
backup-wien*      dnewpgen*       make-mp3*           Paulget*      rar.d/          setfiles*                swipl@
can-read*         do-shn*         make-sata*          perror*       rar_static*     set-transcoder-debug*    swipl-ld@
CheckSize*        FindSSDP.py*    make-series*        pfgw*         remount*        set-transcoder-error*    swipl-win@
CheckSize-slave*  get-audio*      make-series-binge*  pfgwdoc.txt*  RemoveYears*    set-transcoder-verbose*  unrar*
chuck@glock*      get-bitrate*    make-shn*           PlexDBscan*   rtrim*          sieveit*                 USB-maker.bin*
clean-episode*    get-hdd-temps*  make-shn~*          pow*          rtrim.c         snewpgen*                vnc*
clear-cache*      jfrog*          make-specials*      pow.c         rtrim.o         snewpgenlinux.zip*       where*
cloud-ui@         make-ape*       MediaCodecs         pow.o         run-llr*        speedtest.5              x/
convert-vdi*      make-episode*   move-if-bigger      primo@        run-llr-queue*  speedtest.md
CrossVerify*      make-german*    movie*              pyenv@        setdirs*        speedtest-x*
des*              make-movie*     NewPGen.ini*        pyenv.d/      seteng-mkv*     start-pms-syno*
[chuck@lizum ~.2002]$ 

Thank you so much! This GREAT feedback. I think I may have mounted that folder already now that I think about, most likely when I mounted all the media folders.
Another question; I read somewhere what need to be backed up - /var/lib/plexmediaserver/Library/Application Support/Plex Media Server/Plug-in Support/Databases.
Is that not correct? I added the folder that you said which I guess adds a few more sub-folders but otherwise the same.

Thanks again, I will let you know how I go.

Backing up the databases and Preferences.xml (file) is the bare minimum.

I always recommend backing up all of “Library” because:

  1. It’s easier to restore
  2. It’s a full image backup – INCLUDING – any customizations you might make to posters or subtitles or anything else PMS supports.

Two problems (I think).

Problem 1:
my file is in /home/leif.
Here is my PATH:
leif@HP-ProLiant-MicroServer:~$ echo $PATH
/home/leif/bin:/home/leif/.local/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/u sr/bin:/sbin:/bin:/usr/games:/usr/local/games:/snap/bin

Am I missing something?

Problem 2:
The first step looks like it needs manual intervention, please see below. Can I get around that?
Authentication is required to stop ‘plexmediaserver.service’.
Multiple identities can be used for authentication:

  1. leif, (leif)
  2. L Lundberg, (llundberg)
    Choose identity to authenticate as (1-2): 1
    Password:

Thanks again.

Hi me again,
I am still working on this script and I have tried to just back up locally first to try and resolve issues with access to the other location.
I try this:
leif@HP-ProLiant-MicroServer:~$ sudo systemctl stop plexmediaserver
[sudo] password for leif:
leif@HP-ProLiant-MicroServer:~$ tar cf Downloads/plexbu.tar /var/lib/plexmediaserver

And I get this (plus many more ‘Permission denied’, I just copied the first to demonstrate the problem). Why do I get the Permission denied?
Please help, I have nowhere else to turn.

tar: Removing leading `/’ from member names
tar: /var/lib/plexmediaserver/Library.old/Application Support/Plex Media Server/Preferences.xml: Cannot open: Permission denied
tar: /var/lib/plexmediaserver/Library.old/Application Support/Plex Media Server/Cache/CloudAccount.dat: Cannot open: Permission denied
tar: /var/lib/plexmediaserver/Library.old/Application Support/Plex Media Server/Cache/certificate.p12: Cannot open: Permission denied
tar: /var/lib/plexmediaserver/Library.old/Application Support/Plex Media Server/Cache/ca.crt: Cannot open: Permission denied
tar: /var/lib/plexmediaserver/Library.old/Application Support/Plex Media Server/Cache/CloudAccess.dat: Cannot open: Permission denied
tar: /var/lib/plexmediaserver/Library.old/Application Support/Plex Media Server/Cache/PhotoTranscoder/3c/3c1886943a2e6c6d436cbb57382dbf5d328960e2.jpg: Cannot open: Permission denied
tar: /var/lib/plexmediaserver/Library.old/Application Support/Plex Media Server/Cache/PhotoTranscoder/80/808129cfc4af9799f73d3bdd55b3d59ef7192d7b.jpg: Cannot open: Permission denied
tar: /var/lib/plexmediaserver/Library.old/Application Support/Plex Media Server/Cache/PhotoTranscoder/d2/d20a8c1601a1a8facc7b25cb2f3735827a5ba0cc.png: Cannot open: Permission denied
tar: /var/lib/plexmediaserver/Library.old/Application Support/Plex Media Server/Cache/PhotoTranscoder/dd/dd48254469847a37e81af334403590fb6b815270.jpg: Cannot open: Permission denied
tar: /var/lib/plexmediaserver/Library.old/Application Support/Plex Media Server/Cache/PhotoTranscoder/c8/c8901481ac00cfedfc4a23133e930a37c2df8459.jpg: Cannot open: Permission denied
tar: /var/lib/plexmediaserver/Library.old/Application Support/Plex Media Server/Cache/PhotoTranscoder/26/266c02aabb996068fced7a16c03509b1290d881b.jpg: Cannot open: Permission denied
tar: /var/lib/plexmediaserver/Library.old/Application Support/Plex Media Server/Cache/PhotoTranscoder/a1/a145abbb1d17c835232a38a0929328259b7df774.png: Cannot open: Permission denied
tar: /var/lib/plexmediaserver/Library.old/Application Support/Plex Media Server/Cache/PhotoTranscoder/7f/7fa8d8cfcfc858ba2f0ce807316a5ce0e4aac14a.jpg: Cannot open: Permission denied
tar: /var/lib/plexmediaserver/Library.old/Application Support/Plex Media Server/Cache/PhotoTranscoder/f9/f97ac7d1835a4f3a37b19ab76cfeea8e4e4fe733.jpg: Cannot open: Permission denied
tar: /var/lib/plexmediaserver/Library.old/Application Support/Plex Media Server/Cache/PhotoTranscoder/ab/ab96e8964b94322a84d0f7fd2490bb16259396ac.png: Cannot open: Permission denied
tar: /var/lib/plexmediaserver/Library.old/Application Support/Plex Media Server/Plug-in Support/Metadata Combination/com.plexapp.agents.lastfm/Artists.xml: Cannot open: Permission denied
tar: /var/lib/plexmediaserver/Library.old/Application Support/Plex Media Server/Codecs/.device-id: Cannot open: Permission denied
tar: /var/lib/plexmediaserver/.ssh: Cannot open: Permission denied

Probably because PMS was installed with “plex:plex” as the user/group permissions.

If this is the case and you are currently not logged in as “plex” user you can do this:

  1. set password for the plex system account:
    sudo -u root passwd plex

  2. run the script as user plex:
    sudo -u plex ./scriptname.sh

OR

  1. sudo -u root passwd plex
  2. su - plex
  3. ./scriptname.sh

OR

  1. you can add the plex group to your current logged in user:
    sudo -u root usermod -a -G plex YOURCURRENTUSERNAME

  2. then as your current logged in user you can run the script.

IF I may recommend:

cd /var/lib/plexmediaserver
sudo bash
systemctl stop plexmediasever
tar cf /path/to/backup.tar  ./Library

As ROOT, you do not need to alter any system configuration (user plex).

Modifying accounts to do the above is not recommended.

btw. Will Plex ever allow Backup of all settings and data internally? Ie. to Google drive, etc customers own server. thumbs, settings, and other stuff is not that big data to backup.
BUT you could make is so that you could backup ALL with checkboxes… just choose what to backup and tell customer that its xxTB of data… =)

@jhalttu

Backing up all of PMS and its data , while it’s running, is very unlikely

I say this because:

  1. Backing up a live database, where others could be streaming simultaneously, wouldn’t be a full backup.
  2. There are so many different ways and places to backup externally, programming all of them into PMS would be a big task.
  3. IF PMS did have the most common backup methods available , we come back to the problem of how to backup a live & operational database.

Bottom line, PMS wasn’t designed to back itself up and doing so would be a big task.
On some of the platforms, it’s not possible to have it backup itself because of how those OS’s work.

It would be nice but unfortunately it’s not likely to happen because of the amount of work required for such a minimal return.

Thanks Chuck, that worked. Much appreciated. One step closer. I hope it is OK for me asking for advice like this.

@LLundberg

That’s what we’re here for. I’m glad I could help.

Hi me again, I only seem to get time to work on this occasionally, this is why there are time gaps between my posts.
I have created a backup folder on the backup server and tried to mount this:

leif@HP-ProLiant-MicroServer:~$ sudo mount 192.168.1.122:/Backup/plex_backups
mount.nfs: access denied by server while mounting 192.168.1.122:/Backup/plex_backups

Any clues why the failure?

I am so grateful for your help here. If it wasn’t for this forum I would be no where with this.