I'm out on the road right now I'll get back to my house and then I'll be able to respond thank you
---------- Post updated at 03:25 PM ---------- Previous update was at 02:25 PM ----------
This is the piece preceding the partitioning in the recovery script...
All drive designations are for the 2nd external USB Drive -- which gets attached as /dev/sdc.
The USB drive used for the backup process is /dev/sdb. /dev/sdb1 is a LiveCD partition and /dev/sdb2 is the data partition.
#!/bin/bash
#
# This scipt is to be run from a LiveCD environment in a root terminal
#
clear
echo
echo
echo "Mounting USB Hard Drive to mount point /mnt/USBDrive..."
# We'll create a mount point and mount the main directory structure of our backup drive to this point.
mkdir -p /mnt/USBDrive
mount -o defaults /dev/sdb2 /mnt/USBDrive
echo "Done!"
echo
# We are going to need the physical volume name and UUID along with the Volume Group name later in this process
# We also want to determine the starting and ending sector (along with the partition number) of our luks partition
echo "Setting a few variables for later..."
pvname=$(grep Name /mnt/USBDrive/lvminfo/pvparse | cut -d'/' -f4)
pvuuid=$(grep UUID /mnt/USBDrive/lvminfo/pvparse | cut -b 25-62)
vgname=$(grep ^/dev /mnt/USBDrive/lvminfo/fstab_info | cut -d'/' -f4 | cut -d'-' -f1 | sed '2,99d')
This is the section following the partitioning piece...
# We need to inform the kernel about the changes
partprobe
# And format the partition we just created
mkfs -t ext4 /dev/sdc$partnum
echo
echo "Restoring Cryptsetup header and key slots..."
# This command always gives an error that can be disregarded. Thus, the redirection of stderr to null
cryptsetup luksHeaderRestore /dev/sdc$partnum --header-backup-file /mnt/USBDrive/lvminfo/cryptbackup.img 2>/dev/null
echo
echo "Opening crypt partition for writing..."
cryptsetup -vv open /dev/sdc$partnum --type luks $pvname
echo
echo "Restoring Physical Volume..."
# This command always gives an error that can be disregarded. Thus, the redirection of stderr to null
pvcreate -f --uuid $pvuuid --restorefile /mnt/USBDrive/lvminfo/$vgname /dev/mapper/$pvname 2>/dev/null
echo
echo "Restoring & Checking Volume Group and Logical Volumes structures..."
vgcfgrestore $vgname
lvs -a -o +devices
vgchange -ay $vgname
pvck -vv
vgck -vv
mkfs -t ext4 /dev/mapper/$vgname-*
mkswap /dev/mapper/$vgname-swap
echo
echo "Creating Mount Points, mounting individual LVM's from Volume Group and restoring data..."
for fs in ${farry[@]}
do
mkdir -p /mnt/$vgname/$fs
mount /dev/mapper/${vgname}-$fs /mnt/$vgname/$fs
rsync -aHvu /mnt/USBDrive/Recover/$fs /mnt/$vgname/$fs
done
rsync -aHvu --ignore-existing /mnt/USBDrive/Recover/ /mnt/$vgname/
mv -f /mnt/USBDrive/lvminfo/cryptbackup.img /mnt/USBDrive/lvminfo/cryptbackup.img.bak
exec $SHELL
This is what i DID have until I realized that I kept getting a corrupted partition table.
startsec=$(grep ^/dev /mnt/USBDrive/info/fdisk_info | grep G | cut -d" " -f3)
endsect=$(grep ^/dev /mnt/USBDrive/info/fdisk_info | grep G | cut -d" " -f4)
partnum=$(grep $startsec /mnt/USBDrive/info/sgdisk_info | cut -b4)
# The following line initializes an array of variables from a list of lvnames created from fstab_info
read -a farry <<< $(grep $vgname /mnt/USBDrive/lvminfo/fstab_info | cut -d'-' -f2 | cut -d'/' -f1 | cut -d" " -f1 | awk '{printf("%s%s",$0,NR%9?" ":"\n")}')
# Once we have gotten the volume group name from fstab we can create a mount point for the restore process
mkdir -p /mnt/$vgname
echo
# We start the recovery process by writing our saved critical disk areas to the new drive
# This will give us a mountable drive with the partition table, EFI and boot partitions as they were on the original drive
# sgdisk /dev/sda --backup=/mnt/USBDrive/info/sgdisksda
# sgdisk /dev/sdc --load-backup= /mnt/USBDrive/info/sgdisksda
echo "Restoring the first 2048 sectors of /dev/sda..."
dd if=/mnt/USBDrive/dd/partition-restore of=/dev/sdc bs=1 count=2047
echo
echo "Restoring the EFI partition as /dev/sda1..."
dd if=/mnt/USBDrive/dd/sda1EFI-restore of=/dev/sdc1 bs=8192
echo
echo "Restoring the Boot partition as /dev/sda2..."
dd if=/mnt/USBDrive/dd/sda2Boot-restore of=/dev/sdc2 bs=8192
echo
echo "Prepping to restore the luks partition, and VG/LVM structures..."
echo
# Just to ensure that there isn't any old luks data on our new drive that will present errors during the restortion of our saved luks partition header image, we need to wipe out any signs of any previous lvm info that might be there
echo "First we will ensure that no old crypt/lvm info remains on our recovery disk. If there is the restoration will error out."
cryptsetup -v isLuks /dev/sdc$partnum
head -c 3145728 /dev/zero > /dev/sdc$partnum; sync
# Next, we will recreate the partition from scratch and format it to ext4
echo "Then we will create a new /dev/sda3 and restore our backup img"
sgdisk -d $partnum -n $partnum:$startsec:$endsec -t $partnum:8300 /dev/sdc
(As you can see, in the beginning, to work around the corrupt partition issue,
I was trying to simply delete /dev/sdc3, put in a new partition, format it, run partprobe to update the kernel
and then do the cryptsetup luksRestore.
But I kept getting a "partition corrupt' error and exit.)
Which is what brings me to my original question....
How can I parse and initialize an array and a for loop that will just recreate these partitions from scratch rather than doing it otherwise.
Since all information gathered about /dev/sda showed that the EFI partition started at sector 2048,
I just used dd to copy sectors 0-2047 and then restored the other two partitions using the same method.
All of the information that I thought I might need to do the recovery part was saved during the execution of the backup script:
#!/bin/bash
mkdir -p /mnt/USBDrive
echo "Mounting USB Hard Drive..."
mount -o defaults /dev/sdb2 /mnt/USBDrive
echo "Done!"
echo
mkdir -p /mnt/USBDrive/dd
echo "Backing up first 2048 sectors of /dev/sda to /mnt/USBDrive/dd"
dd if=/dev/sda of=/mnt/USBDrive/dd/partition-restore bs=1 count=2047
echo
echo "Backing up the EFI partition to /mnt/USBDrive/dd "
dd if=/dev/sda1 of=/mnt/USBDrive/dd/sda1EFI-restore bs=8192
echo
echo "Backing up the Boot partition to /mnt/USBDrive/dd "
dd if=/dev/sda2 of=/mnt/USBDrive/dd/sda2Boot-restore bs=8192
echo
mkdir -p /mnt/USBDrive/info
echo "Backing up critical hardware information to /mnt/USBDrive/info"
lspci -vv > /mnt/USBDrive/info/lspci_info
lshw > /mnt/USBDrive/info/lshw_info
hdparm -I /dev/sda > /mnt/USBDrive/info/hdparm_info
blkid > /mnt/USBDrive/info/blkid_info
fdisk -l /dev/sda > /mnt/USBDrive/info/fdisk_info
sgdisk -p /dev/sda > /mnt/USBDrive/info/sgdisk_info
smartctl -d ata -a -i /dev/sda > /mnt/USBDrive/info/smartctl_info
df -h > /mnt/USBDrive/info/df_info
echo
echo "Backing up critical LVM info to /mnt/USBDrive/lvminfo"
mkdir -p /mnt/USBDrive/lvminfo
# sgdisk backup
# -b or --backup filename Save a backup of the disk to the specified file.
pvdisplay > /mnt/USBDrive/lvminfo/pvdisplay
vgdisplay > /mnt/USBDrive/lvminfo/vgdisplay
lvscan > /mnt/USBDrive/lvminfo/lvscan
lvdisplay > /mnt/USBDrive/lvminfo/lvdisplay
lvmdiskscan > /mnt/USBDrive/lvminfo/lvmdiskscan
lvs -a -o +devices > /mnt/USBDrive/lvminfo/lvscmd
echo
echo "Backing up cryptsetup info to /mnt/USBDrive/lvminfo"
cryptsetup -q luksHeaderBackup /dev/sda3 --header-backup-file /mnt/USBDrive/lvminfo/cryptbackup.img
echo
echo "Backing up LVM Structure to /mnt/USBDrive/lvminfo "
vgcfgbackup
vgcfgbackup -f /mnt/USBDrive/lvminfo/fedora fedora
vgdisplay --verbose | grep PV > /mnt/USBDrive/lvminfo/pvparse
echo
echo "Backing up crypttab and fstab to /mnt/USBDrive/lvminfo"
cat /etc/crypttab > /mnt/USBDrive/lvminfo/crypttab_info
cat /etc/fstab > /mnt/USBDrive/lvminfo/fstab_info
echo
echo "Running full system backup: Backing up to /mnt/USBDrive/Recover"
mkdir -p /mnt/USBDrive/Recover
echo
rsync -aHvu --delete --exclude={/backup/*,/dev/*,/proc/*,/sys/*,/run/*,/mnt/*,/media/*,/lost+found,/home/*/.thumbnails/*,/home/*/.local/share/Trash/*,/home/*/.gvfs/*,/home/*/.cache/google/*,/home/*/.cache/mozilla/*} /* /mnt/USBDrive/Recover
echo
echo
# Add in prompt to either unmount or leave mounted. umount /mnt/USBDrive
exec $SHELL
But WAIT!! Something just hit me!!
echo "Backing up first 2048 sectors of /dev/sda to /mnt/USBDrive/dd"
dd if=/dev/sda of=/mnt/USBDrive/dd/partition-restore bs=1 count=2047
actually gives me 2048 BLOCKS and not 2047 sectors, right?!?! Am I on the right track here???
---------- Post updated at 03:58 PM ---------- Previous update was at 03:25 PM ----------
So if I understand it correctly, in order to back up everything on the disk prior to the first partition (which starts at sector 2048) I would actually have to use a bs of 512 and indicate a count of 2047. Correct?
dd if=/dev/sda of=/mnt/USBDrive/dd/partition-restore bs=512 count=2047
Since the sector size is 512 bytes, that should give me the equivalent of the first 2047 sectors, correct?