Plexdata backup method

Last night I made a plexdata backup method and I thought I’d post it. And I know this post may seem simplistic on the surface but this post isn’t really about how I do plexdata backups, it’s more about problem solving.

My TrueNAS has datasets for “media” and “data”. These datasets are mounted inside my plex jail on my server via NFS.

For the case of this post my paths are:

* Data dataset (NFS mount):     `/mnt/Media/Data/plexdata`
* 'plexdata' location:          `/usr/local/plexdata`

The natural inclination is to mount the plexdata directory directly into plex but I choose to keep my data backup separate for a few reasons. One being the logs, Cache, and backup databases. These misc files can take up quite a bit of–redundant/unnecessary–room so my main issue is copying the plexdata back and forth without including any of that extra bloat. The other issue is backups; on my TrueNAS system I have automatic snapshots so my data is backed up essentially twice. The last issue is ownerships; plex wants to own the data it consumes and making data owned by root or ricky would require jumping through hoops to get data back and forth. This last issue (ownership) can be solved by having two different datasets for plex and emby data–or whatever scenario you actually have-) but this introduces unnecessary complexity into the problem.

My current data dataset is “Data” with different directories owned by respective users

  Data
    |
    +--- plexdata
    |
    +--- embydata

Backup method

  1. I don’t actually want to have to do anything so I automate this with the use of a cron job to run my backups once a week (-e.g. sunday at 11pm).

  2. Since I’ve removed “directory permissions” from the problem (the data dataset is mounted into several jails and they just write to their respective folders) the only issues remaining are minor and is basically how to actually preform the backup. -i.e. The user plex owns the data and you cannot backup the plexdata directory when plex is running so we need a way to have root stop plex, have plex make the backup, and have root restart plex.

Below is my simple script I use. I say simple because we’d classify this as a “one-liner” but it preforms several operations at once and I’ll un-pack the command.

Backup script:

  #!/bin/sh
  #: backp_plexdata
  #
  # This script should be launched from CRON.
  #
  # EXAMPLE
  # ----   /etc/crontab   ---->%
  #  #
  #  # Backup plexdata directory every Sunday at 11pm.
  #  23 0 * * 1 root service plexmediaserver onestop && /backup_plexdata && service plexmediaserver start
  #
  # ----   /etc/crontab   ---->%
  
  su -m plex -c '( cd "/usr/local/plexdata" ; tar --extract='Cache' --exclude='tmp' \
                   --exclude='~' --exclude='.DS_Store' --exclude='._' --exclude='.[1-9].db' \
                   --exclude='.[1-9].log' --acls -cf - . | (cd "/mnt/Media" ; tar -xpf -) \
                   )'

Shallow-end explanation:

The cron job in the script comments is almost self explanatory so I won’t delve into that other than: As root, stop plex, run backup script, start plex.

The backup script needs unpacking a bit. Most *nix tools read from STDIN/STDOUT -i.e. they write/read output/input to the shell. That means we can use what are called “pipes” to string these commands together so the output from one command is the input to another (this is the main reason why *nix is a very powerful tool). So in this case I’m using tar to pack up the data and pipe that back to another tar to unpack it. That sounds confusing so let me explain one minor point; the other trick *nix has is sub shells. I’m using a pipe to string these instances of output/input for tar together but through the use of a sub shell. So, one shell with tar is located in one directory and sending that information to another shell with it’s tar at a different location. So, the permissions issue I mentioned in #2 above is to make root run the cron job and when it gets to the actual backup step, it drops to another user called ‘plex’ to tar the files.

With this method, I have also introduced a “feature” related to the #1 item above–about not wanting to do anything manual-. With the plex jail actually preforming the backup of it’s data if that jail were to ever be down, not running, destroyed my TrueNAS system (which just holds and distributes my data) doesn’t have any “dead-end jobs”. -i.e. If I were to put the backup task on my NAS system, if my plex jail was ever destroyed I would need to remember to stop those backups.

Final words:

  1. The backup_plexdata can easily be turned into a generic “copyto” script with a few variables.

  2. I wrote the above in FreeBSD, and I deleted my Linux test jail so you may have to check/fix the syntax slightly.