#! /bin/sh

DESTDIR=/var/wwwroot/mtd_backup

mkdir -p $DESTDIR

echo "<HTML><body>" > $DESTDIR/index.html
echo "<br>Right-click and 'Save Target As...' on all of the following:<br>" >> $DESTDIR/index.html

let x=0
while [[ $x -le 10 ]]
do
    if [[ -c /dev/mtd$x ]]; then
        echo "--- Backing up mtd$x ---"
        dd if=/dev/mtd$x of=$DESTDIR/mtd$x.bin
        if [[ $? -ne 0 ]]; then
            echo "=== ERROR backing up mtd$x! Check free space in $DESTDIR! ==="
            exit 1
        fi
        echo "<br><br><A HREF=\"mtd$x.bin\">mtd$x.bin</A>" >> $DESTDIR/index.html
    fi
    let x=$x+1
done

echo "</body></html>" >> $DESTDIR/index.html

echo "=== Backup to $DESTDIR complete:"
ls -l $DESTDIR/mtd*.bin


