NextPVR Forums

Full Version: Remote NextPVR backup script for Linux
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
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.
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
nice. The script even does it's own housekeeping!

At the risk of sounding pedantic, if it were my script, I'd check if scp succeeds before removing the local backup file in case the destination mount point was unavailable or there is a problem with permissions.

Also, I believe that this bit of code is superfluous as the first line (echo "root version of script" >> /home/npvr/backup_script.log) will ensure that the log file always exists.

if [ ! -f $f ]
then
echo "$f does not exist!" >> /home/npvr/backup_script.log
exit
fi
touch ${f}

It's also important to have at least one of these backups stored in the cloud somewhere (to complete the 3-2-1 backup rule).

thanks for sharing.
Thanks for the ideas. It's V1.0 so it can always use some tweaks for sure.