#!/usr/bin/env bash ## - !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! ## - ## - assuming your remote user is called "back", you have ## - to give sudoer rights to him as follow: ## - ## - visudo (edits /etc/sudoers file) ## - ## - add line: ## - back ALL=(root)NOPASSWD:/usr/bin/which ## - back ALL=(root)NOPASSWD:/sbin/hdparm -I /dev/* ## - back ALL=(root)NOPASSWD:/sbin/fdisk ## - back ALL=(root)NOPASSWD:/sbin/sfdisk -d /dev/* ## - back ALL=(root)NOPASSWD:/bin/dd if=/dev/* ## - back ALL=(root)NOPASSWD:/sbin/parted ## - ## - !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! ## - load functions ## - . $rcopy_functions_file # --------------------------------------------------- # # -------------------- Variable --------------------- # # err_Log=${LOCK_DIR}/disksettings.err.log > $err_Log dirname_diskinfo=diskinfo target_dir=$backup_mirror_dir/$dirname_diskinfo if [ $srcHost != "localhost" ] || $_via_ssh_tunnel ;then sudo=`$ssh ${ssh_user}@$srcHost which sudo` fdisk=`$ssh ${ssh_user}@$srcHost $sudo which fdisk` sfdisk=`$ssh ${ssh_user}@$srcHost $sudo which sfdisk` hdparm=`$ssh ${ssh_user}@$srcHost $sudo which hdparm` dd=`$ssh ${ssh_user}@$srcHost which dd` rm=`$ssh ${ssh_user}@$srcHost which rm` parted=`$ssh ${ssh_user}@$srcHost sudo which parted` sgdisk=`$ssh ${ssh_user}@$srcHost sudo which sgdisk` fi # # ------------------ Ende Variable ------------------ # # --------------------------------------------------- # if [ $destHost != "localhost" ] ;then `$ssh $mkdir -p $backup_mirror_dir/$dirname_diskinfo` target_dir="${ssh_user}@${destHost}:$target_dir" else $mkdir -p $target_dir fi if [ "$_TEST" = "1" ];then progArgs="-n $progArgs --stats" elif [ "$_DEBUG" -gt 0 ];then progArgs="$progArgs --stats" if [ "$_DEBUG" -gt 1 ];then formats="%t -- %o %f %b Bytes[%l]" fi fi filedate=`$date +"%Y-%m-%d-%H%M"` ## - Ermittle Partitions Typ ## - if [ $srcHost != "localhost" ] || $_via_ssh_tunnel ;then disks=$($ssh ${ssh_user}@$srcHost "sed -ne 's/^.*\([shm]d[a-zA-Z]\+ *$\)/\1/p' /proc/partitions") else disks=$(sed -ne 's/^.*\([shm]d[a-zA-Z]\+ *$\)/\1/p' /proc/partitions) fi for _disk in $disks ; do disk=/dev/$_disk echolog "\n\n\t========" echolog "\t$disk" echolog "\t========" echolog "\n\tBackup identification info for disk $disk.." if [ $srcHost != "localhost" ] || $_via_ssh_tunnel ;then $ssh ${ssh_user}@$srcHost "$sudo $hdparm -I $disk" > ${LOCK_DIR}/info_${_disk}.txt 2> $err_Log else $hdparm -I $disk > ${LOCK_DIR}/info_${_disk}.txt 2> $err_Log fi if [ "$?" != "0!" ]; then $( $rsync --rsync-path='sudo rsync' $progArgs ${LOCK_DIR}/info_${_disk}.txt $target_dir/info_${_disk}.txt >> $logFile 2> $err_Log exit $? ) retval=$? if [ "$retval" = "0" ]; then echolog "\t--- \"info_${_disk}.txt\" successfully saved ---" else echolog "\t[ERROR] Cannot save identification info for disk $disk: \n\t`$cat $err_Log`\n" fi else echolog "\t[ERROR] Cannot determin identification info for disk $disk: \n\t`$cat $err_Log`\n" fi if [ "X$parted" = "X" ]; then echolog "\t[ERROR] program \"parted\" is needed to determin partition type. please install \"parted\"." continue fi if [ $srcHost != "localhost" ] || $_via_ssh_tunnel ;then #pt_type=$($ssh ${ssh_user}@$srcHost "sudo $parted -s $disk print|sed -ne 's@^.*Table: \(.*\)\$@\1@p'") pt_type=$($ssh ${ssh_user}@$srcHost "sudo $parted -s $disk print | grep -E \"^.*(Table:|tabelle:)\" | cut -d':' -f2 | tr -d ' '") else #pt_type=$($parted -s $disk print|sed -ne 's@^.*Table: \(.*\)$@\1@p') pt_type=$($parted -s $disk print | grep -E "^.*(Table:|tabelle:)" | cut -d':' -f2 | tr -d ' ') fi if [ "$pt_type" = "msdos" ]; then echolog "\n\n\tPartitiontype ${disk}: $pt_type" echolog "\t-----------------------\n" echolog "\tBackup partition tables for $disk (info file).." if [ $srcHost != "localhost" ] || $_via_ssh_tunnel ;then $ssh ${ssh_user}@$srcHost "$sudo $fdisk -lu $disk" > ${LOCK_DIR}/fdisk_${_disk}.txt 2>$err_Log else $fdisk -lu $disk > ${LOCK_DIR}/fdisk_${_disk}.txt 2>$err_Log fi if [ "$?" != "0" ]; then echolog "\t[ERROR] Cannot determin partition tables for $disk (info file): \n\t`$cat $err_Log`\n" else $( $rsync --rsync-path='sudo rsync' $progArgs ${LOCK_DIR}/fdisk_${_disk}.txt $target_dir/fdisk_${_disk}.txt >> $logFile 2> $err_Log exit $? ) retval=$? if [ "$retval" = "0" ]; then echolog "\t--- \"fdisk_${_disk}.txt\" successfully saved ---" else echolog "\t[ERROR] Cannot save partition tables for $disk (info file): \n\t`$cat $err_Log`\n" fi fi echolog "\n\tBackup master boot record of $disk.." if [ $srcHost != "localhost" ] || $_via_ssh_tunnel ;then $ssh ${ssh_user}@$srcHost "$sudo $dd if=$disk bs=512 count=1" > ${LOCK_DIR}/${_disk}.mbr 2> $err_Log else $dd if=$disk bs=512 count=1 > ${LOCK_DIR}/${_disk}.mbr 2> $err_Log fi if [ "$?" != "0!" ]; then $( $rsync --rsync-path='sudo rsync' $progArgs ${LOCK_DIR}/${_disk}.mbr $target_dir/${_disk}.mbr >> $logFile 2> $err_Log exit $? ) retval=$? if [ "$retval" = "0" ]; then echolog "\t--- \"${_disk}.mbr\" successfully saved ---" else echolog "\t[ERROR] Cannot save master boot record of disk $disk: \n\t`$cat $err_Log`\n" fi else echolog "\t[ERROR] Cannot copy master boot record of $disk: \n\t`$cat $err_Log`\n" fi echolog "\n\tBackup partition table of $disk (sfdisk).." if [ $srcHost != "localhost" ] || $_via_ssh_tunnel ;then $ssh ${ssh_user}@$srcHost "$sudo $sfdisk -d $disk" > ${LOCK_DIR}/${_disk}-partitions.sfdisk 2> $err_Log else $sfdisk -d $disk > ${LOCK_DIR}/${_disk}-partitions.sfdisk 2> $err_Log fi if [ "$?" != "0!" ]; then $( $rsync --rsync-path='sudo rsync' $progArgs ${LOCK_DIR}/${_disk}-partitions.sfdisk $target_dir/${_disk}-partitions.sfdisk >> $logFile 2> $err_Log exit $? ) retval=$? if [ "$retval" = "0" ]; then echolog "\t--- \"${_disk}-partitions.sfdisk\" successfully saved ---" else echolog "\t[ERROR] Cannot save partition table of disk $disk: \n\t`$cat $err_Log`\n" fi else echolog "\t[ERROR] Cannot create partition table of of $disk using sfdisk: \n\t`$cat $err_Log`\n" fi if [ $srcHost != "localhost" ] || $_via_ssh_tunnel ;then partitions=`$ssh ${ssh_user}@$srcHost "$sudo $fdisk -l 2>/dev/null" | grep -e"^$disk" | awk '{print$1}'` >> $logFile 2> $err_Log else partitions=`$fdisk -l 2>/dev/null | grep -e"^$disk" | awk '{print$1}'` >> $logFile 2> $err_Log fi for partition in $partitions ; do _part=`echo $partition | cut -d"/" -f3` echolog "\n\tBackup boot sector of $partition .." if [ $srcHost != "localhost" ] || $_via_ssh_tunnel ;then $ssh ${ssh_user}@$srcHost "$sudo $dd if=$partition bs=512 count=1" > ${LOCK_DIR}/boot_sec-${_part}.bin 2> $err_Log else $dd if=$partition bs=512 count=1 > ${LOCK_DIR}/boot_sec-${_part}.bin 2> $err_Log fi if [ "$?" != "0!" ]; then $( $rsync --rsync-path='sudo rsync' $progArgs ${LOCK_DIR}/boot_sec-${_part}.bin $target_dir/boot_sec-${_part}.bin >> $logFile 2> $err_Log exit $? ) retval=$? if [ "$retval" = "0" ]; then echolog "\t--- \"boot_sec-${_part}.bin\" successfully saved ---" else echolog "\t[ERROR] Cannot save boot record of partition $partition: \n\t`$cat $err_Log`\n" fi else echolog "\t[ERROR] Cannot copy boot record of ipartition $partition: \n\t`$cat $err_Log`\n" fi done elif [ "$pt_type" = "gpt" ]; then echolog "\n\n\tPartitiontype ${disk}: $pt_type" echolog "\t-----------------------\n" if [ "X$sgdisk" = "X" ]; then echolog "\t[ERROR] program \"sgdisk\" is needed to determin partition settings. please install \"sgdisk\"." continue fi echolog "\tBackup partition tables for $disk (info file).." if [ $srcHost != "localhost" ] || $_via_ssh_tunnel ;then $ssh ${ssh_user}@$srcHost "$sudo $sgdisk -p $disk" > ${LOCK_DIR}/sgdisk_${_disk}.txt 2>$err_Log else $sgdisk -p $disk > ${LOCK_DIR}/sgdisk_${_disk}.txt 2>$err_Log fi if [ "$?" != "0" ]; then echolog "\t[ERROR] Cannot determin partition tables for $disk (info file): \n\t`$cat $err_Log`\n" else $( $rsync --rsync-path='sudo rsync' $progArgs ${LOCK_DIR}/sgdisk_${_disk}.txt $target_dir/sgdisk_${_disk}.txt >> $logFile 2> $err_Log exit $? ) retval=$? if [ "$retval" = "0" ]; then echolog "\t--- \"fdisk_${_disk}.txt\" successfully saved ---" else echolog "\t[ERROR] Cannot save partition tables for $disk (info file): \n\t`$cat $err_Log`\n" fi fi echolog "\n\tBackup partition table of $disk (sgdisk).." if [ $srcHost != "localhost" ] || $_via_ssh_tunnel ;then $ssh ${ssh_user}@$srcHost "$sudo $sgdisk -b /tmp/${_disk}-partitions.sgdisk $disk" > /dev/null $scp -p ${ssh_user}@$srcHost:/tmp/${_disk}-partitions.sgdisk ${LOCK_DIR}/${_disk}-partitions.sgdisk > /dev/null 2>&1 else $sgdisk -b ${LOCK_DIR}/${_disk}-partitions.sgdisk $disk > /dev/null fi if [ "$?" != "0!" ]; then $( $rsync --rsync-path='sudo rsync' $progArgs ${LOCK_DIR}/${_disk}-partitions.sgdisk $target_dir/${_disk}-partitions.sgdisk >> $logFile 2> $err_Log exit $? ) retval=$? if [ "$retval" = "0" ]; then echolog "\t--- \"${_disk}-partitions.sgdisk\" successfully saved ---" else echolog "\t[ERROR] Cannot save partition table of disk $disk: \n\t`$cat $err_Log`\n" fi else echolog "\t[ERROR] Cannot create partition table of of $disk using sgdisk: \n\t`$cat $err_Log`\n" fi else echolog "\t[ERROR] Cannot determin partition type for disk $disk !!" fi done echolog "" exit 0