#!/bin/ash
#
#
#
# *** BINARIES ***
 if [ ! -e /usr/bin/ddrescue ]; then
   echo "Error: GNU ddrescue is not installed."
   exit
 fi
 if [ ! -e /tmp/ddr.tmp ]; then
   if [ ! -e /usr/sbin/partclone ]; then
     echo "Error: Partclone is not installed."
     exit
   fi
 fi
#
#
#
# *** MENU ***
 if [ -e /tmp/ddr.tmp ]; then
   METHOD=GNUddrescue
   rm /tmp/ddr.tmp
   else
   clear
   echo ""
   echo " Welcome to 4MLinux Backup Scripts!"
   echo ""
   echo " Which program do you wish to run?"
   echo " 1) Partclone."
   echo " 2) GNU ddrescue."
   echo " 3) I have no idea!"
   echo ""
   read -p " Your choice (1,2,3)?: " CHOICE
   CHOICE=X$CHOICE
   if [ $CHOICE = X3 ]; then
     clear
     echo ""
     echo " Do not panic :-) We will try Partclone now."
     echo " In case of read errors, run me again, and choose 'GNU ddrescue'."
     echo ""
     echo -n " Press Enter to continue..."; read
     CHOICE=X1
   fi
   if [ $CHOICE = X2 ]; then
     METHOD=GNUddrescue
     else
     METHOD=Partclone
   fi
   if [ ! $CHOICE = X1 ]; then
     if [ ! $CHOICE = X2 ]; then
       echo " Error: you must choose 1,2,or3."
       exit
     fi
   fi
 fi
#
#
#
# *** INTRODUCTION ***
 clear
 echo ""
 echo " You will be asked to choose a source partition and a target partition."
 echo " $METHOD will make an image of the source partition, which will be "
 echo " saved on the target partition."
 echo ""
 echo " NOTES:"
 echo " 1) This script is safe (it will NOT delete or format any data)."
 echo " 2) The target partition must be big enough to store the image file."
 echo " 3) There is an option to compress your image file using Zstandard." 
 echo " 4) Another option is to send the image file to a remote server."
 echo ""
 echo -n " Press Enter to continue..."; read
#
#
#
# *** SOURCE PARTITION ***
 clear
 lsdisk
 echo ""
 echo "Which partition do you wish to image ?"
 read -p "(for example: sda1): " SOURCE
 if [ ! -b /dev/$SOURCE ]; then
   echo "Error: the block device '/dev/$SOURCE' does not exist."
   exit
 fi
 umount /dev/$SOURCE 2>/dev/null
 cat /etc/mtab | grep $SOURCE > /tmp/clone
 if [ -s /tmp/clone ]; then
   echo "Error: I am unable to unmount /dev/$SOURCE."
   rm /tmp/clone
   exit
 fi
 rm /tmp/clone
#
#
#
# *** TARGET PARTITION ***
 clear
 lsdisk
 echo ""
 echo "Where do you wish to save the image file?"
 read -p "(for example: sda2): " TARGET
 if [ ! -b /dev/$TARGET ]; then
   echo "Error: the block device '/dev/$TARGET' does not exist."
   exit
   else
   if [ $TARGET = $SOURCE ]; then
     echo "Error: you must not image $SOURCE to $SOURCE."
     exit
   fi
 fi
 mkdir /mnt/$TARGET 2>/dev/null
 umount /dev/$TARGET 2>/dev/null
 ntfs-3g /dev/$TARGET /mnt/$TARGET 2>/dev/null
 mount /dev/$TARGET /mnt/$TARGET 2>/dev/null
 rmdir /mnt/$TARGET 2>/dev/null
 if [ ! -e /mnt/$TARGET ]; then
   echo "Error: I am unable to mount /dev/$TARGET."
   exit
 fi
 cd /mnt/$TARGET
#
#
#
# *** METHOD ***
 if [ $METHOD = Partclone ]; then
   CREATE="partclone.dd -N -s /dev/$SOURCE -o Partclone.img"
   else
   CREATE="ddrescue -v /dev/$SOURCE GNUddrescue.img"
 fi
 RESTORE="ddrescue -v --force $METHOD.img /dev/$SOURCE"
#
#
#
# *** IMAGE FILE ***
 clear
 echo ""
 if [ -e $METHOD.img* ]; then
   echo " The file '$METHOD.img' already exists on /dev/$TARGET."
   read -p " Do you wish to overwrite it (y/n)?: " OVERWRITE
   OVERWRITE=x$OVERWRITE
   if [ ! $OVERWRITE = xy ]; then
     echo " Script interrupted by user."
     exit
   fi
 fi
 rm $METHOD.* 2>/dev/null
 ln -s /proc/self/fd/2 /dev/stderr 2>/dev/null
 $CREATE
 echo "exit #Delete this line to execute as a script." > $METHOD.readme
 echo "" >> $METHOD.readme
 echo "#Commands to restore $SOURCE from your image file:" >> $METHOD.readme
 echo "zstd -dv --rm $METHOD.img.zst" >> $METHOD.readme
 echo "$RESTORE && exit" >> $METHOD.readme
 echo "" >> $METHOD.readme
 echo "NOTE:" >> $METHOD.readme
 echo "Before restoring, double check if $SOURCE is a correct target!" >> $METHOD.readme
 chmod 755 $METHOD.readme
#
#
#
# *** PARTCLONE ERROR LOG ***
 if [ $METHOD = Partclone ]; then
   clear
   echo ""
   cat /var/log/partclone.log | grep OK! > /tmp/clone
   if [ ! -s /tmp/clone ]; then
     rm /tmp/clone
     rm partclone.* 2>/dev/null
     echo " Ooops! Something went wrong :-("
     read -p " Do you wish to view partclone.log now (y/n)?: " LOG
     LOG=y$LOG
     if [ $LOG = yy ]; then
       mcview /var/log/partclone.log
     fi
     echo "Bye!"
     exit
     else
     rm /tmp/clone
   fi
 fi
#
#
#
# *** ZSTD ***
 echo ""
 echo " The image file '/mnt/$TARGET/$METHOD.img' is ready."
 read -p " Do you wish to compress it using Zstandard (y/n)?: " ZSTD
 ZSTD=x$ZSTD
 if [ $ZSTD = xy ]; then
   echo ""
   zstd -19v --rm $METHOD.img
   else
   sed -i /zstd/d $METHOD.readme
   sed -i s/Commands/Command/ $METHOD.readme
 fi
#
#
#
# *** FTP ***
 if [ -e $METHOD.img.zst ]; then
   clear
   echo " The image file '/mnt/$TARGET/$METHOD.img.zst' is ready."
   else
   echo ""
 fi
 read -p " Do you wish to send your image to a remote server (y/n)?: " FTP
 FTP=x$FTP
 if [ ! $FTP = xy ]; then
   echo " Bye!"
   else
   echo /mnt/$TARGET > /tmp/local
   ftp-menu
 fi
