chuggerguy

mirrortodevice

Nov 7th, 2024 (edited)
141
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 9.83 KB | Software | 0 0
  1. #!/bin/bash
  2. # Caution, do not run unless you already have a backup you trust.
  3. # I run this on my machine and I trust it.
  4. # You should not trust a new backup plan unless you've not only
  5. # used it to backup, but also to do a successful restore.
  6. # This works for me, but it might not work for you.
  7. # I run Mint Mate with a standard two partition install. If you've done custom partitioning (separate home/swap/etc partitions)
  8. # it won't work for you. I made an attempt to assess whether it will work for you or not but, not guarantees)
  9. # Use at your own risk, I'm only an amateur, I mess up sometimes.
  10. # That said, it works for me. Feel free to try it. (maybe try cloning to USB first?)
  11. #
  12.  
  13. if [ "$(id -u)" != "0" ]; then
  14.   exec sudo "$0" "$@"
  15. fi
  16.  
  17. user="$SUDO_USER"
  18. hostname=$(hostname)
  19. skipcheck="$1"
  20.  
  21. main () {
  22.     determinesource;
  23.     if [ "$skipcheck" == "skipcheck" ]; then
  24.         echo "Do not check the source requested so skipping"
  25.     else
  26.         checksource;
  27.         echo "Checking source"
  28.     fi
  29.     gettarget;
  30.     checktarget;
  31.     askifformatdesired;
  32.     start_time=$(date +%s)
  33.     if [[ $formatRequested == "y" ]]; then
  34.         doformat;
  35.     else
  36.         echo "skipping format";
  37.     fi
  38.     stopservices;
  39.     domirror;
  40.     restartservices;
  41.     fixgrub;
  42.     end_time=$(date +%s)
  43.     elapsed=$(( end_time - start_time ))
  44.     echo "Total time taken: $elapsed seconds"
  45. }
  46.  
  47. #*************************determinesource******************************
  48. determinesource () {
  49.  
  50.     sourcedevice=/dev/$(lsblk -no PKNAME "$(findmnt -no SOURCE -T /)")
  51.  
  52. }
  53. #***********************end determinesource****************************
  54.  
  55. #***********************checksource****************************
  56. checksource () {
  57. numpartitions=$(lsblk -nlpo NAME,TYPE $sourcedevice | awk '$2=="part"{print $1}' | wc -l)
  58.  
  59. if [ $numpartitions != 2 ]; then
  60.     echo "Sorry, this script only works with 2 specific partitions"
  61.     echo  "Partition #1 must be exactly \"EFI System\""
  62.     echo "Partition #2 must be exactly \"Linux filesystem\""
  63.     exit 1
  64. fi
  65.  
  66. if [[ $sourcedevice == *"nvme"* ]]; then
  67.     efipartition="$sourcedevice"p1
  68.     mainpartition="$sourcedevice"p2
  69. elif [[ $sourcedevice == *"sd"* ]]; then
  70.     efipartition="$sourcedevice"1
  71.     mainpartition="$sourcedevice"2
  72. elif [[ $sourcedevice == *"mmcblk"* ]]; then
  73.     efipartition="$sourcedevice"p1
  74.     mainpartition="$sourcedevice"p2
  75. fi
  76.  
  77. efishort=$(echo $efipartition | awk -F'/' '{print $3}')
  78. mainshort=$(echo $mainpartition | awk -F'/' '{print $3}')
  79.  
  80.  
  81. #echo "efishort is: $efishort"
  82.  
  83. part1type=$(lsblk -o name,parttypename | grep $efishort | awk -F "$efishort" '{print $2}' | sed 's/^[[:space:]]*//')
  84.  
  85. part2type=$(lsblk -o name,parttypename | grep $mainshort | awk -F "$mainshort" '{print $2}' | sed 's/^[[:space:]]*//')
  86.  
  87. if [[ $part1type != "EFI System"  || $part2type != "Linux filesystem" ]]; then
  88.     echo "Sorry, source partitions are wrong type"
  89.     echo "Partition #1 must be exactly \"EFI System\""
  90.     echo "Partition #2 must be exactly \"Linux filesystem\""
  91.     exit 2
  92. fi
  93.  
  94. }
  95. #***********************end checksource****************************
  96.  
  97. #*********************gettarget******************************
  98. gettarget () {
  99.     local target=""
  100.     local response="n"
  101.  
  102.     while [[ "$response" != "y" ]]; do
  103.         read -p "Enter the device to mirror to [sdX|nvmeXnX]: " target
  104.         target="${target#/dev/}"
  105.  
  106.         if [[ -z "$target" ]]; then
  107.             echo "Error: Input cannot be blank."
  108.             continue
  109.         elif [[ ! -e "/dev/$target" ]]; then
  110.             echo "Error: /dev/$target does not exist. Check 'lsblk'."
  111.             continue
  112.         fi
  113.  
  114.         read -p "Target is /dev/$target. Continue? [y/N]: " response
  115.         response="${response,,}"
  116.         response="${response:0:1}"
  117.     done
  118.  
  119.     targetdevice="/dev/$target"
  120.  
  121.     echo "Unmounting /dev/${target} to avoid possible corruption..."
  122.     udisksctl unmount -bf /dev/${target}1 2>/dev/null
  123.     udisksctl unmount -bf /dev/${target}2 2>/dev/null
  124. }
  125. #***********************end gettarget****************************
  126.  
  127. #************************checktarget*****************************
  128. checktarget () {
  129.     numtargetpartitions=$(lsblk -nlpo NAME,TYPE $targetdevice | awk '$2=="part"{print $1}' | wc -l)
  130.  
  131.     if [[ $targetdevice == *"nvme"* ]]; then
  132.         efipartition="$targetdevice"p1
  133.         mainpartition="$targetdevice"p2
  134.     elif [[ $targetdevice == *"sd"* ]]; then
  135.         efipartition="$targetdevice"1
  136.         mainpartition="$targetdevice"2
  137.     elif [[ $targetdevice == *"mmcblk"* ]]; then
  138.         efipartition="$targetdevice"p1
  139.         mainpartition="$targetdevice"p2
  140.     fi
  141.  
  142.     efishort=$(echo $efipartition | awk -F'/' '{print $3}')
  143.     mainshort=$(echo $mainpartition | awk -F'/' '{print $3}')
  144.  
  145.     part1type=$(lsblk -o name,parttypename | grep $efishort | awk -F "$efishort" '{print $2}' | sed 's/^[[:space:]]*//')
  146.  
  147.     part2type=$(lsblk -o name,parttypename | grep $mainshort | awk -F "$mainshort" '{print $2}' | sed 's/^[[:space:]]*//')
  148.  
  149.     if [[ $part1type != "EFI System"  || $part2type != "Linux filesystem" ]]; then
  150.         targetneedsformatted="true"
  151.     else
  152.         targetneedsformatted="false"
  153.     fi
  154. }
  155. #**********************end checktarget***************************
  156.  
  157. #*****************askifformatdesired*************************
  158. askifformatdesired () {
  159.  
  160.     if [[ $targetneedsformatted == "true" ]]; then
  161.         echo -e "\n"
  162.         read -p "Target device ($targetdevice) needs formatting, is that okay? [y/N]: " formatRequested
  163.         formatRequested=${formatRequested:-N}
  164.         formatRequested="${formatRequested:0:1}"
  165.         formatRequested="${formatRequested,,}"
  166.         if [[ $formatRequested != "y" ]]; then
  167.             echo "Format needed but refused, aborting."
  168.             exit;
  169.         fi
  170.     else #format not needed but still an option
  171.         echo -e "\nTarget has preexisting mirror."
  172.         echo -e "Preexisting mirror can either be refreshed or created anew. (default is to refresh the existing mirror)"
  173.         read -p "Format and create a new mirror? [y/N]: " formatRequested
  174.         formatRequested=${formatRequested:-N}
  175.         formatRequested="${formatRequested:0:1}"
  176.         formatRequested="${formatRequested,,}"
  177.     fi
  178. }
  179. #**************end askifformatdesired************************
  180.  
  181. #*******************doformat************************
  182. doformat () {
  183.     possiblelabel="$(hostname)usb"
  184.     echo
  185.     read -p "Enter a new label for the target device (e.g. [$possiblelabel]): " label
  186.     label=${label:-$possiblelabel}
  187.     echo "Label of target device will be: $label"
  188.     echo "This might take a few minutes, please be patient."
  189.     fdisk $targetdevice <<EOF
  190. g
  191. n
  192. 1
  193.  
  194. +512M
  195. t
  196. C12A7328-F81F-11D2-BA4B-00A0C93EC93B
  197. n
  198. 2
  199.  
  200.  
  201. w
  202. EOF
  203.  
  204.     echo "efi and ext4 partitions created successfully."
  205.     echo "The new partitions will now be formatted"
  206.     echo "This might take a few minutes, please be patient."
  207.     mkfs.fat -F32 -n ${label^^}EFI "$efipartition"
  208.     yes | mkfs.ext4 -L "$label" "$mainpartition"
  209.     sync
  210.  
  211. }
  212. #******************end doformat*********************
  213.  
  214. #*****************stopservices**********************
  215. stopservices () {
  216.  
  217. systemctl stop cron
  218. systemctl stop mysql
  219.  
  220. }
  221. #***************end stopservices** *****************
  222.  
  223. #*******************domirror************************
  224. domirror () {
  225. echo "making the mirror"
  226.     label="target"
  227.     mount /boot/efi
  228.     mkdir -p /media/$user/EFI
  229.     mount "$efipartition" /media/$user/EFI
  230.     mkdir -p /media/$user/$label
  231.     mount "$mainpartition" /media/$user/$label
  232.  
  233.     sourceuuid=$(findmnt / -o UUID -n)
  234.     targetuuid=$(findmnt /media/$user/"$label" -o UUID -n)
  235.  
  236.     sourceefiuuid=$(grep /boot/efi /etc/fstab | awk '{print $1}' | grep UUID | sed -E 's/UUID=(.*)/\1/' )
  237.     targetefiuuid=$(findmnt /media/$user/EFI/ -o UUID -n)
  238.  
  239.     sourceefilabel=$(findmnt -no label /boot/efi)
  240.     sourcelabel=$(findmnt -no label /)
  241.     targetlabel=$(findmnt -no label /media/$user/target)
  242.     targetefilabel=${targetlabel^^}EFI
  243.  
  244.     rsync -rv --delete /boot/efi/ /media/$user/EFI/
  245.  
  246.     find /media/$user/EFI/ -name "grub.cfg" -exec sed -i "s/$sourceuuid/$targetuuid/" "{}" \;
  247.  
  248.     rsync -axAXHv \
  249. --exclude='/home/*/.cache' \
  250. --exclude='/home/*/.gvfs' \
  251. --exclude='/dev/*' \
  252. --exclude='/proc/*' \
  253. --exclude='/sys/*' \
  254. --exclude='/tmp/*' \
  255. --exclude='/run/*' \
  256. --exclude='/mnt/*' \
  257. --exclude='/media/*' \
  258. --exclude='/lost+found' \
  259. --delete / /media/$user/"$label"/
  260.  
  261. [[ $(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
  262.  
  263. [[ $(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
  264.  
  265.     sync
  266. }
  267. #*******************domirror************************
  268.  
  269. #***************restartservices********************
  270. restartservices () {
  271.  
  272.     systemctl start cron
  273.     systemctl start mysql
  274.  
  275. }
  276. #**************end restartservices*****************
  277.  
  278. #*******************fixgrub************************
  279. fixgrub () {
  280. echo -e "\nMaking the target device bootable... Please be patient."
  281. for i in /sys /run /dev /proc; do mount --bind "$i" "/media/$user/target$i"; done
  282.  
  283. chroot /media/$user/target/ /bin/bash -- << EOT
  284.     update-grub
  285. EOT
  286.  
  287. echo "Writing changes to target, do not remove!"
  288. sync ; sleep 10 # give sync more time to finish syncing (had target busy, unable to dismount error, may need to increase with slower targets?)
  289.  
  290. for i in /proc /dev /run /sys; do umount "/media/$user/target$i"; done
  291.  
  292. umount /media/$user/EFI
  293. umount /media/$user/target
  294.  
  295. rmdir /media/$user/target
  296. rmdir /media/$user/EFI
  297.  
  298. echo "It is now okay to remove the target drive."
  299. }
  300.  
  301. #*****************end fixgrub**********************
  302.  
  303. main
  304.  
Advertisement
Add Comment
Please, Sign In to add comment