automated plex backup to NAS

this is an automated backup approach to save the plex config and metadata away to a NAS every day at 4:30
also will rsync the media directories to the NAS
(cp for the config because.. because.. of this guy!)

edit sudoers file:
(plex user / your user should be member of adm group)
allow the start and stop of plexmediaserver and to execute the backup script
(there are no permissions on the NAS so let’s ignore that for now)

# allow adm members to backup scripts for plex
%adm ALL=(ALL) NOPASSWD: /sbin/start plexmediaserver, /sbin/stop plexmediaserver, /bin/cp -av /var/lib/plexmediaserver /mnt/nas/backups/plex

#includedir /etc/sudoers.d
%andreas ALL=NOPASSWD: /home/andreas/backup-plex.sh

now add the script:

motivation: rsync with delete – whatever I delete from the source can be deleted on the backup, too
full sync for the server directory, only check by size for the media files
I like -v and “–progress” as it gives me an indication what is going on (on the first run…)
however not in the scripts, a simple –stats will have to do, there…

so for the server backup:
rsync -ahvz /var/lib/plexmediaserver/ /mnt/nas/backups/plexmediaserver/ –progress –delete –dry-run –stats
non-verbose:
rsync -az /var/lib/plexmediaserver/ /mnt/nas/backups/plexmediaserver/ –delete

and for files:
rsync -ahv /plex/anime/ /mnt/nas/plex/anime –progress –size-only –delete –stats –dry-run
and non-verbose and loaded:
rsync -a /plex/anime/ /mnt/nas/plex/anime –size-only –delete

first version of the script used copy but this took AGES to finish so rsync all the way now. After all it seems my seagate NAS does rsync :D


#!/bin/bash
echo "+++stopping plex media server"
systemctl stop plexmediaserver.service
sleep 5
echo "+++backing up server and cache"
rsync -ahz /var/lib/plexmediaserver/ /mnt/NAS/backups/plexmediaserver/ --stats --delete
sleep 20
echo "+++backing up anime"
rsync -ah /plex/anime /mnt/NAS/plex/anime --size-only --stats --delete
sleep 10
echo "+++backing up anime series"
rsync -ah /plex/anime_series/ /mnt/NAS/plex/anime_series --size-only --stats --delete
sleep 10
echo "+++backing up german movies"
rsync -ah /plex/german/ /mnt/NAS/plex/german --size-only --stats --delete
sleep 10
echo "+++backing up home movies"
rsync -ah /plex/home_movies/ /mnt/NAS/plex/home_movies --size-only --stats --delete
sleep 10
echo "+++backing up kids movies"
rsync -ah /plex/kids/ /mnt/NAS/plex/kids --size-only --stats --delete
sleep 10
echo "+++backing up movies"
rsync -ah /plex/movies/ /mnt/NAS/plex/movies --size-only --stats --delete
sleep 10
echo "+++backing up non fiction"
rsync -ah /plex/nonfiction/ /mnt/NAS/plex/nonfiction --size-only --stats --delete
sleep 10
echo "+++backing up tv shows"
rsync -ah /plex/tv/ /mnt/NAS/plex/tv --size-only --stats --delete
sleep 10
echo "+++backing up german tv"
rsync -ah /plex/tv_german/ /mnt/NAS/plex/tv_german --size-only --stats --delete
sleep 10
echo "+++backing up Disney Shorts"
rsync -ah /plex/Disney\ Shorts/ /mnt/NAS/plex/Disney\ Shorts/ --size-only --stats --delete
sleep 20
echo "+++backing up music (NAS to plex)"
rsync -ah /mnt/NAS/music/tagged/ /plex/music/ --size-only --stats --delete --chown=plex:plex
sleep 10
echo "+++restarting plex media server"
systemctl start plexmediaserver.service
echo "+++backup complete"

see: https://support.plex.tv/hc/en-us/articles/201539237-Backing-Up-Plex-Media-Server-Data
and https://support.plex.tv/hc/en-us/articles/201370363-Move-an-Install-to-Another-System

make it executable via chmod +x
and schedule using crontab or gnome-scheduler or whatnot – every night at 4 a.m.

0 4 * * * cd /home/andreas && sh backup_plex.sh>>plex_backup.log | mail -s "check the backup of plex" andreas@rudel.nl

but I also want to move my music from the NAS to the plex machine so let’s reverse the process:
here’s a twist: plex would like the ownership of the files to be plex:plex and NFS still is a little “magic” to me so let’s just chown it afterwards:

rsync -ah /mnt/NAS/music/tagged/ /plex/music/ –stats –size-only –delete –chown=plex:plex

once that’s done, just add it to the backup script and bam, music comes from NAS to plex and will be tagged (hopefully)