Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/bin/bash
- # Caution, do not run unless you already have a backup you trust.
- # I run this on my machine and I trust it.
- # You should not trust a new backup plan unless you've not only
- # used it to backup, but also to do a successful restore.
- # This works for me, but it might not work for you.
- # I run Mint Mate with a standard two partition install. If you've done custom partitioning (separate home/swap/etc partitions)
- # it won't work for you. I made an attempt to assess whether it will work for you or not but, not guarantees)
- # Use at your own risk, I'm only an amateur, I mess up sometimes.
- # That said, it works for me. Feel free to try it. (maybe try cloning to USB first?)
- #
- if [ "$(id -u)" != "0" ]; then
- exec sudo "$0" "$@"
- fi
- user="$SUDO_USER"
- hostname=$(hostname)
- skipcheck="$1"
- main () {
- determinesource;
- if [ "$skipcheck" == "skipcheck" ]; then
- echo "Do not check the source requested so skipping"
- else
- checksource;
- echo "Checking source"
- fi
- gettarget;
- checktarget;
- askifformatdesired;
- start_time=$(date +%s)
- if [[ $formatRequested == "y" ]]; then
- doformat;
- else
- echo "skipping format";
- fi
- stopservices;
- domirror;
- restartservices;
- fixgrub;
- end_time=$(date +%s)
- elapsed=$(( end_time - start_time ))
- echo "Total time taken: $elapsed seconds"
- }
- #*************************determinesource******************************
- determinesource () {
- sourcedevice=/dev/$(lsblk -no PKNAME "$(findmnt -no SOURCE -T /)")
- }
- #***********************end determinesource****************************
- #***********************checksource****************************
- checksource () {
- numpartitions=$(lsblk -nlpo NAME,TYPE $sourcedevice | awk '$2=="part"{print $1}' | wc -l)
- if [ $numpartitions != 2 ]; then
- echo "Sorry, this script only works with 2 specific partitions"
- echo "Partition #1 must be exactly \"EFI System\""
- echo "Partition #2 must be exactly \"Linux filesystem\""
- exit 1
- fi
- if [[ $sourcedevice == *"nvme"* ]]; then
- efipartition="$sourcedevice"p1
- mainpartition="$sourcedevice"p2
- elif [[ $sourcedevice == *"sd"* ]]; then
- efipartition="$sourcedevice"1
- mainpartition="$sourcedevice"2
- elif [[ $sourcedevice == *"mmcblk"* ]]; then
- efipartition="$sourcedevice"p1
- mainpartition="$sourcedevice"p2
- fi
- efishort=$(echo $efipartition | awk -F'/' '{print $3}')
- mainshort=$(echo $mainpartition | awk -F'/' '{print $3}')
- #echo "efishort is: $efishort"
- part1type=$(lsblk -o name,parttypename | grep $efishort | awk -F "$efishort" '{print $2}' | sed 's/^[[:space:]]*//')
- part2type=$(lsblk -o name,parttypename | grep $mainshort | awk -F "$mainshort" '{print $2}' | sed 's/^[[:space:]]*//')
- if [[ $part1type != "EFI System" || $part2type != "Linux filesystem" ]]; then
- echo "Sorry, source partitions are wrong type"
- echo "Partition #1 must be exactly \"EFI System\""
- echo "Partition #2 must be exactly \"Linux filesystem\""
- exit 2
- fi
- }
- #***********************end checksource****************************
- #*********************gettarget******************************
- gettarget () {
- local target=""
- local response="n"
- while [[ "$response" != "y" ]]; do
- read -p "Enter the device to mirror to [sdX|nvmeXnX]: " target
- target="${target#/dev/}"
- if [[ -z "$target" ]]; then
- echo "Error: Input cannot be blank."
- continue
- elif [[ ! -e "/dev/$target" ]]; then
- echo "Error: /dev/$target does not exist. Check 'lsblk'."
- continue
- fi
- read -p "Target is /dev/$target. Continue? [y/N]: " response
- response="${response,,}"
- response="${response:0:1}"
- done
- targetdevice="/dev/$target"
- echo "Unmounting /dev/${target} to avoid possible corruption..."
- udisksctl unmount -bf /dev/${target}1 2>/dev/null
- udisksctl unmount -bf /dev/${target}2 2>/dev/null
- }
- #***********************end gettarget****************************
- #************************checktarget*****************************
- checktarget () {
- numtargetpartitions=$(lsblk -nlpo NAME,TYPE $targetdevice | awk '$2=="part"{print $1}' | wc -l)
- if [[ $targetdevice == *"nvme"* ]]; then
- efipartition="$targetdevice"p1
- mainpartition="$targetdevice"p2
- elif [[ $targetdevice == *"sd"* ]]; then
- efipartition="$targetdevice"1
- mainpartition="$targetdevice"2
- elif [[ $targetdevice == *"mmcblk"* ]]; then
- efipartition="$targetdevice"p1
- mainpartition="$targetdevice"p2
- fi
- efishort=$(echo $efipartition | awk -F'/' '{print $3}')
- mainshort=$(echo $mainpartition | awk -F'/' '{print $3}')
- part1type=$(lsblk -o name,parttypename | grep $efishort | awk -F "$efishort" '{print $2}' | sed 's/^[[:space:]]*//')
- part2type=$(lsblk -o name,parttypename | grep $mainshort | awk -F "$mainshort" '{print $2}' | sed 's/^[[:space:]]*//')
- if [[ $part1type != "EFI System" || $part2type != "Linux filesystem" ]]; then
- targetneedsformatted="true"
- else
- targetneedsformatted="false"
- fi
- }
- #**********************end checktarget***************************
- #*****************askifformatdesired*************************
- askifformatdesired () {
- if [[ $targetneedsformatted == "true" ]]; then
- echo -e "\n"
- read -p "Target device ($targetdevice) needs formatting, is that okay? [y/N]: " formatRequested
- formatRequested=${formatRequested:-N}
- formatRequested="${formatRequested:0:1}"
- formatRequested="${formatRequested,,}"
- if [[ $formatRequested != "y" ]]; then
- echo "Format needed but refused, aborting."
- exit;
- fi
- else #format not needed but still an option
- echo -e "\nTarget has preexisting mirror."
- echo -e "Preexisting mirror can either be refreshed or created anew. (default is to refresh the existing mirror)"
- read -p "Format and create a new mirror? [y/N]: " formatRequested
- formatRequested=${formatRequested:-N}
- formatRequested="${formatRequested:0:1}"
- formatRequested="${formatRequested,,}"
- fi
- }
- #**************end askifformatdesired************************
- #*******************doformat************************
- doformat () {
- possiblelabel="$(hostname)usb"
- echo
- read -p "Enter a new label for the target device (e.g. [$possiblelabel]): " label
- label=${label:-$possiblelabel}
- echo "Label of target device will be: $label"
- echo "This might take a few minutes, please be patient."
- fdisk $targetdevice <<EOF
- g
- n
- 1
- +512M
- t
- C12A7328-F81F-11D2-BA4B-00A0C93EC93B
- n
- 2
- w
- EOF
- echo "efi and ext4 partitions created successfully."
- echo "The new partitions will now be formatted"
- echo "This might take a few minutes, please be patient."
- mkfs.fat -F32 -n ${label^^}EFI "$efipartition"
- yes | mkfs.ext4 -L "$label" "$mainpartition"
- sync
- }
- #******************end doformat*********************
- #*****************stopservices**********************
- stopservices () {
- systemctl stop cron
- systemctl stop mysql
- }
- #***************end stopservices** *****************
- #*******************domirror************************
- domirror () {
- echo "making the mirror"
- label="target"
- mount /boot/efi
- mkdir -p /media/$user/EFI
- mount "$efipartition" /media/$user/EFI
- mkdir -p /media/$user/$label
- mount "$mainpartition" /media/$user/$label
- sourceuuid=$(findmnt / -o UUID -n)
- targetuuid=$(findmnt /media/$user/"$label" -o UUID -n)
- sourceefiuuid=$(grep /boot/efi /etc/fstab | awk '{print $1}' | grep UUID | sed -E 's/UUID=(.*)/\1/' )
- targetefiuuid=$(findmnt /media/$user/EFI/ -o UUID -n)
- sourceefilabel=$(findmnt -no label /boot/efi)
- sourcelabel=$(findmnt -no label /)
- targetlabel=$(findmnt -no label /media/$user/target)
- targetefilabel=${targetlabel^^}EFI
- rsync -rv --delete /boot/efi/ /media/$user/EFI/
- find /media/$user/EFI/ -name "grub.cfg" -exec sed -i "s/$sourceuuid/$targetuuid/" "{}" \;
- rsync -axAXHv \
- --exclude='/home/*/.cache' \
- --exclude='/home/*/.gvfs' \
- --exclude='/dev/*' \
- --exclude='/proc/*' \
- --exclude='/sys/*' \
- --exclude='/tmp/*' \
- --exclude='/run/*' \
- --exclude='/mnt/*' \
- --exclude='/media/*' \
- --exclude='/lost+found' \
- --delete / /media/$user/"$label"/
- [[ $(grep " / " /etc/fstab | grep -i UUID) ]] && sed -i "s/$sourceuuid/$targetuuid/" /media/$user/$label/etc/fstab || sed -i "s/$sourcelabel/$targetlabel/" /media/$user/$label/etc/fstab
- [[ $(grep "/boot/efi" /etc/fstab | grep -i UUID) ]] && sed -i "s/$sourceefiuuid/$targetefiuuid/" /media/$user/$label/etc/fstab || sed -i "s/$sourceefilabel/$targetefilabel/" /media/$user/$label/etc/fstab
- sync
- }
- #*******************domirror************************
- #***************restartservices********************
- restartservices () {
- systemctl start cron
- systemctl start mysql
- }
- #**************end restartservices*****************
- #*******************fixgrub************************
- fixgrub () {
- echo -e "\nMaking the target device bootable... Please be patient."
- for i in /sys /run /dev /proc; do mount --bind "$i" "/media/$user/target$i"; done
- chroot /media/$user/target/ /bin/bash -- << EOT
- update-grub
- EOT
- echo "Writing changes to target, do not remove!"
- sync ; sleep 10 # give sync more time to finish syncing (had target busy, unable to dismount error, may need to increase with slower targets?)
- for i in /proc /dev /run /sys; do umount "/media/$user/target$i"; done
- umount /media/$user/EFI
- umount /media/$user/target
- rmdir /media/$user/target
- rmdir /media/$user/EFI
- echo "It is now okay to remove the target drive."
- }
- #*****************end fixgrub**********************
- main
Advertisement
Add Comment
Please, Sign In to add comment