#!/bin/sh

# /root/restorewindows.sh
# 2010-10-04
# by Gernot WALZL

# Creates a backup for a given NTFS partition. In case of an emergency
# this partition can be restored in approximately 10 minutes.

# /etc/lilo.conf
#
# image = /boot/vmlinuz
#   append = "restorewindows"
#   initrd = /boot/initrd.gz
#   root = /dev/sda1
#   label = RestoreWindows
#   read-only

# /etc/rc.d/rc.local
#
# CMDLINE=$(cat /proc/cmdline)
# for CMD in $CMDLINE; do
#   if [ "$CMD" = "restorewindows" ]; then
#     /root/restorewindows.sh restore
#     shutdown -r now
#   fi
# done


PARTITION=/dev/sda3
BACKUPFILE=/root/windowsbackup.img.gz


print_usage () {
  echo "Usage: $0 {backup|restore}"
}

backup () {
  umount $PARTITION
  ntfsclone --save-image -o - $PARTITION | gzip -c > $BACKUPFILE
}

restore () {
  if [ -r $BACKUPFILE ]; then
    umount $PARTITION
    mkntfs -f $PARTITION || exit 1
    gzip -d -c $BACKUPFILE | ntfsclone --restore-image --overwrite ${PARTITION} -
  else
    echo "ERROR: Image $BACKUPFILE not found."
  fi
}


case "$1" in
'backup')
  backup
  ;;
'restore')
  clear
  echo "Es werden alle Ordner und Dateien auf Laufwerk C geloescht."
  echo "Der urspruengliche Zustand wird anschliessend wiederhergestellt."
  echo "Dieser Vorgang dauert ca. 10 Minuten und darf nicht unterbrochen werden."
  echo "Sind Sie sicher? [j/n]"
  read INPUT
  if [ "$INPUT" = "j" ]; then
    restore
  fi
  ;;
*)
  print_usage
  ;;
esac