Kaynağa Gözat

In progress: retrieving partition types as unique word

Laurent HUBERT 8 yıl önce
ebeveyn
işleme
70eeec9bed
1 değiştirilmiş dosya ile 74 ekleme ve 18 silme
  1. 74 18
      devel/diskimg

+ 74 - 18
devel/diskimg

@@ -228,6 +228,58 @@ restore_disk(){
 	fi
 }
 
+backup_mounted_volume(){
+	backup_basename=$1
+	partition_dev=$2
+	part_type=$3
+	device_to_mount=$4
+	
+	if [[ "" = "$device_to_mount" ]] ; then
+		device_to_mount=$partition_dev
+	fi
+	
+	part_type_filename="$backup_basename.type"
+	echo $part_type > $TARGET_DIR/$part_type_filename
+	
+	backup_filename="$backup_basename.tar$COMPRESSION_SUFFIX "
+	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"
+	mkdir -p $mountpoint_dir
+	mount $device_to_mount $mountpoint_dir
+	test "1" = "$VERBOSE" && echo "Backing up from $mountpoint_dir to $TARGET_DIR/$backup_filename"
+	
+	if [[ "$SKIP_TAR" != 1 ]] ; then
+		test "1" != "$DEBUG" && echo "tar --numeric-owner $COMPRESSION_ALGORITHM $VERBOSE_OPTION $EXCLUDE_STRING -pcf $TARGET_DIR/$backup_filename -C $mountpoint_dir ."
+		test "1" != "$DEBUG" && tar --numeric-owner $COMPRESSION_ALGORITHM $VERBOSE_OPTION $EXCLUDE_STRING -pcf $TARGET_DIR/$backup_filename -C $mountpoint_dir .
+	fi
+	
+	umount $device_to_mount
+	rmdir $mountpoint_dir
+}
+
+ensure_crypt_part_is_opened(){
+	partition_dev=$1
+	backup_basename=$2
+	#Are we already opened ? in this case, 
+	#we should see "crypt" string in the output of lsblk -l -n $partition_dev
+	lsblk -l -n $partition_dev | grep crypt > /dev/null 2>&1
+	rv=$?
+	if [ "$rv" != "0"  ] ; then
+		echo '### Please open the crypted volume by providing a passphrase to unlock it ###'
+		cryptsetup luksOpen $partition_dev $backup_basename
+	fi
+	#Now we should get the uncrypted volume name (which is not necessarily the 
+	#same as $backup_basename)
+	current_vol_name=$(lsblk -l -n $partition_dev |grep crypt | awk '{print $1}')
+	
+	#Now get the partition type and returns it
+	part_type=`lsblk -l -n -o NAME,FSTYPE $partition_dev | grep $backup_basename | awk '{print $2}'`
+	_RET=${part_type}
+	_UNCRYPTED_VOL=$current_vol_name
+}
+
 backup_disk(){
 	if [[ ! -d "$TARGET_DIR" ]] ; then
 		echo "$TARGET_DIR does not exist"
@@ -258,27 +310,31 @@ backup_disk(){
 		for partition_dev in $(sfdisk -d $DISK_DEVICE | grep -F 'start=' | awk '{print $1}') ; do
 			backup_basename=$(echo $partition_dev | sed "s+$DISK_DEVICE+partition-+")
 			
+			#Device to mount will be empty unless necessary (ie: if a crypt volume is to be backup-ed)
+			device_to_mount=""
 			#Partition type
 			echo "Saving partition type"
-			part_type_filename="$backup_basename.type"
-			lsblk -no FSTYPE $partition_dev > $TARGET_DIR/$part_type_filename
-			
-			backup_filename="$backup_basename.tar$COMPRESSION_SUFFIX "
-			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"
-			mkdir -p $mountpoint_dir
-			mount $partition_dev $mountpoint_dir
-			test "1" = "$VERBOSE" && echo "Backing up from $mountpoint_dir to $TARGET_DIR/$backup_filename"
-			
-			if [[ "$SKIP_TAR" != 1 ]] ; then
-				test "1" != "$DEBUG" && echo "tar --numeric-owner $COMPRESSION_ALGORITHM $VERBOSE_OPTION $EXCLUDE_STRING -pcf $TARGET_DIR/$backup_filename -C $mountpoint_dir ."
-				test "1" != "$DEBUG" && tar --numeric-owner $COMPRESSION_ALGORITHM $VERBOSE_OPTION $EXCLUDE_STRING -pcf $TARGET_DIR/$backup_filename -C $mountpoint_dir .
-			fi
+			part_type=`lsblk -no FSTYPE $partition_dev`
+			echo "------ lsblk -lno NAME,FSTYPE $partition_dev | grep $DISK_DEVICE | awk '{print $2}'"
+			lsblk -lno NAME,FSTYPE $partition_dev | grep $(basename $partition_dev) | awk '{print $2}'
+			the_part_type=$(lsblk -lno NAME,FSTYPE $partition_dev | grep $(basename $partition_dev)|awk '{print $2}')
+			echo "------"the_part_type=$the_part_type DISK_DEVICE=$DISK_DEVICE partition_dev=$partition_dev
 			
-			umount $partition_dev
-			rmdir $mountpoint_dir
+			#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
+				part_type=${_RET}
+				echo '###'$part_type'###'
+				device_to_mount=${_UNCRYPTED_VOL}
+				;;
+			*)
+				backup_mounted_volume $backup_basename $partition_dev $part_type $device_to_mount
+				;;
+			esac
 		done
 	fi
 }