Sfoglia il codice sorgente

In progress: restoring from backup (adding a fake device mode)

Laurent HUBERT 8 anni fa
parent
commit
028fa3ea42
2 ha cambiato i file con 27 aggiunte e 15 eliminazioni
  1. 1 0
      .gitignore
  2. 26 15
      devel/diskimg

+ 1 - 0
.gitignore

@@ -0,0 +1 @@
+\#*\#

+ 26 - 15
devel/diskimg

@@ -74,7 +74,7 @@ EOF
 exit 0
 }
 
-OPTS=$(getopt c:d:e:DhjJPr:uTvVz $*)
+OPTS=$(getopt c:d:e:FDhjJPr:uTvVz $*)
 #Test les paramètres
 if [ $? != 0 ]
 then
@@ -93,6 +93,7 @@ COMPRESSION_SUFFIX=".xz"
 VERBOSE_OPTION=""
 EXCLUDE_PATTERNS=""
 KEEP_PARTITIONS=""
+FAKE_DEVICE=""
 
 while true ; do
     case "$1" in
@@ -102,6 +103,8 @@ while true ; do
         	;;
         -e) EXCLUDE_PATTERNS="$EXCLUDE_PATTERNS $2" ; shift ; shift
         	;;
+        -F) FAKE_DEVICE="1"; shift
+        	;;
         -r) SOURCE_DIR="$2" ; shift ; shift
         	;;
         -u) COMPRESSION_ALGORITHM="" ; COMPRESSION_SUFFIX="" ; shift
@@ -147,13 +150,19 @@ do_restore_disk(){
 	echo "Restoring disk image from $SOURCE_DIR to $DISK_DEVICE"
 	if [[ "$KEEP_PARTITIONS" != "1" ]] ; then
 		echo "Restoring boot sector backup"
-		test "1" != "$DEBUG" && dcfldd if=$SOURCE_DIR/boot-sector.img of=$DISK_DEVICE  || exit 44
+		if [[ "1" != "$DEBUG" ]] ; then
+			dcfldd if=$SOURCE_DIR/boot-sector.img of=$DISK_DEVICE  || exit 44
+		fi
 		
 		echo "Restoring partition table backup"
-		test "1" != "$DEBUG" && sfdisk $DISK_DEVICE < $SOURCE_DIR/partition-table.sfdisk || exit 45
+		if [[ "1" != "$DEBUG" ]] ; then
+			sfdisk $DISK_DEVICE < $SOURCE_DIR/partition-table.sfdisk || exit 45
+		fi
 		
 		echo "Updating from partition information"
-		test "1" != "$DEBUG" && hdparm -z $DISK_DEVICE
+		if [[ "1" != "$DEBUG" ]] ; then
+			hdparm -z $DISK_DEVICE
+		fi
 	fi
 	
 	ls $SOURCE_DIR/partition*.tar$COMPRESSION_SUFFIX > /dev/null 2>&1
@@ -178,14 +187,16 @@ do_restore_disk(){
 			#First get the partition type
 			partition_type=$(cat $partition_type_file)
 			echo "Formating $partition_dev with type $partition_type"
-			test "1" != "$DEBUG" && mkfs -t $partition_type $partition_dev
-			test "$?" == 0 || exit $?
+			if [[ "1" != "$DEBUG" ]] ; then
+				mkfs -t $partition_type $partition_dev
+				test "$?" == 0 || exit $?
+			fi
 		fi
 		
 		mount_dir_name="diskimg-$backup_basename"
 		mountpoint_dir="/mnt/$mount_dir_name"
 		test "1" = "$VERBOSE" && echo "Mounting $partition_dev to $mountpoint_dir"
-		mkdir -p $mountpoint_dir
+		[[ "$FAKE_DEVICE" != "1" ]] && mkdir -p $mountpoint_dir
 		test "$?" == 0 || exit $?
 		mount $partition_dev $mountpoint_dir
 		test "$?" == 0 || exit $?
@@ -198,10 +209,12 @@ do_restore_disk(){
 		
 		test "$?" == 0 || exit $?
 		test "1" = "$VERBOSE" && echo "Un-mounting $partition_dev"
-		umount $partition_dev
-		test "$?" == 0 || exit $?
-		rmdir $mountpoint_dir
-		test "$?" == 0 || exit $?
+		if [[ "$FAKE_DEVICE" != "1" ]] ; then 
+			umount $partition_dev
+			test "$?" == 0 || exit $?
+			rmdir $mountpoint_dir
+			test "$?" == 0 || exit $?
+		fi
 	done
 }
 
@@ -237,7 +250,7 @@ backup_mounted_volume(){
 	echo $part_type > $TARGET_DIR/$part_type_filename
 	
 	backup_filename="$backup_basename.tar$COMPRESSION_SUFFIX "
-	echo "Creating backup for $partition_dev into $backup_filename"
+	echo "--- Creating backup for $partition_dev into $backup_filename ---"
 	mount_dir_name="diskimg-$backup_basename"
 	mountpoint_dir="/mnt/$mount_dir_name"
 	test "1" = "$VERBOSE" && echo "Mounting $partition_dev to $mountpoint_dir"
@@ -310,15 +323,13 @@ backup_disk(){
 			#Partition type
 			echo "Saving partition type"
 			part_type=`lsblk -lno NAME,FSTYPE $partition_dev | grep $(basename $partition_dev) | awk '{print $2}'`
-			#part_type=`lsblk -lno NAME,FSTYPE $partition_dev | grep $DISK_DEVICE | print '{print $2}'`
 			case $part_type in
 			crypto_LUKS)
 				#We extra steps here:
 				#First decipher the luks partition:
-				echo '###'$part_type'###'
 				ensure_crypt_part_is_opened $partition_dev $backup_basename
+				#The inner partition and device may have changed
 				part_type=${_UNCRYPTED_TYPE}
-				echo '***'$part_type'***'
 				device_to_mount=${_UNCRYPTED_VOL}
 				backup_mounted_volume $backup_basename $device_to_mount $part_type
 				;;