2024-02-05, 07:04 PM
(This post was last modified: 2024-02-06, 09:53 PM by snagglewest.)
Figured I'd share this script in case someone might find it useful. I recently lost my NextPVR setup. I'll spare the gory details but after spending an evening rebuilding NextPVR I swore I'd never go thru that pain again.
The script creates a zip of all the files needed to restore NextPVR to a new system and copies the file to a NAS mount. I'm not a coder but it works, YMMV. You'll need to change any paths to fit your use case and run daily from a cron job to fully automate it.
The script creates a zip file containing copies of the /var/opt/nextpvr/ directory to include the npvr.db3, user defined channel icons, user defined scripts, config.xml and any .xml files such as extra files. It also backs up any .m3u in /home/npvr/ directory. Once the zip is created locally it's moved a mount point on my NAS. (/backups/NextPVR/ in this case)
In short the script does 6 steps:
# 1. Creates local zip named "YYYY-MM-DD_NPVR_backup"
# 2. Copies the local zip to the NAS mount
# 3. Deletes the local copy of the zip
# 4. Deletes any backups older than 30 days
# 5. Checks the log file and rotates it if needed
# 6. Deletes any logs older than 30 days
The idea is to completely automate it and not require any monitoring.
The script creates a zip of all the files needed to restore NextPVR to a new system and copies the file to a NAS mount. I'm not a coder but it works, YMMV. You'll need to change any paths to fit your use case and run daily from a cron job to fully automate it.
The script creates a zip file containing copies of the /var/opt/nextpvr/ directory to include the npvr.db3, user defined channel icons, user defined scripts, config.xml and any .xml files such as extra files. It also backs up any .m3u in /home/npvr/ directory. Once the zip is created locally it's moved a mount point on my NAS. (/backups/NextPVR/ in this case)
In short the script does 6 steps:
# 1. Creates local zip named "YYYY-MM-DD_NPVR_backup"
# 2. Copies the local zip to the NAS mount
# 3. Deletes the local copy of the zip
# 4. Deletes any backups older than 30 days
# 5. Checks the log file and rotates it if needed
# 6. Deletes any logs older than 30 days
The idea is to completely automate it and not require any monitoring.
Code:
#! /usr/bin/bash
# script to zip the data files for NextPVR and move it to a share on a NAS
# the script is designed to require minimal monitoring and performs 6 steps:
# 1. Creates local zip named "YYYY-MM-DD_NPVR_backup"
# 2. Copies the local zip to the NAS mount
# 3. Deletes the local copy of the zip
# 4. Deletes any backups older than 30 days
# 5. Checks the log file and rotates it if needed
# 6. Deletes any logs older than 30 days
# "npvr" is the system user in this case
# Create a mount on the NAS - this is mapped to "/mnt/backups/" on the local machine
# run a a a cron job to automate it
echo "root version of script" >> /home/npvr/backup_script.log
echo "backup_script cronjob started $(date +%FT%T)" >> /home/npvr/backup_script.log
cd /
zip -v -r "$(date +"%Y-%m-%d")_NPVR_backup.zip" /var/opt/nextpvr/media/channels 2>&1 >> /home/npvr/backup_script.log
zip -v -u "$(date +"%Y-%m-%d")_NPVR_backup.zip" /home/npvr/*.m3u 2>&1 >> /home/npvr/backup_script.log
zip -v -u "$(date +"%Y-%m-%d")_NPVR_backup.zip" /var/opt/nextpvr/npvr.db3 2>&1 >> /home/npvr/backup_script.log
zip -v -u "$(date +"%Y-%m-%d")_NPVR_backup.zip" /var/opt/nextpvr/*.xml 2>&1 >> /home/npvr/backup_script.log
zip -v -u "$(date +"%Y-%m-%d")_NPVR_backup.zip" /var/opt/nextpvr/scripts/*.sh 2>&1 >> /home/npvr/backup_script.log
# copy the local zip file to the NAS mount location
scp "$(date +"%Y-%m-%d")_NPVR_backup.zip" /mnt/backups 2>&1 >> /home/npvr/backup_script.log
# remove local zip file
rm "$(date +"%Y-%m-%d")_NPVR_backup.zip" 2>&1 >> /home/npvr/backup_script.log
# delete-backups older than 30 days
find /mnt/backups/ -type f -name "??-??-????_NPVR_backup.zip" -mtime +30 -exec rm -rf {} \;
echo "backup_script cronjob completed$ $(date +%FT%T)" >> /home/npvr/backup_script.log
echo "=====================================================" >> /home/npvr/backup_script.log
# scripting to rotate logs
echo "=== starting log check and rotate ====" >> /home/npvr/backup_script.log
f="/home/npvr/backup_script.log"
touch ${f}
MAXSIZE=$((4096*256))
size=`du -b ${f} | tr -s '\t' ' ' | cut -d' ' -f1`
if [ ${size} -gt ${MAXSIZE} ]
then
echo "Rotating!" >> /home/npvr/backup_script.log
timestamp=`date +%s`
mv ${f} ${f}.$timestamp
touch ${f}
fi
# delete logs older than 30 days
find /home/npvr/ -type f -name "backup_script.log.*" -mtime +30 -exec rm -rf {} \;
echo "log rotate script complete" >> /home/npvr/backup_script.log
echo "=====================================================" >> /home/npvr/backup_script.log
NextPVR V6.1.5.231022 - Ubuntu 22.04 VM / 4 core / 8Gb memory
HDHR Prime X2 / HDFX-2 /Schedule Direct / 2X Pi4 + & 3X Pi3 LibreELEC Kodi clients
Server - TrueNAS/ SuperMicro MBD-X10SL7-F MB / Xeon E3-1246 / 32Gb Unbuffered ECC / 8 X 4TB RAIDZ2
HDHR Prime X2 / HDFX-2 /Schedule Direct / 2X Pi4 + & 3X Pi3 LibreELEC Kodi clients
Server - TrueNAS/ SuperMicro MBD-X10SL7-F MB / Xeon E3-1246 / 32Gb Unbuffered ECC / 8 X 4TB RAIDZ2