#!/bin/sh # # Author: Martti Kuparinen # # $Id: win2bak,v 1.11 2010-01-27 06:14:21 martti Exp $ # usage() { cat << EOF Usage: `basename $0` [-c] [-h] [-p] src dst where -c Compress the backup image with "bzip2 -9" -h This help -p Remove some unwanted files from the NTFS filesystem src is the NTFS filesystem to backup dst is the backup image Examples: `basename $0` -p /dev/sda1 /home/backup/windows.img Remove some unwanted files from the NTFS filesystem and create a backup image. This image can be loopback mounted in order to access the backup image with # mkdir /mnt/ntfsclone # sudo mount -t ntfs -o loop /home/backup/windows.img /mnt/ntfsclone instead of restoring the whole backup image. `basename $0` -p -c /dev/sda1 /home/backup/windows.img.bz2 Remove some unwanted files from the NTFS filesystem and create a compressed backup image. The image can NOT be loopback mounted but it takes less disk space. EOF exit 1 } ARGV=`getopt chp ${*}` [ ${?} != 0 ] && usage SRC= DST= ARGS= WANT_COMPRESSED="false" WANT_PURGED="false" set -- ${ARGV} for i do case "${i}" in -c) ARGS="${ARGS} -c" WANT_COMPRESSED="true" shift ;; -h) usage shift ;; -p) ARGS="${ARGS} -p" WANT_PURGED="true" shift ;; --) shift ;; esac done [ $# -ne 2 ] && usage if [ `id -u` -ne 0 ]; then sudo $0 ${ARGS} $* exit $? fi SRC="${1}" DST="${2}" if [ -z "`which ntfsclone 2> /dev/null`" ]; then echo "ERROR: ntfsclone not found!" if [ -r /etc/lsb-release ]; then . /etc/lsb-release if [ "${DISTRIB_ID}" = "Ubuntu" ]; then echo "" echo "You can fix this by running" echo "" echo "sudo apt-get install ntfsprogs" echo "" exit 1 fi fi fi if [ ! -b "${SRC}" ]; then echo "ERROR: ${SRC} is not a valid block device!" exit 1 fi if [ -f "${DST}" ]; then echo "ERROR: ${DST} already exists!" echo "" ls -lh "${DST}" echo "" exit 1 fi if [ ! -z "`mount | grep ^${SRC}[[:space:]]`" ]; then echo "ERROR: ${SRC} must not be mounted!" exit 1 fi # Get rid of password prompt before actual dump... sudo ls 2>&1 > /dev/null if ${WANT_PURGED}; then echo "Removing some unwanted files from ${SRC}" mkdir /tmp/win2bak.$$ sudo mount -t ntfs-3g ${SRC} /tmp/win2bak.$$ || exit 1 DIR=`pwd` cd /tmp/win2bak.$$ sudo rm -rf \ hiberfil.sys \ pagefile.sys \ Users/*/AppData/Local/Temp/* \ Windows/Temp/* \ Windows/SoftwareDistribution/Download/* # Is this safe to do? #find . -name '*.tmp' -exec sudo rm -rf {} \; 2> /dev/null cd "${DIR}" sudo umount /tmp/win2bak.$$ rmdir /tmp/win2bak.$$ fi T1=`date` echo "Backing up ${SRC}" if ${WANT_COMPRESSED}; then sudo ntfsclone -s -o - "${SRC}" | bzip2 -9 -c > "${DST}" else sudo ntfsclone -o "${DST}" "${SRC}" fi echo "Creating partition table description" DISK=`echo ${SRC} | sed 's+[0-9]*$++'` LANG=C sudo fdisk -l ${DISK} > ${DST}.txt echo >> ${DST}.txt echo >> ${DST}.txt LANG=C sudo cfdisk -Ps ${DISK} >> ${DST}.txt echo >> ${DST}.txt echo >> ${DST}.txt LANG=C sudo cfdisk -Pt ${DISK} >> ${DST}.txt echo >> ${DST}.txt T2=`date` echo "" echo "Started: ${T1}" echo "Completed: ${T2}" echo "" ls -lh "${DST}"* echo "" exit 0