Shell Script help: Making a plex backup

With the migration from Dragonfish to ElectricEel-24.10.0.2 and the Docker containers, I thought I would try again in setting up a backup script to backup my Plex data to a directory on my TrueNAS server. (I already have it configured to have my My configuration is as follows:

  • Plex Configuration Storage: /mnt/pool/app-config/plex
  • Backup Directory: /mnt/pool/backup/Plex

I determined the docker name by going into the TrueNAS shell and executing the command docker stats. The name I found is ix-plex-plex-1.

The script I’m using is as follows:

cd /mnt/pool/app-config/plex
docker stop ix-plex-plex-1

tar --exclude=Library/Application\ Support/Plex\ Media\ Server/Cache --exclude=Library/Application\ Support/Plex\ Media\ Server/Logs --exclude=Library/Application\ Support/Plex\ Media\ Server/Plug-in\ Support/Caches -zcvf /mnt/pool/backup/Plex/Plexbackup.tar.gz .

docker start ix-plex-plex-1

When I’m logged in as root and enter these commands manually, I am able to stop my Plex instance, create a tar file (it just takes 30 minutes) , then restart my plex instance. But, when I put those same commands into a shell script and attempt to run it:

root@vaultron[/mnt/pool/plex]# ./backup.sh
: not found: 3:
./backup.sh: 4: cd: can't cd to /mnt/pool/app-config/plex
Error response from daemon: No such container: ix-plex-plex-1
: not found: 6:
tar: .\r: Cannot stat: No such file or directory
tar: Exiting with failure status due to previous errors
: not found: 8:
ix-plex-plex-1
root@vaultron[/mnt/pool/plex]#

Yes, I have tried the #! /bin/bash and #! /bin/sh as the first line, but those are thowing errors as well. I may need to change some directory permissions, but still… what is this shell script novice doing wrong?

Don’t worry, those of us who write scripts have this problem too.

Let me know if this fixes it for you: cd /mnt/pool/plex && ./backup.sh

root@vaultron[/mnt/pool/plex]# cd /mnt/pool/plex && ./backup.sh
: not found: 3:
./backup.sh: 4: cd: can't cd to /mnt/pool/app-config/plex
Error response from daemon: No such container: ix-plex-plex-1
: not found: 6:
tar: .\r: Cannot stat: No such file or directory
tar: Exiting with failure status due to previous errors
: not found: 8:
ix-plex-plex-1
root@vaultron[/mnt/pool/plex]#

What is the exact script? PM me if you need to.

I created a write up for this here: Plexdata backup method

Also, always code in #!/bin/sh if you can (if you don’t know the difference between sh and bash you can treat the word ‘always’ a law).

1 Like

I added the #!/bin/sh as requested as well as checked the permission of the directories. This is what I’m getting now:

root@vaultron[/mnt/pool/plex]# cat backup.sh
#!/bin/sh

cd /mnt/pool/app-config/plex
docker stop ix-plex-plex-1

tar --exclude=Library/Application\ Support/Plex\ Media\ Server/Cache --exclude=Library/Application\ Support/Plex\ Media\ Server/Logs --exclude=Library/Application\ Support/Plex\ Media\ Server/Plug-in\ Support/Caches -zcvf /mnt/pool/backup/Plex/Plexbackup.tar.gz .

docker start ix-plex-plex-1

root@vaultron[/mnt/pool/plex]# ./backup.sh
zsh: ./backup.sh: bad interpreter: /bin/sh^M: no such file or directory
root@vaultron[/mnt/pool/plex]#

You have a carriage return character after sh instead of a line feed character that Linux requires.

Those characters are typically not visible in text editors, remove it by placing the blinking cursor right after the “h” in #!/bin/sh and press DEL/DELETE to delete the (invisible) carriage return character to the right. Then press ENTER to create the correct line break to replace it.

Save the file and try again.

Oh great googley moogely… and I was editing the file using Notepad++ too… sigh… Thank you for your assistance @neofusion .

It’s working now. Now to set up a cron job.

2 Likes

So who owns the tar and the information within? Did you read my write up?

I’ll let you know when the cron job runs tomorrow morning. When I did my testing, the owner was root, and I’m able to access the tar archive.

I’d recommend making a test. Meaning: restore with it. …I made that write up for a reason.

Yes, I did. These were the same commands that I used months ago when I migrated from TrueNAS CORE to TrueNAS SCALE. One of the bonuses of this upgrade was being able to use Sonic Analysis in Plex which wasn’t available in CORE because it was running FreeBSD.

Yes, I agree that config and data should be in separate directories. One of my mistakes when I migrated from TrueNAS CORE to TrueNAS SCALE was that I had set the Plex Configuration Storage and Plex Data Storage in the app to the same directory of /mnt/pool/plex . I blame it on the rush on getting my Plex instance back online and the newbieness of using Apps in SCALE. After installing multiple Apps since then, I gained experience, and finally sat down and migrated the Plex Configuration Storage over to /mnt/pool/app-config/plex. That’s when I figured I should tackle the backups once again.

The backup completed successfully although it took 58 minutes, and all of the files are there minus the Logs, Cache, and Plug-in Support/Caches as expected. When I extract the tar file using the options -vxf, I’m seeing the following with all of the files:

tar: ./Library/Application Support/Plex Media Server/Plug-in Support/Databases/tv.plex.providers.epg.cloud-d451573b-b195-4387-8a22-2877417cb388.db-shm: Cannot change mode to rwxrw-r--: Operation not permitted

The permissions I’m seeing are 777, go figure. The test on another Plex instance will have to wait until another weekend.

This is why I created the post I did. Being able to extract the data back into plex, in case of emergency, is what you’re after.

I don’t know why seeing 777 permissions.

Here is the latest iteration of my backup script. Other than taking 52 minutes to run, it is working out great. I’ve implemented the date function so that I have two backups locally, and the cron job runs at 6 AM locally on Sunday mornings.

#!/bin/sh

now="plexbackup-$(date +%F).tar.gz"
old="plexbackup-$(date +%F --date='2 weeks ago').tar.gz"

# Plex backup
cd /mnt/pool/app-config/plex

# Turn off Plex instance
docker stop ix-plex-plex-1

# Archive the files
tar --exclude=Library/Application\ Support/Plex\ Media\ Server/Cache --exclude=Library/Application\ Support/Plex\ Media\ Server/Logs --exclude=Library/Application\ Support/Plex\ Media\ Server/Plug-in\ Support/Caches -zcvf /mnt/pool/backup/Plex/$now .

# Turn on Plex instance
docker start ix-plex-plex-1

# Remove backup
rm /mnt/pool/backup/Plex/$old

Of course, a typo prevented the oldest backup from being deleted. :frowning: Fixed.

I did the test extraction to /mnt/pool/app-config/plex-test and temporarily pointed my Plex to that directory. After about a minute, Plex came right up, although some images had to be rebuilt. This was expected… the Cache was intentionally backed up. Playback worked. I then pointed Plex back to /mnt/pool/app-config/plex which is my production. Everything works in terms of backup, plus I have the script rotate my backups so that I have this week and last week.