OP
Sam Widges
Active Member
OK, here's the latest version of progbackup.sh in all its gory glory
Changes since the previous version...
1) A bit more error checking, for instance warning that you really don't want to think about running the script if the box is going into standby in a while
2) Some error checking still missing - I've had the webserver go away when testing but that isn't checked for
3) The sidecar files are now copied across and are amended to reflect the new location and the lack of encryption
4) Despite (3), the series file is not being copied over - that might be useful if the files are restored to a Humax hard drive, but they're not copied for the moment.
It should be totally safe to run as it doesn't suggest overwriting the original files, but if you run this and your gerbil explodes, blame the gods of shell scripting.
Any thoughts/comments and suggestions are appreciated as always.
Code:
#!/bin/sh
echo "Humax HDR-T2 backup utility"
echo
echo "Decrypts and backs up your videos to a local drive of your choosing."
echo "Other content will not be encrypted and can therefore be transferred off separately."
echo "HD content will not be decrypted unless you have cleared the encryption flag,"
echo "so, the script will exit if 'auto-unprotect' is not running"
echo
IFS=$'\n' #You need this or the for loop gets confused by spaces
host='127.0.0.1'
#What about standby? Can this be tweaked on the fly?
used=`df -m | sed 's/ */ /g' | grep /mnt/hd2 | cut -f 4 -d\ `
echo "Examining your local drive, you need up to $used MB for the backup"
echo "Attached drives have the following free space (MB):"
for freespace in `df -m | sed 's/ */ /g' | grep /media/drive | cut -f 5,7 -d\ `
do
echo "$freespace"
done
target=""
answer="N"
while [ "$answer" != "y" ]
do
result=""
while [ "$result" = "" ]
do
echo "Which drive do you want to back up to? (CTRL-C to abort)"
read target
result=`df -m | grep $target`
echo "You have selected $target"
if [ "$result" = "" ]; then
echo "That is not a valid drive"
fi
done
targetspace=`df -m | sed 's/ */ /g' | grep $target | cut -f 5 -d\ `
echo "$target has $targetspace MB free and you need up to $used MB"
echo "Do you want to use this drive? [y/N]"
read answer
done
echo "Backing up to $target"
# What if someone has plugged in an NTFS drive and it's mounted read only?
# Check for Fat32/ntfs or check for R/W, or both?
# You really don't want to run this if you have HD content and you aren't running auto-unprotect
AP=`opkg list-installed | grep auto-unprotect`
if [ "$AP" = "" ]; then
echo "Auto-unprotect isn't running, you don't want to download encrypted HD files"
echo "Install Auto-unprotect and wait 2 hours before rerunning"
echo "If you do not wait 2 hours, you could lose access to some HD files"
exit
fi
pwrdwn=`sqlite3 /var/lib/humaxtv/setup.db "Select itemvalue from tbl_menuconfig where itemName like 'AUTOMATIC_POWER_DOWN'"`
echo "Auto Power Off is set to: $pwrdwn"
pwrstby=`sqlite3 /var/lib/humaxtv/setup.db "Select itemvalue from tbl_menuconfig where itemName like 'PWR_SAVING_ON_STANDBY'" `
echo "Power Saving In Standby is set to: $pwrstby"
if [ "$pwrstby" -gt 0 ] || [ "$pwrdwn" -gt 0 ];then
echo "Script will not run with the powersaving options set"
exit
fi
#How many programmes are there to backup?
lines=`sqlite3 /mnt/hd2/dms_cds.db "select mediaID,folder from tblMedia" | grep -v '|(NULL)' | wc -l`
echo "There are $lines programmes to backup"
#Should we have a pause here to confirm the target destination and whether or not to proceed?
#For each of the recorded programmes...
for line in `sqlite3 /mnt/hd2/dms_cds.db "select mediaID,folder from tblMedia"`
do
sep=`expr index "$line" \|` #There is a '|' separator between the fields
mediaID="${line:0:$(($sep-1))}"
filename="${line:$sep}"
filename=${filename%.ts}
dir=`dirname $filename`
file=`basename $filename`
if [ "$filename" != "(NULL)" ]; then
echo -n "$lines files left: "
newdir="$target/$dir"
mkdir -p "$newdir"
cd "$newdir"
if [ ! -e "$file.ts" ]; then
echo "Processing \"$filename\", Media ID is $mediaID"
url="http://$host:9000/web/media/$mediaID.TS"
echo "Getting $url"
# Check that port 9000 is open and exit if not
# Needs to be implemented
if [ -f "$mediaID.TS" ]; then
rm "$mediaID.TS"
fi
wget $url
mv "$mediaID.TS" "$file.ts"
else
echo "Skipping $file.ts"
fi
if [ -e "/media/$filename.nts" ] && [ ! -e "$newdir/$file.nts" ]; then
cp "/media/$filename.nts" "$newdir/$file.nts"
fi
if [ -e "/media/$filename.thm" ] && [ ! -e "$newdir/$file.thm" ]; then
cp "/media/$filename.thm" "$newdir/$file.thm"
fi
if [ -e "/media/$filename.hmt" ] && [ ! -e "$newdir/$file.hmt" ]; then
cp "/media/$filename.hmt" "$newdir/$file.hmt"
hmt -encrypted "$newdir/$file.hmt"
hmt +setfolder="$newdir" "$newdir/$file.hmt"
fi
# Is it worth copying the .series file?
lines=$(($lines-1))
fi
done
Changes since the previous version...
1) A bit more error checking, for instance warning that you really don't want to think about running the script if the box is going into standby in a while
2) Some error checking still missing - I've had the webserver go away when testing but that isn't checked for
3) The sidecar files are now copied across and are amended to reflect the new location and the lack of encryption
4) Despite (3), the series file is not being copied over - that might be useful if the files are restored to a Humax hard drive, but they're not copied for the moment.
It should be totally safe to run as it doesn't suggest overwriting the original files, but if you run this and your gerbil explodes, blame the gods of shell scripting.
Any thoughts/comments and suggestions are appreciated as always.